import { IMidwayContainer } from '@midwayjs/core'; import { type ClzInstance, type Context, type DecoratorExecutorParamBase, type GrpcContext, Application } from '@mwcp/share'; import { Context as TraceContext, Span, SpanOptions, TimeInput } from '@opentelemetry/api'; import type { MethodTypeUnknown } from '@waiting/shared-types'; import { OtelComponent } from '../component.js'; import { type Config, type MiddlewareConfig, type SpanStatusOptions, AttrNames, TraceScopeType } from '../types.js'; import { TraceServiceSpan } from './trace.service.span.js'; import type { DecoratorTraceDataResp, DecoratorTraceDataRespAsync } from './trace.service.types.js'; export declare class TraceService extends TraceServiceSpan { readonly app: Application; readonly applicationContext: IMidwayContainer; readonly config: Config; readonly mwConfig: MiddlewareConfig; readonly otel: OtelComponent; init(): Promise; startOnRequest(webCtx: Context): Promise; /** * Finish the root span and clean the context. */ finish(webCtx: Application | Context | GrpcContext, spanStatusOptions?: SpanStatusOptions, endTime?: TimeInput): void; protected startOnInit(webApplication: Application): Promise; protected initRootSpan(scope: Context): TraceContext; protected genRootSpanName(scope: Context): string; } export interface GenDecoratorExecutorOptions { config: Config; traceService: TraceService; } export type ExecutorParamBase = DecoratorExecutorParamBase; export type DecoratorExecutorParam = ExecutorParamBase & GenDecoratorExecutorOptions & { readonly rootTraceContext: TraceContext; callerAttr: { [AttrNames.CallerClass]: string; [AttrNames.CallerMethod]: string; }; spanName: string; spanOptions: Partial; startActiveSpan: boolean; traceContext: TraceContext | undefined; traceScope: TraceScopeType | undefined; span: Span | undefined; }; export type TraceOptions = Partial> | string; export interface TraceDecoratorOptions< /** Decorated method */ M extends MethodTypeUnknown | undefined = undefined, /** Arguments of decorated method */ MParamType = M extends MethodTypeUnknown ? P : unknown[], MResultType = M extends MethodTypeUnknown ? R : unknown, MThis = unknown extends ThisParameterType ? ClzInstance : ThisParameterType> extends SpanOptions { /** @default `{target.name}/{methodName}` */ spanName: string | KeyGenerator | undefined; /** * @default true */ startActiveSpan: boolean; traceContext: TraceContext | undefined; /** * Used as the prefix of the span name, * if spanName is not provided, * and the Caller ClassName is `AutoConfiguration` | `ContainerConfiguration`, * and the Caller MethodName is event name, such as `onReady` | `onServerReady`, */ namespace: string | undefined; /** * @default `/` */ spanNameDelimiter: string | undefined; before: MethodTypeUnknown<[ MParamType, DecoratorContext ], // input args // input args DecoratorTraceDataResp | DecoratorTraceDataRespAsync, // output data ThisParameterType> | undefined; after: MethodTypeUnknown<[ MParamType, Awaited, DecoratorContext ], // input args // input args DecoratorTraceDataResp | DecoratorTraceDataRespAsync, // output data ThisParameterType> | undefined; afterThrow: MethodTypeUnknown<[ MParamType, Error, DecoratorContext ], // input args // input args DecoratorTraceDataResp | DecoratorTraceDataRespAsync, // output data ThisParameterType> | undefined; /** * @default true */ autoEndSpan: boolean | undefined; } export type KeyGenerator = (this: TThis, /** Arguments of the method */ args: ArgsType, context: DContext) => string | undefined; export type ScopeGenerator = (this: TThis, /** Arguments of the method */ args: ArgsType, context: DContext) => object | symbol; export interface DecoratorContextBase { webApp: Application | undefined; webContext: Context | undefined; traceService: TraceService | undefined; traceScope: TraceScopeType | undefined; /** Caller Class name */ instanceName: string; methodName: string; } export interface DecoratorContext extends DecoratorContextBase { traceContext: TraceContext | undefined; traceSpan: Span | undefined; instance: T; }