/** * Minimal, dependency-free distributed tracing: W3C trace-context propagation, * spans, and an OTLP/HTTP exporter that speaks to any OpenTelemetry collector. * Compatible with the wider ecosystem without pulling the OTel SDK. */ export type SpanKind = 'internal' | 'server' | 'client' | 'producer' | 'consumer'; export type SpanStatus = 'unset' | 'ok' | 'error'; export type AttributeValue = string | number | boolean; export interface SpanContext { traceId: string; spanId: string; /** W3C trace flags; bit 0 = sampled. */ traceFlags: number; } export interface FinishedSpan { name: string; kind: SpanKind; context: SpanContext; parentSpanId?: string; startTime: number; endTime: number; attributes: Record; status: SpanStatus; statusMessage?: string; } /** Parses a W3C `traceparent` header. */ export declare function parseTraceparent(header: string | undefined): SpanContext | null; /** Formats a W3C `traceparent` header. */ export declare function formatTraceparent(context: SpanContext): string; export interface SpanExporter { export(spans: FinishedSpan[]): void | Promise; forceFlush?(): Promise; } /** Collects spans in memory — for tests and debugging. */ export declare class InMemorySpanExporter implements SpanExporter { readonly spans: FinishedSpan[]; export(spans: FinishedSpan[]): void; } /** Prints a one-line summary per span — for local development. */ export declare class ConsoleSpanExporter implements SpanExporter { private readonly log; constructor(log?: (message: string) => void); export(spans: FinishedSpan[]): void; } export declare class Span { readonly name: string; readonly kind: SpanKind; readonly context: SpanContext; private readonly attributes; private readonly startTime; private readonly onEnd; private readonly clock; readonly parentSpanId?: string | undefined; private ended; constructor(name: string, kind: SpanKind, context: SpanContext, attributes: Record, startTime: number, onEnd: (span: FinishedSpan) => void, clock: () => number, parentSpanId?: string | undefined); setAttribute(key: string, value: AttributeValue): this; private _status; private _statusMessage?; setStatus(status: SpanStatus, message?: string): this; end(): void; } export interface TracerOptions { exporter?: SpanExporter; serviceName?: string; sampled?: boolean; clock?: () => number; idGenerator?: { traceId(): string; spanId(): string; }; } /** The span currently in scope (set by `Tracer.inSpan`). */ export declare function activeSpan(): Span | undefined; export declare class Tracer { readonly serviceName: string; private readonly exporter; private readonly clock; private readonly traceFlags; private readonly ids; constructor(options?: TracerOptions); startSpan(name: string, options?: { parent?: SpanContext; kind?: SpanKind; attributes?: Record; }): Span; /** Runs `fn` with `span` as the active span, ending it afterwards. */ inSpan(span: Span, fn: () => T | Promise): Promise; forceFlush(): Promise; } /** Serializes spans to the OTLP/JSON `ExportTraceServiceRequest` shape. */ export declare function toOtlpJson(spans: FinishedSpan[], serviceName: string): unknown; export interface OtlpHttpExporterOptions { /** Collector base URL, e.g. http://localhost:4318 (path /v1/traces is appended). */ url: string; serviceName?: string; headers?: Record; /** Flush when the buffer reaches this size. Default 100. */ maxBatch?: number; fetchImpl?: typeof fetch; } /** Buffers spans and POSTs them to an OTLP/HTTP collector as OTLP/JSON. */ export declare class OtlpHttpExporter implements SpanExporter { private buffer; private readonly endpoint; private readonly serviceName; private readonly headers; private readonly maxBatch; private readonly fetchImpl; constructor(options: OtlpHttpExporterOptions); export(spans: FinishedSpan[]): void; forceFlush(): Promise; }