@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/index.js CHANGED
@@ -1926,6 +1926,127 @@ var describeResolvedConfig = (config) => {
1926
1926
  };
1927
1927
  };
1928
1928
 
1929
+ // src/config/otlp-headers.ts
1930
+ var MAX_OTLP_HEADERS = 32;
1931
+ var OTLP_HEADER_VARIABLES = Object.freeze({
1932
+ generic: "OTEL_EXPORTER_OTLP_HEADERS",
1933
+ traces: "OTEL_EXPORTER_OTLP_TRACES_HEADERS",
1934
+ logs: "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
1935
+ });
1936
+ var HEADER_NAME_PATTERN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
1937
+ var isUnsafeHeaderValue = (value) => [...value].some((char) => {
1938
+ const code = char.codePointAt(0) ?? 0;
1939
+ return code < 32 && code !== 9 || code === 127 || code > 255;
1940
+ });
1941
+ var decode = (raw) => {
1942
+ try {
1943
+ return decodeURIComponent(raw);
1944
+ } catch {
1945
+ return void 0;
1946
+ }
1947
+ };
1948
+ var parseOtlpHeadersValue = (raw) => {
1949
+ const headers = {};
1950
+ const warnings = [];
1951
+ let overflowed = false;
1952
+ for (const [index, rawEntry] of raw.split(",").entries()) {
1953
+ const position = String(index + 1);
1954
+ const entry = rawEntry.trim();
1955
+ if (entry === "") {
1956
+ continue;
1957
+ }
1958
+ const equals = entry.indexOf("=");
1959
+ if (equals <= 0) {
1960
+ warnings.push(`entry ${position} is not a key=value pair; ignored`);
1961
+ continue;
1962
+ }
1963
+ const key = decode(entry.slice(0, equals).trim());
1964
+ const value = decode(entry.slice(equals + 1).trim());
1965
+ if (key === void 0 || value === void 0) {
1966
+ warnings.push(`entry ${position} has malformed percent-encoding; ignored`);
1967
+ continue;
1968
+ }
1969
+ const name = key.trim();
1970
+ const trimmedValue = value.trim();
1971
+ if (!HEADER_NAME_PATTERN.test(name)) {
1972
+ warnings.push(`entry ${position} does not name a valid HTTP header; ignored`);
1973
+ continue;
1974
+ }
1975
+ if (trimmedValue === "") {
1976
+ warnings.push(`entry ${position} has an empty value; ignored`);
1977
+ continue;
1978
+ }
1979
+ if (isUnsafeHeaderValue(trimmedValue)) {
1980
+ warnings.push(`entry ${position} has a character not allowed in an HTTP header value; ignored`);
1981
+ continue;
1982
+ }
1983
+ const existing = findHeaderName(headers, name);
1984
+ if (existing === void 0 && Object.keys(headers).length >= MAX_OTLP_HEADERS) {
1985
+ overflowed = true;
1986
+ continue;
1987
+ }
1988
+ if (existing !== void 0) {
1989
+ delete headers[existing];
1990
+ }
1991
+ headers[name] = trimmedValue;
1992
+ }
1993
+ if (overflowed) {
1994
+ warnings.push(`more than ${String(MAX_OTLP_HEADERS)} headers were supplied; the excess was ignored`);
1995
+ }
1996
+ return { headers, warnings };
1997
+ };
1998
+ var findHeaderName = (headers, name) => {
1999
+ const lower = name.toLowerCase();
2000
+ return Object.keys(headers).find((candidate) => candidate.toLowerCase() === lower);
2001
+ };
2002
+ var mergeOtlpHeaders = (...sources) => {
2003
+ const merged = {};
2004
+ for (const source of sources) {
2005
+ for (const [name, value] of Object.entries(source)) {
2006
+ const existing = findHeaderName(merged, name);
2007
+ if (existing !== void 0) {
2008
+ delete merged[existing];
2009
+ }
2010
+ merged[name] = value;
2011
+ }
2012
+ }
2013
+ return merged;
2014
+ };
2015
+ var resolveOtlpHeaders = (env, explicit = {}) => {
2016
+ const warnings = [];
2017
+ const read = (variable) => {
2018
+ const raw = env[variable];
2019
+ if (raw === void 0 || raw.trim() === "") {
2020
+ return {};
2021
+ }
2022
+ const parsed = parseOtlpHeadersValue(raw);
2023
+ for (const detail of parsed.warnings) {
2024
+ warnings.push({ variable, detail });
2025
+ }
2026
+ return parsed.headers;
2027
+ };
2028
+ const generic = read(OTLP_HEADER_VARIABLES.generic);
2029
+ const traces = read(OTLP_HEADER_VARIABLES.traces);
2030
+ const logs = read(OTLP_HEADER_VARIABLES.logs);
2031
+ return {
2032
+ traces: mergeOtlpHeaders(generic, traces, explicit),
2033
+ logs: mergeOtlpHeaders(generic, logs, explicit),
2034
+ warnings
2035
+ };
2036
+ };
2037
+ var unionOtlpHeaderNames = (...lists) => {
2038
+ const names = [];
2039
+ const seen = /* @__PURE__ */ new Set();
2040
+ for (const name of lists.flat()) {
2041
+ if (!seen.has(name.toLowerCase())) {
2042
+ seen.add(name.toLowerCase());
2043
+ names.push(name);
2044
+ }
2045
+ }
2046
+ return names.sort().slice(0, MAX_OTLP_HEADERS);
2047
+ };
2048
+ var describeOtlpHeaderNames = (...sources) => unionOtlpHeaderNames(...sources.map((source) => Object.keys(source)));
2049
+
1929
2050
  // src/config/environment.ts
1930
2051
  var ENVIRONMENT_VARIABLES = Object.freeze({
1931
2052
  exporterEnabled: "OTEL_HOOK_ENABLED",
@@ -2095,11 +2216,17 @@ var parseEnvironmentConfig = (env) => {
2095
2216
  ...logsEndpoint === void 0 ? {} : { endpoint: logsEndpoint },
2096
2217
  ...logsIncludeContent === void 0 ? {} : { includeContent: logsIncludeContent }
2097
2218
  };
2219
+ const otlpHeaders = resolveOtlpHeaders(env);
2220
+ for (const { variable, detail } of otlpHeaders.warnings) {
2221
+ warn(variable, detail);
2222
+ }
2223
+ const headerNames = describeOtlpHeaderNames(otlpHeaders.traces, otlpHeaders.logs);
2098
2224
  const exporter = {
2099
2225
  ...exporterEnabled === void 0 ? {} : { enabled: exporterEnabled },
2100
2226
  ...endpoint === void 0 ? {} : { endpoint },
2101
2227
  ...protocol === void 0 ? {} : { protocol },
2102
2228
  ...timeoutMillis === void 0 ? {} : { timeoutMillis },
2229
+ ...headerNames.length === 0 ? {} : { headerNames: [...headerNames] },
2103
2230
  ...serviceName === void 0 ? {} : { serviceName },
2104
2231
  ...serviceNamespace === void 0 ? {} : { serviceNamespace },
2105
2232
  ...Object.keys(resourceAttributes).length === 0 ? {} : { resourceAttributes },
@@ -7681,7 +7808,7 @@ import {
7681
7808
  } from "@opentelemetry/semantic-conventions/incubating";
7682
7809
 
7683
7810
  // src/version.ts
7684
- var VERSION = "0.6.0";
7811
+ var VERSION = "0.7.0";
7685
7812
 
7686
7813
  // src/telemetry/export-identity.ts
7687
7814
  import { ATTR_SESSION_ID } from "@opentelemetry/semantic-conventions/incubating";
@@ -7696,7 +7823,10 @@ var ATTR_OTELHOOK_INVOCATION_ID = "otelhook.invocation.id";
7696
7823
  var ATTR_OTELHOOK_PROVIDER_ID = "otelhook.provider.id";
7697
7824
  var ATTR_OTELHOOK_PROVIDER_VERSION = "otelhook.provider.version";
7698
7825
  var ATTR_OTELHOOK_WORKSPACE_ID = "otelhook.workspace.id";
7699
- var HAR_SESSION_KEY = "har.session_key";
7826
+ var EXTERNAL_SESSION_KEY_ATTRIBUTES = Object.freeze([
7827
+ ATTR_SESSION_ID,
7828
+ "har.session_key"
7829
+ ]);
7700
7830
  var ATTR_OTELHOOK_SUBAGENT_ID = "otelhook.subagent.id";
7701
7831
  var ATTR_VCS_REF_HEAD_NAME = "vcs.ref.head.name";
7702
7832
  var ATTR_OTELHOOK_TARGET_WORKSPACE_ID = "otelhook.target.workspace.id";
@@ -7729,11 +7859,13 @@ var passthroughHarAttributes = (resource) => {
7729
7859
  return attributes;
7730
7860
  };
7731
7861
  var resolveExportedSessionId = (eventSessionId, resource) => {
7732
- const har = resource.attributes[HAR_SESSION_KEY];
7733
- if (typeof har === "string") {
7734
- const trimmed = har.trim();
7735
- if (trimmed.length > 0) {
7736
- return { sessionId: trimmed, providerSessionId: eventSessionId };
7862
+ for (const key of EXTERNAL_SESSION_KEY_ATTRIBUTES) {
7863
+ const supplied = resource.attributes[key];
7864
+ if (typeof supplied === "string") {
7865
+ const trimmed = supplied.trim();
7866
+ if (trimmed.length > 0) {
7867
+ return { sessionId: trimmed, providerSessionId: eventSessionId };
7868
+ }
7737
7869
  }
7738
7870
  }
7739
7871
  return { sessionId: eventSessionId, providerSessionId: eventSessionId };
@@ -10600,9 +10732,10 @@ var createHookRuntime = (options) => {
10600
10732
  ...spool === void 0 ? {} : { spool },
10601
10733
  correlate
10602
10734
  });
10735
+ const logHeaders = options.logHeaders ?? options.headers;
10603
10736
  const logSink = createOtlpLogSink({
10604
10737
  exporter: options.config.exporter,
10605
- ...options.headers === void 0 ? {} : { headers: options.headers },
10738
+ ...logHeaders === void 0 ? {} : { headers: logHeaders },
10606
10739
  providerId: options.providerNamespace,
10607
10740
  installationId: options.installationId,
10608
10741
  clock,
@@ -12473,6 +12606,7 @@ export {
12473
12606
  MAX_LOG_BODY_CHARACTERS,
12474
12607
  MAX_LOG_RECORDS_PER_BATCH,
12475
12608
  MAX_LOG_RECORDS_PER_EVENT,
12609
+ MAX_OTLP_HEADERS,
12476
12610
  MAX_RECOVERED_START_ATTRIBUTES,
12477
12611
  MAX_RESOURCE_ATTRIBUTES,
12478
12612
  MAX_RESOURCE_ATTRIBUTE_KEY_LENGTH,
@@ -12482,6 +12616,7 @@ export {
12482
12616
  MAX_WORKTREE_WALK_DEPTH,
12483
12617
  MCP_TOOL_NAME_PATTERN,
12484
12618
  NO_LOG_CONTENT,
12619
+ OTLP_HEADER_VARIABLES,
12485
12620
  OtelHookError,
12486
12621
  PROVIDERS_WITHOUT_VERIFIED_LOCATION,
12487
12622
  PROVIDERS_WITH_VERIFIED_LOCATION,
@@ -12582,6 +12717,7 @@ export {
12582
12717
  describeAdapter,
12583
12718
  describeErrorCode,
12584
12719
  describeLogsDeliverability,
12720
+ describeOtlpHeaderNames,
12585
12721
  describeProviderCatalog,
12586
12722
  describeResolvedConfig,
12587
12723
  describeResourceAttributeNames,
@@ -12638,6 +12774,7 @@ export {
12638
12774
  mergeCursorHookRegistration,
12639
12775
  mergeGeminiHookRegistration,
12640
12776
  mergeNestedHookRegistration,
12777
+ mergeOtlpHeaders,
12641
12778
  minimumStaleClaimMillis,
12642
12779
  modelDescriptorSchema,
12643
12780
  nonEmptyStringSchema,
@@ -12653,6 +12790,7 @@ export {
12653
12790
  parseEnvironmentConfig,
12654
12791
  parseGitDirFile,
12655
12792
  parseHeadBranch,
12793
+ parseOtlpHeadersValue,
12656
12794
  parseResourceAttributesValue,
12657
12795
  planProviderRegistration,
12658
12796
  privacyLimitsSchema,
@@ -12688,6 +12826,7 @@ export {
12688
12826
  resolveInstallPath,
12689
12827
  resolveInvocationIdentity,
12690
12828
  resolveLogsEndpoint,
12829
+ resolveOtlpHeaders,
12691
12830
  resolvePrivacyPolicy,
12692
12831
  resolvedProviderIdSchema,
12693
12832
  resourceAttributeValueSchema,
@@ -12717,6 +12856,7 @@ export {
12717
12856
  toolEndEventSchema,
12718
12857
  toolKindSchema,
12719
12858
  toolStartEventSchema,
12859
+ unionOtlpHeaderNames,
12720
12860
  unknownDetection,
12721
12861
  unknownWorkspaceIdentity,
12722
12862
  usageReportSchema,