/** * @db4/core - OpenTelemetry Integration * * This module provides OpenTelemetry-compatible instrumentation for db4 operations. * It integrates with the standard OpenTelemetry API for distributed tracing and metrics. * * IMPORTANT: @opentelemetry/api is an optional peer dependency. * This module gracefully handles cases where OpenTelemetry is not installed. * * @packageDocumentation */ /** * OpenTelemetry Span interface (subset of @opentelemetry/api Span) */ export interface OTelSpan { setAttribute(key: string, value: string | number | boolean): this; setStatus(status: { code: SpanStatusCode; message?: string; }): this; recordException(exception: Error | string): this; addEvent(name: string, attributes?: Record): this; end(): void; isRecording(): boolean; } /** * OpenTelemetry Tracer interface (subset of @opentelemetry/api Tracer) */ export interface OTelTracer { startSpan(name: string, options?: OTelSpanOptions): OTelSpan; } /** * OpenTelemetry Meter interface (subset of @opentelemetry/api Meter) */ export interface OTelMeter { createCounter(name: string, options?: OTelMetricOptions): OTelCounter; createHistogram(name: string, options?: OTelMetricOptions): OTelHistogram; createUpDownCounter(name: string, options?: OTelMetricOptions): OTelUpDownCounter; } /** * OpenTelemetry Counter interface */ export interface OTelCounter { add(value: number, attributes?: Record): void; } /** * OpenTelemetry UpDownCounter interface */ export interface OTelUpDownCounter { add(value: number, attributes?: Record): void; } /** * OpenTelemetry Histogram interface */ export interface OTelHistogram { record(value: number, attributes?: Record): void; } /** * Span options for starting a new span */ export interface OTelSpanOptions { attributes?: Record; kind?: SpanKind; } /** * Metric options */ export interface OTelMetricOptions { description?: string; unit?: string; } /** * Span status codes (matches @opentelemetry/api SpanStatusCode) */ export declare enum SpanStatusCode { UNSET = 0, OK = 1, ERROR = 2 } /** * Span kinds (matches @opentelemetry/api SpanKind) */ export declare enum SpanKind { INTERNAL = 0, SERVER = 1, CLIENT = 2, PRODUCER = 3, CONSUMER = 4 } /** * Check if OpenTelemetry API is available. */ export declare function isOTelAvailable(): boolean; /** * Get the OpenTelemetry tracer for @db4/core. * Returns a no-op tracer if OpenTelemetry is not available. */ export declare function getTracer(): OTelTracer; /** * Get the OpenTelemetry meter for @db4/core. * Returns a no-op meter if OpenTelemetry is not available. */ export declare function getMeter(): OTelMeter; /** * Counter for total queries executed. * Attributes: collection, operation, status */ export declare function getQueryCounter(): OTelCounter; /** * Counter for total errors. * Attributes: error_type, error_code, collection */ export declare function getErrorCounter(): OTelCounter; /** * Counter for write operations. * Attributes: collection, operation (insert/update/delete/upsert), status */ export declare function getWriteCounter(): OTelCounter; /** * Counter for CDC events emitted. * Attributes: collection, operation */ export declare function getCDCEventsCounter(): OTelCounter; /** * Gauge for active transactions. * Attributes: isolation_level */ export declare function getActiveTransactionsGauge(): OTelUpDownCounter; /** * Histogram for query duration. * Attributes: collection, operation, status */ export declare function getQueryDurationHistogram(): OTelHistogram; /** * Histogram for transaction duration. * Attributes: isolation_level, status */ export declare function getTransactionDurationHistogram(): OTelHistogram; /** * Histogram for storage operation duration. * Attributes: operation (read/write/delete), storage_type */ export declare function getStorageOperationDurationHistogram(): OTelHistogram; /** * Histogram for document sizes. * Attributes: collection */ export declare function getDocumentSizeHistogram(): OTelHistogram; /** * Histogram for batch sizes. * Attributes: collection, operation */ export declare function getBatchSizeHistogram(): OTelHistogram; /** * Start a new span for a db4 operation. * * @param operation - The operation name (e.g., 'query', 'insert', 'transaction') * @param options - Optional span options * @returns The started span */ export declare function startQuerySpan(operation: string, options?: OTelSpanOptions): OTelSpan; /** * Start a new span for a storage operation. * * @param operation - The operation name (e.g., 'read', 'write', 'delete') * @param options - Optional span options * @returns The started span */ export declare function startStorageSpan(operation: string, options?: OTelSpanOptions): OTelSpan; /** * Start a new span for a transaction operation. * * @param operation - The operation name (e.g., 'begin', 'commit', 'rollback') * @param options - Optional span options * @returns The started span */ export declare function startTransactionSpan(operation: string, options?: OTelSpanOptions): OTelSpan; /** * Start a new span for a CDC operation. * * @param operation - The operation name (e.g., 'emit', 'process', 'subscribe') * @param options - Optional span options * @returns The started span */ export declare function startCDCSpan(operation: string, options?: OTelSpanOptions): OTelSpan; /** * Execute a synchronous function within a traced span. * Automatically records duration and handles errors. * * @param name - Span name * @param fn - Function to execute * @param options - Optional span options * @returns The result of the function */ export declare function instrument(name: string, fn: () => T, options?: OTelSpanOptions): T; /** * Execute an async function within a traced span. * Automatically records duration and handles errors. * * @param name - Span name * @param fn - Async function to execute * @param options - Optional span options * @returns The result of the function */ export declare function instrumentAsync(name: string, fn: () => Promise, options?: OTelSpanOptions): Promise; /** * Options for query instrumentation */ export interface QueryInstrumentationOptions { collection?: string; operation?: 'find' | 'findOne' | 'count' | 'aggregate'; filters?: string; } /** * Instrument a query operation. * * @param fn - Query function to execute * @param options - Query instrumentation options * @returns The result of the query */ export declare function instrumentQuery(fn: () => Promise, options?: QueryInstrumentationOptions): Promise; /** * Options for write instrumentation */ export interface WriteInstrumentationOptions { collection?: string; operation?: 'insert' | 'update' | 'delete' | 'upsert'; documentCount?: number; sizeBytes?: number; } /** * Instrument a write operation. * * @param fn - Write function to execute * @param options - Write instrumentation options * @returns The result of the write */ export declare function instrumentWrite(fn: () => Promise, options?: WriteInstrumentationOptions): Promise; /** * Options for transaction instrumentation */ export interface TransactionInstrumentationOptions { isolationLevel?: 'read_committed' | 'repeatable_read' | 'serializable'; transactionId?: string; } /** * Instrument a transaction. * * @param fn - Transaction function to execute * @param options - Transaction instrumentation options * @returns The result of the transaction */ export declare function instrumentTransaction(fn: () => Promise, options?: TransactionInstrumentationOptions): Promise; /** * Options for storage instrumentation */ export interface StorageInstrumentationOptions { operation: 'read' | 'write' | 'delete' | 'list'; storageType?: 'memory' | 'durable_object' | 'r2' | 'd1'; key?: string; sizeBytes?: number; } /** * Instrument a storage operation. * * @param fn - Storage function to execute * @param options - Storage instrumentation options * @returns The result of the storage operation */ export declare function instrumentStorage(fn: () => Promise, options: StorageInstrumentationOptions): Promise; /** * Options for CDC event instrumentation */ export interface CDCInstrumentationOptions { collection?: string; operation?: 'insert' | 'update' | 'delete'; eventCount?: number; } /** * Record a CDC event emission. * * @param options - CDC instrumentation options */ export declare function recordCDCEvent(options?: CDCInstrumentationOptions): void; /** * Instrument a CDC subscription or processing operation. * * @param fn - CDC function to execute * @param options - CDC instrumentation options * @returns The result of the CDC operation */ export declare function instrumentCDC(fn: () => Promise, options?: CDCInstrumentationOptions): Promise; /** * Reset the OpenTelemetry state (for testing purposes). * This clears the cached tracer, meter, and metrics. */ export declare function resetOTel(): void; //# sourceMappingURL=otel.d.ts.map