@osfactory/otel-hook 0.8.0 → 0.10.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.
Files changed (52) hide show
  1. package/README.md +159 -2
  2. package/dist/{adapter-BJiz6lqM.d.ts → adapter-B9RwW6TO.d.ts} +14 -2
  3. package/dist/cli.js +2514 -1238
  4. package/dist/cli.js.map +1 -1
  5. package/dist/config.d.ts +5 -2
  6. package/dist/config.js +479 -169
  7. package/dist/config.js.map +1 -1
  8. package/dist/destinations-DWOkVxV1.d.ts +169 -0
  9. package/dist/diagnostics.d.ts +8 -0
  10. package/dist/diagnostics.js +2 -1
  11. package/dist/diagnostics.js.map +1 -1
  12. package/dist/{events-B3MHoJTD.d.ts → events-CSD_YiJ2.d.ts} +2 -0
  13. package/dist/{otlp-log-sink-zuHojHSP.d.ts → export-destinations-lORbe25J.d.ts} +241 -9
  14. package/dist/{filesystem-store-r-nNA3BA.d.ts → filesystem-store-DX3dGIgu.d.ts} +1 -1
  15. package/dist/{hook-Dh8OO4Wa.d.ts → hook-BAxXoKsp.d.ts} +10 -5
  16. package/dist/index.d.ts +15 -14
  17. package/dist/index.js +1936 -730
  18. package/dist/index.js.map +1 -1
  19. package/dist/install.d.ts +4 -4
  20. package/dist/install.js +31 -2
  21. package/dist/install.js.map +1 -1
  22. package/dist/integration.d.ts +37 -12
  23. package/dist/integration.js +1942 -617
  24. package/dist/integration.js.map +1 -1
  25. package/dist/lifecycle.d.ts +3 -3
  26. package/dist/lifecycle.js +8 -1
  27. package/dist/lifecycle.js.map +1 -1
  28. package/dist/{memory-Bl9ANVff.d.ts → memory-aJilVG4R.d.ts} +2 -2
  29. package/dist/model.d.ts +1 -1
  30. package/dist/model.js +7 -0
  31. package/dist/model.js.map +1 -1
  32. package/dist/{payload-Ci8_ERm6.d.ts → payload-D7gc0-WQ.d.ts} +41 -2
  33. package/dist/{ports-n7KFOpKo.d.ts → ports-plO5MKrY.d.ts} +63 -2
  34. package/dist/providers.d.ts +146 -10
  35. package/dist/providers.js +397 -13
  36. package/dist/providers.js.map +1 -1
  37. package/dist/{registry-CIVvRlVK.d.ts → registry-C7XO4DzD.d.ts} +2 -2
  38. package/dist/runtime.d.ts +8 -8
  39. package/dist/runtime.js +1426 -1163
  40. package/dist/runtime.js.map +1 -1
  41. package/dist/{schema-Cn0-xz8L.d.ts → schema-CHb3GU1r.d.ts} +98 -3
  42. package/dist/{semconv-fPRb-dpU.d.ts → semconv-Dw7gcQcg.d.ts} +1 -1
  43. package/dist/state.d.ts +4 -4
  44. package/dist/state.js +607 -39
  45. package/dist/state.js.map +1 -1
  46. package/dist/telemetry.d.ts +23 -66
  47. package/dist/telemetry.js +637 -34
  48. package/dist/telemetry.js.map +1 -1
  49. package/dist/testing.d.ts +10 -8
  50. package/dist/testing.js +868 -560
  51. package/dist/testing.js.map +1 -1
  52. package/package.json +1 -1
package/README.md CHANGED
@@ -286,7 +286,10 @@ otel-hook doctor --json
286
286
 
287
287
  Reports the version, an attribute-safe configuration snapshot, a real
288
288
  write/read/delete probe of the state directory, spool depth, exporter
289
- configuration, privacy mode, and the adapter count. Exits 1 when any check fails,
289
+ configuration, privacy mode, and the adapter count. With
290
+ [multiple destinations](#multiple-destinations) it also reports each one: its
291
+ signals and endpoint origins, the environment variables its headers reference (by
292
+ name — never a value), any that are missing, and its spool depth and drops. Exits 1 when any check fails,
290
293
  so CI can gate on it. Every check is local: `doctor` never sends a span.
291
294
 
292
295
  ### `setup`, `diagnose`, `uninstall`
@@ -324,6 +327,10 @@ conservative by construction:
324
327
  create no directories; `--dry-run` prints the exact bytes a real run would
325
328
  write.
326
329
 
330
+ To register a hook that reads a configuration file — for example one with
331
+ [multiple destinations](#multiple-destinations) — pass the full command:
332
+ `--hook-command "otel-hook run --provider claude-code --config-file /path/to/otel-hook.json"`.
333
+
327
334
  `--scope` is `project` by default for `setup` and `uninstall` (writing into a home
328
335
  directory is the surprising choice) and `all` for `diagnose`. Only events the
329
336
  adapter turns into telemetry are registered; `--event` overrides that.
@@ -434,6 +441,8 @@ the remaining gap, and it is a *location* gap rather than a shape one: `setup`
434
441
  requires `--settings-file`. `PROVIDER_REGISTRATION_SUPPORT` carries the reason and
435
442
  the `evidenceBlocker` per provider;
436
443
  [docs/registration-evidence.md](docs/registration-evidence.md) is the long form.
444
+ End-to-end proof runs per provider (a real session, a disk OTLP receiver, and an
445
+ evidence checker) follow [docs/e2e-proof.md](docs/e2e-proof.md).
437
446
 
438
447
  ## Canonical model
439
448
 
@@ -813,6 +822,150 @@ export OTEL_RESOURCE_ATTRIBUTES="session.id=run-2041,factory.work_unit.id=wu-42,
813
822
  - Langfuse filters on span attributes, which is why `factory.*` and `vcs.*` are
814
823
  [copied onto every span](#on-records-too); `factory.local.*` is not.
815
824
 
825
+ #### Claude Code: model and token usage
826
+
827
+ Claude Code's `Stop` hook names no model and carries no tokens. Out of the box,
828
+ each generation span carries the session's model, which `SessionStart` reports
829
+ and the state store remembers (so `--state-dir`, or the default durable store,
830
+ matters).
831
+
832
+ For the exact per-turn model and the turn's summed token usage, turn on the
833
+ opt-in setting. otel-hook then reads the session transcript the payload names,
834
+ from where the previous turn ended, and sums every model request of the turn:
835
+
836
+ ```bash
837
+ otel-hook run --provider claude-code --claude-transcript-usage
838
+ # or: OTEL_HOOK_CLAUDE_TRANSCRIPT_USAGE=1
839
+ # or, in --config-file: { "providers": { "claudeCode": { "readTranscriptUsage": true } } }
840
+ ```
841
+
842
+ This is the one sanctioned exception to "no transcript reads in the hook": off by
843
+ default, done by the hook runtime and never by the adapter (ADR 0003), limited to
844
+ the one `.jsonl` file the payload names, capped at 8 MiB per turn and 500 ms, and
845
+ fail-open. When a turn cannot be summed exactly (no `SessionStart` was seen, the
846
+ state was lost, the turn wrote more than the cap), the last request is reported
847
+ instead and the span says so with `otelhook.usage.partial=true`.
848
+
849
+ A harness that wants to attach usage itself can call the library helper, which
850
+ reports the last request only (`usage_partial: true`):
851
+
852
+ ```ts
853
+ import { attachClaudeTranscriptTurn } from "@osfactory/otel-hook";
854
+
855
+ // Bounded: last 1 MiB of transcript_path, 250 ms, never throws, never
856
+ // overwrites a field the payload already has. Non-stop payloads pass through.
857
+ const enriched = await attachClaudeTranscriptTurn(hookPayload);
858
+ ```
859
+
860
+ See [finding 8](docs/claude-code-usage-contract.md#finding-8-the-model-is-on-sessionstart-and-in-the-transcript-never-on-stop).
861
+
862
+ ### Multiple destinations
863
+
864
+ `exporter.destinations` sends every batch to several OTLP endpoints at once — a
865
+ local Langfuse and a team Collector, say — with no proxy or Collector in between.
866
+ otel-hook is a short-lived process, so it does the fan-out itself: each enabled
867
+ destination is sent the same spans in parallel, each bounded by the exporter's
868
+ `timeoutMillis`, and whatever a destination could not take is kept in that
869
+ destination's own disk spool for the next hook event.
870
+
871
+ A standalone configuration, Langfuse plus a local Collector:
872
+
873
+ ```json
874
+ {
875
+ "exporter": {
876
+ "destinations": [
877
+ {
878
+ "name": "langfuse",
879
+ "endpoint": "http://localhost:3000/api/public/otel/v1/traces",
880
+ "headers": { "authorization": "env:LANGFUSE_AUTH" },
881
+ "signals": ["traces"]
882
+ },
883
+ {
884
+ "name": "collector",
885
+ "endpoint": "http://127.0.0.1:4318/v1/traces"
886
+ }
887
+ ]
888
+ }
889
+ }
890
+ ```
891
+
892
+ ```bash
893
+ # Langfuse's OTLP endpoint takes HTTP Basic auth: base64 of "public-key:secret-key".
894
+ export LANGFUSE_AUTH="Basic $(printf '%s' "$LANGFUSE_PUBLIC_KEY:$LANGFUSE_SECRET_KEY" | base64)"
895
+
896
+ otel-hook doctor --config-file ~/.config/otel-hook/destinations.json
897
+ otel-hook setup --provider claude-code \
898
+ --hook-command "otel-hook run --provider claude-code --config-file $HOME/.config/otel-hook/destinations.json"
899
+ ```
900
+
901
+ Each destination takes:
902
+
903
+ | Field | Default | Meaning |
904
+ | --- | --- | --- |
905
+ | `name` | — | Unique; lowercase letters, digits, `_` and `-`. Also its spool directory. `default` is reserved. |
906
+ | `endpoint` | — | Traces URL, as `exporter.endpoint`. The logs URL is derived from it (`/v1/traces` → `/v1/logs`). |
907
+ | `tracesEndpoint`, `logsEndpoint` | — | Per-signal URLs, overriding `endpoint`. At least one of the three is required. |
908
+ | `protocol` | `exporter.protocol` | `http/protobuf` is the protocol this build sends. |
909
+ | `headers` | `{}` | Header name → value. `"env:NAME"` reads the value from that environment variable when sending. |
910
+ | `signals` | `["traces", "logs"]` | Which signals it receives. Logs still need `exporter.logs.enabled`. |
911
+ | `enabled` | `true` | `false` keeps the entry but neither sends to it nor drains its spool. |
912
+
913
+ Everything else — timeout, batch sizes, service identity, resource attributes,
914
+ log content policy — is shared, so every destination receives the same spans with
915
+ the same trace and span ids.
916
+
917
+ **Secrets stay out of the file.** A header value of `env:LANGFUSE_AUTH` is read from
918
+ the hook's environment at send time; the variable's *name* is the only part that
919
+ ever appears in a snapshot, a diagnostic, or `doctor`. If the variable is unset or
920
+ empty, that destination sits the invocation out with a stderr diagnostic naming
921
+ the variable — it is never sent a request without its credentials — and `doctor`
922
+ fails the `destinations` check with the same name. Literal header values are
923
+ accepted for things that are not secrets (a tenant id) and are never printed either.
924
+
925
+ **The single endpoint is the `default` destination.** `exporter.endpoint`,
926
+ `exporter.logs.endpoint`, `--endpoint`, `--header`, the standard
927
+ `OTEL_EXPORTER_OTLP_ENDPOINT` / `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` /
928
+ `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` variables (see [Exporter
929
+ endpoints](#exporter-endpoints)), and the `OTEL_EXPORTER_OTLP_HEADERS` /
930
+ `OTEL_EXPORTER_OTLP_TRACES_HEADERS` / `OTEL_EXPORTER_OTLP_LOGS_HEADERS` variables
931
+ (see [Exporter headers](#exporter-headers)) still describe one destination, named
932
+ `default`, resolved exactly as described in those sections. Those header
933
+ variables apply to `default` only; a `destinations[]` entry sends exactly the
934
+ headers it lists. With no `destinations` list nothing changes. With a list,
935
+ `default` is sent to as well whenever one of those settings names an endpoint —
936
+ so an existing setup keeps receiving alongside the new ones.
937
+ The list merges as one leaf: a higher configuration layer that sets it replaces it
938
+ whole.
939
+
940
+ **A failing destination never blocks the others or the agent.** A destination
941
+ that refuses the connection, times out, or answers a retryable status (408, 429,
942
+ 500, 502, 503, 504) has the batch written to its spool; one that answers any other
943
+ error status has refused *this batch*, so it is reported and not spooled. A 500 is
944
+ treated as transient, like the OTLP-specified retryable set, because a backend
945
+ restarting or overloaded commonly answers with it; the spool's bounds below are
946
+ what keep a backend that answers 500 forever from holding batches forever. Either
947
+ way the hook exits 0 with a diagnostic on stderr, and the other destinations are
948
+ unaffected. A callback that reached at least one destination (or a spool) is
949
+ committed — retrying it would duplicate spans at the destinations that accepted.
950
+
951
+ **Spools are per destination and bounded.** Every destination queues under
952
+ `<state-dir>/<provider>/<installation>/destinations/<name>/` (`default` keeps the
953
+ original `spool` and `spool-logs` directories), and each spool is drained at the
954
+ end of the next hook event. Bounds apply per destination and per signal, and can
955
+ be set under `exporter.spool`:
956
+
957
+ | Bound | Default |
958
+ | --- | --- |
959
+ | `maxBytes` | 16 MiB |
960
+ | `maxAgeMillis` | 72 hours |
961
+ | `maxBatches` | 500 |
962
+
963
+ When a bound is reached the **oldest** batches are dropped first; a batch older
964
+ than `maxAgeMillis` is dropped rather than sent; a spooled batch a destination
965
+ later refuses with a non-retryable status is dropped instead of blocking the
966
+ queue behind it. Every drop is counted on disk, by reason, and `otel-hook doctor`
967
+ reports each destination's queued batches, bytes, and drops.
968
+
816
969
  ### Resource attributes
817
970
 
818
971
  `exporter.resourceAttributes` adds custom attributes to the OTLP Resource every
@@ -1050,7 +1203,11 @@ fixing one means updating that test rather than discovering a silent change.
1050
1203
  `usage.cache_read_input_tokens` and `usage.cache_creation_input_tokens` (whose
1051
1204
  TTL split is a breakdown, reconciled and never added). No hook callback carries
1052
1205
  a token counter at all, so `usage` is read only when a wrapping harness
1053
- attaches it. A harness that attaches an excluded counter anyway is told which
1206
+ attaches it, or the host turns on `providers.claudeCode.readTranscriptUsage`
1207
+ (`--claude-transcript-usage`), which makes the hook runtime sum each turn from
1208
+ `transcript_path`; the adapter itself never reads the transcript. Without it, generations
1209
+ still carry the session's model from `SessionStart`, remembered in the state
1210
+ store. A harness that attaches an excluded counter anyway is told which
1054
1211
  field was declined. `ADAPTER-NOTE-001`; see
1055
1212
  [docs/claude-code-usage-contract.md](docs/claude-code-usage-contract.md).
1056
1213
  6. **`contextTokensBefore` is an explicit exclusion for Claude Code.** Neither
@@ -1,11 +1,11 @@
1
1
  import { z } from 'zod';
2
2
  import { OtelHookErrorInfo } from './errors.js';
3
- import { I as UsageTemporality, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-B3MHoJTD.js';
3
+ import { I as UsageTemporality, n as ModelDescriptor, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-CSD_YiJ2.js';
4
4
  import { I as IdentityClaim, d as InvocationIdentity } from './identity-BrVBs901.js';
5
5
  import { R as ResolvedProviderId, g as ProviderId, h as SourceTransport, d as EventId } from './primitives-CJ8-rn-f.js';
6
6
  import { e as PrivacyLimits } from './policy-CimmCfRp.js';
7
7
  import { P as PrivacyService } from './service-CbI9CObT.js';
8
- import { C as Clock, I as IdGenerator, d as Logger } from './ports-n7KFOpKo.js';
8
+ import { C as Clock, I as IdGenerator, d as Logger } from './ports-plO5MKrY.js';
9
9
  import { A as AttributionOutcome, a as AttributionReason } from './taxonomy-DwhTtuWd.js';
10
10
 
11
11
  /** Raw input offered to adapters. `payload` is `unknown` on purpose. */
@@ -185,6 +185,18 @@ type ProviderParseInput = ProviderDetectionInput & {
185
185
  * consecutively from here so event ids stay stable across replays.
186
186
  */
187
187
  readonly sequenceBase: number;
188
+ /**
189
+ * The model this session last reported, as the core remembers it across
190
+ * invocations (`session.start` or a `generation.*` edge with a known model).
191
+ *
192
+ * Read from the state store by the core and handed in, because an adapter must
193
+ * not hold cross-invocation state itself (ADR 0006). Absent when no earlier
194
+ * invocation of this session named a model, or when the store is unreadable.
195
+ * An adapter whose generation callbacks carry no model id (Claude Code's
196
+ * `Stop`) uses it instead of the `unknown` sentinel; one whose callbacks carry
197
+ * their own model can ignore it.
198
+ */
199
+ readonly sessionModel?: ModelDescriptor;
188
200
  };
189
201
  type ProviderParseResult = {
190
202
  readonly status: "parsed";