UNPKG

@erickluis00/otelviewer

Version:

Shared OpenTelemetry tracing utilities, types, and batch processor for Realtime OpenTelemetry Viewer [WIP]

153 lines 6.46 kB
import { type Span, type Attributes } from '@opentelemetry/api'; interface RemoteExecutableMethod { className: string; methodName: string; isStatic: boolean; originalFunction: Function; constructor?: any; spanPrefix?: string; icon?: string; iconColor?: string; } /** * Get all registered remote executable methods */ export declare function getRemoteExecutables(): Map<string, RemoteExecutableMethod>; /** * Execute a registered method by its key with full tracing */ export declare function executeRemoteMethod(key: string, args: any[]): Promise<{ success: boolean; result?: any; error?: string; traceId?: string; }>; type FunctionParameters<T> = T extends (...args: infer P) => any ? P : never; /** * Configure breadcrumb callback for span events */ export declare function configureAddEventCallback(callback: ((eventName: string, attributes?: Attributes) => void) | null): void; /** * Executes an async function with OpenTelemetry tracing immediately. * * This function automatically: * - Creates a new span for the function execution. * - Sets the function arguments as an 'input' attribute on the span. * - Sets the function's return value as an 'output' attribute. * - Records any exceptions and sets the span status to ERROR. * - Ends the span. * * The wrapped function can access the active span using `getCurrentSpan()`, allowing * for custom attributes or events to be added. If 'input' or 'output' attributes * are set within the wrapped function, the automatic attributes will not be applied. * * @param functionName The name for the span. * @param fn The async function to be traced. * @param args The arguments to pass to the function. * @returns A promise with the result of the function execution. */ export declare function track<T extends (...args: any[]) => Promise<any>>(functionName: string, fn: T, ...args: FunctionParameters<T>): Promise<ReturnType<T>>; /** * Wraps an async function with OpenTelemetry tracing and returns a callable function. * * This function creates a wrapper that can be called multiple times. * Use this when you need to create reusable traced functions. * * @param functionName The name for the span. * @param fn The async function to be traced. * @returns A new async function with tracing enabled. */ export declare function trackWrapper<T extends (...args: any[]) => Promise<any>>(functionName: string, fn: T): (...args: FunctionParameters<T>) => Promise<ReturnType<T>>; /** * Wraps all functions in an object with OpenTelemetry tracing. * * This function iterates over an object of functions and applies the `trackWrapper` * decorator to each one, using the function's key as the span name. * * @param functions An object where keys are function names and values are async functions to be traced. * @returns A new object with the same keys, where each function is wrapped with tracing. */ export declare function trackAll<T extends { [key: string]: (...args: any[]) => Promise<any>; }>(functions: T): { [K in keyof T]: (...args: FunctionParameters<T[K]>) => Promise<ReturnType<T[K]>>; }; /** * Simple function to add custom attributes to the current active span */ export declare function addSpanAttributes(attributes: Record<string, string | number | boolean>): void; /** * Simple function to add an event to the current active span */ export declare function addSpanEvent(name: string, attributes?: Record<string, string | number | boolean>): void; /** * Gets the current active span (useful inside @TrackClass methods) * Returns undefined if no span is currently active */ export declare function getCurrentSpan(): Span | undefined; /** * Create a manual span for custom tracing scenarios */ export declare function createSpan<T>(name: string, fn: (span: Span) => Promise<T> | T, attributes?: Record<string, string | number | boolean>): Promise<T>; /** * Configuration options for class tracking */ interface TrackClassOptions { /** Optional prefix for span names (defaults to class name) */ spanPrefix?: string; /** Methods to exclude from tracking */ excludeMethods?: string[]; /** Methods to include (if specified, only these will be tracked) */ includeMethods?: string[]; /** Whether to track constructor calls (defaults to false) */ trackConstructor?: boolean; /** Icon to display for this class (icons from https://icon-sets.iconify.design/, e.g., 'logos:whatsapp-icon') */ icon?: string; /** Icon color to display for this class (hex color string or any css color string) */ iconColor?: string; /** Allow remote execution of methods from OtelViewer UI (DEV ONLY) */ allowRemoteRun?: boolean; } /** * Class decorator that automatically instruments ALL methods of a class (both static and instance). * Also tracks internal calls (this.method() and ClassName.staticMethod()). * * Features: * - Automatic span creation for each method call * - Input/output parameter extraction and logging * - Source code extraction in development mode (like track functions) * - Support for both sync and async methods * - Breadcrumb integration support * - Access to current span via getCurrentSpan() within methods * * Usage: * ```typescript * @TrackClass() * class MyService { * async getData() { * const span = getCurrentSpan() // ✅ Access current span * span?.addEvent('Custom event') * return "data" * } * async processData(data: string) { * return MyService.staticHelper(data.toUpperCase()) // ✅ Also tracked! * } * static async staticHelper(input: string) { return input } // ✅ Tracked! * } * ``` * * @param options Configuration options for tracking */ export declare function TrackClass(options?: TrackClassOptions): <T extends { new (...args: any[]): any; }>(constructor: T) => T; /** * Creates a tracked version of an instance method */ export declare function createTrackedInstanceMethod(originalMethod: Function, methodName: string, spanPrefix: string, icon?: string, iconColor?: string): (this: any, ...args: any[]) => any; /** * Creates a tracked version of a static method */ export declare function createTrackedStaticMethod(originalMethod: Function, methodName: string, spanPrefix: string, icon: string | undefined, iconColor: string | undefined, constructor: any, allowRemoteRun?: boolean): (...args: any[]) => any; export {}; //# sourceMappingURL=tracing-utils.d.ts.map