UNPKG

@lokalise/fastify-extras

Version:

Opinionated set of fastify plugins, commonly used in Lokalise

84 lines (83 loc) 4.12 kB
import type { TransactionObservabilityManager } from '@lokalise/node-core'; import type { Span, Tracer } from '@opentelemetry/api'; import type { FastifyPluginCallback } from 'fastify'; declare module 'fastify' { interface FastifyInstance { openTelemetryTransactionManager: OpenTelemetryTransactionManager; } } export interface OpenTelemetryTransactionManagerOptions { isEnabled: boolean; /** * The name used to identify the tracer (instrumentation scope name). * This is NOT the OpenTelemetry resource `service.name` attribute. * To set the service name for your traces, configure it via the OpenTelemetry SDK * resource configuration (e.g., OTEL_SERVICE_NAME environment variable or SDK Resource). * @default 'unknown-tracer' */ tracerName?: string; /** * The version used to identify the tracer (instrumentation scope version). * This is NOT the OpenTelemetry resource `service.version` attribute. * To set the service version for your traces, configure it via the OpenTelemetry SDK * resource configuration. * @default '1.0.0' */ tracerVersion?: string; /** * Maximum number of concurrent spans to track. When this limit is reached, * the oldest spans will be evicted and automatically ended to prevent leaks. * @default 2000 */ maxConcurrentSpans?: number; } export declare class OpenTelemetryTransactionManager implements TransactionObservabilityManager { private readonly isEnabled; private readonly tracer; private readonly spanMap; /** * Creates a new OpenTelemetryTransactionManager. * * @param isEnabled - Whether tracing is enabled * @param tracerName - The instrumentation scope name for the tracer. This identifies * the instrumentation library, not the service. Service identification should be * configured via OpenTelemetry SDK resource attributes (e.g., OTEL_SERVICE_NAME). * @param tracerVersion - The instrumentation scope version for the tracer. * @param maxConcurrentSpans - Maximum number of concurrent spans to track before eviction. */ constructor(isEnabled: boolean, tracerName?: string, tracerVersion?: string, maxConcurrentSpans?: number); static createDisabled(): OpenTelemetryTransactionManager; /** * @param transactionName - used for grouping similar transactions together * @param uniqueTransactionKey - used for identifying specific ongoing transaction. Must be reasonably unique to reduce possibility of collisions */ start(transactionName: string, uniqueTransactionKey: string): void; /** * @param transactionName - used for grouping similar transactions together * @param uniqueTransactionKey - used for identifying specific ongoing transaction. Must be reasonably unique to reduce possibility of collisions * @param transactionGroup - group is used for grouping related transactions with different names */ startWithGroup(transactionName: string, uniqueTransactionKey: string, transactionGroup: string): void; stop(uniqueTransactionKey: string, wasSuccessful?: boolean): void; addCustomAttribute(attrName: string, attrValue: string | number | boolean): void; addCustomAttributes(uniqueTransactionKey: string, atts: { [p: string]: string | number | boolean; }): void; setUserID(userId: string): void; setControllerName(name: string, action: string): void; /** * Get a span by its unique transaction key. Useful for advanced use cases * where direct span manipulation is needed. */ getSpan(uniqueTransactionKey: string): Span | null; /** * Get the underlying tracer for advanced use cases. */ getTracer(): Tracer; /** * Run a function within the context of a specific span. * Useful when you need child spans to be automatically linked to a parent. */ runInSpanContext<T>(uniqueTransactionKey: string, fn: () => T): T; } export declare const openTelemetryTransactionManagerPlugin: FastifyPluginCallback<OpenTelemetryTransactionManagerOptions>;