@osfactory/otel-hook 0.10.0 → 0.12.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 (51) hide show
  1. package/README.md +108 -4
  2. package/dist/{adapter-B9RwW6TO.d.ts → adapter-DQiPu8xF.d.ts} +2 -2
  3. package/dist/cli.js +1495 -135
  4. package/dist/cli.js.map +1 -1
  5. package/dist/config.d.ts +11 -4
  6. package/dist/config.js +116 -21
  7. package/dist/config.js.map +1 -1
  8. package/dist/{destinations-DWOkVxV1.d.ts → destinations-DvqsPulb.d.ts} +12 -2
  9. package/dist/diagnostics.d.ts +1 -1
  10. package/dist/diagnostics.js.map +1 -1
  11. package/dist/{events-CSD_YiJ2.d.ts → events-fcomX6d5.d.ts} +22 -0
  12. package/dist/{export-destinations-lORbe25J.d.ts → export-destinations-CI7z4n3i.d.ts} +136 -6
  13. package/dist/{filesystem-store-DX3dGIgu.d.ts → filesystem-store-CmnUlj1O.d.ts} +1 -1
  14. package/dist/{hook-BAxXoKsp.d.ts → hook-vM_cgqS2.d.ts} +11 -5
  15. package/dist/index.d.ts +16 -16
  16. package/dist/index.js +1496 -183
  17. package/dist/index.js.map +1 -1
  18. package/dist/install.d.ts +4 -4
  19. package/dist/install.js +9 -1
  20. package/dist/install.js.map +1 -1
  21. package/dist/integration.d.ts +36 -12
  22. package/dist/integration.js +1464 -231
  23. package/dist/integration.js.map +1 -1
  24. package/dist/lifecycle.d.ts +55 -4
  25. package/dist/lifecycle.js +279 -5
  26. package/dist/lifecycle.js.map +1 -1
  27. package/dist/{memory-aJilVG4R.d.ts → memory-DdU1U91y.d.ts} +2 -2
  28. package/dist/{semconv-Dw7gcQcg.d.ts → metric-model-C9lLqqpI.d.ts} +157 -2
  29. package/dist/model.d.ts +1 -1
  30. package/dist/model.js +9 -1
  31. package/dist/model.js.map +1 -1
  32. package/dist/{payload-D7gc0-WQ.d.ts → payload-BT19hiVZ.d.ts} +1 -1
  33. package/dist/{ports-plO5MKrY.d.ts → ports-C3k_5eOQ.d.ts} +57 -2
  34. package/dist/providers.d.ts +10 -10
  35. package/dist/providers.js +10 -1
  36. package/dist/providers.js.map +1 -1
  37. package/dist/{registry-C7XO4DzD.d.ts → registry-gyJqaWmp.d.ts} +2 -2
  38. package/dist/runtime.d.ts +97 -9
  39. package/dist/runtime.js +312 -33
  40. package/dist/runtime.js.map +1 -1
  41. package/dist/{schema-CHb3GU1r.d.ts → schema-SpFM6YQY.d.ts} +87 -1
  42. package/dist/state.d.ts +4 -4
  43. package/dist/state.js +32 -1
  44. package/dist/state.js.map +1 -1
  45. package/dist/telemetry.d.ts +6 -6
  46. package/dist/telemetry.js +929 -31
  47. package/dist/telemetry.js.map +1 -1
  48. package/dist/testing.d.ts +11 -9
  49. package/dist/testing.js +164 -33
  50. package/dist/testing.js.map +1 -1
  51. package/package.json +3 -1
package/README.md CHANGED
@@ -286,7 +286,7 @@ 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. With
289
+ configuration, privacy mode, [session context](docs/session-context.md), and the adapter count. With
290
290
  [multiple destinations](#multiple-destinations) it also reports each one: its
291
291
  signals and endpoint origins, the environment variables its headers reference (by
292
292
  name — never a value), any that are missing, and its spool depth and drops. Exits 1 when any check fails,
@@ -581,6 +581,79 @@ signal, because a collector with no logs receiver leaves traces perfectly health
581
581
  The full mapping table, attribute vocabulary, bounds, and durability matrix are in
582
582
  [docs/canonical-log-mapping.md](docs/canonical-log-mapping.md).
583
583
 
584
+ ## Metrics
585
+
586
+ otel-hook can also send OpenTelemetry **metrics**, derived from the hook events it
587
+ already normalizes. They work for every supported provider, not only the ones with
588
+ native telemetry, and they go through the same destinations and spools as traces.
589
+
590
+ **Off by default.** Turn them on for the `default` destination with `--metrics`
591
+ (or `exporter.metrics.enabled` in a config file, or `OTEL_HOOK_METRICS_ENABLED=1`).
592
+ A `destinations[]` entry receives them only when its `signals` list names
593
+ `metrics`. A destination that lists only `traces` never receives a metric.
594
+
595
+ ```bash
596
+ otel-hook run --provider codex --endpoint http://localhost:4318/v1/traces --metrics
597
+ # metrics go to http://localhost:4318/v1/metrics
598
+ ```
599
+
600
+ | Metric | Type | Unit | Recorded |
601
+ | --- | --- | --- | --- |
602
+ | `gen_ai.client.token.usage` | histogram | `{token}` | Once per generation with usage, per `gen_ai.token.type`: `input`, `output`, `cache_read`, `cache_creation` |
603
+ | `gen_ai.client.operation.duration` | histogram | `s` | Once per generation whose duration is known (see below) |
604
+ | `otelhook.sessions` | counter | `{session}` | Once per `session.start` |
605
+ | `otelhook.tool.calls` | counter | `{call}` | Once per completed tool call, by `gen_ai.tool.name` and `otelhook.outcome` |
606
+ | `otelhook.cost.usage` | counter | `USD` | Only when the provider reports a cost. Never estimated |
607
+
608
+ Data-point attributes: `otelhook.provider.id` (the agent), `gen_ai.operation.name`,
609
+ `gen_ai.provider.name` (the model vendor, when known), `gen_ai.request.model`,
610
+ `gen_ai.response.model`, and `session.id` (the launcher's key when it sets one, like
611
+ spans). The resource is the same as for traces. Metrics carry **no** account
612
+ identity (`user.*`, `enduser.*` and `organization.*` keys are stripped, even from the
613
+ resource), no content, and no per-event ids.
614
+
615
+ - **Tokens.** `input` includes cache reads, as in the GenAI conventions, and
616
+ `cache_read` and `cache_creation` describe parts of it. Do not add the token types
617
+ together. A provider that reports a running total (Codex) is diffed against the
618
+ previous turn first, exactly as usage accounting does, so a session is never
619
+ counted twice.
620
+ - **Duration.** The first source that has it: the duration the provider states, a
621
+ `generation.start` recorded by an earlier hook, or the time the turn's prompt was
622
+ submitted. For Claude Code and Codex a generation is a turn, so this is the
623
+ turn's wall-clock time, tool calls included. A start the adapter made in the same
624
+ callback as the end is not used: it would record zero.
625
+ - **Temporality.** `cumulative` by default: each series' running total is kept in
626
+ the state directory, so a later hook process continues it, and receivers that
627
+ merge counters by keeping the largest value stay correct. Set
628
+ `exporter.metrics.temporality` to `delta` (or `--metrics-temporality delta`, or
629
+ `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta`) to send what each
630
+ callback observed and keep no state.
631
+ - **Cardinality.** `session.id` makes one series per session. Leave it off with
632
+ `exporter.metrics.includeSessionId: false` or `--no-metrics-session-id`.
633
+ - **Delivery.** Points are sent once, right after the callback's traces and logs,
634
+ inside the same bounded flush. There is no periodic reader, because a hook process
635
+ lives for one callback. What a destination refuses is kept in that destination's
636
+ `spool-metrics` queue and replayed by the next hook. Replaying a cumulative point
637
+ restates a total; it does not add to it.
638
+ - **Exactly once.** A redelivered callback, or one whose traces were lost and will be
639
+ retried, records nothing. A callback replayed after a crash restates its series'
640
+ totals instead of adding to them.
641
+
642
+ These numbers are **hook-derived**. They can differ from a provider's own
643
+ telemetry: for example, Claude Code's native `claude_code.token.usage` counts
644
+ subagent spend that hooks do not see yet (#68). A harness that already books Claude
645
+ Code usage from native telemetry should not add both.
646
+
647
+ | Field | Default | Flag | Variable |
648
+ | --- | --- | --- | --- |
649
+ | `exporter.metrics.enabled` | `false` | `--metrics` / `--no-metrics` | `OTEL_HOOK_METRICS_ENABLED` |
650
+ | `exporter.metrics.endpoint` | derived from `endpoint` (`/v1/traces` → `/v1/metrics`) | `--metrics-endpoint` | `OTEL_HOOK_METRICS_ENDPOINT`, then `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`, then `OTEL_EXPORTER_OTLP_ENDPOINT` |
651
+ | `exporter.metrics.temporality` | `cumulative` | `--metrics-temporality` | `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` (`lowmemory` reads as `delta`) |
652
+ | `exporter.metrics.includeSessionId` | `true` | `--no-metrics-session-id` | — |
653
+
654
+ Claude Code does not pass `OTEL_*` variables to hook processes, so under Claude Code
655
+ use the flags or a config file.
656
+
584
657
  ## Privacy
585
658
 
586
659
  Content is omitted by default; only lengths and a stable salted hash are
@@ -696,6 +769,12 @@ set (and not blank) wins:
696
769
  | logs | `OTEL_EXPORTER_OTLP_ENDPOINT` | base URL; `/v1/logs` is appended (see below) |
697
770
  | logs | `exporter.logs.endpoint` in a config file | full logs URL |
698
771
  | logs | derived from the resolved traces endpoint | `/v1/traces` swapped for `/v1/logs`, or `/v1/logs` appended |
772
+ | metrics | `--metrics-endpoint` | full metrics URL |
773
+ | metrics | `OTEL_HOOK_METRICS_ENDPOINT` | full metrics URL |
774
+ | metrics | `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` | full metrics URL, used as-is |
775
+ | metrics | `OTEL_EXPORTER_OTLP_ENDPOINT` | base URL; `/v1/metrics` is appended (same rule as logs) |
776
+ | metrics | `exporter.metrics.endpoint` in a config file | full metrics URL |
777
+ | metrics | derived from the resolved traces endpoint | `/v1/traces` swapped for `/v1/metrics`, or `/v1/metrics` appended |
699
778
 
700
779
  `OTEL_EXPORTER_OTLP_ENDPOINT` follows the OpenTelemetry exporter
701
780
  specification: it is a **base** URL, and the signal path is appended to it.
@@ -859,6 +938,28 @@ const enriched = await attachClaudeTranscriptTurn(hookPayload);
859
938
 
860
939
  See [finding 8](docs/claude-code-usage-contract.md#finding-8-the-model-is-on-sessionstart-and-in-the-transcript-never-on-stop).
861
940
 
941
+ #### Session context
942
+
943
+ A session's environment is fixed when the agent starts, but the work it does is
944
+ not: it can start in the main checkout and move into a slot later. A harness can
945
+ write what the session is working on to a **session context** file, and
946
+ otel-hook puts it on every span, log record and metric point from then on:
947
+
948
+ ```bash
949
+ otel-hook run --provider claude-code --session-context-dir /home/dev/.har/sessions
950
+ # or, in --config-file: { "sessionContext": { "dir": "/home/dev/.har/sessions" } }
951
+ ```
952
+
953
+ The hook reads `<dir>/<agent-tool>/<session-id>.json` (`claude_code`, `codex`,
954
+ `cursor`, …), picks the segment in force at each event's time, and applies its
955
+ attributes. They override the same keys from the Resource, on records and on the
956
+ Resource itself, so a stale `factory.attempt.id` from the environment is not
957
+ exported. Subagent events use their parent session's file. `factory.local.*` is
958
+ never applied. No file, an invalid one, or no segment in force: nothing is added,
959
+ and the hook never fails. There is no environment variable for it, because Claude
960
+ Code strips `OTEL_*` variables from hooks. Format and rules:
961
+ [docs/session-context.md](docs/session-context.md).
962
+
862
963
  ### Multiple destinations
863
964
 
864
965
  `exporter.destinations` sends every batch to several OTLP endpoints at once — a
@@ -904,10 +1005,10 @@ Each destination takes:
904
1005
  | --- | --- | --- |
905
1006
  | `name` | — | Unique; lowercase letters, digits, `_` and `-`. Also its spool directory. `default` is reserved. |
906
1007
  | `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. |
1008
+ | `tracesEndpoint`, `logsEndpoint`, `metricsEndpoint` | — | Per-signal URLs, overriding `endpoint`. At least one of the four is required. |
908
1009
  | `protocol` | `exporter.protocol` | `http/protobuf` is the protocol this build sends. |
909
1010
  | `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`. |
1011
+ | `signals` | `["traces", "logs"]` | Which signals it receives: `traces`, `logs`, `metrics`. Logs still need `exporter.logs.enabled`. Metrics are sent only when listed (see [Metrics](#metrics)). |
911
1012
  | `enabled` | `true` | `false` keeps the entry but neither sends to it nor drains its spool. |
912
1013
 
913
1014
  Everything else — timeout, batch sizes, service identity, resource attributes,
@@ -950,7 +1051,7 @@ committed — retrying it would duplicate spans at the destinations that accepte
950
1051
 
951
1052
  **Spools are per destination and bounded.** Every destination queues under
952
1053
  `<state-dir>/<provider>/<installation>/destinations/<name>/` (`default` keeps the
953
- original `spool` and `spool-logs` directories), and each spool is drained at the
1054
+ original `spool`, `spool-logs` and `spool-metrics` directories), and each spool is drained at the
954
1055
  end of the next hook event. Bounds apply per destination and per signal, and can
955
1056
  be set under `exporter.spool`:
956
1057
 
@@ -1045,6 +1146,9 @@ these namespaces are therefore also copied onto every span and log record:
1045
1146
  described above, resolved (and trimmed) on its own. Other attributes, such as
1046
1147
  `deployment.environment`, stay on the Resource only.
1047
1148
 
1149
+ [Session context](#session-context) attributes are layered over these on every
1150
+ record, and override the same keys on the Resource.
1151
+
1048
1152
  Diagnostics report resource attribute **names** only — `doctor --json` shows
1049
1153
  `exporter.resource_attribute_names`. A name is already on the wire in every
1050
1154
  exported resource; a value is not disclosed anywhere a name would do.
@@ -1,11 +1,11 @@
1
1
  import { z } from 'zod';
2
2
  import { OtelHookErrorInfo } from './errors.js';
3
- import { I as UsageTemporality, n as ModelDescriptor, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-CSD_YiJ2.js';
3
+ import { I as UsageTemporality, n as ModelDescriptor, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-fcomX6d5.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-plO5MKrY.js';
8
+ import { C as Clock, I as IdGenerator, d as Logger } from './ports-C3k_5eOQ.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. */