@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/README.md CHANGED
@@ -79,9 +79,11 @@ fail its host agent is a liability.
79
79
 
80
80
  ```bash
81
81
  # Register this as the hook command; the payload arrives on stdin.
82
+ # The credential comes from the standard OTLP header variable rather than a
83
+ # --header flag, so it never appears in `ps` or /proc/<pid>/cmdline.
84
+ export OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20$COLLECTOR_TOKEN"
82
85
  otel-hook run --provider claude-code \
83
- --endpoint https://collector.example/v1/traces \
84
- --header "authorization=Bearer $COLLECTOR_TOKEN"
86
+ --endpoint https://collector.example/v1/traces
85
87
  ```
86
88
 
87
89
  Provider selection:
@@ -119,9 +121,12 @@ provider payload — the result is an identity **conflict**: attribution is decl
119
121
  and nothing is exported, rather than one assertion silently winning.
120
122
 
121
123
  No environment variable sets identity. The `OTEL_HOOK_*` variables and the
122
- standard `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_SERVICE_NAME`, and
123
- `OTEL_RESOURCE_ATTRIBUTES` configure the exporter, privacy, detection, and
124
- logging only.
124
+ standard `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_*HEADERS`,
125
+ `OTEL_SERVICE_NAME`, and `OTEL_RESOURCE_ATTRIBUTES` configure the exporter,
126
+ privacy, detection, and logging only. (A launcher may still put an exported
127
+ *session key* on the resource — see [Externally supplied session
128
+ key](#externally-supplied-session-key) — but that changes only the exported
129
+ `session.id` attribute, never invocation identity or attribution.)
125
130
 
126
131
  Exporter and runtime policy (never identity):
127
132
 
@@ -134,7 +139,8 @@ Exporter and runtime policy (never identity):
134
139
 
135
140
  Only header *names* enter the resolved-config snapshot that is logged and
136
141
  reported; the values go straight to the exporter, so a snapshot cannot leak a
137
- credential.
142
+ credential. Prefer the standard header variables over `--header` for secrets —
143
+ see [Exporter headers](#exporter-headers).
138
144
 
139
145
  `--resource-attr` and `--attr` are different channels and never mix.
140
146
  `--resource-attr` sets an OTLP **resource** attribute — a fact about this
@@ -676,7 +682,9 @@ file may enable the signal while the environment sets only the endpoint:
676
682
  | `maxBatchSize` | `128` | — | — |
677
683
 
678
684
  Everything else — protocol, headers, timeout, service identity, resource
679
- attributes — is shared with the traces signal, which is why `logs` is nested under
685
+ attributes — is shared with the traces signal (headers can additionally be set
686
+ for logs alone with `OTEL_EXPORTER_OTLP_LOGS_HEADERS`; see [Exporter
687
+ headers](#exporter-headers)), which is why `logs` is nested under
680
688
  `exporter` rather than being a second top-level section that could drift.
681
689
 
682
690
  Two combinations are reported as notes rather than errors, because both are things
@@ -685,6 +693,63 @@ with no endpoint and none derivable, and `includeContent` set while `contentMode
685
693
  `omit` (so there is nothing disclosed to carry — the symptom, every body withheld,
686
694
  otherwise looks like a bug).
687
695
 
696
+ ### Exporter headers
697
+
698
+ Header *values* — typically a credential — come from three standard OpenTelemetry
699
+ variables and the `--header` flag, resolved **per signal** and **per header
700
+ name** (case-insensitively), highest first:
701
+
702
+ ```
703
+ --header name=value > OTEL_EXPORTER_OTLP_TRACES_HEADERS > OTEL_EXPORTER_OTLP_HEADERS (traces)
704
+ --header name=value > OTEL_EXPORTER_OTLP_LOGS_HEADERS > OTEL_EXPORTER_OTLP_HEADERS (logs)
705
+ ```
706
+
707
+ So the generic variable applies to both signals, a signal-specific variable
708
+ overrides it for the names it sets on its own signal only, and a flag overrides
709
+ both. Prefer the variables for secrets: a flag is visible to every local user in
710
+ `ps` and `/proc/<pid>/cmdline`, an environment variable is not.
711
+
712
+ ```bash
713
+ # Basic auth: base64("public-key:secret-key"); the space is percent-encoded.
714
+ export OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic%20$(printf '%s' "$PUBLIC_KEY:$SECRET_KEY" | base64 -w0)"
715
+ ```
716
+
717
+ The format is the one the OpenTelemetry exporter specification defines:
718
+ comma-separated `key=value` pairs, surrounding whitespace trimmed, keys and
719
+ values **percent-decoded** (so a space is `%20` and a `,` inside a value is
720
+ `%2C`). Only the first `=` splits a pair, so base64 `=` padding survives. An
721
+ unusable entry — no `=`, an empty key or value, a key that is not an HTTP token,
722
+ malformed percent-encoding, a control or non-Latin-1 character in the value — is
723
+ skipped with a warning naming the variable and the entry's *position*, never its
724
+ key or value; the rest of the variable still applies. At most 32 headers per
725
+ variable.
726
+
727
+ Values never enter configuration: the environment layer contributes header
728
+ *names* to `exporter.headerNames` (reported as `exporter.header_names` by
729
+ `doctor --json` and in every snapshot), and the values are handed to the sinks
730
+ at construction. The CLI also keeps all of these `*_HEADERS` variables out of
731
+ the environment provider adapters can see.
732
+
733
+ ### Externally supplied session key
734
+
735
+ A launcher that runs coding agents for a larger unit of work can supply its own
736
+ session key, so every record of that run shares one `session.id`. It is read from
737
+ the resource attributes (normally `OTEL_RESOURCE_ATTRIBUTES`), in this order:
738
+
739
+ 1. `session.id` — the OpenTelemetry semantic-convention key;
740
+ 2. `har.session_key` — the legacy key earlier launchers send (also settable
741
+ through `HAR_SESSION_KEY`);
742
+ 3. otherwise, the provider's own session id.
743
+
744
+ ```bash
745
+ export OTEL_RESOURCE_ATTRIBUTES="session.id=run-2041,vcs.ref.head.name=feat%2Fexample"
746
+ ```
747
+
748
+ The provider's own session id is never discarded: it is always exported as
749
+ `otelhook.session.id` and `gen_ai.conversation.id`. The supplied key changes
750
+ only the exported `session.id` attribute; trace and span ids, state, and
751
+ attribution remain derived from the provider's identity.
752
+
688
753
  ### Resource attributes
689
754
 
690
755
  `exporter.resourceAttributes` adds custom attributes to the OTLP Resource every
package/dist/cli.js CHANGED
@@ -1154,6 +1154,127 @@ var errorInfoFromThrown = (thrown, input) => {
1154
1154
  });
1155
1155
  };
1156
1156
 
1157
+ // src/config/otlp-headers.ts
1158
+ var MAX_OTLP_HEADERS = 32;
1159
+ var OTLP_HEADER_VARIABLES = Object.freeze({
1160
+ generic: "OTEL_EXPORTER_OTLP_HEADERS",
1161
+ traces: "OTEL_EXPORTER_OTLP_TRACES_HEADERS",
1162
+ logs: "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
1163
+ });
1164
+ var HEADER_NAME_PATTERN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
1165
+ var isUnsafeHeaderValue = (value) => [...value].some((char) => {
1166
+ const code = char.codePointAt(0) ?? 0;
1167
+ return code < 32 && code !== 9 || code === 127 || code > 255;
1168
+ });
1169
+ var decode = (raw) => {
1170
+ try {
1171
+ return decodeURIComponent(raw);
1172
+ } catch {
1173
+ return void 0;
1174
+ }
1175
+ };
1176
+ var parseOtlpHeadersValue = (raw) => {
1177
+ const headers = {};
1178
+ const warnings = [];
1179
+ let overflowed = false;
1180
+ for (const [index, rawEntry] of raw.split(",").entries()) {
1181
+ const position = String(index + 1);
1182
+ const entry = rawEntry.trim();
1183
+ if (entry === "") {
1184
+ continue;
1185
+ }
1186
+ const equals = entry.indexOf("=");
1187
+ if (equals <= 0) {
1188
+ warnings.push(`entry ${position} is not a key=value pair; ignored`);
1189
+ continue;
1190
+ }
1191
+ const key = decode(entry.slice(0, equals).trim());
1192
+ const value = decode(entry.slice(equals + 1).trim());
1193
+ if (key === void 0 || value === void 0) {
1194
+ warnings.push(`entry ${position} has malformed percent-encoding; ignored`);
1195
+ continue;
1196
+ }
1197
+ const name = key.trim();
1198
+ const trimmedValue = value.trim();
1199
+ if (!HEADER_NAME_PATTERN.test(name)) {
1200
+ warnings.push(`entry ${position} does not name a valid HTTP header; ignored`);
1201
+ continue;
1202
+ }
1203
+ if (trimmedValue === "") {
1204
+ warnings.push(`entry ${position} has an empty value; ignored`);
1205
+ continue;
1206
+ }
1207
+ if (isUnsafeHeaderValue(trimmedValue)) {
1208
+ warnings.push(`entry ${position} has a character not allowed in an HTTP header value; ignored`);
1209
+ continue;
1210
+ }
1211
+ const existing = findHeaderName(headers, name);
1212
+ if (existing === void 0 && Object.keys(headers).length >= MAX_OTLP_HEADERS) {
1213
+ overflowed = true;
1214
+ continue;
1215
+ }
1216
+ if (existing !== void 0) {
1217
+ delete headers[existing];
1218
+ }
1219
+ headers[name] = trimmedValue;
1220
+ }
1221
+ if (overflowed) {
1222
+ warnings.push(`more than ${String(MAX_OTLP_HEADERS)} headers were supplied; the excess was ignored`);
1223
+ }
1224
+ return { headers, warnings };
1225
+ };
1226
+ var findHeaderName = (headers, name) => {
1227
+ const lower = name.toLowerCase();
1228
+ return Object.keys(headers).find((candidate) => candidate.toLowerCase() === lower);
1229
+ };
1230
+ var mergeOtlpHeaders = (...sources) => {
1231
+ const merged = {};
1232
+ for (const source of sources) {
1233
+ for (const [name, value] of Object.entries(source)) {
1234
+ const existing = findHeaderName(merged, name);
1235
+ if (existing !== void 0) {
1236
+ delete merged[existing];
1237
+ }
1238
+ merged[name] = value;
1239
+ }
1240
+ }
1241
+ return merged;
1242
+ };
1243
+ var resolveOtlpHeaders = (env, explicit = {}) => {
1244
+ const warnings = [];
1245
+ const read = (variable) => {
1246
+ const raw = env[variable];
1247
+ if (raw === void 0 || raw.trim() === "") {
1248
+ return {};
1249
+ }
1250
+ const parsed = parseOtlpHeadersValue(raw);
1251
+ for (const detail of parsed.warnings) {
1252
+ warnings.push({ variable, detail });
1253
+ }
1254
+ return parsed.headers;
1255
+ };
1256
+ const generic = read(OTLP_HEADER_VARIABLES.generic);
1257
+ const traces = read(OTLP_HEADER_VARIABLES.traces);
1258
+ const logs = read(OTLP_HEADER_VARIABLES.logs);
1259
+ return {
1260
+ traces: mergeOtlpHeaders(generic, traces, explicit),
1261
+ logs: mergeOtlpHeaders(generic, logs, explicit),
1262
+ warnings
1263
+ };
1264
+ };
1265
+ var unionOtlpHeaderNames = (...lists) => {
1266
+ const names = [];
1267
+ const seen = /* @__PURE__ */ new Set();
1268
+ for (const name of lists.flat()) {
1269
+ if (!seen.has(name.toLowerCase())) {
1270
+ seen.add(name.toLowerCase());
1271
+ names.push(name);
1272
+ }
1273
+ }
1274
+ return names.sort().slice(0, MAX_OTLP_HEADERS);
1275
+ };
1276
+ var describeOtlpHeaderNames = (...sources) => unionOtlpHeaderNames(...sources.map((source) => Object.keys(source)));
1277
+
1157
1278
  // src/config/environment.ts
1158
1279
  var ENVIRONMENT_VARIABLES = Object.freeze({
1159
1280
  exporterEnabled: "OTEL_HOOK_ENABLED",
@@ -1323,11 +1444,17 @@ var parseEnvironmentConfig = (env) => {
1323
1444
  ...logsEndpoint === void 0 ? {} : { endpoint: logsEndpoint },
1324
1445
  ...logsIncludeContent === void 0 ? {} : { includeContent: logsIncludeContent }
1325
1446
  };
1447
+ const otlpHeaders = resolveOtlpHeaders(env);
1448
+ for (const { variable, detail } of otlpHeaders.warnings) {
1449
+ warn(variable, detail);
1450
+ }
1451
+ const headerNames = describeOtlpHeaderNames(otlpHeaders.traces, otlpHeaders.logs);
1326
1452
  const exporter = {
1327
1453
  ...exporterEnabled === void 0 ? {} : { enabled: exporterEnabled },
1328
1454
  ...endpoint === void 0 ? {} : { endpoint },
1329
1455
  ...protocol === void 0 ? {} : { protocol },
1330
1456
  ...timeoutMillis === void 0 ? {} : { timeoutMillis },
1457
+ ...headerNames.length === 0 ? {} : { headerNames: [...headerNames] },
1331
1458
  ...serviceName === void 0 ? {} : { serviceName },
1332
1459
  ...serviceNamespace === void 0 ? {} : { serviceNamespace },
1333
1460
  ...Object.keys(resourceAttributes).length === 0 ? {} : { resourceAttributes },
@@ -1982,6 +2109,10 @@ var policyFlagsToPatch = (policy) => {
1982
2109
  ...Object.keys(diagnostics).length === 0 ? {} : { diagnostics }
1983
2110
  };
1984
2111
  };
2112
+ var resolveCliExporterHeaders = (policy, io) => {
2113
+ const { traces, logs } = resolveOtlpHeaders(io.env, policy.headers);
2114
+ return { traces, logs };
2115
+ };
1985
2116
  var readJsonFile = async (filePath) => {
1986
2117
  let raw;
1987
2118
  try {
@@ -2031,7 +2162,17 @@ var resolveCliConfig = async (policy, io) => {
2031
2162
  const environment = parseEnvironmentConfig(io.env);
2032
2163
  warnings.push(...environment.warnings);
2033
2164
  layers.push({ source: "environment", patch: environment.patch });
2034
- layers.push({ source: "inline-override", patch: policyFlagsToPatch(policy) });
2165
+ const inline = policyFlagsToPatch(policy);
2166
+ const environmentHeaderNames = environment.patch.exporter?.headerNames;
2167
+ const inlineHeaderNames = inline.exporter?.headerNames;
2168
+ const exporterPatch = environmentHeaderNames !== void 0 && inlineHeaderNames !== void 0 ? {
2169
+ ...inline.exporter,
2170
+ headerNames: [...unionOtlpHeaderNames(environmentHeaderNames, inlineHeaderNames)]
2171
+ } : inline.exporter;
2172
+ layers.push({
2173
+ source: "inline-override",
2174
+ patch: { ...inline, ...exporterPatch === void 0 ? {} : { exporter: exporterPatch } }
2175
+ });
2035
2176
  const resolution = resolveConfig(layers);
2036
2177
  if (resolution.status !== "ok") {
2037
2178
  warnings.push(...resolution.errors);
@@ -7682,7 +7823,10 @@ var ATTR_OTELHOOK_INVOCATION_ID = "otelhook.invocation.id";
7682
7823
  var ATTR_OTELHOOK_PROVIDER_ID = "otelhook.provider.id";
7683
7824
  var ATTR_OTELHOOK_PROVIDER_VERSION = "otelhook.provider.version";
7684
7825
  var ATTR_OTELHOOK_WORKSPACE_ID = "otelhook.workspace.id";
7685
- var HAR_SESSION_KEY = "har.session_key";
7826
+ var EXTERNAL_SESSION_KEY_ATTRIBUTES = Object.freeze([
7827
+ ATTR_SESSION_ID,
7828
+ "har.session_key"
7829
+ ]);
7686
7830
  var ATTR_OTELHOOK_SUBAGENT_ID = "otelhook.subagent.id";
7687
7831
  var ATTR_VCS_REF_HEAD_NAME = "vcs.ref.head.name";
7688
7832
  var ATTR_OTELHOOK_TARGET_WORKSPACE_ID = "otelhook.target.workspace.id";
@@ -7715,11 +7859,13 @@ var passthroughHarAttributes = (resource) => {
7715
7859
  return attributes;
7716
7860
  };
7717
7861
  var resolveExportedSessionId = (eventSessionId, resource) => {
7718
- const har = resource.attributes[HAR_SESSION_KEY];
7719
- if (typeof har === "string") {
7720
- const trimmed = har.trim();
7721
- if (trimmed.length > 0) {
7722
- 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
+ }
7723
7869
  }
7724
7870
  }
7725
7871
  return { sessionId: eventSessionId, providerSessionId: eventSessionId };
@@ -7764,7 +7910,7 @@ import {
7764
7910
  } from "@opentelemetry/semantic-conventions/incubating";
7765
7911
 
7766
7912
  // src/version.ts
7767
- var VERSION = "0.6.0";
7913
+ var VERSION = "0.7.0";
7768
7914
 
7769
7915
  // src/telemetry/semconv.ts
7770
7916
  var SCOPE_NAME = "@osfactory/otel-hook";
@@ -9178,9 +9324,10 @@ var collectDoctorReport = async (command, io) => {
9178
9324
  }
9179
9325
  }
9180
9326
  }
9327
+ const traceHeaders = resolveCliExporterHeaders(command.policy, io).traces;
9181
9328
  const sink = createOtlpTraceSink({
9182
9329
  exporter: resolved.config.exporter,
9183
- ...Object.keys(command.policy.headers).length === 0 ? {} : { headers: command.policy.headers },
9330
+ ...Object.keys(traceHeaders).length === 0 ? {} : { headers: traceHeaders },
9184
9331
  providerId: DOCTOR_STATE_NAMESPACE,
9185
9332
  installationId,
9186
9333
  clock,
@@ -12793,9 +12940,10 @@ var createHookRuntime = (options) => {
12793
12940
  ...spool === void 0 ? {} : { spool },
12794
12941
  correlate
12795
12942
  });
12943
+ const logHeaders = options.logHeaders ?? options.headers;
12796
12944
  const logSink = createOtlpLogSink({
12797
12945
  exporter: options.config.exporter,
12798
- ...options.headers === void 0 ? {} : { headers: options.headers },
12946
+ ...logHeaders === void 0 ? {} : { headers: logHeaders },
12799
12947
  providerId: options.providerNamespace,
12800
12948
  installationId: options.installationId,
12801
12949
  clock,
@@ -13395,6 +13543,7 @@ var runHookCommand = async (command, io) => {
13395
13543
  const fromFlags = claimFromIdentityFlags(command.identity);
13396
13544
  logDiagnostics(logger, fromFlags.errors);
13397
13545
  identityClaims.push(...fromFlags.claims);
13546
+ const headers = resolveCliExporterHeaders(command.policy, io);
13398
13547
  const runtime = createHookRuntime({
13399
13548
  // Detection policy narrowed to the selected adapter; everything else is the
13400
13549
  // resolved configuration exactly as it stands.
@@ -13403,7 +13552,8 @@ var runHookCommand = async (command, io) => {
13403
13552
  stateRootDir: resolveStateRootDir(command.policy, io),
13404
13553
  installationId: resolveInstallationId(command.policy, io),
13405
13554
  providerNamespace: selection.namespace,
13406
- ...Object.keys(command.policy.headers).length === 0 ? {} : { headers: command.policy.headers },
13555
+ ...Object.keys(headers.traces).length === 0 ? {} : { headers: headers.traces },
13556
+ ...Object.keys(headers.logs).length === 0 ? {} : { logHeaders: headers.logs },
13407
13557
  clock,
13408
13558
  ids,
13409
13559
  logger,