UNPKG

@mwcp/otel

Version:
95 lines (94 loc) 4.62 kB
import { MidwayDecoratorService, MidwayEnvironmentService } from '@midwayjs/core'; import { ILogger } from '@midwayjs/logger'; import { Application, IMidwayContainer } from '@mwcp/share'; import { Attributes, Context as TraceContext, Span, SpanOptions, TimeInput } from '@opentelemetry/api'; import { node } from '@opentelemetry/sdk-node'; import { AddEventOptions, Config, InitTraceOptions, SpanStatusOptions, TraceScopeType } from './types.js'; /** OpenTelemetry Component */ export declare class OtelComponent { readonly app: Application; readonly applicationContext: IMidwayContainer; protected readonly config: Config; protected readonly otlpGrpcExporterConfig: InitTraceOptions['otlpGrpcExporterConfig']; protected readonly decoratorService: MidwayDecoratorService; protected readonly environmentService: MidwayEnvironmentService; protected readonly logger: ILogger; /** Active during Midway Lifecycle between onReady and onServerReady */ appInitProcessContext: TraceContext | undefined; /** Active during Midway Lifecycle between onReady and onServerReady */ appInitProcessSpan: Span | undefined; otelLibraryName: string; otelLibraryVersion: string; readonly captureHeadersMap: Map<string, Map<string, string>>; readonly traceContextMap: WeakMap<symbol | object, TraceContext[]>; protected traceProvider: node.NodeTracerProvider | undefined; protected spanProcessors: node.SpanProcessor[]; instId: symbol; init(): Promise<void>; getActiveContext(): TraceContext; getActiveSpan(traceContext?: TraceContext): Span | undefined; getTraceId(): string | undefined; /** * Starts a new {@link Span}. Start the span without setting it on context. * This method do NOT modify the current Context. */ startSpan(name: string, options?: SpanOptions, traceContext?: TraceContext): { span: Span; traceContext: TraceContext; }; /** * Starts a new {@link Span}. Start the span without setting it on context. */ startSpanContext(name: string, options?: SpanOptions, traceContext?: TraceContext): { span: Span; traceContext: TraceContext; }; /** * Starts a new {@link Span} and calls the given function passing it the created span as first argument. * Additionally the new span gets set in context and this context is activated * for the duration of the function call. */ startActiveSpan<F extends (...args: [Span, TraceContext]) => ReturnType<F>>(name: string, callback: F, options?: SpanOptions, traceContext?: TraceContext): ReturnType<F>; flush(): Promise<void>; shutdown(): Promise<void>; /** * Adds an event to the given span. */ addEvent(span: Span, input: Attributes, options?: AddEventOptions): void; addSpanEventWithError(span: Span, error?: Error): void; /** * Sets the attributes to the given span. */ setAttributes(span: Span, input: Attributes): void; setAttributesLater(span: Span, input: Attributes): void; /** * Sets the span with the error passed in params, note span not ended. */ setSpanWithError(rootSpan: Span | undefined, span: Span, error: Error | undefined, eventName?: string): void; /** * - ends the given span * - set span with error if error passed in params * - set span status * - call span.end(), except span is root span */ endSpan(rootSpan: Span | undefined, span: Span, spanStatusOptions?: SpanStatusOptions, endTime?: TimeInput): void; endRootSpan(rootSpan: Span, spanStatusOptions?: SpanStatusOptions, endTime?: TimeInput): void; addAppInitEvent(input: Attributes, options?: AddEventOptions, /** if omit, use this.appInitProcessSpan */ span?: Span): void; /** Called when onServerReady */ endAppInitEvent(): void; getScopeRootTraceContext(scope: TraceScopeType): TraceContext | undefined; getScopeActiveContext(scope: TraceScopeType): TraceContext | undefined; setScopeActiveContext(scope: TraceScopeType, ctx: TraceContext): void; emptyScopeActiveContext(scope: TraceScopeType): void; getRootSpan(scope: TraceScopeType): Span | undefined; spanIsRootSpan(scope: TraceScopeType, span: Span): boolean; /** * Inactive context will be removed from the array */ protected getActiveContextFromArray(input: TraceContext[]): TraceContext | undefined; protected prepareCaptureHeaders(type: 'request' | 'response', headersKey: string[]): void; protected _init(): Promise<void>; protected _app_init_start(): Promise<void>; }