UNPKG

autotel

Version:
92 lines (88 loc) 2.53 kB
import { TracingOptions } from './functional.cjs'; export { T as TraceContext } from './trace-context-t5X1AP-e.cjs'; import '@opentelemetry/api'; import './sampling.cjs'; import './logger.cjs'; import 'pino'; /** * TypeScript 5+ Decorators for autotel * * Provides @Trace decorator for class-based code. * * **Requires TypeScript 5.0+** * * @example Method decorator * ```typescript * import { Trace } from 'autotel/decorators' * * class OrderService { * @Trace('order.create', { withMetrics: true }) * async createOrder(data: OrderData) { * return await db.orders.create(data) * } * * @Trace() // Uses method name as span name * async processPayment(orderId: string) { * return await stripe.charge(orderId) * } * } * ``` */ /** * Options for @Trace method decorator */ interface TraceDecoratorOptions extends Omit<TracingOptions, 'name'> { /** * Custom span name. If not provided, uses the method name. */ name?: string; } /** * @Trace - Method decorator for fine-grained tracing * * Wraps a class method with automatic tracing. Supports both patterns: * - Simple: method doesn't use ctx * - Advanced: method accesses ctx via this.ctx * * @example Simple usage (no ctx) * ```typescript * class OrderService { * @Trace() * async createOrder(data: OrderData) { * return await db.orders.create(data) * } * } * ``` * * @example With custom name and options * ```typescript * class PaymentService { * @Trace('payment.charge', { withMetrics: true }) * async chargeCard(amount: number) { * return await stripe.charges.create({ amount }) * } * } * ``` * * @example Accessing ctx * ```typescript * interface WithTraceContext { * ctx?: TraceContext * } * * class UserService { * @Trace() * async createUser(data: UserData) { * // Access ctx via this.ctx (available during execution) * const ctx = (this as unknown as WithTraceContext).ctx * if (ctx) { * ctx.setAttribute('user.id', data.id) * } * return await db.users.create(data) * } * } * ``` */ declare function Trace(options?: TraceDecoratorOptions): <T extends (...args: unknown[]) => Promise<unknown>>(originalMethod: T, context: ClassMethodDecoratorContext) => T; declare function Trace(name?: string, options?: TraceDecoratorOptions): <T extends (...args: unknown[]) => Promise<unknown>>(originalMethod: T, context: ClassMethodDecoratorContext) => T; export { Trace, type TraceDecoratorOptions, TracingOptions };