import type { TraceSpan, TraceRecord, Emitter } from './types.js'; /** Input for adding a span to the current trace. */ export interface AddSpanInput { label: string; category: TraceSpan['category']; startOffset: number; duration: number; metadata?: Record; } /** * Wrap an async function in a traced span. * * If tracing is not enabled or no request is active, the function * is executed directly without overhead. * * @example * ```ts * import { trace } from 'adonisjs-server-stats' * * const result = await trace('fetchMembers', async () => { * return OrganizationService.getMembers(orgId) * }) * ``` */ export declare function trace(label: string, fn: () => Promise): Promise; /** * Collects per-request traces using AsyncLocalStorage. * * Automatically captures DB queries and console.warn calls within * the request context. Users can add custom spans via {@link trace}. */ export declare class TraceCollector { private static readonly MAX_SPANS_PER_TRACE; private buffer; private als; private emitter; private dbHandler; private originalConsoleWarn; constructor(maxTraces?: number); /** Start a new trace context for an HTTP request. */ startTrace(callback: () => Promise): Promise; /** Finish the current trace and save it to the ring buffer. Returns the record, or null if no context. */ finishTrace(method: string, url: string, statusCode: number, httpRequestId?: string): TraceRecord | null; /** Add a span to the current trace (if active). */ addSpan(input: AddSpanInput): void; /** Wrap a function in a traced span with automatic nesting. */ span(label: string, category: TraceSpan['category'], fn: () => Promise): Promise; /** Hook into db:query events and console.warn to auto-create spans. */ start(emitter: Emitter): void; /** Unhook event listeners and restore console.warn. */ stop(): void; getTraces(): TraceRecord[]; getLatest(n: number): TraceRecord[]; getTrace(id: number): TraceRecord | undefined; getTotalCount(): number; getBufferInfo(): { current: number; max: number; }; clear(): void; /** Register a callback that fires whenever a new trace is recorded. */ onNewItem(cb: ((item: TraceRecord) => void) | null): void; /** Restore persisted records into the buffer and reset the ID counter. */ loadRecords(records: TraceRecord[]): void; }