@bastani/atomic 0.10.14 → 0.10.15-alpha.1
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/CHANGELOG.md +17 -0
- package/dist/_shared/dist-KOELCESV.js +2 -2
- package/dist/_shared/fork-row-index-UAUAQRD5.js +2 -2
- package/dist/_shared/lib-4PES5P4V.js +2 -2
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +3 -3
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +3 -3
- package/dist/_shared/mode-shared-2QJSCUM6.js +3 -3
- package/dist/_shared/p1a-prewarm-XIU57JHF.js +3 -3
- package/dist/_shared/runtime-O4KCSG4C.js +2 -2
- package/dist/_shared/{sdk-B5LEWGGI.js → sdk-OHKMDJ74.js} +27 -5
- package/dist/_shared/{sdk-B5LEWGGI.js.map → sdk-OHKMDJ74.js.map} +1 -1
- package/dist/_shared/session-data-AHTU7IS6.js +2 -2
- package/dist/_shared/startup-0-0-Q3MWKUK4.js +2 -2
- package/dist/_shared/startup-0-1-M6OWAYHH.js +2 -2
- package/dist/_shared/{startup-0-2-5QHPCEON.js → startup-0-2-2HUMCPKX.js} +14 -16
- package/dist/_shared/startup-0-2-2HUMCPKX.js.map +1 -0
- package/dist/_shared/startup-0-3-4ZYBYQNM.js +3 -3
- package/dist/_shared/startup-0-4-ZMJV547D.js +3 -3
- package/dist/_shared/startup-0-5-YDW47QX6.js +3 -3
- package/dist/_shared/startup-10-0-TDQNBDDL.js +3 -3
- package/dist/_shared/startup-14-0-MLX36RBG.js +1 -1
- package/dist/_shared/startup-15-0-YUKGAC63.js +1 -1
- package/dist/_shared/startup-15-1-4S7FLMNG.js +6 -30
- package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
- package/dist/_shared/startup-8-0-34CBHPZH.js +3 -3
- package/dist/_shared/startup-8-1-LZFXSD3Z.js +3 -3
- package/dist/_shared/storage-command-7MGXLJGZ.js +2 -2
- package/dist/_shared/storage-status-K7PX2RXA.js +3 -3
- package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
- package/dist/_shared/tree-outline-MUEPK5B7.js +2 -2
- package/dist/builtin/intercom/index.bundle.mjs +1 -1
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/index.bundle.mjs +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/cli/auth-cli.code.js +1 -1
- package/dist/cli/auth-cli.impl.js +3 -3
- package/dist/cli/auth-cli.js +3 -3
- package/dist/cli/blueprint-command.impl.js +3 -3
- package/dist/cli/blueprint-command.js +3 -3
- package/dist/cli/intercom-command.code.js +3 -3
- package/dist/cli/intercom-command.impl.js +3 -3
- package/dist/cli/intercom-command.js +3 -3
- package/dist/cli/list-models.impl.js +3 -3
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/session-picker.code.js +1 -1
- package/dist/cli/session-picker.impl.js +3 -3
- package/dist/cli/session-picker.js +3 -3
- package/dist/cli/startup-ui.code.js +1 -1
- package/dist/cli/startup-ui.impl.js +3 -3
- package/dist/cli/startup-ui.js +3 -3
- package/dist/cli/storage-command.impl.js +3 -3
- package/dist/cli/storage-command.js +3 -3
- package/dist/cli/storage-search-session.code.js +1 -1
- package/dist/cli/storage-search-session.impl.js +3 -3
- package/dist/cli/storage-search-session.js +3 -3
- package/dist/cli/team-command.code.js +1 -1
- package/dist/cli/team-command.impl.js +3 -3
- package/dist/cli/team-command.js +3 -3
- package/dist/cli.js +2 -2
- package/dist/core/compaction/compactor.d.ts +0 -8
- package/dist/core/debug.impl.js +3 -3
- package/dist/core/debug.js +3 -3
- package/dist/core/export-html/card-projection.impl.js +3 -3
- package/dist/core/export-html/card-projection.js +3 -3
- package/dist/core/export-html/index.code.js +2 -2
- package/dist/core/export-html/index.impl.js +3 -3
- package/dist/core/export-html/index.js +3 -3
- package/dist/core/export-html/json-spans.impl.js +3 -3
- package/dist/core/export-html/json-spans.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.js +3 -3
- package/dist/core/export-html/tool-renderer.impl.js +3 -3
- package/dist/core/export-html/tool-renderer.js +3 -3
- package/dist/core/extensions/jiti-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-loader.js +3 -3
- package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-static-loader.js +3 -3
- package/dist/core/extensions/loader-host-modules.impl.js +3 -3
- package/dist/core/extensions/loader-host-modules.js +3 -3
- package/dist/core/intercom-broker-control-host.code.js +1 -1
- package/dist/core/intercom-broker-control-host.impl.js +3 -3
- package/dist/core/intercom-broker-control-host.js +3 -3
- package/dist/core/intercom-broker-control.code.js +2 -2
- package/dist/core/intercom-broker-control.impl.js +3 -3
- package/dist/core/intercom-broker-control.js +3 -3
- package/dist/core/package-manager-storage-adapters.impl.js +3 -3
- package/dist/core/package-manager-storage-adapters.js +3 -3
- package/dist/core/sdk.code.js +1 -1
- package/dist/core/sdk.impl.js +3 -3
- package/dist/core/sdk.js +3 -3
- package/dist/core/session-manager-core.js +3 -3
- package/dist/core/session-manager-list.impl.js +3 -3
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager.impl.js +3 -3
- package/dist/core/session-manager.js +3 -3
- package/dist/core/session-storage.impl.js +3 -3
- package/dist/core/session-storage.js +3 -3
- package/dist/core/storage/index.js +2 -2
- package/dist/core/storage/node-sqlite-thread.js +2 -2
- package/dist/core/storage/node-sqlite.js +2 -2
- package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
- package/dist/core/storage/offline-snapshot-file.js +3 -3
- package/dist/core/storage/provider.js +2 -2
- package/dist/core/storage/selection.js +2 -2
- package/dist/core/storage/session/branch-fork-source.code.js +2 -2
- package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
- package/dist/core/storage/session/branch-fork-source.js +3 -3
- package/dist/core/storage/session/card-view.impl.js +3 -3
- package/dist/core/storage/session/card-view.js +3 -3
- package/dist/core/storage/session/child-reader.code.js +2 -2
- package/dist/core/storage/session/child-reader.impl.js +3 -3
- package/dist/core/storage/session/child-reader.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.js +3 -3
- package/dist/core/storage/session/expanded-child-details.code.js +2 -2
- package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
- package/dist/core/storage/session/expanded-child-details.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.js +3 -3
- package/dist/core/storage/session/fork-row-index.impl.js +3 -3
- package/dist/core/storage/session/fork-row-index.js +3 -3
- package/dist/core/storage/session/fork-staging.code.js +1 -1
- package/dist/core/storage/session/fork-staging.impl.js +3 -3
- package/dist/core/storage/session/fork-staging.js +3 -3
- package/dist/core/storage/session/fork-window.code.js +2 -2
- package/dist/core/storage/session/fork-window.impl.js +3 -3
- package/dist/core/storage/session/fork-window.js +3 -3
- package/dist/core/storage/session/history-pages.impl.js +3 -3
- package/dist/core/storage/session/history-pages.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.js +3 -3
- package/dist/core/storage/session/memory-session-store.impl.js +3 -3
- package/dist/core/storage/session/memory-session-store.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.code.js +2 -2
- package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.js +3 -3
- package/dist/core/storage/session/released-history-files.code.js +1 -1
- package/dist/core/storage/session/released-history-files.impl.js +3 -3
- package/dist/core/storage/session/released-history-files.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.js +3 -3
- package/dist/core/storage/session/session-resume.impl.js +3 -3
- package/dist/core/storage/session/session-resume.js +3 -3
- package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
- package/dist/core/storage/session/snapshot-cut.js +3 -3
- package/dist/core/storage/session/snapshot-history.code.js +2 -2
- package/dist/core/storage/session/snapshot-history.impl.js +3 -3
- package/dist/core/storage/session/snapshot-history.js +3 -3
- package/dist/core/storage/session/sql-session-store.code.js +1 -1
- package/dist/core/storage/session/sql-session-store.impl.js +3 -3
- package/dist/core/storage/session/sql-session-store.js +3 -3
- package/dist/core/storage/session/tree-outline.impl.js +3 -3
- package/dist/core/storage/session/tree-outline.js +3 -3
- package/dist/core/storage/session/tree-presentation.impl.js +3 -3
- package/dist/core/storage/session/tree-presentation.js +3 -3
- package/dist/core/storage/storage-status.impl.js +3 -3
- package/dist/core/storage/storage-status.js +3 -3
- package/dist/core/storage/team.impl.js +3 -3
- package/dist/core/storage/team.js +3 -3
- package/dist/core/structured-output/index.code.js +1 -1
- package/dist/core/structured-output/index.impl.js +3 -3
- package/dist/core/structured-output/index.js +3 -3
- package/dist/core/tasks/task-completion-entry.impl.js +3 -3
- package/dist/core/tasks/task-completion-entry.js +3 -3
- package/dist/core/tasks/task-file-history.code.js +2 -2
- package/dist/core/tasks/task-file-history.impl.js +3 -3
- package/dist/core/tasks/task-file-history.js +3 -3
- package/dist/core/tasks/task-reference-archive.code.js +2 -2
- package/dist/core/tasks/task-reference-archive.js +3 -3
- package/dist/core/tasks/task-reference-identity.js +3 -3
- package/dist/core/tasks/task-reference-stream.impl.js +3 -3
- package/dist/core/tasks/task-reference-stream.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.js +3 -3
- package/dist/core/tasks/task-view-source.impl.js +3 -3
- package/dist/core/tasks/task-view-source.js +3 -3
- package/dist/core/tools/read-url.impl.js +3 -3
- package/dist/core/tools/read-url.js +3 -3
- package/dist/core/worktree-snapshot/blob-store.js +2 -2
- package/dist/core/worktree-snapshot/errors.code.js +1 -1
- package/dist/core/worktree-snapshot/errors.impl.js +3 -3
- package/dist/core/worktree-snapshot/errors.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
- package/dist/extensions/codemode/execute.impl.js +3 -3
- package/dist/extensions/codemode/execute.js +3 -3
- package/dist/extensions/codemode/renderer.impl.js +3 -3
- package/dist/extensions/codemode/renderer.js +3 -3
- package/dist/extensions/mcp/cli.code.js +1 -1
- package/dist/extensions/mcp/cli.impl.js +3 -3
- package/dist/extensions/mcp/cli.js +3 -3
- package/dist/extensions/mcp/cli.lazy.code.js +2 -2
- package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
- package/dist/extensions/mcp/cli.lazy.js +3 -3
- package/dist/extensions/mcp/runtime.code.js +1 -1
- package/dist/extensions/mcp/runtime.impl.js +3 -3
- package/dist/extensions/mcp/runtime.js +3 -3
- package/dist/index.d.ts +21 -1
- package/dist/index.js +2 -2
- package/dist/main-first-time-setup.code.js +1 -1
- package/dist/main-first-time-setup.impl.js +3 -3
- package/dist/main-first-time-setup.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.code.js +2 -2
- package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
- package/dist/modes/print-mode.js +1 -1
- package/dist/package-manager-cli.code.js +2 -2
- package/dist/package-manager-cli.impl.js +3 -3
- package/dist/package-manager-cli.js +3 -3
- package/dist/startup-files.json +1 -1
- package/dist/startup-stages/6.js +1 -1
- package/dist/utils/startup-database-spare.code.js +1 -1
- package/dist/utils/startup-database-spare.impl.js +3 -3
- package/dist/utils/startup-database-spare.js +3 -3
- package/dist/utils/tools-manager.impl.js +3 -3
- package/dist/utils/tools-manager.js +3 -3
- package/docs/background-tasks/reference.md +182 -0
- package/docs/background-tasks.md +98 -405
- package/docs/build.md +67 -27
- package/docs/changelog.mdx +1 -1
- package/docs/codemode.md +108 -39
- package/docs/compaction/branch-summaries.md +54 -0
- package/docs/compaction/custom-compactors.md +161 -0
- package/docs/compaction/memory-compaction.md +77 -0
- package/docs/compaction/reference.md +86 -38
- package/docs/compaction.md +51 -252
- package/docs/computer-use.md +169 -321
- package/docs/containerization.md +71 -57
- package/docs/custom-provider/api-reference.md +119 -25
- package/docs/custom-provider/oauth.md +114 -132
- package/docs/custom-provider/override.md +37 -27
- package/docs/custom-provider/registration.md +107 -83
- package/docs/custom-provider/streaming.md +85 -71
- package/docs/custom-provider.md +85 -52
- package/docs/development.md +39 -29
- package/docs/docs.json +76 -63
- package/docs/environment-variables.md +94 -44
- package/docs/extensions/api-reference.md +86 -43
- package/docs/extensions/authoring.md +162 -154
- package/docs/extensions/events.md +80 -25
- package/docs/extensions/examples.md +90 -85
- package/docs/extensions/ui.md +206 -82
- package/docs/extensions.md +79 -108
- package/docs/getting-started/authentication.md +67 -36
- package/docs/getting-started/first-session.md +53 -43
- package/docs/getting-started/installation.md +116 -89
- package/docs/getting-started/project-instructions.md +48 -34
- package/docs/guides/configuration.md +82 -20
- package/docs/guides/non-interactive.md +66 -24
- package/docs/guides/resume-on-any-machine.md +135 -82
- package/docs/guides/team-setup.md +114 -105
- package/docs/guides.md +76 -32
- package/docs/herdr.md +15 -16
- package/docs/how-atomic-works.md +140 -0
- package/docs/index.md +58 -44
- package/docs/intercom/operations.md +141 -151
- package/docs/intercom/reference.md +85 -108
- package/docs/intercom.md +84 -145
- package/docs/json.md +120 -67
- package/docs/keybindings.md +85 -49
- package/docs/libraries/agent-core.md +137 -275
- package/docs/libraries/ai.md +100 -84
- package/docs/libraries/codemode.md +210 -300
- package/docs/libraries/schema.md +59 -53
- package/docs/libraries/storage-postgres.md +49 -35
- package/docs/libraries/telemetry.md +153 -354
- package/docs/libraries/tui.md +265 -393
- package/docs/libraries.md +42 -26
- package/docs/llama-cpp.md +171 -161
- package/docs/mcp-servers.md +90 -51
- package/docs/models/choose-a-model.md +188 -0
- package/docs/models/reference.md +74 -30
- package/docs/models.md +153 -88
- package/docs/orchestration/migrating-workflows.md +23 -16
- package/docs/orchestration/patterns.md +39 -44
- package/docs/orchestration.md +65 -26
- package/docs/packages/authoring.md +51 -29
- package/docs/packages/reference.md +42 -21
- package/docs/packages.md +69 -58
- package/docs/programmatic.md +118 -24
- package/docs/prompt-templates.md +84 -83
- package/docs/providers/reference.md +43 -48
- package/docs/providers.md +219 -451
- package/docs/quickstart.md +140 -120
- package/docs/reference/cli.md +135 -97
- package/docs/reference.md +20 -18
- package/docs/rpc/examples.md +76 -20
- package/docs/rpc/extension-ui.md +64 -38
- package/docs/rpc/protocol.md +361 -110
- package/docs/rpc.md +57 -17
- package/docs/sdk/reference.md +409 -534
- package/docs/sdk/storage.md +53 -13
- package/docs/sdk/structured-decisions.md +82 -56
- package/docs/sdk/web-server.md +57 -43
- package/docs/sdk.md +106 -84
- package/docs/security.md +42 -64
- package/docs/session-format.md +62 -38
- package/docs/sessions.md +99 -214
- package/docs/settings.md +165 -195
- package/docs/shell-aliases.md +9 -6
- package/docs/skills/authoring.md +69 -48
- package/docs/skills/reference.md +51 -20
- package/docs/skills.md +99 -148
- package/docs/slash-commands.md +129 -0
- package/docs/storage/custom-adapters.md +160 -85
- package/docs/storage/jsonl.md +81 -66
- package/docs/storage/postgres/pgbouncer.md +120 -126
- package/docs/storage/postgres/tuning.md +120 -143
- package/docs/storage/postgres.md +314 -330
- package/docs/storage/sqlite.md +71 -42
- package/docs/storage.md +298 -302
- package/docs/subagents/authoring.md +55 -38
- package/docs/subagents/reference.md +73 -33
- package/docs/subagents.md +119 -187
- package/docs/terminal-setup.md +50 -38
- package/docs/termux.md +67 -41
- package/docs/themes/reference.md +23 -19
- package/docs/themes.md +142 -127
- package/docs/tmux.md +47 -55
- package/docs/tools.md +147 -109
- package/docs/tui/reference.md +166 -214
- package/docs/tui.md +196 -341
- package/docs/usage.md +118 -138
- package/docs/verification.md +79 -51
- package/docs/web-access.md +121 -101
- package/docs/windows.md +26 -27
- package/docs/worktree-snapshots/reference.md +245 -0
- package/docs/worktree-snapshots.md +71 -259
- package/package.json +8 -8
- package/dist/_shared/startup-0-2-5QHPCEON.js.map +0 -1
- package/docs/guides/intercom.md +0 -32
- package/docs/guides/orchestration.md +0 -28
- package/docs/guides/subagents.md +0 -37
|
@@ -4,19 +4,15 @@ sidebarTitle: "@bastani/atomic-telemetry"
|
|
|
4
4
|
description: "Vendor-neutral telemetry contracts, no-op and in-memory contexts, metrics, trace rendering, and typed telemetry schemas."
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
Vendor-neutral telemetry contracts and typed schema utilities for Atomic's libraries. This package contains no exporter and sends nothing on its own; you supply the adapter. See [Standalone libraries](/libraries) for all standalone packages.
|
|
7
|
+
Vendor-neutral telemetry contracts and typed schema utilities for Atomic's libraries. This package contains no exporter and sends nothing on its own; you supply the adapter for OpenTelemetry, Sentry, logs, or another backend. See [Standalone libraries](/libraries) for all standalone packages.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Key capabilities:
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- Opaque cross-process propagation and span links.
|
|
14
|
-
- Counters, up-down counters, histograms, and typed metric schemas.
|
|
15
|
-
-
|
|
16
|
-
- Serializable schema definitions with inferred TypeScript types.
|
|
17
|
-
- No exporter, global current-span state, or dependency on a telemetry backend.
|
|
18
|
-
|
|
19
|
-
Applications can use the in-memory reference or supply an adapter for OpenTelemetry, Sentry, logs, or another backend. Atomic's packages pass telemetry contexts explicitly and define their domain schemas separately.
|
|
11
|
+
- Explicit, callback-based `TelemetryContext` and `TelemetrySpan` contracts with zero ambient global state.
|
|
12
|
+
- Shared `NOOP_TELEMETRY_CONTEXT`, `NOOP_TELEMETRY_METRICS`, and `NOOP_TELEMETRY_PROPAGATION`.
|
|
13
|
+
- Opaque cross-process propagation (`TelemetryCarrier`) and span links.
|
|
14
|
+
- Counters, up-down counters, histograms, and compile-time typed metric and span schemas.
|
|
15
|
+
- Reference `InMemoryTelemetryContext` adapter, `TelemetryStore` queries, trace tree renderer, and conformance test suite.
|
|
20
16
|
|
|
21
17
|
## Installation
|
|
22
18
|
|
|
@@ -28,26 +24,26 @@ npm install @bastani/atomic-telemetry
|
|
|
28
24
|
|
|
29
25
|
```typescript
|
|
30
26
|
import {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
InMemoryTelemetryContext,
|
|
28
|
+
NOOP_TELEMETRY_CONTEXT,
|
|
29
|
+
type TelemetryContext,
|
|
34
30
|
} from "@bastani/atomic-telemetry";
|
|
35
31
|
import { renderTrace } from "@bastani/atomic-telemetry/render";
|
|
36
32
|
|
|
37
33
|
const accounts = new Map<string, { id: string; plan: string }>([["acct-1", { id: "acct-1", plan: "pro" }]]);
|
|
38
34
|
|
|
39
35
|
async function loadAccount(accountId: string, telemetry: TelemetryContext = NOOP_TELEMETRY_CONTEXT) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
36
|
+
return telemetry.startSpan(
|
|
37
|
+
{
|
|
38
|
+
name: "example.account.load",
|
|
39
|
+
attributes: { "example.account.id": accountId },
|
|
40
|
+
},
|
|
41
|
+
async (span) => {
|
|
42
|
+
const account = accounts.get(accountId);
|
|
43
|
+
span.setAttributes({ "example.account.found": account !== undefined });
|
|
44
|
+
return account;
|
|
45
|
+
},
|
|
46
|
+
);
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
const recorder = new InMemoryTelemetryContext();
|
|
@@ -59,18 +55,14 @@ if (traces[0]) console.log(renderTrace(traces[0].spans));
|
|
|
59
55
|
|
|
60
56
|
## Telemetry concepts
|
|
61
57
|
|
|
62
|
-
Telemetry describes what a program did while running using spans, attributes, events, statuses, and explicit context:
|
|
63
|
-
|
|
64
58
|
| Concept | Plain-language meaning |
|
|
65
|
-
|
|
66
|
-
| **Span** | A timed record of one operation,
|
|
67
|
-
| **Parent and child spans** | Operations can
|
|
68
|
-
| **Attribute** | A named fact attached to a span, such as `provider: "openai"
|
|
69
|
-
| **Event** | A
|
|
70
|
-
| **Status** | The
|
|
71
|
-
| **Context** | A handle identifying where new
|
|
72
|
-
|
|
73
|
-
For example, loading an account produces this structure:
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| **Span** | A timed record of one operation, starting before the work and ending when it settles. |
|
|
61
|
+
| **Parent and child spans** | Operations can nest inside one another, forming a tree showing where time was spent. |
|
|
62
|
+
| **Attribute** | A named fact attached to a span, such as `provider: "openai"` or `cache.hit: true`. |
|
|
63
|
+
| **Event** | A point-in-time occurrence inside a span (such as `retry.scheduled`), optionally carrying its own attributes. |
|
|
64
|
+
| **Status** | The final outcome: `ok` or `error` (optionally with an error name and message). |
|
|
65
|
+
| **Context** | A handle identifying where new child spans belong in the span tree. |
|
|
74
66
|
|
|
75
67
|
```text
|
|
76
68
|
example.account.load span
|
|
@@ -80,57 +72,26 @@ example.account.load span
|
|
|
80
72
|
└─ status: ok final outcome
|
|
81
73
|
```
|
|
82
74
|
|
|
83
|
-
A span is diagnostic data, not business state. An adapter translates these generic concepts into the corresponding concepts used by OpenTelemetry, Sentry, logs, or another backend.
|
|
84
|
-
|
|
85
75
|
## Core context API
|
|
86
76
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
import {
|
|
91
|
-
NOOP_TELEMETRY_CONTEXT,
|
|
92
|
-
type TelemetryContext,
|
|
93
|
-
} from '@bastani/atomic-telemetry';
|
|
94
|
-
|
|
95
|
-
async function loadAccount(
|
|
96
|
-
accountId: string,
|
|
97
|
-
telemetryContext: TelemetryContext = NOOP_TELEMETRY_CONTEXT,
|
|
98
|
-
) {
|
|
99
|
-
return telemetryContext.startSpan(
|
|
100
|
-
{
|
|
101
|
-
name: 'example.account.load',
|
|
102
|
-
attributes: { 'example.account.id': accountId },
|
|
103
|
-
},
|
|
104
|
-
async (span) => {
|
|
105
|
-
const account = await readAccount(accountId);
|
|
106
|
-
span.setAttributes({ 'example.account.found': account !== undefined });
|
|
107
|
-
return account;
|
|
108
|
-
},
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
Pass the callback span to lower-level work to create explicit nesting:
|
|
77
|
+
`TelemetryContext.startSpan()` starts a span around a callback. The callback receives a `TelemetrySpan` that also serves as the explicit parent context for nested child spans. Contexts may expose optional `metrics?: TelemetryMetrics` and `propagation?: TelemetryPropagation` capabilities, inherited by child spans and extracted parents.
|
|
114
78
|
|
|
115
79
|
```typescript
|
|
116
|
-
return telemetryContext.startSpan({ name:
|
|
117
|
-
return parentSpan.startSpan({ name:
|
|
118
|
-
childSpan.addEvent(
|
|
80
|
+
return telemetryContext.startSpan({ name: "example.parent" }, async (parentSpan) => {
|
|
81
|
+
return parentSpan.startSpan({ name: "example.child" }, async (childSpan) => {
|
|
82
|
+
childSpan.addEvent("example.cache.lookup", { "example.cache.hit": true });
|
|
119
83
|
return performWork();
|
|
120
84
|
});
|
|
121
85
|
});
|
|
122
86
|
```
|
|
123
87
|
|
|
124
|
-
There is no public `end()` method
|
|
88
|
+
There is no public `end()` method—`startSpan()` settles the span automatically when the callback returns, resolves, throws, or rejects. For expected failures returned normally, set the status explicitly:
|
|
125
89
|
|
|
126
90
|
```typescript
|
|
127
|
-
return telemetryContext.startSpan({ name:
|
|
91
|
+
return telemetryContext.startSpan({ name: "example.save" }, async (span) => {
|
|
128
92
|
const result = await save();
|
|
129
93
|
if (!result.ok) {
|
|
130
|
-
span.setStatus({
|
|
131
|
-
status: 'error',
|
|
132
|
-
error: { name: 'SaveError', message: result.reason },
|
|
133
|
-
});
|
|
94
|
+
span.setStatus({ status: "error", error: { name: "SaveError", message: result.reason } });
|
|
134
95
|
}
|
|
135
96
|
return result;
|
|
136
97
|
});
|
|
@@ -138,25 +99,19 @@ return telemetryContext.startSpan({ name: 'example.save' }, async (span) => {
|
|
|
138
99
|
|
|
139
100
|
## Adapter contract
|
|
140
101
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
The callback and recording contract requires an adapter to:
|
|
102
|
+
Adapters implement `TelemetryContext` and bridge it to a backend. Typed inputs are trusted (adapters do not need to validate inputs or suppress backend errors). Required behaviors:
|
|
144
103
|
|
|
145
104
|
- Create a child span and invoke the callback synchronously, exactly once.
|
|
146
|
-
-
|
|
147
|
-
-
|
|
148
|
-
-
|
|
149
|
-
-
|
|
150
|
-
- Make repeated `setStatus()` calls last-write-wins.
|
|
151
|
-
- Merge `setAttributes()` calls so later defined values replace earlier ones while `undefined` is ignored.
|
|
152
|
-
- Make recording methods synchronous.
|
|
153
|
-
- Ignore calls made after settlement.
|
|
105
|
+
- Preserve return values and rejections, converting synchronous throws to rejected promises with the same error.
|
|
106
|
+
- Keep the native span open until any returned promise settles; mark normal completion `ok` and throws/rejections `error` unless `setStatus()` was explicitly called.
|
|
107
|
+
- Make `setStatus()` last-write-wins, merge `setAttributes()` (later defined values replace earlier ones; `undefined` is ignored), keep recording methods synchronous, and ignore calls after settlement.
|
|
108
|
+
- Propagate optional `metrics` and `propagation` capabilities onto child spans, extracted parents, and inert fallback spans.
|
|
154
109
|
|
|
155
|
-
|
|
110
|
+
Run the [adapter conformance suite](#adapter-conformance) to verify any custom adapter.
|
|
156
111
|
|
|
157
112
|
## Propagation and span links
|
|
158
113
|
|
|
159
|
-
|
|
114
|
+
Expose optional cross-process propagation via `context.propagation`:
|
|
160
115
|
|
|
161
116
|
```typescript
|
|
162
117
|
interface TelemetryPropagation {
|
|
@@ -166,27 +121,27 @@ interface TelemetryPropagation {
|
|
|
166
121
|
type TelemetryCarrier = Record<string, string>;
|
|
167
122
|
```
|
|
168
123
|
|
|
169
|
-
|
|
124
|
+
Carriers are opaque JSON-safe string maps (for example W3C `traceparent`/`tracestate`). Transport code sends only the carrier, never a context or span:
|
|
170
125
|
|
|
171
126
|
```typescript
|
|
172
127
|
const caller = new InMemoryTelemetryContext();
|
|
173
128
|
const provider = new InMemoryTelemetryContext();
|
|
174
|
-
await caller.startSpan({ name:
|
|
129
|
+
await caller.startSpan({ name: "rpc.client" }, async (span) => {
|
|
175
130
|
const wire = JSON.stringify(caller.inject(span));
|
|
176
131
|
await provider.extract(JSON.parse(wire)).startSpan(
|
|
177
|
-
{ name:
|
|
132
|
+
{ name: "rpc.server" },
|
|
178
133
|
(serverSpan) => handleRequest(serverSpan),
|
|
179
134
|
);
|
|
180
135
|
});
|
|
181
136
|
```
|
|
182
137
|
|
|
183
|
-
|
|
138
|
+
`inject()` returns a detached carrier (or `undefined` for roots, no-op contexts, or unsupported contexts). `extract()` returns a receiver-owned parent, falling back to the adapter's root for absent or unknown carriers.
|
|
184
139
|
|
|
185
|
-
|
|
140
|
+
Relate spans without changing parentage via `SpanOptions.links` (at creation) or `span.addLink()` (while active):
|
|
186
141
|
|
|
187
142
|
```typescript
|
|
188
143
|
await telemetry.startSpan(
|
|
189
|
-
{ name:
|
|
144
|
+
{ name: "activation", links: [{ context: firstGetter, attributes: { reason: "trigger" } }] },
|
|
190
145
|
async (activation) => {
|
|
191
146
|
laterGetter.addLink({ context: activation });
|
|
192
147
|
await activate();
|
|
@@ -194,53 +149,46 @@ await telemetry.startSpan(
|
|
|
194
149
|
);
|
|
195
150
|
```
|
|
196
151
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
`NOOP_TELEMETRY_PROPAGATION` ignores its arguments: injection returns `undefined`, extraction returns `NOOP_TELEMETRY_CONTEXT`.
|
|
152
|
+
Settled targets remain linkable and injectable; settled sources ignore `addLink()`. `NOOP_TELEMETRY_PROPAGATION` returns `undefined` from `inject()` and `NOOP_TELEMETRY_CONTEXT` from `extract()`.
|
|
200
153
|
|
|
201
154
|
## Metrics
|
|
202
155
|
|
|
203
|
-
|
|
156
|
+
Expose optional metrics via `context.metrics` so nested calls can record measurements without separate configuration:
|
|
204
157
|
|
|
205
158
|
```typescript
|
|
206
|
-
const bytes = telemetry.counter(
|
|
207
|
-
|
|
208
|
-
});
|
|
209
|
-
bytes.add(encodedSize, { direction: 'out' });
|
|
159
|
+
const bytes = telemetry.counter("network.bytes", { description: "Encoded WebSocket bytes", unit: "By" });
|
|
160
|
+
bytes.add(encodedSize, { direction: "out" });
|
|
210
161
|
|
|
211
|
-
const active = telemetry.upDownCounter(
|
|
162
|
+
const active = telemetry.upDownCounter("calls.active");
|
|
212
163
|
active.add(1);
|
|
213
164
|
active.add(-1);
|
|
214
165
|
|
|
215
|
-
const duration = telemetry.histogram(
|
|
216
|
-
|
|
217
|
-
});
|
|
218
|
-
duration.record(elapsedMs, { method: 'read' }, currentSpan);
|
|
166
|
+
const duration = telemetry.histogram("call.duration", { unit: "ms", boundaries: [1, 10, 100, 1000] });
|
|
167
|
+
duration.record(elapsedMs, { method: "read" }, currentSpan);
|
|
219
168
|
```
|
|
220
169
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
Callers must supply finite measurements and non-negative counter increments, while up-down counters and histograms accept signed values. Histogram `record(value, attributes?, context?)` optionally correlates a measurement with a span as an exemplar. Counter `add(value, attributes?)` takes no context argument.
|
|
224
|
-
|
|
225
|
-
Factories and recording methods are synchronous and trust typed inputs. Adapters do not need to validate measurement values, metadata, attributes, or boundaries, and malformed inputs have unspecified behavior. `NOOP_TELEMETRY_METRICS` returns shared frozen instruments that discard measurements. Record only values already available to application code; do not serialize or measure extra data solely for telemetry.
|
|
226
|
-
|
|
227
|
-
Use only low-cardinality attributes with bounded values such as direction or method, rather than request IDs, user IDs, prompts, or arbitrary paths. The low-level attribute bag remains open, so instrumentation code enforces this policy.
|
|
170
|
+
Supply finite values and non-negative counter increments (up-down counters and histograms accept signed values). `histogram.record(value, attributes?, context?)` optionally attaches a span exemplar. Keep metric attributes strictly low-cardinality (direction, method, provider—never request IDs, user IDs, prompts, or arbitrary paths).
|
|
228
171
|
|
|
229
172
|
### Typed metric schemas
|
|
230
173
|
|
|
231
|
-
`TelemetrySchemaDefinition.metrics`
|
|
174
|
+
Declare metrics inside `TelemetrySchemaDefinition.metrics` and bind them with `createTypedMetrics()` for compile-time validation of names, kinds, and low-cardinality attributes:
|
|
232
175
|
|
|
233
176
|
```typescript
|
|
234
177
|
const schema = defineTelemetrySchema({
|
|
235
178
|
version: 1,
|
|
236
179
|
spans: {},
|
|
237
180
|
metrics: {
|
|
238
|
-
|
|
239
|
-
kind:
|
|
181
|
+
"network.bytes": {
|
|
182
|
+
kind: "counter",
|
|
183
|
+
description: "Encoded bytes",
|
|
184
|
+
unit: "By",
|
|
240
185
|
attributes: {
|
|
241
186
|
direction: {
|
|
242
|
-
type:
|
|
243
|
-
|
|
187
|
+
type: "string",
|
|
188
|
+
required: true,
|
|
189
|
+
values: ["in", "out"],
|
|
190
|
+
description: "Direction",
|
|
191
|
+
cardinality: "low",
|
|
244
192
|
},
|
|
245
193
|
},
|
|
246
194
|
},
|
|
@@ -248,93 +196,45 @@ const schema = defineTelemetrySchema({
|
|
|
248
196
|
} as const);
|
|
249
197
|
|
|
250
198
|
const metrics = createTypedMetrics(telemetry, [schema]);
|
|
251
|
-
metrics.counter(
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
Metric definitions use `kind: 'counter' | 'upDownCounter' | 'histogram'`; only histograms may declare `boundaries`. Metric attributes use the same scalar/array definitions as spans, but reject `cardinality: 'high'`. Omitting the cardinality annotation does not waive the low-cardinality policy. Unlike the typed span starter, the metric helper reads and retains schema instrument metadata at runtime. It performs no runtime attribute validation.
|
|
255
|
-
|
|
256
|
-
`InMemoryTelemetryContext.getMetrics()` returns ordered, detached `RecordedTelemetryMeasurement` snapshots: `kind`, `name`, optional `description`/`unit`/`boundaries`, `value`, copied `attributes`, and a carrier-shaped exemplar `context` or `null`. This is a measurement log, not aggregated counters or buckets; storage is unbounded. Measurement times are retained privately for store queries, not exposed in these snapshots.
|
|
257
|
-
|
|
258
|
-
## No-op context
|
|
259
|
-
|
|
260
|
-
Use `NOOP_TELEMETRY_CONTEXT` when telemetry is optional:
|
|
261
|
-
|
|
262
|
-
```typescript
|
|
263
|
-
import { NOOP_TELEMETRY_CONTEXT } from '@bastani/atomic-telemetry';
|
|
264
|
-
|
|
265
|
-
const result = await NOOP_TELEMETRY_CONTEXT.startSpan(
|
|
266
|
-
{ name: 'example.operation' },
|
|
267
|
-
() => runOperation(),
|
|
268
|
-
);
|
|
199
|
+
metrics.counter("network.bytes").add(encodedSize, { direction: "out" });
|
|
269
200
|
```
|
|
270
201
|
|
|
271
|
-
|
|
202
|
+
Only `kind: "histogram"` may declare `boundaries`, and metric attributes reject `cardinality: "high"`. `InMemoryTelemetryContext.getMetrics()` returns detached `RecordedTelemetryMeasurement` logs in recording order (unbounded in memory).
|
|
272
203
|
|
|
273
|
-
-
|
|
274
|
-
- Preserves returned values and asynchronous rejections, and converts a synchronous throw to a promise rejected with the same value.
|
|
275
|
-
- Uses one shared frozen inert span, including for nested spans.
|
|
276
|
-
- Exposes `NOOP_TELEMETRY_METRICS` and `NOOP_TELEMETRY_PROPAGATION` on its root and spans.
|
|
277
|
-
- Does not inspect or retain names, attributes, events, or statuses.
|
|
204
|
+
## No-op and in-memory contexts
|
|
278
205
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
`InMemoryTelemetryContext` is a backend-neutral reference adapter for tests, local diagnostics, and process-local capture without an exporter:
|
|
282
|
-
|
|
283
|
-
```typescript
|
|
284
|
-
import { InMemoryTelemetryContext } from '@bastani/atomic-telemetry';
|
|
285
|
-
|
|
286
|
-
const telemetry = new InMemoryTelemetryContext();
|
|
287
|
-
|
|
288
|
-
await telemetry.startSpan(
|
|
289
|
-
{ name: 'example.operation', attributes: { input: 'demo' } },
|
|
290
|
-
async (span) => {
|
|
291
|
-
span.addEvent('example.started');
|
|
292
|
-
span.setAttributes({ output_count: 3 });
|
|
293
|
-
},
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
console.log(telemetry.getSpans());
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
`getSpans()` returns detached snapshots in span-start order. Each `RecordedTelemetrySpan` contains a deterministic numeric ID, local parent ID, carrier-shaped `context` and `parentContext` snapshots, ordered `links`, merged attributes, ordered events, final status, settlement state, and deterministic end sequence without timestamps. A remote parent has `parentId: null` and a non-null `parentContext`; a root has both `null`. A `RecordedTelemetryLink` contains a target carrier and copied attributes.
|
|
300
|
-
|
|
301
|
-
`InMemoryTelemetryContext` implements `TelemetryPropagation` with JSON-safe carriers that work across independent processes without a shared registry. Treat its carrier keys as opaque and specific to `InMemoryTelemetryContext`, not as a shared wire format for other adapters.
|
|
302
|
-
|
|
303
|
-
Storage in `InMemoryTelemetryContext` is unbounded and process-local. Create a fresh instance per test or recording scope, and avoid recording sensitive attributes unless your data policy permits them.
|
|
206
|
+
- **`NOOP_TELEMETRY_CONTEXT`:** Shared passive default when telemetry is disabled. Invokes callbacks synchronously, preserves return values and rejections (converting sync throws to rejections), exposes `NOOP_TELEMETRY_METRICS` and `NOOP_TELEMETRY_PROPAGATION`, and retains nothing.
|
|
207
|
+
- **`InMemoryTelemetryContext`:** Process-local reference adapter for tests and diagnostics with deterministic IDs, JSON-safe cross-process carriers, unbounded in-memory recording, `getSpans()`, `getMetrics()`, and `TelemetryStore` queries. Create a fresh instance per test or recording scope.
|
|
304
208
|
|
|
305
209
|
## Stored telemetry
|
|
306
210
|
|
|
307
|
-
`TelemetryStore` separates querying from recording
|
|
308
|
-
|
|
309
|
-
- `findTraces(query?)` returns `Promise<{ traces: readonly StoredTrace[]; cursor?: string }>`, newest trace start first, where each `StoredTrace` is `{ traceId, spans }`. `TraceQuery` filters include the earliest stored span's `name`, inclusive `startMin` and `startMax`, `errors: true`, `attributes` (scalar equalities that must all match one span anywhere in the trace), `limit` (default `20`), and opaque `cursor` (pass with the same filters to fetch the next page). Equal start times order by `traceId`.
|
|
310
|
-
- `getTrace(traceId)` returns `Promise<StoredTrace | undefined>`.
|
|
311
|
-
- `findMetrics({ name, startMin?, startMax?, attributes? })` returns `Promise<readonly StoredMetric[]>`, where each `StoredMetric` has `{ intervalMs, name, kind, unit?, attributes, count, sum, min, max }`. Time bounds are inclusive interval-start times. `InMemoryTelemetryContext` maps each raw measurement to a count-one row in its 10-second interval, while persistent stores can aggregate measurements per series and interval.
|
|
211
|
+
`TelemetryStore` separates querying from recording (implemented by `InMemoryTelemetryContext` and persistent adapters):
|
|
312
212
|
|
|
313
|
-
|
|
213
|
+
- `findTraces(query?)` → `Promise<{ traces: readonly StoredTrace[]; cursor?: string }>` (newest trace start first; supports filters `name`, inclusive `startMin`/`startMax`, `errors: true`, scalar `attributes`, `limit` default `20`, and opaque `cursor`).
|
|
214
|
+
- `getTrace(traceId)` → `Promise<StoredTrace | undefined>`.
|
|
215
|
+
- `findMetrics({ name, startMin?, startMax?, attributes? })` → `Promise<readonly StoredMetric[]>` (`{ intervalMs, name, kind, unit?, attributes, count, sum, min, max }`).
|
|
314
216
|
|
|
315
|
-
`StoredSpan`
|
|
217
|
+
Only settled spans are returned. `StoredSpan` records `{ id, traceId, parentId?, name, startedMs, endedMs, attributes, events: [{ name, timeMs, attributes }], links: [{ traceId, spanId, attributes? }], status }` using monotonic durations anchored to epoch milliseconds (`telemetryNow()`).
|
|
316
218
|
|
|
317
|
-
|
|
219
|
+
Format stored spans into an indented chronological tree with `@bastani/atomic-telemetry/render`:
|
|
318
220
|
|
|
319
221
|
```typescript
|
|
320
|
-
import { renderTrace } from
|
|
222
|
+
import { renderTrace } from "@bastani/atomic-telemetry/render";
|
|
321
223
|
|
|
322
224
|
const page = await telemetry.findTraces({ errors: true, limit: 10 });
|
|
323
225
|
for (const trace of page.traces) console.log(renderTrace(trace.spans));
|
|
324
226
|
```
|
|
325
227
|
|
|
326
|
-
`renderTrace()` accepts stored spans, deduplicates IDs, treats absent parents as roots, and formats attributes, durations, statuses, event offsets, and cross-trace links. It never follows links or queries a store.
|
|
327
|
-
|
|
328
228
|
## Adapter conformance
|
|
329
229
|
|
|
330
|
-
`@bastani/atomic-telemetry/testing` exports a runner-independent conformance suite
|
|
230
|
+
`@bastani/atomic-telemetry/testing` exports a runner-independent conformance suite (`createTelemetryAdapterConformance`) that verifies capability identity, synchronous single invocation, return/rejection preservation, status rules, attribute merging, event ordering, post-settlement inertness, nested/concurrent parentage, span links, and optional propagation and metrics fixtures:
|
|
331
231
|
|
|
332
232
|
```typescript
|
|
333
233
|
import {
|
|
334
234
|
createTelemetryAdapterConformance,
|
|
335
235
|
type TelemetryAdapterFixture,
|
|
336
|
-
} from
|
|
337
|
-
import { describe, it } from
|
|
236
|
+
} from "@bastani/atomic-telemetry/testing";
|
|
237
|
+
import { describe, it } from "vitest";
|
|
338
238
|
|
|
339
239
|
const conformance = createTelemetryAdapterConformance(async () => {
|
|
340
240
|
const adapter = createMyTelemetryAdapter();
|
|
@@ -356,168 +256,85 @@ for (const group of new Set(conformance.map((testCase) => testCase.group))) {
|
|
|
356
256
|
}
|
|
357
257
|
```
|
|
358
258
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
Fixtures may supply `propagation` to verify JSON round trips, multi-adapter hops, and root fallback for absent or unknown carriers. Fixtures may also supply `metrics` and `getMetrics()` to verify ordered measurements, histogram exemplars, and input copying. The suite does not test malformed inputs or require suppressing backend failures. `getSpans()` may flush an asynchronous exporter before returning. `@bastani/atomic-telemetry/testing` uses Node's assertion APIs, while the root package remains runtime-neutral.
|
|
259
|
+
`@bastani/atomic-telemetry/testing` uses Node's assertion APIs, while the root package remains runtime-neutral.
|
|
362
260
|
|
|
363
261
|
## Typed schemas
|
|
364
262
|
|
|
365
|
-
|
|
263
|
+
Define closed, JSON-serializable span and metric vocabularies with `defineTelemetrySchema()` and bind them with `createTypedSpanStarter()`:
|
|
366
264
|
|
|
367
265
|
```typescript
|
|
368
|
-
import {
|
|
369
|
-
createTypedSpanStarter,
|
|
370
|
-
defineTelemetrySchema,
|
|
371
|
-
} from '@bastani/atomic-telemetry';
|
|
266
|
+
import { createTypedSpanStarter, defineTelemetrySchema } from "@bastani/atomic-telemetry";
|
|
372
267
|
|
|
373
268
|
export const EXAMPLE_TELEMETRY_SCHEMA = defineTelemetrySchema({
|
|
374
269
|
version: 1,
|
|
375
270
|
spans: {
|
|
376
|
-
|
|
377
|
-
description:
|
|
378
|
-
parents: { kind:
|
|
271
|
+
"example.read": {
|
|
272
|
+
description: "Read one resource",
|
|
273
|
+
parents: { kind: "any" },
|
|
379
274
|
startAttributes: {
|
|
380
|
-
|
|
381
|
-
type:
|
|
275
|
+
"example.resource": {
|
|
276
|
+
type: "string",
|
|
382
277
|
required: true,
|
|
383
|
-
values: [
|
|
384
|
-
description:
|
|
278
|
+
values: ["account", "project"],
|
|
279
|
+
description: "Resource kind",
|
|
385
280
|
},
|
|
386
281
|
},
|
|
387
282
|
endAttributes: {
|
|
388
|
-
|
|
389
|
-
type:
|
|
390
|
-
description:
|
|
283
|
+
"example.item_count": {
|
|
284
|
+
type: "number",
|
|
285
|
+
description: "Number of returned items",
|
|
391
286
|
},
|
|
392
287
|
},
|
|
393
288
|
events: {
|
|
394
|
-
|
|
395
|
-
description:
|
|
289
|
+
"example.cache": {
|
|
290
|
+
description: "Cache lookup result",
|
|
396
291
|
attributes: {
|
|
397
|
-
|
|
398
|
-
type:
|
|
292
|
+
"example.cache.hit": {
|
|
293
|
+
type: "boolean",
|
|
399
294
|
required: true,
|
|
400
|
-
description:
|
|
295
|
+
description: "Whether the cache contained the resource",
|
|
401
296
|
},
|
|
402
297
|
},
|
|
403
298
|
},
|
|
404
299
|
},
|
|
405
|
-
status: {
|
|
406
|
-
default: 'ok',
|
|
407
|
-
errorWhen: 'The read throws or returns an error result',
|
|
408
|
-
},
|
|
300
|
+
status: { default: "ok", errorWhen: "The read throws or returns an error result" },
|
|
409
301
|
},
|
|
410
302
|
},
|
|
411
303
|
} as const);
|
|
412
304
|
|
|
413
|
-
const startSpan = createTypedSpanStarter(
|
|
414
|
-
telemetryContext,
|
|
415
|
-
[EXAMPLE_TELEMETRY_SCHEMA],
|
|
416
|
-
);
|
|
417
|
-
```
|
|
305
|
+
const startSpan = createTypedSpanStarter(telemetryContext, [EXAMPLE_TELEMETRY_SCHEMA]);
|
|
418
306
|
|
|
419
|
-
|
|
307
|
+
await startSpan("example.read", { "example.resource": "account" }, async (span, startChildSpan) => {
|
|
308
|
+
span.addEvent("example.cache", { "example.cache.hit": true });
|
|
309
|
+
const accounts = await readAccounts();
|
|
310
|
+
span.setAttributes({ "example.item_count": accounts.length });
|
|
420
311
|
|
|
421
|
-
|
|
422
|
-
await
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
async (span, startChildSpan) => {
|
|
426
|
-
span.addEvent('example.cache', { 'example.cache.hit': true });
|
|
427
|
-
const accounts = await readAccounts();
|
|
428
|
-
span.setAttributes({ 'example.item_count': accounts.length });
|
|
429
|
-
|
|
430
|
-
await startChildSpan(
|
|
431
|
-
'example.read',
|
|
432
|
-
{ 'example.resource': 'project' },
|
|
433
|
-
async (childSpan) => {
|
|
434
|
-
const projects = await readProjects();
|
|
435
|
-
childSpan.setAttributes({ 'example.item_count': projects.length });
|
|
436
|
-
},
|
|
437
|
-
);
|
|
312
|
+
await startChildSpan("example.read", { "example.resource": "project" }, async (childSpan) => {
|
|
313
|
+
const projects = await readProjects();
|
|
314
|
+
childSpan.setAttributes({ "example.item_count": projects.length });
|
|
315
|
+
});
|
|
438
316
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
);
|
|
317
|
+
return accounts;
|
|
318
|
+
});
|
|
442
319
|
```
|
|
443
320
|
|
|
444
|
-
|
|
321
|
+
Always pass the second `attributes` argument to `startSpan()` (`{}` when `startAttributes` is empty). `startSpan()` can compose multiple schemas (`[SCHEMA_A, SCHEMA_B] as const`) and rejects duplicate literal span names, missing required attributes, unknown keys, and invalid closed-set values at compile time. `defineTelemetrySchema()` is a typed identity helper that performs no runtime validation.
|
|
445
322
|
|
|
446
|
-
|
|
323
|
+
### Start and completion attributes
|
|
447
324
|
|
|
448
325
|
| Schema field | How values are recorded | Requiredness |
|
|
449
|
-
|
|
450
|
-
| `startAttributes` | Passed in
|
|
451
|
-
| `endAttributes` | Added later
|
|
452
|
-
|
|
453
|
-
Both sets become ordinary attributes on the same backend span. There is no separate end-attribute payload or end callback. In the preceding example, `example.resource` is known when `example.read` starts, while `example.item_count` is known only after `readAccounts()` returns:
|
|
454
|
-
|
|
455
|
-
```typescript
|
|
456
|
-
await startSpan(
|
|
457
|
-
'example.read',
|
|
458
|
-
{ 'example.resource': 'account' }, // required start attribute
|
|
459
|
-
async (span) => {
|
|
460
|
-
const accounts = await readAccounts();
|
|
461
|
-
span.setAttributes({
|
|
462
|
-
'example.item_count': accounts.length, // optional completion attribute
|
|
463
|
-
});
|
|
464
|
-
return accounts;
|
|
465
|
-
},
|
|
466
|
-
); // resolving the callback settles the span
|
|
467
|
-
```
|
|
326
|
+
| --- | --- | --- |
|
|
327
|
+
| `startAttributes` | Passed in `startSpan()`'s `attributes` argument when the span starts | Explicit `required: true` or `false` |
|
|
328
|
+
| `endAttributes` | Added later via `span.setAttributes()` while the callback runs | Always optional (calling zero times is valid) |
|
|
468
329
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
Repeated `setAttributes()` calls merge into the same attribute bag. A later defined value replaces an earlier value for the same key, while `undefined` is ignored. The schema-scoped method accepts only the current span's declared end attributes.
|
|
472
|
-
|
|
473
|
-
Attributes do not end the span. Returning, resolving, throwing, or rejecting from the callback controls settlement; `startSpan()` performs the actual end operation. Adapter calls made after settlement are inert.
|
|
474
|
-
|
|
475
|
-
A starter can compose multiple independently versioned schemas, each defined with `defineTelemetrySchema()`:
|
|
476
|
-
|
|
477
|
-
```typescript
|
|
478
|
-
const startAppSpan = createTypedSpanStarter(telemetryContext, [
|
|
479
|
-
EXAMPLE_TELEMETRY_SCHEMA,
|
|
480
|
-
CACHE_TELEMETRY_SCHEMA,
|
|
481
|
-
] as const);
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
Inline schema arrays retain their tuple types automatically. Separately declared arrays should use `as const`. Literal duplicate span names across the array are rejected at compile time; schemas are not merged, inspected, or retained at runtime.
|
|
485
|
-
|
|
486
|
-
Schema-derived types reject missing required attributes, unknown keys, invalid closed-set values, undeclared events, and attributes on empty schemas. End attributes are always optional enrichment; the type system does not require `setAttributes()` to be called.
|
|
487
|
-
|
|
488
|
-
`defineTelemetrySchema()` is a typed identity function. It returns ordinary JSON-serializable data and performs no runtime validation or parent-rule enforcement.
|
|
489
|
-
|
|
490
|
-
## Schema metadata
|
|
491
|
-
|
|
492
|
-
Supported attribute types are:
|
|
493
|
-
|
|
494
|
-
- `string`, `number`, and `boolean`
|
|
495
|
-
- `string[]`, `number[]`, and `boolean[]`
|
|
496
|
-
|
|
497
|
-
Attribute definitions support:
|
|
498
|
-
|
|
499
|
-
- `values`: a closed set for scalar values
|
|
500
|
-
- `elementValues`: a closed set for array elements
|
|
501
|
-
- `examples`: documentation examples
|
|
502
|
-
- `sensitive`: marks data requiring special handling
|
|
503
|
-
- `cardinality`: records expected `low` or `high` cardinality
|
|
504
|
-
|
|
505
|
-
Start and event attributes declare `required`. End attributes do not; see [Start and completion attributes](#start-and-completion-attributes).
|
|
506
|
-
|
|
507
|
-
Parent metadata is descriptive schema data:
|
|
508
|
-
|
|
509
|
-
- `{ kind: 'any' }`: root or any caller span
|
|
510
|
-
- `{ kind: 'root_or_external' }`: root or a caller-owned span outside the schema
|
|
511
|
-
- `{ kind: 'spans', spans: [...] }`: only the listed schema spans
|
|
512
|
-
|
|
513
|
-
Adapters do not need to understand schema objects. Instrumentation helpers and tests use them to keep emitted names and attributes consistent.
|
|
330
|
+
Both sets merge into the same backend span attribute bag (later defined values win; `undefined` is ignored). Supported attribute types are `string`, `number`, `boolean`, and their homogeneous arrays (`string[]`, `number[]`, `boolean[]`), with optional metadata fields `values`, `elementValues`, `examples`, `sensitive`, and `cardinality` (`"low"` | `"high"`). Descriptive `parents` rules accept `{ kind: "any" }`, `{ kind: "root_or_external" }`, or `{ kind: "spans", spans: [...] }`.
|
|
514
331
|
|
|
515
332
|
## Using with `@bastani/atomic-ai`
|
|
516
333
|
|
|
517
|
-
Pass a parent `TelemetryContext` to [`@bastani/atomic-ai`](/libraries/ai)
|
|
334
|
+
Pass a parent `TelemetryContext` to [`@bastani/atomic-ai`](/libraries/ai) via `telemetryContext` in request options:
|
|
518
335
|
|
|
519
336
|
```typescript
|
|
520
|
-
import { InMemoryTelemetryContext } from
|
|
337
|
+
import { InMemoryTelemetryContext } from "@bastani/atomic-telemetry";
|
|
521
338
|
|
|
522
339
|
const telemetry = new InMemoryTelemetryContext();
|
|
523
340
|
await models.completeSimple(model, context, { telemetryContext: telemetry });
|
|
@@ -525,67 +342,58 @@ await models.completeSimple(model, context, { telemetryContext: telemetry });
|
|
|
525
342
|
|
|
526
343
|
## Security and portability
|
|
527
344
|
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
345
|
+
<Warning>
|
|
346
|
+
Telemetry is diagnostic data, not durable application state. Never persist a `TelemetryContext`, `TelemetrySpan`, or backend trace object inside records, messages, or snapshots, and never record prompts, completions, tool arguments/output, file contents, credentials, or raw headers unless your schema and data policy explicitly permit them.
|
|
347
|
+
</Warning>
|
|
531
348
|
|
|
532
|
-
This package
|
|
349
|
+
An injected `TelemetryCarrier` may cross a transport or be stored as optional diagnostic correlation data. This package uses no `AsyncLocalStorage` or runtime-specific globals, so it works in Node.js, Bun, browsers, and workers (backend adapters own their own runtime compatibility).
|
|
533
350
|
|
|
534
351
|
## API reference
|
|
535
352
|
|
|
536
353
|
### Core types and values
|
|
537
354
|
|
|
538
355
|
| Export | Purpose |
|
|
539
|
-
|
|
540
|
-
| `TelemetryContext` |
|
|
541
|
-
| `
|
|
542
|
-
| `
|
|
543
|
-
| `
|
|
544
|
-
| `
|
|
545
|
-
| `
|
|
546
|
-
| `
|
|
547
|
-
| `
|
|
548
|
-
| `
|
|
549
|
-
| `TelemetryHistogram` | Instrument with `record()` and optional exemplar context |
|
|
550
|
-
| `InstrumentOptions` / `HistogramOptions` | Description, unit, and histogram boundary hints |
|
|
551
|
-
| `NOOP_TELEMETRY_METRICS` | Shared passive metric instruments |
|
|
552
|
-
| `RecordedTelemetryMeasurement` | Captured measurement and optional exemplar |
|
|
553
|
-
| `SpanAttributes` / `AttributeValue` | Open adapter-level attribute bag and supported values |
|
|
554
|
-
| `SpanStatus` | Explicit `ok` or `error` status |
|
|
555
|
-
| `NOOP_TELEMETRY_CONTEXT` | Shared passive context for disabled telemetry |
|
|
556
|
-
| `InMemoryTelemetryContext` | Reference adapter with deterministic process-local recording |
|
|
557
|
-
| `RecordedTelemetrySpan` | Normalized captured span snapshot |
|
|
558
|
-
| `RecordedTelemetryEvent` | Normalized captured event snapshot |
|
|
559
|
-
| `RecordedTelemetryLink` | Captured target carrier and link attributes |
|
|
560
|
-
| `TelemetryStore` | Settled trace discovery, trace retrieval, and metric queries |
|
|
561
|
-
| `StoredSpan` / `StoredTrace` | Backend-independent timed span and trace snapshots |
|
|
562
|
-
| `TraceQuery` / `MetricQuery` | Trace filters, pagination, and metric filters |
|
|
563
|
-
| `StoredMetric` | Metric interval row with count, sum, minimum, and maximum |
|
|
356
|
+
| --- | --- |
|
|
357
|
+
| `TelemetryContext` / `TelemetrySpan` | Callback-managed span starter and active span handle |
|
|
358
|
+
| `SpanOptions` / `SpanLink` / `SpanAttributes` / `AttributeValue` / `SpanStatus` | Core span creation and recording types |
|
|
359
|
+
| `TelemetryCarrier` / `TelemetryPropagation` | Cross-process opaque carrier and inject/extract contract |
|
|
360
|
+
| `TelemetryMetrics` / `TelemetryCounter` / `TelemetryUpDownCounter` / `TelemetryHistogram` | Metric instrument factories and recorders |
|
|
361
|
+
| `InstrumentOptions` / `HistogramOptions` / `RecordedTelemetryMeasurement` | Metric metadata, bucket hints, and recorded measurement snapshots |
|
|
362
|
+
| `NOOP_TELEMETRY_CONTEXT` / `NOOP_TELEMETRY_METRICS` / `NOOP_TELEMETRY_PROPAGATION` | Shared passive no-op implementations |
|
|
363
|
+
| `InMemoryTelemetryContext` | Deterministic in-memory reference adapter |
|
|
364
|
+
| `RecordedTelemetrySpan` / `RecordedTelemetryEvent` / `RecordedTelemetryLink` | Normalized captured span snapshots |
|
|
365
|
+
| `TelemetryStore` / `StoredSpan` / `StoredTrace` / `StoredMetric` / `TraceQuery` / `MetricQuery` | Backend-independent settled trace and metric query API |
|
|
564
366
|
| `telemetryNow()` | Monotonic-anchored wall-clock milliseconds used by `StoredSpan` |
|
|
565
367
|
|
|
566
|
-
###
|
|
368
|
+
### Subpaths
|
|
567
369
|
|
|
568
|
-
|
|
|
569
|
-
|
|
570
|
-
| `renderTrace()`
|
|
370
|
+
| Subpath | Exports |
|
|
371
|
+
| --- | --- |
|
|
372
|
+
| `@bastani/atomic-telemetry/render` | `renderTrace()` — formats `StoredSpan[]` into an indented chronological trace tree |
|
|
373
|
+
| `@bastani/atomic-telemetry/testing` | `createTelemetryAdapterConformance()`, `TelemetryAdapterFixture`, `TelemetryAdapterFixtureFactory`, `TelemetryAdapterConformanceCase` |
|
|
571
374
|
|
|
572
375
|
### Schema definitions and inference
|
|
573
376
|
|
|
377
|
+
**Primary helpers:**
|
|
378
|
+
|
|
574
379
|
| Export | Purpose |
|
|
575
|
-
|
|
576
|
-
| `defineTelemetrySchema()` | Typed identity helper for serializable schema
|
|
577
|
-
| `createTypedSpanStarter()` | Binds a parent context to one or more
|
|
380
|
+
| --- | --- |
|
|
381
|
+
| `defineTelemetrySchema()` | Typed identity helper for serializable schema definitions |
|
|
382
|
+
| `createTypedSpanStarter()` | Binds a parent context to one or more span schemas |
|
|
578
383
|
| `createTypedMetrics()` / `TypedTelemetryMetrics` | Binds metric names, kinds, metadata, and attributes to schemas |
|
|
579
|
-
| `
|
|
580
|
-
| `SchemaTelemetryCounter` / `SchemaTelemetryHistogram` | Schema-constrained
|
|
581
|
-
| `TypedSpanStarter` | Exact starter type with recursively child-bound callbacks |
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
|
384
|
+
| `TelemetrySchemaDefinition` / `TelemetrySpanDefinition` / `TelemetryMetricDefinition` | Top-level schema, span, and metric definition shapes |
|
|
385
|
+
| `SchemaTelemetrySpan` / `SchemaTelemetryCounter` / `SchemaTelemetryHistogram` | Schema-constrained span and metric views |
|
|
386
|
+
| `TypedSpanStarter` | Exact span starter type with recursively child-bound callbacks |
|
|
387
|
+
|
|
388
|
+
<Accordion title="TypeScript inference utility types">
|
|
389
|
+
| Export | Purpose |
|
|
390
|
+
| --- | --- |
|
|
391
|
+
| `TelemetryAttributeType` | Supported scalar and array attribute type names |
|
|
585
392
|
| `TelemetryAttributeMetadata` | Description, sensitivity, and cardinality metadata |
|
|
586
393
|
| `TelemetryAttributeDefinition` | Attribute type, allowed values, examples, and metadata |
|
|
587
394
|
| `TelemetryStartAttributeDefinition` | Start attribute definition with requiredness |
|
|
588
395
|
| `TelemetryEventAttributeDefinition` | Event attribute definition with requiredness |
|
|
396
|
+
| `TelemetryMetricAttributeDefinition` | Low-cardinality metric attribute definition |
|
|
589
397
|
| `TelemetryEventDefinition` | Event description and attribute definitions |
|
|
590
398
|
| `TelemetryParentDefinition` | Open, external-root, or finite schema-parent rule |
|
|
591
399
|
| `TelemetrySchemaSpanName` | Union of declared span names |
|
|
@@ -593,19 +401,10 @@ This package does not use `AsyncLocalStorage` or any runtime-specific ambient co
|
|
|
593
401
|
| `TelemetrySchemaSpanEndAttributes` | Optional inferred end attributes for one span |
|
|
594
402
|
| `TelemetrySchemaSpanEventName` | Union of events declared by one span |
|
|
595
403
|
| `TelemetrySchemaSpanEventAttributes` | Exact inferred attributes for one event |
|
|
596
|
-
| `SchemaTelemetrySpan` | Span view restricted to one schema span |
|
|
597
404
|
| `TelemetrySchemaSpanUnion` | Discriminated union of all spans in a schema |
|
|
598
405
|
| `InferStartAttributes` | Required and optional values inferred from start definitions |
|
|
599
406
|
| `InferOptionalAttributes` | Optional values inferred from end definitions |
|
|
600
407
|
| `InferEventAttributes` | Required and optional values inferred from event definitions |
|
|
601
408
|
| `InferRequiredAndOptionalAttributes` | Shared inference utility for definitions with requiredness |
|
|
602
409
|
| `ExactTelemetryAttributes` | Rejects keys outside an expected attribute set |
|
|
603
|
-
|
|
604
|
-
### Testing subpath
|
|
605
|
-
|
|
606
|
-
| Export | Purpose |
|
|
607
|
-
|---|---|
|
|
608
|
-
| `createTelemetryAdapterConformance()` | Creates runner-independent adapter conformance cases |
|
|
609
|
-
| `TelemetryAdapterFixture` | Fresh context and normalized snapshot reader for one case |
|
|
610
|
-
| `TelemetryAdapterFixtureFactory` | Creates isolated fixtures |
|
|
611
|
-
| `TelemetryAdapterConformanceCase` | Grouped case that test runners execute |
|
|
410
|
+
</Accordion>
|