chore(vendor): import pi 0.85.1

Imported as a linear vendor commit so forge rebases do not replay the upstream root tree.

git-subtree-dir: third_party/pi

git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
This commit is contained in:
2026-09-14 10:48:47 -04:00
parent 5ebc7018c3
commit 4def357d09
1669 changed files with 397627 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
# Changelog
## [0.85.1] - 2026-09-05
## [0.85.0] - 2026-09-04
## [0.84.4] - 2026-08-28
## [0.84.3] - 2026-08-24
## [0.84.2] - 2026-08-14
## [0.84.1] - 2026-08-07
## [0.84.0] - 2026-08-06
### Added
- Added the callback-based telemetry context contract, shared no-op context, deterministic in-memory reference adapter, reusable adapter conformance suite, typed serializable schema utilities, and multi-schema typed span starters with explicit child propagation.

View File

@@ -0,0 +1,464 @@
# @earendil-works/pi-telemetry
Vendor-neutral telemetry contracts and typed schema utilities for pi packages.
This package provides:
- an explicit, callback-based `TelemetryContext` / `TelemetrySpan` contract;
- a shared `NOOP_TELEMETRY_CONTEXT`;
- a reference `InMemoryTelemetryContext` implementation;
- serializable schema definitions with inferred TypeScript types;
- no exporter, global current-span state, or dependency on a telemetry backend.
Applications can use the in-memory reference or provide an adapter for OpenTelemetry, Sentry, logs, or another backend. Pi packages pass telemetry contexts explicitly and define their domain schemas separately.
## Table of Contents
- [Installation](#installation)
- [Telemetry Concepts](#telemetry-concepts)
- [Core Context API](#core-context-api)
- [Adapter Contract](#adapter-contract)
- [No-op Context](#no-op-context)
- [In-Memory Reference Adapter](#in-memory-reference-adapter)
- [Adapter Conformance](#adapter-conformance)
- [Typed Schemas](#typed-schemas)
- [Start and Completion Attributes](#start-and-completion-attributes)
- [Schema Metadata](#schema-metadata)
- [Pi Package Integration](#pi-package-integration)
- [Security and Portability](#security-and-portability)
- [API Reference](#api-reference)
- [Development](#development)
- [License](#license)
## Installation
```bash
npm install @earendil-works/pi-telemetry
```
## Telemetry Concepts
Telemetry describes what a program did while it was running. This package models that work using spans, attributes, events, statuses, and explicit context:
| Concept | Plain-language meaning |
|---|---|
| **Span** | A timed record of one operation, such as loading an account or making an AI request. It begins before the work and ends when the work finishes. |
| **Parent and child spans** | Operations can contain smaller operations. A request span might contain a cache lookup and a database query. Together they form a tree showing where time was spent. |
| **Attribute** | A named fact attached to a span, such as `provider: "openai"`, `cache.hit: true`, or `item_count: 12`. Attributes describe the operation and its result. |
| **Event** | A named occurrence at a point during a span, such as `retry.scheduled` or `cache.lookup`. Events have no duration and may carry their own attributes. |
| **Status** | The operation's outcome: `ok` or `error`. An error status may include an error name and message. |
| **Context** | A handle identifying where new work belongs in the span tree. Starting a span from a context makes it a child of that context. |
For example, loading an account could produce this telemetry:
```text
example.account.load span
├─ attributes: account.id=123, found=true facts about the span
├─ event: example.cache.lookup occurrence during the span
│ └─ attribute: cache.hit=false fact about the event
└─ status: ok final outcome
```
A span is diagnostic data, not business state. Recording it must not change whether the account load runs, succeeds, fails, or is persisted. An adapter translates these generic concepts into the corresponding concepts used by OpenTelemetry, Sentry, logs, or another backend.
## Core Context API
A `TelemetryContext` starts a span around a callback. The callback receives a `TelemetrySpan`, which is also the explicit parent context for child spans.
```typescript
import {
NOOP_TELEMETRY_CONTEXT,
type TelemetryContext,
} from '@earendil-works/pi-telemetry';
async function loadAccount(
accountId: string,
telemetryContext: TelemetryContext = NOOP_TELEMETRY_CONTEXT,
) {
return telemetryContext.startSpan(
{
name: 'example.account.load',
attributes: { 'example.account.id': accountId },
},
async (span) => {
const account = await readAccount(accountId);
span.setAttributes({ 'example.account.found': account !== undefined });
return account;
},
);
}
```
Pass the callback span to lower-level work to create explicit nesting:
```typescript
return telemetryContext.startSpan({ name: 'example.parent' }, async (parentSpan) => {
return parentSpan.startSpan({ name: 'example.child' }, async (childSpan) => {
childSpan.addEvent('example.cache.lookup', { 'example.cache.hit': true });
return performWork();
});
});
```
There is no public `end()` method. `startSpan()` owns settlement and keeps the span open until the callback's value or promise settles. For an expected failure represented by a normal return value, set the status explicitly:
```typescript
return telemetryContext.startSpan({ name: 'example.save' }, async (span) => {
const result = await save();
if (!result.ok) {
span.setStatus({
status: 'error',
error: { name: 'SaveError', message: result.reason },
});
}
return result;
});
```
## Adapter Contract
An adapter implements `TelemetryContext` and bridges the generic API to its backend. It must:
- create a child span and invoke the callback synchronously, exactly once;
- preserve the callback's returned value and rejection value, returning a promise rejected with the same value after a synchronous throw;
- keep the native span open until a returned promise settles;
- treat normal completion as `ok` and throws/rejections as errors unless an explicit status was set;
- make repeated `setStatus()` calls last-write-wins;
- merge `setAttributes()` calls, with later defined values replacing earlier values and `undefined` ignored;
- make recording methods synchronous, passive, and non-throwing;
- ignore calls made after settlement;
- ignore a failed recording call atomically, suppress backend failures, and still execute the business callback exactly once.
Adapters may activate backend-native ambient context internally for automatic instrumentation, but pi code always propagates the parent through `TelemetryContext` arguments. Exporter buffering, flushing, sampling, backend IDs, and backend-specific context objects belong to the adapter. Use the [adapter conformance suite](#adapter-conformance) to check these observable semantics.
## No-op Context
Use `NOOP_TELEMETRY_CONTEXT` when telemetry is optional:
```typescript
import { NOOP_TELEMETRY_CONTEXT } from '@earendil-works/pi-telemetry';
const result = await NOOP_TELEMETRY_CONTEXT.startSpan(
{ name: 'example.operation' },
() => runOperation(),
);
```
The no-op context:
- invokes callbacks synchronously;
- preserves returned values and asynchronous rejections, and converts a synchronous throw to a promise rejected with the same value;
- uses one shared frozen inert span, including for nested spans;
- does not inspect or retain names, attributes, events, or statuses.
## In-Memory Reference Adapter
`InMemoryTelemetryContext` is the backend-neutral reference implementation. It is useful for tests, local diagnostics, and applications that intentionally want process-local capture without an exporter:
```typescript
import { InMemoryTelemetryContext } from '@earendil-works/pi-telemetry';
const telemetry = new InMemoryTelemetryContext();
await telemetry.startSpan(
{ name: 'example.operation', attributes: { input: 'demo' } },
async (span) => {
span.addEvent('example.started');
span.setAttributes({ output_count: 3 });
},
);
console.log(telemetry.getSpans());
```
`getSpans()` returns detached snapshots in span-start order. Each `RecordedTelemetrySpan` contains a deterministic numeric ID, parent ID, merged attributes, ordered events, final status, settlement state, and deterministic end sequence. It records no timestamps.
The adapter is safe to use as an ordinary `TelemetryContext`, but storage is unbounded and process-local. Create a fresh instance to isolate tests or recording scopes, and do not capture sensitive attributes unless the caller's data policy allows them.
## Adapter Conformance
`@earendil-works/pi-telemetry/testing` exports a runner-independent conformance suite modeled as grouped cases. A fixture supplies a fresh context and converts its backend's finished spans into normalized `RecordedTelemetrySpan` snapshots:
```typescript
import {
createTelemetryAdapterConformance,
type TelemetryAdapterFixture,
} from '@earendil-works/pi-telemetry/testing';
import { describe, it } from 'vitest';
const conformance = createTelemetryAdapterConformance(async () => {
const adapter = createMyTelemetryAdapter();
return {
context: adapter.context,
getSpans: async () => adapter.normalizedSpans(),
async [Symbol.asyncDispose]() {
await adapter.close();
},
} satisfies TelemetryAdapterFixture;
});
for (const group of new Set(conformance.map((testCase) => testCase.group))) {
describe(group, () => {
for (const testCase of conformance.filter((candidate) => candidate.group === group)) {
it(testCase.name, () => testCase.run());
}
});
}
```
The suite checks synchronous single admission, result and rejection identity, automatic and explicit status, attribute merging, event ordering, inert post-settlement calls, nested and concurrent parentage, and suppression of unreadable telemetry payload failures. `getSpans()` may flush an asynchronous exporter before returning. The testing subpath uses Node's assertion APIs; the root telemetry package remains runtime-neutral.
## Typed Schemas
The low-level span API intentionally accepts open names and attribute bags so adapters remain generic. Domain packages can define closed, serializable schemas and infer exact TypeScript types from them.
```typescript
import {
createTypedSpanStarter,
defineTelemetrySchema,
} from '@earendil-works/pi-telemetry';
export const EXAMPLE_TELEMETRY_SCHEMA = defineTelemetrySchema({
version: 1,
spans: {
'example.read': {
description: 'Read one resource',
parents: { kind: 'any' },
startAttributes: {
'example.resource': {
type: 'string',
required: true,
values: ['account', 'project'],
description: 'Resource kind',
},
},
endAttributes: {
'example.item_count': {
type: 'number',
description: 'Number of returned items',
},
},
events: {
'example.cache': {
description: 'Cache lookup result',
attributes: {
'example.cache.hit': {
type: 'boolean',
required: true,
description: 'Whether the cache contained the resource',
},
},
},
},
status: {
default: 'ok',
errorWhen: 'The read throws or returns an error result',
},
},
},
} as const);
const startSpan = createTypedSpanStarter(
telemetryContext,
[EXAMPLE_TELEMETRY_SCHEMA],
);
```
The starter exposes one overload per span and checks names and attributes at compile time. Union-valued names must be narrowed before a call, preserving the relationship between each runtime name and its attribute schema. Its callback receives a child starter over the same schemas, already bound to the callback span:
```typescript
await startSpan(
'example.read',
{ 'example.resource': 'account' },
async (span, startChildSpan) => {
span.addEvent('example.cache', { 'example.cache.hit': true });
const accounts = await readAccounts();
span.setAttributes({ 'example.item_count': accounts.length });
await startChildSpan(
'example.read',
{ 'example.resource': 'project' },
async (childSpan) => {
const projects = await readProjects();
childSpan.setAttributes({ 'example.item_count': projects.length });
},
);
return accounts;
},
);
```
### Start and Completion Attributes
`startAttributes` and `endAttributes` describe when an attribute is normally known, not separate runtime storage:
| Schema field | How values are recorded | Requiredness |
|---|---|---|
| `startAttributes` | Passed in the typed starter's `attributes` argument when the span is created | Each definition explicitly sets `required: true` or `false` |
| `endAttributes` | Added later through the schema-scoped span's `setAttributes()` method | Always optional |
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:
```typescript
await startSpan(
'example.read',
{ 'example.resource': 'account' }, // required start attribute
async (span) => {
const accounts = await readAccounts();
span.setAttributes({
'example.item_count': accounts.length, // optional completion attribute
});
return accounts;
},
); // resolving the callback settles the span
```
“End” means completion enrichment: an end attribute may be set at any point while the callback is active, and it may be omitted when unavailable. Calling `setAttributes()` zero times is valid. This matters for early failures, cancellation, and provider-specific data that may not exist on every path.
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.
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.
A starter can compose multiple independently versioned schemas:
```typescript
import { AGENT_TELEMETRY_SCHEMAS } from '@earendil-works/pi-agent-core';
const startAgentSpan = createTypedSpanStarter(
telemetryContext,
AGENT_TELEMETRY_SCHEMAS,
);
```
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.
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.
`defineTelemetrySchema()` is a typed identity function. It returns ordinary JSON-serializable data and performs no runtime validation or parent-rule enforcement.
## Schema Metadata
Supported attribute types are:
- `string`, `number`, and `boolean`;
- `string[]`, `number[]`, and `boolean[]`.
Attribute definitions support:
- `values`: a closed set for scalar values;
- `elementValues`: a closed set for array elements;
- `examples`: documentation examples;
- `sensitive`: marks data requiring special handling;
- `cardinality`: records expected `low` or `high` cardinality.
Start and event attributes declare `required`. End attributes do not; see [Start and Completion Attributes](#start-and-completion-attributes).
Parent metadata is descriptive schema data:
- `{ kind: 'any' }`: root or any caller span;
- `{ kind: 'root_or_external' }`: root or a caller-owned span outside the schema;
- `{ kind: 'spans', spans: [...] }`: only the listed schema spans.
Adapters do not need to understand schema objects. Instrumentation helpers and tests use them to keep emitted names and attributes consistent.
## Pi Package Integration
Package ownership is intentionally split:
- `@earendil-works/pi-telemetry` owns the vendor-neutral contract, no-op and in-memory reference contexts, schema utilities, and adapter conformance suite;
- `@earendil-works/pi-ai` accepts and propagates `telemetryContext` in provider request options but owns no telemetry schema;
- `@earendil-works/pi-agent-core` owns and exports the pi AI-request and harness schemas, their combined readonly schema tuple, and typed span helpers.
```typescript
import {
AGENT_TELEMETRY_SCHEMAS,
AI_TELEMETRY_SCHEMA,
HARNESS_TELEMETRY_SCHEMA,
startAiSpan,
startHarnessSpan,
} from '@earendil-works/pi-agent-core';
```
The pi schemas use pi-owned `pi.ai.*`, `pi.harness.*`, and `pi.session.*` names. Adapters may translate them to backend conventions without changing the emitted pi vocabulary.
## Security and Portability
Telemetry is process-local diagnostics, not durable application state. Do not persist a `TelemetryContext`, `TelemetrySpan`, or backend-native trace object in records, messages, snapshots, or deferred handles.
Attribute values are intentionally limited to primitive scalars and arrays. Domain instrumentation should avoid prompts, completions, tool arguments or output, file contents, provider payloads, headers, credentials, and free-form error details unless its schema and data policy explicitly allow them.
The package does not use `AsyncLocalStorage` or another runtime-specific ambient context API. It is suitable for Node.js, Bun, browsers, and workers; backend adapters remain responsible for their own runtime compatibility.
## API Reference
### Core types and values
| Export | Purpose |
|---|---|
| `TelemetryContext` | Starts callback-managed child spans |
| `TelemetrySpan` | Records attributes, events, and status; also acts as a child context |
| `SpanOptions` | Span name and optional start attributes |
| `SpanAttributes` / `AttributeValue` | Open adapter-level attribute bag and supported values |
| `SpanStatus` | Explicit `ok` or `error` status |
| `NOOP_TELEMETRY_CONTEXT` | Shared passive context for disabled telemetry |
| `InMemoryTelemetryContext` | Reference adapter with deterministic process-local recording |
| `RecordedTelemetrySpan` | Normalized captured span snapshot |
| `RecordedTelemetryEvent` | Normalized captured event snapshot |
### Schema definitions and inference
| Export | Purpose |
|---|---|
| `defineTelemetrySchema()` | Typed identity helper for serializable schema data |
| `createTypedSpanStarter()` | Binds a parent context to one or more schema vocabularies |
| `TypedSpanStarter` | Exact starter type with recursively child-bound callbacks |
| `TelemetrySchemaDefinition` | Top-level schema shape |
| `TelemetrySpanDefinition` | Span metadata, parents, attributes, events, and status rule |
| `TelemetryAttributeType` | Supported scalar and array type names |
| `TelemetryAttributeMetadata` | Description, sensitivity, and cardinality metadata |
| `TelemetryAttributeDefinition` | Attribute type, allowed values, examples, and metadata |
| `TelemetryStartAttributeDefinition` | Start attribute definition with requiredness |
| `TelemetryEventAttributeDefinition` | Event attribute definition with requiredness |
| `TelemetryEventDefinition` | Event description and attribute definitions |
| `TelemetryParentDefinition` | Open, external-root, or finite schema-parent rule |
| `TelemetrySchemaSpanName` | Union of declared span names |
| `TelemetrySchemaSpanStartAttributes` | Exact inferred start attributes for one span |
| `TelemetrySchemaSpanEndAttributes` | Optional inferred end attributes for one span |
| `TelemetrySchemaSpanEventName` | Union of events declared by one span |
| `TelemetrySchemaSpanEventAttributes` | Exact inferred attributes for one event |
| `SchemaTelemetrySpan` | Span view restricted to one schema span |
| `TelemetrySchemaSpanUnion` | Discriminated union of all spans in a schema |
| `InferStartAttributes` | Required and optional values inferred from start definitions |
| `InferOptionalAttributes` | Optional values inferred from end definitions |
| `InferEventAttributes` | Required and optional values inferred from event definitions |
| `InferRequiredAndOptionalAttributes` | Shared inference utility for definitions with requiredness |
| `ExactTelemetryAttributes` | Rejects keys outside an expected attribute set |
### Testing subpath
| Export | Purpose |
|---|---|
| `createTelemetryAdapterConformance()` | Creates runner-independent adapter conformance cases |
| `TelemetryAdapterFixture` | Fresh context and normalized snapshot reader for one case |
| `TelemetryAdapterFixtureFactory` | Creates isolated fixtures |
| `TelemetryAdapterConformanceCase` | Grouped case that test runners execute |
## Development
From this package directory:
```bash
npm test
npm run build
```
Repository-wide type checking, formatting, linting, and smoke checks run with:
```bash
npm run check
```
## License
MIT

View File

@@ -0,0 +1,47 @@
{
"name": "@earendil-works/pi-telemetry",
"version": "0.85.1",
"description": "Vendor-neutral telemetry contracts and typed schema utilities for pi",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./testing": {
"types": "./dist/testing/index.d.ts",
"import": "./dist/testing/index.js"
}
},
"files": [
"dist",
"README.md"
],
"scripts": {
"clean": "shx rm -rf dist",
"build": "tsgo -p tsconfig.build.json",
"test": "vitest --run",
"prepublishOnly": "npm run clean && npm run build"
},
"keywords": [
"telemetry",
"tracing",
"observability"
],
"author": "Mario Zechner",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/earendil-works/pi.git",
"directory": "packages/telemetry"
},
"engines": {
"node": ">=22.19.0"
},
"devDependencies": {
"@types/node": "22.19.19",
"vitest": "4.1.9"
}
}

View File

@@ -0,0 +1,357 @@
export type AttributeValue = string | number | boolean | readonly string[] | readonly number[] | readonly boolean[];
export interface SpanAttributes {
[name: string]: AttributeValue | undefined;
}
export interface SpanOptions {
name: string;
attributes?: SpanAttributes;
}
export type SpanStatus = { status: "ok" } | { status: "error"; error?: { name: string; message: string } };
export interface TelemetryContext {
startSpan<T>(options: SpanOptions, callback: (span: TelemetrySpan) => T | Promise<T>): Promise<T>;
}
export interface TelemetrySpan extends TelemetryContext {
addEvent(name: string, attributes?: SpanAttributes): void;
setAttributes(attributes: SpanAttributes): void;
setStatus(status: SpanStatus): void;
}
export { NOOP_TELEMETRY_CONTEXT } from "./noop.ts";
export type TelemetryAttributeType = "string" | "number" | "boolean" | "string[]" | "number[]" | "boolean[]";
export interface TelemetryAttributeMetadata {
description: string;
sensitive?: boolean;
cardinality?: "low" | "high";
}
export type TelemetryAttributeDefinition = TelemetryAttributeMetadata &
(
| { type: "string"; values?: readonly string[]; examples?: readonly string[] }
| { type: "number"; values?: readonly number[]; examples?: readonly number[] }
| { type: "boolean"; values?: readonly boolean[]; examples?: readonly boolean[] }
| { type: "string[]"; elementValues?: readonly string[]; examples?: readonly (readonly string[])[] }
| { type: "number[]"; elementValues?: readonly number[]; examples?: readonly (readonly number[])[] }
| { type: "boolean[]"; elementValues?: readonly boolean[]; examples?: readonly (readonly boolean[])[] }
);
export type TelemetryStartAttributeDefinition = TelemetryAttributeDefinition & { required: boolean };
export type TelemetryEventAttributeDefinition = TelemetryAttributeDefinition & { required: boolean };
export interface TelemetryEventDefinition {
description: string;
attributes: Record<string, TelemetryEventAttributeDefinition>;
}
export type TelemetryParentDefinition =
| { kind: "any" }
| { kind: "root_or_external" }
| { kind: "spans"; spans: readonly string[] };
export interface TelemetrySpanDefinition {
description: string;
parents: TelemetryParentDefinition;
startAttributes: Record<string, TelemetryStartAttributeDefinition>;
endAttributes: Record<string, TelemetryAttributeDefinition>;
events?: Record<string, TelemetryEventDefinition>;
status: { default: "ok"; errorWhen: string };
}
export interface TelemetrySchemaDefinition {
version: number;
spans: Record<string, TelemetrySpanDefinition>;
}
/** Typed identity helper for serializable telemetry schema data. */
export function defineTelemetrySchema<const T extends TelemetrySchemaDefinition>(schema: T): T {
return schema;
}
type AttributeDefinitionValue<Definition extends TelemetryAttributeDefinition> = Definition extends {
type: "string";
values: readonly (infer Value extends string)[];
}
? Value
: Definition extends { type: "string" }
? string
: Definition extends { type: "number"; values: readonly (infer Value extends number)[] }
? Value
: Definition extends { type: "number" }
? number
: Definition extends { type: "boolean"; values: readonly (infer Value extends boolean)[] }
? Value
: Definition extends { type: "boolean" }
? boolean
: Definition extends {
type: "string[]";
elementValues: readonly (infer Value extends string)[];
}
? readonly Value[]
: Definition extends { type: "string[]" }
? readonly string[]
: Definition extends {
type: "number[]";
elementValues: readonly (infer Value extends number)[];
}
? readonly Value[]
: Definition extends { type: "number[]" }
? readonly number[]
: Definition extends {
type: "boolean[]";
elementValues: readonly (infer Value extends boolean)[];
}
? readonly Value[]
: readonly boolean[];
type RequiredAttributeNames<
Definitions extends Record<string, TelemetryStartAttributeDefinition | TelemetryEventAttributeDefinition>,
> = {
[Name in keyof Definitions]-?: Definitions[Name]["required"] extends true ? Name : never;
}[keyof Definitions];
type OptionalAttributeNames<
Definitions extends Record<string, TelemetryStartAttributeDefinition | TelemetryEventAttributeDefinition>,
> = Exclude<keyof Definitions, RequiredAttributeNames<Definitions>>;
export type InferRequiredAndOptionalAttributes<
Definitions extends Record<string, TelemetryStartAttributeDefinition | TelemetryEventAttributeDefinition>,
> = keyof Definitions extends never
? Record<string, never>
: {
[Name in RequiredAttributeNames<Definitions>]: AttributeDefinitionValue<Definitions[Name]>;
} & {
[Name in OptionalAttributeNames<Definitions>]?: AttributeDefinitionValue<Definitions[Name]>;
};
export type InferStartAttributes<Definitions extends Record<string, TelemetryStartAttributeDefinition>> =
InferRequiredAndOptionalAttributes<Definitions>;
export type InferOptionalAttributes<Definitions extends Record<string, TelemetryAttributeDefinition>> =
keyof Definitions extends never
? Record<string, never>
: { [Name in keyof Definitions]?: AttributeDefinitionValue<Definitions[Name]> };
export type ExactTelemetryAttributes<Expected, Actual extends Expected> = Actual &
Record<Exclude<keyof Actual, keyof Expected>, never>;
export type InferEventAttributes<Definitions extends Record<string, TelemetryEventAttributeDefinition>> =
InferRequiredAndOptionalAttributes<Definitions>;
export type TelemetrySchemaSpanName<Schema extends TelemetrySchemaDefinition> = keyof Schema["spans"] & string;
type SchemaSpan<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = Schema["spans"][Name];
export type TelemetrySchemaSpanStartAttributes<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = SchemaSpan<Schema, Name>["startAttributes"] extends infer Definitions extends Record<
string,
TelemetryStartAttributeDefinition
>
? InferStartAttributes<Definitions>
: never;
export type TelemetrySchemaSpanEndAttributes<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = SchemaSpan<Schema, Name>["endAttributes"] extends infer Definitions extends Record<
string,
TelemetryAttributeDefinition
>
? InferOptionalAttributes<Definitions>
: never;
type SchemaSpanEvents<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = SchemaSpan<Schema, Name> extends { events: infer Events extends Record<string, TelemetryEventDefinition> }
? Events
: Record<never, never>;
export type TelemetrySchemaSpanEventName<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = keyof SchemaSpanEvents<Schema, Name> & string;
type SchemaSpanEvent<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
EventName extends TelemetrySchemaSpanEventName<Schema, Name>,
> = SchemaSpanEvents<Schema, Name> extends infer Events
? EventName extends keyof Events
? Events[EventName]
: never
: never;
export type TelemetrySchemaSpanEventAttributes<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
EventName extends TelemetrySchemaSpanEventName<Schema, Name>,
> = SchemaSpanEvent<Schema, Name, EventName> extends {
attributes: infer Definitions extends Record<string, TelemetryEventAttributeDefinition>;
}
? InferEventAttributes<Definitions>
: never;
type SchemaSpanEventAttributeDefinitions<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
EventName extends TelemetrySchemaSpanEventName<Schema, Name>,
> = SchemaSpanEvent<Schema, Name, EventName> extends {
attributes: infer Definitions extends Record<string, TelemetryEventAttributeDefinition>;
}
? Definitions
: Record<never, never>;
type EventArguments<
Definitions extends Record<string, TelemetryEventAttributeDefinition>,
Attributes extends InferEventAttributes<Definitions>,
> = [RequiredAttributeNames<Definitions>] extends [never]
? [attributes?: ExactTelemetryAttributes<InferEventAttributes<Definitions>, Attributes>]
: [attributes: ExactTelemetryAttributes<InferEventAttributes<Definitions>, Attributes>];
export type SchemaTelemetrySpan<
Schema extends TelemetrySchemaDefinition,
Name extends TelemetrySchemaSpanName<Schema>,
> = Omit<TelemetrySpan, "addEvent" | "setAttributes"> & {
addEvent<
EventName extends TelemetrySchemaSpanEventName<Schema, Name>,
const Attributes extends InferEventAttributes<
SchemaSpanEventAttributeDefinitions<Schema, Name, EventName>
> = InferEventAttributes<SchemaSpanEventAttributeDefinitions<Schema, Name, EventName>>,
>(
name: EventName,
...args: EventArguments<SchemaSpanEventAttributeDefinitions<Schema, Name, EventName>, Attributes>
): void;
setAttributes<const Attributes extends TelemetrySchemaSpanEndAttributes<Schema, Name>>(
attributes: ExactTelemetryAttributes<TelemetrySchemaSpanEndAttributes<Schema, Name>, Attributes>,
): void;
};
export type TelemetrySchemaSpanUnion<Schema extends TelemetrySchemaDefinition> = {
[Name in TelemetrySchemaSpanName<Schema>]: {
name: Name;
startAttributes: TelemetrySchemaSpanStartAttributes<Schema, Name>;
endAttributes: TelemetrySchemaSpanEndAttributes<Schema, Name>;
events: {
[EventName in TelemetrySchemaSpanEventName<Schema, Name>]: TelemetrySchemaSpanEventAttributes<
Schema,
Name,
EventName
>;
};
};
}[TelemetrySchemaSpanName<Schema>];
type TelemetrySchemaTuple = readonly [TelemetrySchemaDefinition, ...TelemetrySchemaDefinition[]];
type SpanNameInSchema<Schema extends TelemetrySchemaDefinition> = Schema extends TelemetrySchemaDefinition
? TelemetrySchemaSpanName<Schema>
: never;
type SpanNameInSchemas<Schemas extends TelemetrySchemaTuple> = SpanNameInSchema<Schemas[number]>;
type SpanStartAttributesInSchema<
Schema extends TelemetrySchemaDefinition,
Name extends string,
> = Schema extends TelemetrySchemaDefinition
? Name extends TelemetrySchemaSpanName<Schema>
? TelemetrySchemaSpanStartAttributes<Schema, Name>
: never
: never;
type SpanInSchema<
Schema extends TelemetrySchemaDefinition,
Name extends string,
> = Schema extends TelemetrySchemaDefinition
? Name extends TelemetrySchemaSpanName<Schema>
? SchemaTelemetrySpan<Schema, Name>
: never
: never;
type DuplicateTelemetrySpanNames<
Schemas extends readonly TelemetrySchemaDefinition[],
Seen extends string = never,
> = Schemas extends readonly [
infer Schema extends TelemetrySchemaDefinition,
...infer Rest extends readonly TelemetrySchemaDefinition[],
]
?
| Extract<TelemetrySchemaSpanName<Schema>, Seen>
| DuplicateTelemetrySpanNames<Rest, Seen | TelemetrySchemaSpanName<Schema>>
: never;
type UniqueTelemetrySchemas<Schemas extends TelemetrySchemaTuple> = [DuplicateTelemetrySpanNames<Schemas>] extends [
never,
]
? unknown
: { readonly "duplicate telemetry span names": DuplicateTelemetrySpanNames<Schemas> };
type UnionToIntersection<Union> = (Union extends unknown ? (value: Union) => void : never) extends (
value: infer Intersection,
) => void
? Intersection
: never;
type TypedSpanStarterForName<Schemas extends TelemetrySchemaTuple, Name extends SpanNameInSchemas<Schemas>> = <
const Attributes extends SpanStartAttributesInSchema<Schemas[number], Name>,
Result,
>(
name: Name,
attributes: ExactTelemetryAttributes<SpanStartAttributesInSchema<Schemas[number], Name>, Attributes>,
callback: (
span: SpanInSchema<Schemas[number], Name>,
startChildSpan: TypedSpanStarter<Schemas>,
) => Result | Promise<Result>,
) => Promise<Result>;
/** A per-span overload set bound to one explicit parent context and one or more schemas. */
export type TypedSpanStarter<Schemas extends TelemetrySchemaTuple> = UnionToIntersection<
{
[Name in SpanNameInSchemas<Schemas>]: TypedSpanStarterForName<Schemas, Name>;
}[SpanNameInSchemas<Schemas>]
>;
function bindTypedSpanStarter<Schemas extends TelemetrySchemaTuple>(
telemetryContext: TelemetryContext,
): TypedSpanStarter<Schemas> {
const startSpan = (
name: SpanNameInSchemas<Schemas>,
attributes: SpanAttributes,
callback: (
span: SpanInSchema<Schemas[number], SpanNameInSchemas<Schemas>>,
startChildSpan: TypedSpanStarter<Schemas>,
) => unknown,
): Promise<unknown> =>
telemetryContext.startSpan({ name, attributes }, (span) =>
callback(
span as SpanInSchema<Schemas[number], SpanNameInSchemas<Schemas>>,
bindTypedSpanStarter<Schemas>(span),
),
);
return startSpan as TypedSpanStarter<Schemas>;
}
/**
* Bind an explicit parent context to the combined span vocabulary of one or more schemas.
* Schema values are used only for type inference; no runtime schema validation is performed.
*/
export function createTypedSpanStarter<const Schemas extends TelemetrySchemaTuple>(
telemetryContext: TelemetryContext,
_schemas: Schemas & UniqueTelemetrySchemas<Schemas>,
): TypedSpanStarter<Schemas> {
return bindTypedSpanStarter<Schemas>(telemetryContext);
}
export type { RecordedTelemetryEvent, RecordedTelemetrySpan } from "./memory.ts";
export { InMemoryTelemetryContext } from "./memory.ts";

View File

@@ -0,0 +1,219 @@
import type {
AttributeValue,
SpanAttributes,
SpanOptions,
SpanStatus,
TelemetryContext,
TelemetrySpan,
} from "./index.ts";
import { NOOP_TELEMETRY_CONTEXT } from "./noop.ts";
export interface RecordedTelemetryEvent {
readonly name: string;
readonly attributes: Readonly<SpanAttributes>;
}
export interface RecordedTelemetrySpan {
readonly id: number;
readonly parentId: number | null;
readonly name: string;
readonly attributes: Readonly<SpanAttributes>;
readonly events: readonly RecordedTelemetryEvent[];
readonly status: SpanStatus;
readonly settled: boolean;
readonly endSequence?: number;
}
interface MutableRecordedTelemetryEvent {
name: string;
attributes: SpanAttributes;
}
interface MutableRecordedTelemetrySpan {
id: number;
parentId: number | null;
name: string;
attributes: SpanAttributes;
events: MutableRecordedTelemetryEvent[];
status: SpanStatus;
explicitStatus: boolean;
settled: boolean;
endSequence?: number;
}
interface InMemoryTelemetryState {
spans: MutableRecordedTelemetrySpan[];
nextSpanId: number;
nextEndSequence: number;
}
function copyAttributeValue(value: AttributeValue): AttributeValue {
return Array.isArray(value) ? ([...value] as AttributeValue) : value;
}
function copyAttributes(attributes?: SpanAttributes): SpanAttributes {
const copy: SpanAttributes = {};
if (!attributes) return copy;
for (const [name, value] of Object.entries(attributes)) {
if (value !== undefined) copy[name] = copyAttributeValue(value);
}
return copy;
}
function mergeAttributes(current: SpanAttributes, attributes: SpanAttributes): SpanAttributes {
const merged = copyAttributes(current);
for (const [name, value] of Object.entries(attributes)) {
if (value !== undefined) merged[name] = copyAttributeValue(value);
}
return merged;
}
function copyStatus(status: SpanStatus): SpanStatus {
if (status.status === "ok") return { status: "ok" };
return status.error
? { status: "error", error: { name: status.error.name, message: status.error.message } }
: { status: "error" };
}
function automaticErrorStatus(error: unknown): SpanStatus {
try {
if (error instanceof Error) {
return { status: "error", error: { name: error.name, message: error.message } };
}
} catch {
// Error inspection is passive. Fall through to an error status without details.
}
return { status: "error" };
}
function settleSpan(
state: InMemoryTelemetryState,
span: MutableRecordedTelemetrySpan,
failed: boolean,
error?: unknown,
): void {
if (span.settled) return;
if (failed && !span.explicitStatus) span.status = automaticErrorStatus(error);
span.settled = true;
span.endSequence = state.nextEndSequence++;
}
function createSpan(
state: InMemoryTelemetryState,
parent: MutableRecordedTelemetrySpan | undefined,
options: SpanOptions,
): MutableRecordedTelemetrySpan {
const name = options.name;
const attributes = copyAttributes(options.attributes);
return {
id: state.nextSpanId++,
parentId: parent?.id ?? null,
name,
attributes,
events: [],
status: { status: "ok" },
explicitStatus: false,
settled: false,
};
}
function startInMemorySpan<T>(
state: InMemoryTelemetryState,
parent: MutableRecordedTelemetrySpan | undefined,
options: SpanOptions,
callback: (span: TelemetrySpan) => T | Promise<T>,
): Promise<T> {
if (parent?.settled) return NOOP_TELEMETRY_CONTEXT.startSpan(options, callback);
let recordedSpan: MutableRecordedTelemetrySpan;
try {
recordedSpan = createSpan(state, parent, options);
state.spans.push(recordedSpan);
} catch {
return NOOP_TELEMETRY_CONTEXT.startSpan(options, callback);
}
const span: TelemetrySpan = {
startSpan: <Result>(
childOptions: SpanOptions,
childCallback: (child: TelemetrySpan) => Result | Promise<Result>,
) => startInMemorySpan(state, recordedSpan, childOptions, childCallback),
addEvent(name, attributes) {
if (recordedSpan.settled) return;
try {
recordedSpan.events.push({ name, attributes: copyAttributes(attributes) });
} catch {
// Recording is passive. Ignore malformed or unreadable telemetry payloads.
}
},
setAttributes(attributes) {
if (recordedSpan.settled) return;
try {
recordedSpan.attributes = mergeAttributes(recordedSpan.attributes, attributes);
} catch {
// Recording is passive. Ignore malformed or unreadable telemetry payloads.
}
},
setStatus(status) {
if (recordedSpan.settled) return;
try {
recordedSpan.status = copyStatus(status);
recordedSpan.explicitStatus = true;
} catch {
// Recording is passive. Ignore malformed or unreadable telemetry payloads.
}
},
};
let result: T | Promise<T>;
try {
result = callback(span);
} catch (error) {
settleSpan(state, recordedSpan, true, error);
return Promise.reject(error);
}
return Promise.resolve(result).then(
(value) => {
settleSpan(state, recordedSpan, false);
return value;
},
(error: unknown) => {
settleSpan(state, recordedSpan, true, error);
throw error;
},
);
}
/**
* Backend-neutral reference implementation that records spans in process memory.
* Create a fresh instance to isolate tests or independent recording scopes.
*/
export class InMemoryTelemetryContext implements TelemetryContext {
private readonly state: InMemoryTelemetryState = {
spans: [],
nextSpanId: 1,
nextEndSequence: 1,
};
startSpan<T>(options: SpanOptions, callback: (span: TelemetrySpan) => T | Promise<T>): Promise<T> {
return startInMemorySpan(this.state, undefined, options, callback);
}
/** Returns detached snapshots in span-start order. */
getSpans(): readonly RecordedTelemetrySpan[] {
return this.state.spans.map((span) => ({
id: span.id,
parentId: span.parentId,
name: span.name,
attributes: copyAttributes(span.attributes),
events: span.events.map((event) => ({
name: event.name,
attributes: copyAttributes(event.attributes),
})),
status: copyStatus(span.status),
settled: span.settled,
...(span.endSequence === undefined ? {} : { endSequence: span.endSequence }),
}));
}
}

View File

@@ -0,0 +1,20 @@
import type { SpanOptions, TelemetryContext, TelemetrySpan } from "./index.ts";
function startNoopSpan<T>(_options: SpanOptions, callback: (span: TelemetrySpan) => T | Promise<T>): Promise<T> {
try {
return Promise.resolve(callback(noopTelemetrySpan));
} catch (error) {
return Promise.reject(error);
}
}
const noopTelemetrySpan: TelemetrySpan = {
startSpan: startNoopSpan,
addEvent: () => {},
setAttributes: () => {},
setStatus: () => {},
};
Object.freeze(noopTelemetrySpan);
/** Shared telemetry context used when an application does not provide one. */
export const NOOP_TELEMETRY_CONTEXT: TelemetryContext = noopTelemetrySpan;

View File

@@ -0,0 +1,315 @@
import { deepStrictEqual, doesNotThrow, fail, ok, strictEqual } from "node:assert/strict";
import type { SpanAttributes, SpanOptions, SpanStatus, TelemetrySpan } from "../index.ts";
import type { RecordedTelemetrySpan } from "../memory.ts";
import type {
TelemetryAdapterConformanceCase,
TelemetryAdapterFixture,
TelemetryAdapterFixtureFactory,
} from "./types.ts";
type ConformanceTest = (fixture: TelemetryAdapterFixture) => Promise<void>;
function createCase(
factory: TelemetryAdapterFixtureFactory,
group: string,
name: string,
test: ConformanceTest,
): TelemetryAdapterConformanceCase {
return {
group,
name,
async run() {
await using fixture = await factory();
await test(fixture);
},
};
}
function findSpan(spans: readonly RecordedTelemetrySpan[], name: string): RecordedTelemetrySpan {
const span = spans.find((candidate) => candidate.name === name);
ok(span, `Expected recorded span ${name}`);
return span;
}
async function rejectsWithSameValue(operation: Promise<unknown>, expected: unknown): Promise<void> {
try {
await operation;
fail("Expected operation to reject");
} catch (error) {
strictEqual(error, expected);
}
}
function unreadable<T extends object>(value: T): T {
return new Proxy(value, {
get: () => {
throw new Error("read");
},
getOwnPropertyDescriptor: () => {
throw new Error("inspect");
},
getPrototypeOf: () => {
throw new Error("prototype");
},
ownKeys: () => {
throw new Error("enumerate");
},
});
}
/** Creates runner-independent cases for the callback telemetry adapter contract. */
export function createTelemetryAdapterConformance(
factory: TelemetryAdapterFixtureFactory,
): readonly TelemetryAdapterConformanceCase[] {
return [
createCase(
factory,
"callback lifecycle",
"admits once synchronously and preserves the result",
async (fixture) => {
let admitted = false;
let calls = 0;
const expected = { value: 42 };
const result = fixture.context.startSpan({ name: "success" }, () => {
admitted = true;
calls++;
return expected;
});
strictEqual(admitted, true);
strictEqual(calls, 1);
strictEqual(await result, expected);
deepStrictEqual(findSpan(await fixture.getSpans(), "success").status, { status: "ok" });
strictEqual(findSpan(await fixture.getSpans(), "success").settled, true);
},
),
createCase(
factory,
"callback lifecycle",
"preserves synchronous and asynchronous rejection values",
async (fixture) => {
const syncError = new Error("sync");
await rejectsWithSameValue(
fixture.context.startSpan({ name: "sync-error" }, () => {
throw syncError;
}),
syncError,
);
const asyncError = { kind: "async" };
await rejectsWithSameValue(
fixture.context.startSpan({ name: "async-error" }, async () => {
throw asyncError;
}),
asyncError,
);
await rejectsWithSameValue(
fixture.context.startSpan({ name: "undefined-error" }, () => Promise.reject(undefined)),
undefined,
);
const unreadableError = unreadable({ kind: "unreadable" });
await rejectsWithSameValue(
fixture.context.startSpan({ name: "unreadable-error" }, () => {
throw unreadableError;
}),
unreadableError,
);
const asyncUnreadableError = unreadable({ kind: "async-unreadable" });
await rejectsWithSameValue(
fixture.context.startSpan({ name: "async-unreadable-error" }, () =>
Promise.reject(asyncUnreadableError),
),
asyncUnreadableError,
);
const spans = await fixture.getSpans();
for (const name of [
"sync-error",
"async-error",
"undefined-error",
"unreadable-error",
"async-unreadable-error",
]) {
strictEqual(findSpan(spans, name).status.status, "error");
}
},
),
createCase(factory, "status", "uses last explicit status without automatic overwrite", async (fixture) => {
await fixture.context.startSpan({ name: "last-status" }, (span) => {
span.setStatus({ status: "error", error: { name: "Expected", message: "first" } });
span.setStatus({ status: "ok" });
});
const thrown = new Error("after explicit status");
await rejectsWithSameValue(
fixture.context.startSpan({ name: "explicit-before-throw" }, (span) => {
span.setStatus({ status: "ok" });
throw thrown;
}),
thrown,
);
const rejected = new Error("after async explicit status");
await rejectsWithSameValue(
fixture.context.startSpan({ name: "explicit-before-rejection" }, (span) => {
span.setStatus({ status: "error", error: { name: "Expected", message: "async failure" } });
return Promise.reject(rejected);
}),
rejected,
);
await fixture.context.startSpan({ name: "expected-failure" }, (span) => {
span.setStatus({ status: "error", error: { name: "Expected", message: "returned failure" } });
return { ok: false };
});
const spans = await fixture.getSpans();
deepStrictEqual(findSpan(spans, "last-status").status, { status: "ok" });
deepStrictEqual(findSpan(spans, "explicit-before-throw").status, { status: "ok" });
deepStrictEqual(findSpan(spans, "explicit-before-rejection").status, {
status: "error",
error: { name: "Expected", message: "async failure" },
});
deepStrictEqual(findSpan(spans, "expected-failure").status, {
status: "error",
error: { name: "Expected", message: "returned failure" },
});
}),
createCase(factory, "recording", "merges attributes and records ordered events", async (fixture) => {
await fixture.context.startSpan(
{
name: "recording",
attributes: { start: "value", overwrite: "start", ignored: undefined },
},
(span) => {
span.setAttributes({ count: 1, overwrite: "middle" });
span.setAttributes({ count: undefined, overwrite: "end" });
span.addEvent("first", { index: 1, ignored: undefined });
span.addEvent("second", { index: 2 });
},
);
const span = findSpan(await fixture.getSpans(), "recording");
deepStrictEqual(span.attributes, { start: "value", overwrite: "end", count: 1 });
deepStrictEqual(span.events, [
{ name: "first", attributes: { index: 1 } },
{ name: "second", attributes: { index: 2 } },
]);
}),
createCase(factory, "recording", "ignores failed attribute calls atomically", async (fixture) => {
await fixture.context.startSpan({ name: "atomic-attributes", attributes: { retained: "value" } }, (span) => {
const attributes: SpanAttributes = {
partial: "must not survive",
unreadable: unreadable(["value"]),
};
doesNotThrow(() => span.setAttributes(attributes));
});
deepStrictEqual(findSpan(await fixture.getSpans(), "atomic-attributes").attributes, {
retained: "value",
});
}),
createCase(factory, "recording", "makes calls after settlement inert", async (fixture) => {
let settledSpan: TelemetrySpan | undefined;
await fixture.context.startSpan({ name: "settled", attributes: { value: "initial" } }, (span) => {
settledSpan = span;
});
const capturedSpan = settledSpan;
if (!capturedSpan) throw new Error("Expected callback span");
doesNotThrow(() => capturedSpan.setAttributes({ value: "late" }));
doesNotThrow(() => capturedSpan.addEvent("late", { value: true }));
doesNotThrow(() => capturedSpan.setStatus({ status: "error" }));
let childAdmitted = false;
const childResult = capturedSpan.startSpan({ name: "late-child" }, () => {
childAdmitted = true;
return 7;
});
strictEqual(childAdmitted, true);
strictEqual(await childResult, 7);
const spans = await fixture.getSpans();
strictEqual(spans.length, 1);
deepStrictEqual(spans[0]?.attributes, { value: "initial" });
deepStrictEqual(spans[0]?.events, []);
deepStrictEqual(spans[0]?.status, { status: "ok" });
}),
createCase(factory, "parentage", "records nested and concurrent child relationships", async (fixture) => {
let releaseFirst: (() => void) | undefined;
const firstGate = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
await fixture.context.startSpan({ name: "parent" }, async (parent) => {
const first = parent.startSpan({ name: "first-child" }, async () => {
await firstGate;
});
const second = parent.startSpan({ name: "second-child" }, () => "done");
strictEqual(await second, "done");
releaseFirst?.();
await first;
});
const spans = await fixture.getSpans();
const parent = findSpan(spans, "parent");
const first = findSpan(spans, "first-child");
const second = findSpan(spans, "second-child");
strictEqual(parent.parentId, null);
strictEqual(first.parentId, parent.id);
strictEqual(second.parentId, parent.id);
ok(second.endSequence !== undefined && first.endSequence !== undefined && parent.endSequence !== undefined);
ok(second.endSequence < first.endSequence);
ok(first.endSequence < parent.endSequence);
}),
createCase(factory, "passivity", "suppresses unreadable telemetry payload failures", async (fixture) => {
let calls = 0;
const options = unreadable<SpanOptions>({ name: "unreadable-options", attributes: { secret: "value" } });
const result = fixture.context.startSpan(options, () => {
calls++;
return 9;
});
strictEqual(calls, 1);
strictEqual(await result, 9);
deepStrictEqual(await fixture.getSpans(), []);
await fixture.context.startSpan({ name: "unreadable-recording" }, (span) => {
const attributes = unreadable<SpanAttributes>({ secret: "value" });
const status = unreadable<SpanStatus>({ status: "ok" });
doesNotThrow(() => span.setAttributes(attributes));
doesNotThrow(() => span.addEvent("unreadable-event", attributes));
doesNotThrow(() => span.setStatus(status));
});
const recorded = await fixture.getSpans();
strictEqual(recorded.length, 1);
deepStrictEqual(recorded[0]?.attributes, {});
deepStrictEqual(recorded[0]?.events, []);
deepStrictEqual(recorded[0]?.status, { status: "ok" });
}),
createCase(factory, "passivity", "ignores failed status calls atomically", async (fixture) => {
const rejection = new Error("rejected after unreadable status");
await rejectsWithSameValue(
fixture.context.startSpan({ name: "unreadable-status" }, (span) => {
const status = unreadable<SpanStatus>({ status: "ok" });
doesNotThrow(() => span.setStatus(status));
return Promise.reject(rejection);
}),
rejection,
);
strictEqual(findSpan(await fixture.getSpans(), "unreadable-status").status.status, "error");
}),
];
}

View File

@@ -0,0 +1,6 @@
export { createTelemetryAdapterConformance } from "./conformance.ts";
export type {
TelemetryAdapterConformanceCase,
TelemetryAdapterFixture,
TelemetryAdapterFixtureFactory,
} from "./types.ts";

View File

@@ -0,0 +1,18 @@
import type { TelemetryContext } from "../index.ts";
import type { RecordedTelemetrySpan } from "../memory.ts";
/** A fresh adapter instance and normalized snapshot reader owned by one conformance case. */
export interface TelemetryAdapterFixture extends AsyncDisposable {
readonly context: TelemetryContext;
getSpans(): Promise<readonly RecordedTelemetrySpan[]>;
}
/** Creates an isolated adapter fixture for one conformance case. */
export type TelemetryAdapterFixtureFactory = () => Promise<TelemetryAdapterFixture>;
/** A runner-independent conformance case that can be registered with any test framework. */
export interface TelemetryAdapterConformanceCase {
readonly group: string;
readonly name: string;
run(): Promise<void>;
}

View File

@@ -0,0 +1,46 @@
import { describe, expect, it } from "vitest";
import { InMemoryTelemetryContext, type SpanAttributes } from "../src/index.ts";
import { createTelemetryAdapterConformance, type TelemetryAdapterFixture } from "../src/testing/index.ts";
const conformance = createTelemetryAdapterConformance(() => {
const context = new InMemoryTelemetryContext();
return Promise.resolve<TelemetryAdapterFixture>({
context,
getSpans: () => Promise.resolve(context.getSpans()),
[Symbol.asyncDispose]: () => Promise.resolve(),
});
});
describe("InMemoryTelemetryContext conformance", () => {
for (const group of new Set(conformance.map((testCase) => testCase.group))) {
describe(group, () => {
for (const testCase of conformance.filter((candidate) => candidate.group === group)) {
it(testCase.name, () => testCase.run());
}
});
}
it("returns detached snapshots without exposing mutable recording state", async () => {
const context = new InMemoryTelemetryContext();
let openSettled: boolean | undefined;
let openEndSequence: number | undefined;
await context.startSpan({ name: "snapshot", attributes: { tags: ["initial"] } }, (span) => {
span.addEvent("event", { value: 1 });
const [open] = context.getSpans();
openSettled = open?.settled;
openEndSequence = open?.endSequence;
});
expect(openSettled).toBe(false);
expect(openEndSequence).toBeUndefined();
const [first] = context.getSpans();
expect(first?.settled).toBe(true);
expect(first?.endSequence).toBe(1);
(first?.attributes as SpanAttributes).tags = ["mutated"];
(first?.events[0]?.attributes as SpanAttributes).value = 2;
const [second] = context.getSpans();
expect(second?.attributes).toEqual({ tags: ["initial"] });
expect(second?.events).toEqual([{ name: "event", attributes: { value: 1 } }]);
});
});

View File

@@ -0,0 +1,197 @@
import { describe, expect, expectTypeOf, it } from "vitest";
import {
createTypedSpanStarter,
defineTelemetrySchema,
InMemoryTelemetryContext,
NOOP_TELEMETRY_CONTEXT,
type SchemaTelemetrySpan,
type SpanAttributes,
type SpanOptions,
type SpanStatus,
type TelemetrySchemaSpanStartAttributes,
type TelemetrySpan,
} from "../src/index.ts";
function unreadable<T extends object>(value: T): T {
return new Proxy(value, {
get: () => {
throw new Error("read");
},
getOwnPropertyDescriptor: () => {
throw new Error("inspect");
},
ownKeys: () => {
throw new Error("enumerate");
},
});
}
describe("telemetry schemas", () => {
it("preserves serializable definitions and infers exact attributes", () => {
const definition = {
version: 1,
spans: {
operation: {
description: "Test operation",
parents: { kind: "any" },
startAttributes: {
kind: { type: "string", required: true, values: ["read", "write"], description: "Kind" },
},
endAttributes: {},
events: {
result: {
description: "Result",
attributes: {
outcome: { type: "string", required: true, values: ["ok", "error"], description: "Outcome" },
},
},
},
status: { default: "ok", errorWhen: "The operation fails" },
},
},
} as const;
const schema = defineTelemetrySchema(definition);
expect(schema).toBe(definition);
expect(() => JSON.stringify(schema)).not.toThrow();
expectTypeOf<TelemetrySchemaSpanStartAttributes<typeof schema, "operation">>().toMatchTypeOf<{
kind: "read" | "write";
}>();
const compileTimeFailures = (span: SchemaTelemetrySpan<typeof schema, "operation">) => {
span.addEvent("result", { outcome: "ok" });
// @ts-expect-error required event attributes cannot be omitted
span.addEvent("result");
// @ts-expect-error closed-set event values are exact
span.addEvent("result", { outcome: "other" });
// @ts-expect-error undeclared events are rejected
span.addEvent("unknown", {});
// @ts-expect-error empty end schemas reject every attribute
span.setAttributes({ unknown: true });
};
expectTypeOf(compileTimeFailures).toBeFunction();
});
it("combines schema vocabularies and binds child starters to their parent spans", async () => {
const operationSchema = defineTelemetrySchema({
version: 1,
spans: {
operation: {
description: "Operation",
parents: { kind: "root_or_external" },
startAttributes: {
kind: { type: "string", required: true, values: ["read", "write"], description: "Kind" },
},
endAttributes: {},
status: { default: "ok", errorWhen: "The operation fails" },
},
},
} as const);
const requestSchema = defineTelemetrySchema({
version: 3,
spans: {
request: {
description: "Request",
parents: { kind: "spans", spans: ["operation"] },
startAttributes: {
provider: { type: "string", required: true, description: "Provider" },
},
endAttributes: {
response: { type: "string", description: "Response kind" },
},
status: { default: "ok", errorWhen: "The request fails" },
},
},
} as const);
const telemetryContext = new InMemoryTelemetryContext();
const startSpan = createTypedSpanStarter(telemetryContext, [operationSchema, requestSchema]);
const result = await startSpan("operation", { kind: "read" }, (_operationSpan, startChildSpan) =>
startChildSpan("request", { provider: "example" }, (requestSpan) => {
requestSpan.setAttributes({ response: "cached" });
return 42;
}),
);
expect(result).toBe(42);
expectTypeOf(result).toEqualTypeOf<number>();
const spans = telemetryContext.getSpans();
const operationSpan = spans.find((span) => span.name === "operation");
const requestSpan = spans.find((span) => span.name === "request");
expect(operationSpan?.parentId).toBeNull();
expect(requestSpan?.parentId).toBe(operationSpan?.id);
expect(() =>
createTypedSpanStarter(telemetryContext, unreadable([operationSchema, requestSchema] as const)),
).not.toThrow();
const syncError = { kind: "sync" };
const syncResult = startSpan("operation", { kind: "write" }, () => {
throw syncError;
});
await expect(syncResult).rejects.toBe(syncError);
const asyncError = { kind: "async" };
const asyncResult = startSpan("request", { provider: "example" }, async () => {
throw asyncError;
});
await expect(asyncResult).rejects.toBe(asyncError);
const compileTimeFailures = () => {
const spanName: "operation" | "request" = Math.random() > 0.5 ? "operation" : "request";
// @ts-expect-error union-valued names must be narrowed to preserve name and attribute correlation
void startSpan(spanName, { kind: "read" }, () => {});
const extraRequestAttributes = { provider: "example", unknown: true } as const;
// @ts-expect-error variables with unknown attributes are rejected
void startSpan("request", extraRequestAttributes, () => {});
// @ts-expect-error unknown span names are rejected across the combined vocabulary
void startSpan("unknown", {}, () => {});
// @ts-expect-error attributes are selected from the schema that owns the span
void startSpan("request", { kind: "read" }, () => {});
// @ts-expect-error duplicate span names across schemas are rejected
void createTypedSpanStarter(telemetryContext, [operationSchema, operationSchema]);
};
expectTypeOf(compileTimeFailures).toBeFunction();
});
});
describe("NOOP_TELEMETRY_CONTEXT", () => {
it("admits callbacks synchronously and reuses one inert span", async () => {
let admitted = false;
let firstSpan: TelemetrySpan | undefined;
const result = NOOP_TELEMETRY_CONTEXT.startSpan({ name: "first" }, async (span) => {
admitted = true;
firstSpan = span;
const child = await span.startSpan({ name: "child" }, (childSpan) => childSpan);
expect(child).toBe(span);
return 42;
});
expect(admitted).toBe(true);
expect(await result).toBe(42);
expect(Object.isFrozen(firstSpan)).toBe(true);
});
it("preserves synchronous and asynchronous rejection values", async () => {
const syncError = { kind: "sync" };
const sync = NOOP_TELEMETRY_CONTEXT.startSpan({ name: "sync" }, () => {
throw syncError;
});
await expect(sync).rejects.toBe(syncError);
const asyncError = { kind: "async" };
const asyncResult = NOOP_TELEMETRY_CONTEXT.startSpan({ name: "async" }, async () => {
throw asyncError;
});
await expect(asyncResult).rejects.toBe(asyncError);
});
it("does not inspect or retain telemetry payloads", async () => {
const options = unreadable<SpanOptions>({ name: "operation", attributes: { secret: "prompt content" } });
await NOOP_TELEMETRY_CONTEXT.startSpan(options, (span) => {
const attributes = unreadable<SpanAttributes>({ secret: "content" });
const status = unreadable<SpanStatus>({ status: "ok" });
expect(() => span.addEvent("event", attributes)).not.toThrow();
expect(() => span.setAttributes(attributes)).not.toThrow();
expect(() => span.setStatus(status)).not.toThrow();
});
});
});

View File

@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist", "**/*.d.ts", "src/**/*.d.ts"]
}