@osfactory/otel-hook 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.d.ts CHANGED
@@ -195,6 +195,11 @@ type EnvironmentConfigResult = {
195
195
  * No identity variable appears here by design: session, invocation, and
196
196
  * workspace identity are supplied per invocation as identity claims, never
197
197
  * through ambient configuration (ADR 0001).
198
+ *
199
+ * The standard OTLP header variables are read too, but listed separately in
200
+ * `OTLP_HEADER_VARIABLES`: only the header *names* they set enter the
201
+ * configuration patch, and their values are resolved by `resolveOtlpHeaders`
202
+ * for the sinks alone.
198
203
  */
199
204
  declare const ENVIRONMENT_VARIABLES: Readonly<{
200
205
  exporterEnabled: "OTEL_HOOK_ENABLED";
@@ -230,4 +235,83 @@ declare const ENVIRONMENT_VARIABLES: Readonly<{
230
235
  */
231
236
  declare const parseEnvironmentConfig: (env: EnvironmentRecord) => EnvironmentConfigResult;
232
237
 
233
- export { CONFIG_SOURCE_PRECEDENCE, type ConfigLayer, type ConfigProvenance, type ConfigResolution, type ConfigSource, EMPTY_RESOURCE_ATTRIBUTES, ENVIRONMENT_VARIABLES, type EnvironmentConfigResult, type EnvironmentRecord, MAX_RESOURCE_ATTRIBUTES, MAX_RESOURCE_ATTRIBUTE_KEY_LENGTH, MAX_RESOURCE_ATTRIBUTE_VALUE_LENGTH, OtelHookConfig, OtelHookConfigPatch, type ParsedResourceAttributes, RESERVED_RESOURCE_ATTRIBUTE_KEYS, RESOURCE_ATTRIBUTE_KEY_PATTERN, RESOURCE_ATTRIBUTE_KEY_REJECTION_DETAIL, type ResourceAttributeKeyRejection, type ResourceAttributeValue, type ResourceAttributes, checkResourceAttributeKey, configSourceSchema, describeResolvedConfig, describeResourceAttributeNames, isReservedResourceAttributeKey, isSecretResourceAttributeKey, parseEnvironmentConfig, parseResourceAttributesValue, resolveConfig, resourceAttributeValueSchema, resourceAttributesSchema, sanitizeResourceAttributes };
238
+ /**
239
+ * The standard OTLP exporter header variables.
240
+ *
241
+ * `OTEL_EXPORTER_OTLP_HEADERS` applies to every signal;
242
+ * `OTEL_EXPORTER_OTLP_TRACES_HEADERS` and `OTEL_EXPORTER_OTLP_LOGS_HEADERS`
243
+ * apply to one signal each. They exist so a credential (`authorization=Basic …`)
244
+ * can reach the exporter without being written on a command line, where `ps` and
245
+ * `/proc/<pid>/cmdline` would show it to every local user.
246
+ *
247
+ * Header *values* never enter configuration. Parsing returns them to the caller,
248
+ * which hands them straight to the sink at construction; configuration carries
249
+ * header *names* only (`exporter.headerNames`), because a resolved-config
250
+ * snapshot is logged and exported. Every diagnostic here names an entry by its
251
+ * position and the variable by its name — never a key, never a value — because a
252
+ * malformed entry may be the secret itself (`Basic abc` with no `=`).
253
+ */
254
+ /** Upper bound on headers per variable, matching `exporter.headerNames`. */
255
+ declare const MAX_OTLP_HEADERS = 32;
256
+ declare const OTLP_HEADER_VARIABLES: Readonly<{
257
+ generic: "OTEL_EXPORTER_OTLP_HEADERS";
258
+ traces: "OTEL_EXPORTER_OTLP_TRACES_HEADERS";
259
+ logs: "OTEL_EXPORTER_OTLP_LOGS_HEADERS";
260
+ }>;
261
+ type ParsedOtlpHeaders = {
262
+ readonly headers: Readonly<Record<string, string>>;
263
+ /** Position-only explanations; never a key or a value. */
264
+ readonly warnings: readonly string[];
265
+ };
266
+ /**
267
+ * Parse one `OTEL_EXPORTER_OTLP_*HEADERS` value.
268
+ *
269
+ * The OpenTelemetry exporter specification defines the format as W3C
270
+ * Baggage-like: comma-separated `key=value` pairs, surrounding whitespace
271
+ * trimmed, keys and values percent-decoded. So `authorization=Basic%20abc` is
272
+ * the header `authorization: Basic abc`, and a `,` inside a value must be
273
+ * written `%2C`. A malformed entry is skipped and reported; the rest of the
274
+ * variable still applies. A repeated key takes its last value.
275
+ */
276
+ declare const parseOtlpHeadersValue: (raw: string) => ParsedOtlpHeaders;
277
+ /**
278
+ * Overlay header maps, lowest precedence first, per header name.
279
+ *
280
+ * A higher source replaces only the names it sets (case-insensitively), so a
281
+ * signal-specific variable can override `authorization` while a generic tenant
282
+ * header still applies — the same per-key merge the OpenTelemetry SDKs apply.
283
+ */
284
+ declare const mergeOtlpHeaders: (...sources: readonly Readonly<Record<string, string>>[]) => Readonly<Record<string, string>>;
285
+ type ResolvedOtlpHeaders = {
286
+ /** Headers for the traces exporter. */
287
+ readonly traces: Readonly<Record<string, string>>;
288
+ /** Headers for the logs exporter. */
289
+ readonly logs: Readonly<Record<string, string>>;
290
+ /** Variable-qualified, value-free explanations of skipped entries. */
291
+ readonly warnings: readonly {
292
+ readonly variable: string;
293
+ readonly detail: string;
294
+ }[];
295
+ };
296
+ /**
297
+ * Resolve exporter headers per signal.
298
+ *
299
+ * Precedence, per header name, highest first:
300
+ *
301
+ * ```
302
+ * explicit headers (--header) > OTEL_EXPORTER_OTLP_{TRACES,LOGS}_HEADERS > OTEL_EXPORTER_OTLP_HEADERS
303
+ * ```
304
+ *
305
+ * Pure: the environment is passed in, never read from `process.env`.
306
+ */
307
+ declare const resolveOtlpHeaders: (env: Readonly<Record<string, string | undefined>>, explicit?: Readonly<Record<string, string>>) => ResolvedOtlpHeaders;
308
+ /**
309
+ * Header names, deduplicated case-insensitively (first spelling kept), sorted
310
+ * and bounded — the only part of a header that may enter configuration or a
311
+ * diagnostic.
312
+ */
313
+ declare const unionOtlpHeaderNames: (...lists: readonly (readonly string[])[]) => readonly string[];
314
+ /** {@link unionOtlpHeaderNames} over the names of header maps. */
315
+ declare const describeOtlpHeaderNames: (...sources: readonly Readonly<Record<string, string>>[]) => readonly string[];
316
+
317
+ export { CONFIG_SOURCE_PRECEDENCE, type ConfigLayer, type ConfigProvenance, type ConfigResolution, type ConfigSource, EMPTY_RESOURCE_ATTRIBUTES, ENVIRONMENT_VARIABLES, type EnvironmentConfigResult, type EnvironmentRecord, MAX_OTLP_HEADERS, MAX_RESOURCE_ATTRIBUTES, MAX_RESOURCE_ATTRIBUTE_KEY_LENGTH, MAX_RESOURCE_ATTRIBUTE_VALUE_LENGTH, OTLP_HEADER_VARIABLES, OtelHookConfig, OtelHookConfigPatch, type ParsedOtlpHeaders, type ParsedResourceAttributes, RESERVED_RESOURCE_ATTRIBUTE_KEYS, RESOURCE_ATTRIBUTE_KEY_PATTERN, RESOURCE_ATTRIBUTE_KEY_REJECTION_DETAIL, type ResolvedOtlpHeaders, type ResourceAttributeKeyRejection, type ResourceAttributeValue, type ResourceAttributes, checkResourceAttributeKey, configSourceSchema, describeOtlpHeaderNames, describeResolvedConfig, describeResourceAttributeNames, isReservedResourceAttributeKey, isSecretResourceAttributeKey, mergeOtlpHeaders, parseEnvironmentConfig, parseOtlpHeadersValue, parseResourceAttributesValue, resolveConfig, resolveOtlpHeaders, resourceAttributeValueSchema, resourceAttributesSchema, sanitizeResourceAttributes, unionOtlpHeaderNames };
package/dist/config.js CHANGED
@@ -761,6 +761,127 @@ var describeResolvedConfig = (config) => {
761
761
  };
762
762
  };
763
763
 
764
+ // src/config/otlp-headers.ts
765
+ var MAX_OTLP_HEADERS = 32;
766
+ var OTLP_HEADER_VARIABLES = Object.freeze({
767
+ generic: "OTEL_EXPORTER_OTLP_HEADERS",
768
+ traces: "OTEL_EXPORTER_OTLP_TRACES_HEADERS",
769
+ logs: "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
770
+ });
771
+ var HEADER_NAME_PATTERN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
772
+ var isUnsafeHeaderValue = (value) => [...value].some((char) => {
773
+ const code = char.codePointAt(0) ?? 0;
774
+ return code < 32 && code !== 9 || code === 127 || code > 255;
775
+ });
776
+ var decode = (raw) => {
777
+ try {
778
+ return decodeURIComponent(raw);
779
+ } catch {
780
+ return void 0;
781
+ }
782
+ };
783
+ var parseOtlpHeadersValue = (raw) => {
784
+ const headers = {};
785
+ const warnings = [];
786
+ let overflowed = false;
787
+ for (const [index, rawEntry] of raw.split(",").entries()) {
788
+ const position = String(index + 1);
789
+ const entry = rawEntry.trim();
790
+ if (entry === "") {
791
+ continue;
792
+ }
793
+ const equals = entry.indexOf("=");
794
+ if (equals <= 0) {
795
+ warnings.push(`entry ${position} is not a key=value pair; ignored`);
796
+ continue;
797
+ }
798
+ const key = decode(entry.slice(0, equals).trim());
799
+ const value = decode(entry.slice(equals + 1).trim());
800
+ if (key === void 0 || value === void 0) {
801
+ warnings.push(`entry ${position} has malformed percent-encoding; ignored`);
802
+ continue;
803
+ }
804
+ const name = key.trim();
805
+ const trimmedValue = value.trim();
806
+ if (!HEADER_NAME_PATTERN.test(name)) {
807
+ warnings.push(`entry ${position} does not name a valid HTTP header; ignored`);
808
+ continue;
809
+ }
810
+ if (trimmedValue === "") {
811
+ warnings.push(`entry ${position} has an empty value; ignored`);
812
+ continue;
813
+ }
814
+ if (isUnsafeHeaderValue(trimmedValue)) {
815
+ warnings.push(`entry ${position} has a character not allowed in an HTTP header value; ignored`);
816
+ continue;
817
+ }
818
+ const existing = findHeaderName(headers, name);
819
+ if (existing === void 0 && Object.keys(headers).length >= MAX_OTLP_HEADERS) {
820
+ overflowed = true;
821
+ continue;
822
+ }
823
+ if (existing !== void 0) {
824
+ delete headers[existing];
825
+ }
826
+ headers[name] = trimmedValue;
827
+ }
828
+ if (overflowed) {
829
+ warnings.push(`more than ${String(MAX_OTLP_HEADERS)} headers were supplied; the excess was ignored`);
830
+ }
831
+ return { headers, warnings };
832
+ };
833
+ var findHeaderName = (headers, name) => {
834
+ const lower = name.toLowerCase();
835
+ return Object.keys(headers).find((candidate) => candidate.toLowerCase() === lower);
836
+ };
837
+ var mergeOtlpHeaders = (...sources) => {
838
+ const merged = {};
839
+ for (const source of sources) {
840
+ for (const [name, value] of Object.entries(source)) {
841
+ const existing = findHeaderName(merged, name);
842
+ if (existing !== void 0) {
843
+ delete merged[existing];
844
+ }
845
+ merged[name] = value;
846
+ }
847
+ }
848
+ return merged;
849
+ };
850
+ var resolveOtlpHeaders = (env, explicit = {}) => {
851
+ const warnings = [];
852
+ const read = (variable) => {
853
+ const raw = env[variable];
854
+ if (raw === void 0 || raw.trim() === "") {
855
+ return {};
856
+ }
857
+ const parsed = parseOtlpHeadersValue(raw);
858
+ for (const detail of parsed.warnings) {
859
+ warnings.push({ variable, detail });
860
+ }
861
+ return parsed.headers;
862
+ };
863
+ const generic = read(OTLP_HEADER_VARIABLES.generic);
864
+ const traces = read(OTLP_HEADER_VARIABLES.traces);
865
+ const logs = read(OTLP_HEADER_VARIABLES.logs);
866
+ return {
867
+ traces: mergeOtlpHeaders(generic, traces, explicit),
868
+ logs: mergeOtlpHeaders(generic, logs, explicit),
869
+ warnings
870
+ };
871
+ };
872
+ var unionOtlpHeaderNames = (...lists) => {
873
+ const names = [];
874
+ const seen = /* @__PURE__ */ new Set();
875
+ for (const name of lists.flat()) {
876
+ if (!seen.has(name.toLowerCase())) {
877
+ seen.add(name.toLowerCase());
878
+ names.push(name);
879
+ }
880
+ }
881
+ return names.sort().slice(0, MAX_OTLP_HEADERS);
882
+ };
883
+ var describeOtlpHeaderNames = (...sources) => unionOtlpHeaderNames(...sources.map((source) => Object.keys(source)));
884
+
764
885
  // src/config/environment.ts
765
886
  var ENVIRONMENT_VARIABLES = Object.freeze({
766
887
  exporterEnabled: "OTEL_HOOK_ENABLED",
@@ -930,11 +1051,17 @@ var parseEnvironmentConfig = (env) => {
930
1051
  ...logsEndpoint === void 0 ? {} : { endpoint: logsEndpoint },
931
1052
  ...logsIncludeContent === void 0 ? {} : { includeContent: logsIncludeContent }
932
1053
  };
1054
+ const otlpHeaders = resolveOtlpHeaders(env);
1055
+ for (const { variable, detail } of otlpHeaders.warnings) {
1056
+ warn(variable, detail);
1057
+ }
1058
+ const headerNames = describeOtlpHeaderNames(otlpHeaders.traces, otlpHeaders.logs);
933
1059
  const exporter = {
934
1060
  ...exporterEnabled === void 0 ? {} : { enabled: exporterEnabled },
935
1061
  ...endpoint === void 0 ? {} : { endpoint },
936
1062
  ...protocol === void 0 ? {} : { protocol },
937
1063
  ...timeoutMillis === void 0 ? {} : { timeoutMillis },
1064
+ ...headerNames.length === 0 ? {} : { headerNames: [...headerNames] },
938
1065
  ...serviceName === void 0 ? {} : { serviceName },
939
1066
  ...serviceNamespace === void 0 ? {} : { serviceNamespace },
940
1067
  ...Object.keys(resourceAttributes).length === 0 ? {} : { resourceAttributes },
@@ -971,15 +1098,18 @@ export {
971
1098
  DEFAULT_LOGS_POLICY,
972
1099
  EMPTY_RESOURCE_ATTRIBUTES,
973
1100
  ENVIRONMENT_VARIABLES,
1101
+ MAX_OTLP_HEADERS,
974
1102
  MAX_RESOURCE_ATTRIBUTES,
975
1103
  MAX_RESOURCE_ATTRIBUTE_KEY_LENGTH,
976
1104
  MAX_RESOURCE_ATTRIBUTE_VALUE_LENGTH,
1105
+ OTLP_HEADER_VARIABLES,
977
1106
  RESERVED_RESOURCE_ATTRIBUTE_KEYS,
978
1107
  RESOURCE_ATTRIBUTE_KEY_PATTERN,
979
1108
  RESOURCE_ATTRIBUTE_KEY_REJECTION_DETAIL,
980
1109
  checkResourceAttributeKey,
981
1110
  configSourceSchema,
982
1111
  contentModeSchema,
1112
+ describeOtlpHeaderNames,
983
1113
  describeResolvedConfig,
984
1114
  describeResourceAttributeNames,
985
1115
  detectionPolicySchema,
@@ -988,13 +1118,17 @@ export {
988
1118
  isReservedResourceAttributeKey,
989
1119
  isSecretResourceAttributeKey,
990
1120
  logsPolicySchema,
1121
+ mergeOtlpHeaders,
991
1122
  otelHookConfigPatchSchema,
992
1123
  otelHookConfigSchema,
993
1124
  parseEnvironmentConfig,
1125
+ parseOtlpHeadersValue,
994
1126
  parseResourceAttributesValue,
995
1127
  resolveConfig,
1128
+ resolveOtlpHeaders,
996
1129
  resourceAttributeValueSchema,
997
1130
  resourceAttributesSchema,
998
- sanitizeResourceAttributes
1131
+ sanitizeResourceAttributes,
1132
+ unionOtlpHeaderNames
999
1133
  };
1000
1134
  //# sourceMappingURL=config.js.map