autotel
Version:
Write Once, Observe Anywhere
59 lines (55 loc) • 2.14 kB
TypeScript
import { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
/**
* TestSpanCollector — SpanExporter that groups finished spans by traceId
* and drains per-trace for embedding in test metadata.
*
* @example
* ```typescript
* import { TestSpanCollector } from 'autotel/test-span-collector';
* import { SimpleSpanProcessor } from 'autotel/processors';
* import { getAutotelTracerProvider } from 'autotel';
*
* const collector = new TestSpanCollector();
* const provider = getAutotelTracerProvider();
* provider.addSpanProcessor(new SimpleSpanProcessor(collector));
*
* // After a test span ends:
* const spans = collector.drainTrace(traceId, rootSpanId);
* // spans contains only descendants of rootSpanId
* ```
*/
/** Attribute value types that survive serialization */
type SerializableValue = string | number | boolean | string[] | number[] | boolean[];
/**
* Portable serialized span for embedding in test metadata.
* `startTimeMs` is derived from OTel HrTime — epoch-based wall-clock ms in the current SDK.
*
* Defined as a `type` (not `interface`) so it is assignable to
* `Record<string, unknown>` in TypeScript 6+ strict mode.
*/
type SerializedSpan = {
spanId: string;
parentSpanId?: string;
name: string;
startTimeMs: number;
durationMs: number;
status: 'ok' | 'error' | 'unset';
statusMessage?: string;
attributes?: Record<string, SerializableValue>;
};
declare class TestSpanCollector implements SpanExporter {
private traces;
export(spans: ReadableSpan[], callback: (result: {
code: number;
}) => void): void;
/**
* Drain and serialize spans that are descendants of `rootSpanId` within `traceId`.
* Filters to the subtree rooted at the test span to prevent cross-test mixing.
* Removes the entire traceId entry from the collector.
*/
drainTrace(traceId: string, rootSpanId: string): SerializedSpan[];
shutdown(): Promise<void>;
forceFlush(): Promise<void>;
}
declare function serializeSpan(span: ReadableSpan): SerializedSpan;
export { type SerializedSpan, TestSpanCollector, serializeSpan };