/** * TracingManager - Manages distributed tracing for agent execution * * Provides: * - Trace and span management with correlation IDs * - OpenTelemetry-compatible span structure * - Event emission for observability integrations * - Optional export to OTel backends */ import type { Span, SpanAttributes, SpanContext, SpanStatus, Trace, TracingManagerOptions, StartSpanOptions, EndSpanOptions, TracingEventHandler, TracingManagerInterface } from './types.js'; /** * Manages distributed tracing for agent operations * * @example * ```typescript * const tracing = new TracingManager({ * serviceName: 'my-agent', * defaultAttributes: { environment: 'production' }, * }); * * // Start a trace * const traceId = tracing.startTrace(); * * // Create spans * const span = tracing.startSpan({ name: 'process-request' }); * span.attributes['request.id'] = '123'; * * // End span and trace * tracing.endSpan(span.spanId, { status: 'ok' }); * const trace = tracing.endTrace(traceId); * ``` */ export declare class TracingManager implements TracingManagerInterface { private readonly serviceName; private readonly serviceVersion; private readonly defaultAttributes; private readonly maxSpansPerTrace; private readonly autoGenerateTraceId; private readonly traces; private readonly spans; private readonly activeSpanStack; private readonly eventHandlers; private readonly otelExporter?; private spanIdCounter; private traceIdCounter; constructor(options?: TracingManagerOptions); /** * Start a new trace * * @param attributes - Additional trace-level attributes * @returns Trace ID */ startTrace(attributes?: SpanAttributes): string; /** * End a trace and optionally export it * * @param traceId - Trace ID to end * @returns Completed trace or undefined if not found */ endTrace(traceId: string): Trace | undefined; /** * Get a trace by ID */ getTrace(traceId: string): Trace | undefined; /** * Get all active traces */ getActiveTraces(): Trace[]; /** * Clear a trace from memory */ clearTrace(traceId: string): boolean; /** * Start a new span * * @param options - Span options * @returns Created span */ startSpan(options: StartSpanOptions): Span; /** * End a span * * @param spanId - Span ID to end * @param options - End options * @returns Ended span or undefined if not found */ endSpan(spanId: string, options?: EndSpanOptions): Span | undefined; /** * Get a span by ID */ getSpan(spanId: string): Span | undefined; /** * Get the current active span */ getCurrentSpan(): Span | undefined; /** * Get current span context for propagation */ getCurrentContext(): SpanContext | undefined; /** * Add an event to a span * * @param spanId - Span ID * @param name - Event name * @param attributes - Event attributes */ addSpanEvent(spanId: string, name: string, attributes?: SpanAttributes): void; /** * Set attributes on a span * * @param spanId - Span ID * @param attributes - Attributes to set */ setSpanAttributes(spanId: string, attributes: SpanAttributes): void; /** * Set span status * * @param spanId - Span ID * @param status - Status * @param message - Optional status message */ setSpanStatus(spanId: string, status: SpanStatus, message?: string): void; /** * Record an error on a span * * @param spanId - Span ID * @param error - Error to record */ recordError(spanId: string, error: Error): void; /** * Subscribe to tracing events * * @param handler - Event handler * @returns Unsubscribe function */ onEvent(handler: TracingEventHandler): () => void; private emit; /** * Export a trace to configured exporter */ private exportTrace; /** * Manually export a trace */ export(traceId: string): Promise; private generateTraceId; private generateSpanId; /** * Get tracing statistics */ getStats(): { activeTraces: number; totalSpans: number; activeSpans: number; }; /** * Clear all traces and spans */ clear(): void; }