autotel 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1946 -0
- package/dist/chunk-2LNRY4QK.js +273 -0
- package/dist/chunk-2LNRY4QK.js.map +1 -0
- package/dist/chunk-3HENGDW2.js +587 -0
- package/dist/chunk-3HENGDW2.js.map +1 -0
- package/dist/chunk-4OAT42CA.cjs +73 -0
- package/dist/chunk-4OAT42CA.cjs.map +1 -0
- package/dist/chunk-5GWX5LFW.js +70 -0
- package/dist/chunk-5GWX5LFW.js.map +1 -0
- package/dist/chunk-5R2M36QB.js +195 -0
- package/dist/chunk-5R2M36QB.js.map +1 -0
- package/dist/chunk-5ZN622AO.js +73 -0
- package/dist/chunk-5ZN622AO.js.map +1 -0
- package/dist/chunk-77MSMAUQ.cjs +498 -0
- package/dist/chunk-77MSMAUQ.cjs.map +1 -0
- package/dist/chunk-ABPEQ6RK.cjs +596 -0
- package/dist/chunk-ABPEQ6RK.cjs.map +1 -0
- package/dist/chunk-BWYGJKRB.js +95 -0
- package/dist/chunk-BWYGJKRB.js.map +1 -0
- package/dist/chunk-BZHG5IZ4.js +73 -0
- package/dist/chunk-BZHG5IZ4.js.map +1 -0
- package/dist/chunk-G7VZBCD6.cjs +35 -0
- package/dist/chunk-G7VZBCD6.cjs.map +1 -0
- package/dist/chunk-GVLK7YUU.cjs +30 -0
- package/dist/chunk-GVLK7YUU.cjs.map +1 -0
- package/dist/chunk-HCCXC7XG.js +205 -0
- package/dist/chunk-HCCXC7XG.js.map +1 -0
- package/dist/chunk-HE6T6FIX.cjs +203 -0
- package/dist/chunk-HE6T6FIX.cjs.map +1 -0
- package/dist/chunk-KIXWPOCO.cjs +100 -0
- package/dist/chunk-KIXWPOCO.cjs.map +1 -0
- package/dist/chunk-KVGNW3FC.js +87 -0
- package/dist/chunk-KVGNW3FC.js.map +1 -0
- package/dist/chunk-LITNXTTT.js +3 -0
- package/dist/chunk-LITNXTTT.js.map +1 -0
- package/dist/chunk-M4ANN7RL.js +114 -0
- package/dist/chunk-M4ANN7RL.js.map +1 -0
- package/dist/chunk-NC52UBR2.cjs +32 -0
- package/dist/chunk-NC52UBR2.cjs.map +1 -0
- package/dist/chunk-NHCNRQD3.cjs +212 -0
- package/dist/chunk-NHCNRQD3.cjs.map +1 -0
- package/dist/chunk-NZ72VDNY.cjs +4 -0
- package/dist/chunk-NZ72VDNY.cjs.map +1 -0
- package/dist/chunk-P6JUDYNO.js +57 -0
- package/dist/chunk-P6JUDYNO.js.map +1 -0
- package/dist/chunk-RJYY7BWX.js +1349 -0
- package/dist/chunk-RJYY7BWX.js.map +1 -0
- package/dist/chunk-TRI4V5BF.cjs +126 -0
- package/dist/chunk-TRI4V5BF.cjs.map +1 -0
- package/dist/chunk-UL33I6IS.js +139 -0
- package/dist/chunk-UL33I6IS.js.map +1 -0
- package/dist/chunk-URRW6M2C.cjs +61 -0
- package/dist/chunk-URRW6M2C.cjs.map +1 -0
- package/dist/chunk-UY3UYPBZ.cjs +77 -0
- package/dist/chunk-UY3UYPBZ.cjs.map +1 -0
- package/dist/chunk-W3253FGB.cjs +277 -0
- package/dist/chunk-W3253FGB.cjs.map +1 -0
- package/dist/chunk-W7LHZVQF.js +26 -0
- package/dist/chunk-W7LHZVQF.js.map +1 -0
- package/dist/chunk-WBWNM6LB.cjs +1360 -0
- package/dist/chunk-WBWNM6LB.cjs.map +1 -0
- package/dist/chunk-WFJ7L2RV.js +494 -0
- package/dist/chunk-WFJ7L2RV.js.map +1 -0
- package/dist/chunk-X4RMFFMR.js +28 -0
- package/dist/chunk-X4RMFFMR.js.map +1 -0
- package/dist/chunk-Y4Y2S7BM.cjs +92 -0
- package/dist/chunk-Y4Y2S7BM.cjs.map +1 -0
- package/dist/chunk-YLPNXZFI.cjs +143 -0
- package/dist/chunk-YLPNXZFI.cjs.map +1 -0
- package/dist/chunk-YTXEZ4SD.cjs +77 -0
- package/dist/chunk-YTXEZ4SD.cjs.map +1 -0
- package/dist/chunk-Z6ZWNWWR.js +30 -0
- package/dist/chunk-Z6ZWNWWR.js.map +1 -0
- package/dist/config.cjs +26 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +75 -0
- package/dist/config.d.ts +75 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/db.cjs +233 -0
- package/dist/db.cjs.map +1 -0
- package/dist/db.d.cts +123 -0
- package/dist/db.d.ts +123 -0
- package/dist/db.js +228 -0
- package/dist/db.js.map +1 -0
- package/dist/decorators.cjs +67 -0
- package/dist/decorators.cjs.map +1 -0
- package/dist/decorators.d.cts +91 -0
- package/dist/decorators.d.ts +91 -0
- package/dist/decorators.js +65 -0
- package/dist/decorators.js.map +1 -0
- package/dist/event-subscriber.cjs +6 -0
- package/dist/event-subscriber.cjs.map +1 -0
- package/dist/event-subscriber.d.cts +116 -0
- package/dist/event-subscriber.d.ts +116 -0
- package/dist/event-subscriber.js +3 -0
- package/dist/event-subscriber.js.map +1 -0
- package/dist/event-testing.cjs +21 -0
- package/dist/event-testing.cjs.map +1 -0
- package/dist/event-testing.d.cts +110 -0
- package/dist/event-testing.d.ts +110 -0
- package/dist/event-testing.js +4 -0
- package/dist/event-testing.js.map +1 -0
- package/dist/event.cjs +30 -0
- package/dist/event.cjs.map +1 -0
- package/dist/event.d.cts +282 -0
- package/dist/event.d.ts +282 -0
- package/dist/event.js +13 -0
- package/dist/event.js.map +1 -0
- package/dist/exporters.cjs +17 -0
- package/dist/exporters.cjs.map +1 -0
- package/dist/exporters.d.cts +1 -0
- package/dist/exporters.d.ts +1 -0
- package/dist/exporters.js +4 -0
- package/dist/exporters.js.map +1 -0
- package/dist/functional.cjs +46 -0
- package/dist/functional.cjs.map +1 -0
- package/dist/functional.d.cts +478 -0
- package/dist/functional.d.ts +478 -0
- package/dist/functional.js +13 -0
- package/dist/functional.js.map +1 -0
- package/dist/http.cjs +189 -0
- package/dist/http.cjs.map +1 -0
- package/dist/http.d.cts +169 -0
- package/dist/http.d.ts +169 -0
- package/dist/http.js +184 -0
- package/dist/http.js.map +1 -0
- package/dist/index.cjs +333 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +758 -0
- package/dist/index.d.ts +758 -0
- package/dist/index.js +143 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation.cjs +182 -0
- package/dist/instrumentation.cjs.map +1 -0
- package/dist/instrumentation.d.cts +49 -0
- package/dist/instrumentation.d.ts +49 -0
- package/dist/instrumentation.js +179 -0
- package/dist/instrumentation.js.map +1 -0
- package/dist/logger.cjs +19 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +146 -0
- package/dist/logger.d.ts +146 -0
- package/dist/logger.js +6 -0
- package/dist/logger.js.map +1 -0
- package/dist/metric-helpers.cjs +31 -0
- package/dist/metric-helpers.cjs.map +1 -0
- package/dist/metric-helpers.d.cts +13 -0
- package/dist/metric-helpers.d.ts +13 -0
- package/dist/metric-helpers.js +6 -0
- package/dist/metric-helpers.js.map +1 -0
- package/dist/metric-testing.cjs +21 -0
- package/dist/metric-testing.cjs.map +1 -0
- package/dist/metric-testing.d.cts +110 -0
- package/dist/metric-testing.d.ts +110 -0
- package/dist/metric-testing.js +4 -0
- package/dist/metric-testing.js.map +1 -0
- package/dist/metric.cjs +26 -0
- package/dist/metric.cjs.map +1 -0
- package/dist/metric.d.cts +240 -0
- package/dist/metric.d.ts +240 -0
- package/dist/metric.js +9 -0
- package/dist/metric.js.map +1 -0
- package/dist/processors.cjs +17 -0
- package/dist/processors.cjs.map +1 -0
- package/dist/processors.d.cts +1 -0
- package/dist/processors.d.ts +1 -0
- package/dist/processors.js +4 -0
- package/dist/processors.js.map +1 -0
- package/dist/sampling.cjs +40 -0
- package/dist/sampling.cjs.map +1 -0
- package/dist/sampling.d.cts +260 -0
- package/dist/sampling.d.ts +260 -0
- package/dist/sampling.js +7 -0
- package/dist/sampling.js.map +1 -0
- package/dist/semantic-helpers.cjs +35 -0
- package/dist/semantic-helpers.cjs.map +1 -0
- package/dist/semantic-helpers.d.cts +442 -0
- package/dist/semantic-helpers.d.ts +442 -0
- package/dist/semantic-helpers.js +14 -0
- package/dist/semantic-helpers.js.map +1 -0
- package/dist/tail-sampling-processor.cjs +13 -0
- package/dist/tail-sampling-processor.cjs.map +1 -0
- package/dist/tail-sampling-processor.d.cts +27 -0
- package/dist/tail-sampling-processor.d.ts +27 -0
- package/dist/tail-sampling-processor.js +4 -0
- package/dist/tail-sampling-processor.js.map +1 -0
- package/dist/testing.cjs +286 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +291 -0
- package/dist/testing.d.ts +291 -0
- package/dist/testing.js +263 -0
- package/dist/testing.js.map +1 -0
- package/dist/trace-context-DRZdUvVY.d.cts +181 -0
- package/dist/trace-context-DRZdUvVY.d.ts +181 -0
- package/dist/trace-helpers.cjs +54 -0
- package/dist/trace-helpers.cjs.map +1 -0
- package/dist/trace-helpers.d.cts +524 -0
- package/dist/trace-helpers.d.ts +524 -0
- package/dist/trace-helpers.js +5 -0
- package/dist/trace-helpers.js.map +1 -0
- package/dist/tracer-provider.cjs +21 -0
- package/dist/tracer-provider.cjs.map +1 -0
- package/dist/tracer-provider.d.cts +169 -0
- package/dist/tracer-provider.d.ts +169 -0
- package/dist/tracer-provider.js +4 -0
- package/dist/tracer-provider.js.map +1 -0
- package/package.json +280 -0
- package/src/baggage-span-processor.test.ts +202 -0
- package/src/baggage-span-processor.ts +98 -0
- package/src/circuit-breaker.test.ts +341 -0
- package/src/circuit-breaker.ts +184 -0
- package/src/config.test.ts +94 -0
- package/src/config.ts +169 -0
- package/src/db.test.ts +252 -0
- package/src/db.ts +447 -0
- package/src/decorators.test.ts +203 -0
- package/src/decorators.ts +188 -0
- package/src/env-config.test.ts +246 -0
- package/src/env-config.ts +158 -0
- package/src/event-queue.test.ts +222 -0
- package/src/event-queue.ts +203 -0
- package/src/event-subscriber.ts +136 -0
- package/src/event-testing.ts +197 -0
- package/src/event.test.ts +718 -0
- package/src/event.ts +556 -0
- package/src/exporters.ts +96 -0
- package/src/functional.test.ts +1059 -0
- package/src/functional.ts +2295 -0
- package/src/http.test.ts +487 -0
- package/src/http.ts +424 -0
- package/src/index.ts +158 -0
- package/src/init.customization.test.ts +210 -0
- package/src/init.integrations.test.ts +366 -0
- package/src/init.openllmetry.test.ts +282 -0
- package/src/init.protocol.test.ts +215 -0
- package/src/init.ts +1426 -0
- package/src/instrumentation.test.ts +108 -0
- package/src/instrumentation.ts +308 -0
- package/src/logger.test.ts +117 -0
- package/src/logger.ts +246 -0
- package/src/metric-helpers.ts +47 -0
- package/src/metric-testing.ts +197 -0
- package/src/metric.ts +434 -0
- package/src/metrics.test.ts +205 -0
- package/src/operation-context.ts +93 -0
- package/src/processors.ts +106 -0
- package/src/rate-limiter.test.ts +199 -0
- package/src/rate-limiter.ts +98 -0
- package/src/sampling.test.ts +513 -0
- package/src/sampling.ts +428 -0
- package/src/semantic-helpers.test.ts +311 -0
- package/src/semantic-helpers.ts +584 -0
- package/src/shutdown.test.ts +311 -0
- package/src/shutdown.ts +222 -0
- package/src/stub.integration.test.ts +361 -0
- package/src/tail-sampling-processor.test.ts +226 -0
- package/src/tail-sampling-processor.ts +51 -0
- package/src/testing.ts +670 -0
- package/src/trace-context.ts +470 -0
- package/src/trace-helpers.new.test.ts +278 -0
- package/src/trace-helpers.test.ts +242 -0
- package/src/trace-helpers.ts +690 -0
- package/src/tracer-provider.test.ts +183 -0
- package/src/tracer-provider.ts +266 -0
- package/src/track.test.ts +153 -0
- package/src/track.ts +120 -0
- package/src/validation.test.ts +306 -0
- package/src/validation.ts +239 -0
- package/src/variable-name-inference.test.ts +178 -0
- package/src/variable-name-inference.ts +242 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
import { NodeSDKConfiguration, NodeSDK } from '@opentelemetry/sdk-node';
|
|
2
|
+
import { SpanProcessor, SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
3
|
+
import { Resource } from '@opentelemetry/resources';
|
|
4
|
+
import { Sampler } from './sampling.cjs';
|
|
5
|
+
export { AdaptiveSampler, AlwaysSampler, NeverSampler, RandomSampler, SamplingContext, UserIdSampler } from './sampling.cjs';
|
|
6
|
+
import { EventSubscriber } from './event-subscriber.cjs';
|
|
7
|
+
export { EventAttributes, FunnelStatus, OutcomeStatus } from './event-subscriber.cjs';
|
|
8
|
+
import { Logger } from './logger.cjs';
|
|
9
|
+
import { Attributes, Span, Context } from '@opentelemetry/api';
|
|
10
|
+
export { Context, Span, SpanContext, SpanKind, SpanStatusCode, Tracer, context, trace as otelTrace, propagation } from '@opentelemetry/api';
|
|
11
|
+
import { MetricReader } from '@opentelemetry/sdk-metrics';
|
|
12
|
+
import { LogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
13
|
+
export { InstrumentOptions, SpanOptions, WithBaggageOptions, WithNewContextOptions, ctx, instrument, span, trace, withBaggage, withNewContext, withTracing } from './functional.cjs';
|
|
14
|
+
export { Event, EventsOptions, getEvents, resetEvents } from './event.cjs';
|
|
15
|
+
export { Metric, MetricsOptions, getMetrics, resetMetrics } from './metric.cjs';
|
|
16
|
+
export { createCounter, createHistogram, createObservableGauge, createUpDownCounter, getMeter } from './metric-helpers.cjs';
|
|
17
|
+
export { createDeterministicTraceId, finalizeSpan, flattenMetadata, getActiveContext, getActiveSpan, getTracer, runWithSpan } from './trace-helpers.cjs';
|
|
18
|
+
export { getAutotelTracer, getAutotelTracerProvider, setAutotelTracerProvider } from './tracer-provider.cjs';
|
|
19
|
+
export { DBConfig, HTTPConfig, LLMConfig, MessagingConfig, traceDB, traceHTTP, traceLLM, traceMessaging } from './semantic-helpers.cjs';
|
|
20
|
+
export { T as TraceContext, d as defineBaggageSchema } from './trace-context-DRZdUvVY.cjs';
|
|
21
|
+
import 'pino';
|
|
22
|
+
import './event-testing.cjs';
|
|
23
|
+
import './metric-testing.cjs';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Input validation for events events and attributes
|
|
27
|
+
*
|
|
28
|
+
* Prevents:
|
|
29
|
+
* - Invalid event names
|
|
30
|
+
* - Oversized payloads
|
|
31
|
+
* - Circular references
|
|
32
|
+
* - Sensitive data leaks
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
interface ValidationConfig {
|
|
36
|
+
/** Max event name length (default: 100) */
|
|
37
|
+
maxEventNameLength: number;
|
|
38
|
+
/** Max attribute key length (default: 100) */
|
|
39
|
+
maxAttributeKeyLength: number;
|
|
40
|
+
/** Max attribute value length for strings (default: 1000) */
|
|
41
|
+
maxAttributeValueLength: number;
|
|
42
|
+
/** Max total attributes per event (default: 50) */
|
|
43
|
+
maxAttributeCount: number;
|
|
44
|
+
/** Max nesting depth for objects (default: 3) */
|
|
45
|
+
maxNestingDepth: number;
|
|
46
|
+
/** Sensitive field patterns to redact */
|
|
47
|
+
sensitivePatterns: RegExp[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Simplified initialization for autotel
|
|
52
|
+
*
|
|
53
|
+
* Single init() function with sensible defaults.
|
|
54
|
+
* Replaces initInstrumentation() and separate events config.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
interface AutotelConfig {
|
|
58
|
+
/** Service name (required) */
|
|
59
|
+
service: string;
|
|
60
|
+
/** Event subscribers - bring your own (PostHog, Mixpanel, etc.) */
|
|
61
|
+
subscribers?: EventSubscriber[];
|
|
62
|
+
/**
|
|
63
|
+
* Additional OpenTelemetry instrumentations to register.
|
|
64
|
+
* Useful when you want HTTP/Prisma/etc auto instrumentation alongside
|
|
65
|
+
* the functional helpers.
|
|
66
|
+
*
|
|
67
|
+
* **Important:** If you need custom instrumentation configs (like `requireParentSpan: false`),
|
|
68
|
+
* use EITHER manual instrumentations OR integrations, not both for the same library.
|
|
69
|
+
* Manual instrumentations always take precedence over auto-instrumentations.
|
|
70
|
+
*
|
|
71
|
+
* @example Manual instrumentations with custom config
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb'
|
|
74
|
+
*
|
|
75
|
+
* init({
|
|
76
|
+
* service: 'my-app',
|
|
77
|
+
* integrations: false, // Disable auto-instrumentations
|
|
78
|
+
* instrumentations: [
|
|
79
|
+
* new MongoDBInstrumentation({
|
|
80
|
+
* requireParentSpan: false // Custom config
|
|
81
|
+
* })
|
|
82
|
+
* ]
|
|
83
|
+
* })
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @example Mix auto + manual (auto for most, manual for specific configs)
|
|
87
|
+
* ```typescript
|
|
88
|
+
* import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb'
|
|
89
|
+
*
|
|
90
|
+
* init({
|
|
91
|
+
* service: 'my-app',
|
|
92
|
+
* integrations: ['http', 'express'], // Auto for these
|
|
93
|
+
* instrumentations: [
|
|
94
|
+
* new MongoDBInstrumentation({
|
|
95
|
+
* requireParentSpan: false // Manual config for MongoDB
|
|
96
|
+
* })
|
|
97
|
+
* ]
|
|
98
|
+
* })
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
instrumentations?: NodeSDKConfiguration['instrumentations'];
|
|
102
|
+
/**
|
|
103
|
+
* Simple integration names for auto-instrumentation.
|
|
104
|
+
* Uses @opentelemetry/auto-instrumentations-node (peer dependency).
|
|
105
|
+
*
|
|
106
|
+
* **Important:** If you provide manual instrumentations for the same library,
|
|
107
|
+
* the manual config takes precedence and auto-instrumentation for that library is disabled.
|
|
108
|
+
*
|
|
109
|
+
* @example Enable all integrations (simple approach)
|
|
110
|
+
* ```typescript
|
|
111
|
+
* init({
|
|
112
|
+
* service: 'my-app',
|
|
113
|
+
* integrations: true // Enable all with defaults
|
|
114
|
+
* })
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example Enable specific integrations
|
|
118
|
+
* ```typescript
|
|
119
|
+
* init({
|
|
120
|
+
* service: 'my-app',
|
|
121
|
+
* integrations: ['express', 'pino', 'http']
|
|
122
|
+
* })
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @example Configure specific integrations
|
|
126
|
+
* ```typescript
|
|
127
|
+
* init({
|
|
128
|
+
* service: 'my-app',
|
|
129
|
+
* integrations: {
|
|
130
|
+
* express: { enabled: true },
|
|
131
|
+
* pino: { enabled: true },
|
|
132
|
+
* http: { enabled: false }
|
|
133
|
+
* }
|
|
134
|
+
* })
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example Manual config when you need custom settings
|
|
138
|
+
* ```typescript
|
|
139
|
+
* import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb'
|
|
140
|
+
*
|
|
141
|
+
* init({
|
|
142
|
+
* service: 'my-app',
|
|
143
|
+
* integrations: false, // Use manual control
|
|
144
|
+
* instrumentations: [
|
|
145
|
+
* new MongoDBInstrumentation({
|
|
146
|
+
* requireParentSpan: false // Custom config not available with auto
|
|
147
|
+
* })
|
|
148
|
+
* ]
|
|
149
|
+
* })
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
integrations?: string[] | boolean | Record<string, {
|
|
153
|
+
enabled?: boolean;
|
|
154
|
+
}>;
|
|
155
|
+
/**
|
|
156
|
+
* OTLP endpoint for traces/metrics/logs
|
|
157
|
+
* Only used if you don't provide custom exporters/processors
|
|
158
|
+
* @default process.env.OTLP_ENDPOINT || 'http://localhost:4318'
|
|
159
|
+
*/
|
|
160
|
+
endpoint?: string;
|
|
161
|
+
/**
|
|
162
|
+
* Custom span processors for traces (supports multiple processors)
|
|
163
|
+
* Allows you to use any backend: Jaeger, Zipkin, Datadog, New Relic, etc.
|
|
164
|
+
* If not provided, defaults to OTLP with tail sampling
|
|
165
|
+
*
|
|
166
|
+
* @example Multiple processors
|
|
167
|
+
* ```typescript
|
|
168
|
+
* import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
|
|
169
|
+
* import { BatchSpanProcessor, SimpleSpanProcessor, ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base'
|
|
170
|
+
*
|
|
171
|
+
* init({
|
|
172
|
+
* service: 'my-app',
|
|
173
|
+
* spanProcessors: [
|
|
174
|
+
* new BatchSpanProcessor(new JaegerExporter()),
|
|
175
|
+
* new SimpleSpanProcessor(new ConsoleSpanExporter()) // Debug alongside production
|
|
176
|
+
* ]
|
|
177
|
+
* })
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @example Single processor
|
|
181
|
+
* ```typescript
|
|
182
|
+
* import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'
|
|
183
|
+
*
|
|
184
|
+
* init({
|
|
185
|
+
* service: 'my-app',
|
|
186
|
+
* spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())]
|
|
187
|
+
* })
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
spanProcessors?: SpanProcessor[];
|
|
191
|
+
/**
|
|
192
|
+
* Custom span exporters for traces (alternative to spanProcessors, supports multiple exporters)
|
|
193
|
+
* Provide either spanProcessors OR spanExporters, not both
|
|
194
|
+
* Each exporter will be wrapped in TailSamplingSpanProcessor + BatchSpanProcessor
|
|
195
|
+
*
|
|
196
|
+
* @example Multiple exporters
|
|
197
|
+
* ```typescript
|
|
198
|
+
* import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'
|
|
199
|
+
* import { JaegerExporter } from '@opentelemetry/exporter-jaeger'
|
|
200
|
+
*
|
|
201
|
+
* init({
|
|
202
|
+
* service: 'my-app',
|
|
203
|
+
* spanExporters: [
|
|
204
|
+
* new ZipkinExporter({ url: 'http://localhost:9411/api/v2/spans' }),
|
|
205
|
+
* new JaegerExporter() // Send to multiple backends simultaneously
|
|
206
|
+
* ]
|
|
207
|
+
* })
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* @example Single exporter
|
|
211
|
+
* ```typescript
|
|
212
|
+
* import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'
|
|
213
|
+
*
|
|
214
|
+
* init({
|
|
215
|
+
* service: 'my-app',
|
|
216
|
+
* spanExporters: [new ZipkinExporter({ url: 'http://localhost:9411/api/v2/spans' })]
|
|
217
|
+
* })
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
spanExporters?: SpanExporter[];
|
|
221
|
+
/**
|
|
222
|
+
* Custom metric readers (supports multiple readers)
|
|
223
|
+
* Allows sending metrics to multiple backends: OTLP, Prometheus, custom readers
|
|
224
|
+
* Defaults to OTLP metrics exporter when metrics are enabled.
|
|
225
|
+
*
|
|
226
|
+
* @example Multiple metric readers
|
|
227
|
+
* ```typescript
|
|
228
|
+
* import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'
|
|
229
|
+
* import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'
|
|
230
|
+
* import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
|
|
231
|
+
*
|
|
232
|
+
* init({
|
|
233
|
+
* service: 'my-app',
|
|
234
|
+
* metricReaders: [
|
|
235
|
+
* new PeriodicExportingMetricReader({ exporter: new OTLPMetricExporter() }),
|
|
236
|
+
* new PrometheusExporter() // Export to multiple backends
|
|
237
|
+
* ]
|
|
238
|
+
* })
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
metricReaders?: MetricReader[];
|
|
242
|
+
/**
|
|
243
|
+
* Custom log record processors. When omitted, logs are not configured.
|
|
244
|
+
*/
|
|
245
|
+
logRecordProcessors?: LogRecordProcessor[];
|
|
246
|
+
/** Additional resource attributes to merge with defaults. */
|
|
247
|
+
resourceAttributes?: Attributes;
|
|
248
|
+
/** Provide a fully custom Resource to merge (advanced use case). */
|
|
249
|
+
resource?: Resource;
|
|
250
|
+
/**
|
|
251
|
+
* Headers for default OTLP exporters. Accepts either an object map or
|
|
252
|
+
* a "key=value" comma separated string.
|
|
253
|
+
*/
|
|
254
|
+
otlpHeaders?: Record<string, string> | string;
|
|
255
|
+
/**
|
|
256
|
+
* OTLP protocol to use for traces, metrics, and logs
|
|
257
|
+
* - 'http': HTTP/protobuf (default, uses port 4318)
|
|
258
|
+
* - 'grpc': gRPC (uses port 4317)
|
|
259
|
+
*
|
|
260
|
+
* Can be overridden with OTEL_EXPORTER_OTLP_PROTOCOL env var.
|
|
261
|
+
*
|
|
262
|
+
* Note: gRPC exporters are optional peer dependencies. Install them with:
|
|
263
|
+
* ```bash
|
|
264
|
+
* pnpm add @opentelemetry/exporter-trace-otlp-grpc @opentelemetry/exporter-metrics-otlp-grpc
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @example HTTP (default)
|
|
268
|
+
* ```typescript
|
|
269
|
+
* init({
|
|
270
|
+
* service: 'my-app',
|
|
271
|
+
* protocol: 'http', // or omit (defaults to http)
|
|
272
|
+
* endpoint: 'http://localhost:4318'
|
|
273
|
+
* })
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @example gRPC
|
|
277
|
+
* ```typescript
|
|
278
|
+
* init({
|
|
279
|
+
* service: 'my-app',
|
|
280
|
+
* protocol: 'grpc',
|
|
281
|
+
* endpoint: 'grpc://localhost:4317'
|
|
282
|
+
* })
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* @default 'http'
|
|
286
|
+
*/
|
|
287
|
+
protocol?: 'http' | 'grpc';
|
|
288
|
+
/**
|
|
289
|
+
* Optional factory to build a customised NodeSDK instance from our defaults.
|
|
290
|
+
*/
|
|
291
|
+
sdkFactory?: (defaults: Partial<NodeSDKConfiguration>) => NodeSDK;
|
|
292
|
+
/**
|
|
293
|
+
* Infrastructure metrics configuration
|
|
294
|
+
* - true: always enabled (default)
|
|
295
|
+
* - false: always disabled
|
|
296
|
+
* - 'auto': always enabled (same as true)
|
|
297
|
+
*
|
|
298
|
+
* Can be overridden with AUTOTELEMETRY_METRICS=on|off env var
|
|
299
|
+
*/
|
|
300
|
+
metrics?: boolean | 'auto';
|
|
301
|
+
/** Sampling strategy (default: AdaptiveSampler with 10% baseline) */
|
|
302
|
+
sampler?: Sampler;
|
|
303
|
+
/** Service version (default: auto-detect from package.json or '1.0.0') */
|
|
304
|
+
version?: string;
|
|
305
|
+
/** Environment (default: process.env.NODE_ENV || 'development') */
|
|
306
|
+
environment?: string;
|
|
307
|
+
/**
|
|
308
|
+
* Logger instance for structured logging with automatic trace correlation
|
|
309
|
+
*
|
|
310
|
+
* **Recommended:** Bring your own Pino or Winston instance
|
|
311
|
+
*
|
|
312
|
+
* Autotel automatically instruments Pino and Winston loggers to:
|
|
313
|
+
* - Inject trace context (traceId, spanId) into every log record
|
|
314
|
+
* - Record errors in the active OpenTelemetry span
|
|
315
|
+
* - Bridge logs to the OpenTelemetry Logs API for OTLP export to Grafana, Datadog, etc.
|
|
316
|
+
*
|
|
317
|
+
* Supports any logger with 4 methods: info/warn/error/debug
|
|
318
|
+
* Default: silent logger (no-op)
|
|
319
|
+
*
|
|
320
|
+
* @example Using Pino (recommended)
|
|
321
|
+
* ```typescript
|
|
322
|
+
* import pino from 'pino' // npm install pino
|
|
323
|
+
* import { init } from 'autotel'
|
|
324
|
+
*
|
|
325
|
+
* const logger = pino({ level: 'info' })
|
|
326
|
+
* init({ service: 'my-app', logger })
|
|
327
|
+
*
|
|
328
|
+
* // Logs automatically include traceId/spanId and export via OTLP!
|
|
329
|
+
* logger.info('User created', { userId: '123' })
|
|
330
|
+
* ```
|
|
331
|
+
*
|
|
332
|
+
* @example Using Winston
|
|
333
|
+
* ```typescript
|
|
334
|
+
* import winston from 'winston' // npm install winston
|
|
335
|
+
* import { init } from 'autotel'
|
|
336
|
+
*
|
|
337
|
+
* const logger = winston.createLogger({
|
|
338
|
+
* level: 'info',
|
|
339
|
+
* format: winston.format.json()
|
|
340
|
+
* })
|
|
341
|
+
* init({ service: 'my-app', logger })
|
|
342
|
+
* ```
|
|
343
|
+
*
|
|
344
|
+
* @example Custom logger (any logger with 4 methods)
|
|
345
|
+
* ```typescript
|
|
346
|
+
* const logger = {
|
|
347
|
+
* info: (msg, extra) => console.log(msg, extra),
|
|
348
|
+
* warn: (msg, extra) => console.warn(msg, extra),
|
|
349
|
+
* error: (msg, err, extra) => console.error(msg, err, extra),
|
|
350
|
+
* debug: (msg, extra) => console.debug(msg, extra),
|
|
351
|
+
* }
|
|
352
|
+
* init({ service: 'my-app', logger })
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
logger?: Logger;
|
|
356
|
+
/**
|
|
357
|
+
* Automatically flush events queue when root spans end
|
|
358
|
+
* - true: Auto-flush on root span completion (default)
|
|
359
|
+
* - false: Use batching (events flush every 10 seconds automatically)
|
|
360
|
+
*
|
|
361
|
+
* Only flushes on root spans to avoid excessive network calls.
|
|
362
|
+
* Default is true for serverless/short-lived processes. Set to false
|
|
363
|
+
* for long-running services where batching is more efficient.
|
|
364
|
+
*/
|
|
365
|
+
autoFlushEvents?: boolean;
|
|
366
|
+
/**
|
|
367
|
+
* Include OpenTelemetry span flushing in auto-flush (default: false)
|
|
368
|
+
*
|
|
369
|
+
* When enabled, spans are force-flushed along with events events on root
|
|
370
|
+
* span completion. This is useful for serverless/short-lived processes where
|
|
371
|
+
* spans may not export before the process ends.
|
|
372
|
+
*
|
|
373
|
+
* - true: Force-flush spans on root span completion (~50-200ms latency)
|
|
374
|
+
* - false: Spans export via normal batch processor (default behavior)
|
|
375
|
+
*
|
|
376
|
+
* Only applies when autoFlushEvents is also enabled.
|
|
377
|
+
*
|
|
378
|
+
* Note: For edge runtimes (Cloudflare Workers, Vercel Edge), use the
|
|
379
|
+
* 'autotel-edge' package instead, which handles this automatically.
|
|
380
|
+
*
|
|
381
|
+
* @example Serverless with auto-flush
|
|
382
|
+
* ```typescript
|
|
383
|
+
* init({
|
|
384
|
+
* service: 'my-lambda',
|
|
385
|
+
* autoFlushEvents: true,
|
|
386
|
+
* autoFlush: true, // Force-flush spans
|
|
387
|
+
* });
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
autoFlush?: boolean;
|
|
391
|
+
/**
|
|
392
|
+
* Automatically copy baggage entries to span attributes
|
|
393
|
+
*
|
|
394
|
+
* When enabled, all baggage entries are automatically added as span attributes,
|
|
395
|
+
* making them visible in trace UIs (Jaeger, Grafana, DataDog, etc.) without
|
|
396
|
+
* manually calling ctx.setAttribute() for each entry.
|
|
397
|
+
*
|
|
398
|
+
* - `true`: adds baggage with 'baggage.' prefix (e.g. baggage.tenant.id)
|
|
399
|
+
* - `string`: uses custom prefix (e.g. 'ctx' → ctx.tenant.id, '' → tenant.id)
|
|
400
|
+
* - `false` or omit: disabled (default)
|
|
401
|
+
*
|
|
402
|
+
* @default false
|
|
403
|
+
*
|
|
404
|
+
* @example Enable with default prefix
|
|
405
|
+
* ```typescript
|
|
406
|
+
* init({
|
|
407
|
+
* service: 'my-app',
|
|
408
|
+
* baggage: true
|
|
409
|
+
* });
|
|
410
|
+
*
|
|
411
|
+
* // Now baggage automatically appears as span attributes
|
|
412
|
+
* await withBaggage({
|
|
413
|
+
* baggage: { 'tenant.id': 't1', 'user.id': 'u1' },
|
|
414
|
+
* fn: async () => {
|
|
415
|
+
* // Span has baggage.tenant.id and baggage.user.id attributes!
|
|
416
|
+
* }
|
|
417
|
+
* });
|
|
418
|
+
* ```
|
|
419
|
+
*
|
|
420
|
+
* @example Custom prefix
|
|
421
|
+
* ```typescript
|
|
422
|
+
* init({
|
|
423
|
+
* service: 'my-app',
|
|
424
|
+
* baggage: 'ctx' // Uses 'ctx.' prefix
|
|
425
|
+
* });
|
|
426
|
+
* // Creates attributes: ctx.tenant.id, ctx.user.id
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* @example No prefix
|
|
430
|
+
* ```typescript
|
|
431
|
+
* init({
|
|
432
|
+
* service: 'my-app',
|
|
433
|
+
* baggage: '' // No prefix
|
|
434
|
+
* });
|
|
435
|
+
* // Creates attributes: tenant.id, user.id
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
baggage?: boolean | string;
|
|
439
|
+
/**
|
|
440
|
+
* Validation configuration for events events
|
|
441
|
+
* - Override default sensitive field patterns for redaction
|
|
442
|
+
* - Customize max lengths, nesting depth, etc.
|
|
443
|
+
*
|
|
444
|
+
* @example Disable redaction for development
|
|
445
|
+
* ```typescript
|
|
446
|
+
* init({
|
|
447
|
+
* service: 'my-app',
|
|
448
|
+
* validation: {
|
|
449
|
+
* sensitivePatterns: [] // Disable all redaction
|
|
450
|
+
* }
|
|
451
|
+
* })
|
|
452
|
+
* ```
|
|
453
|
+
*
|
|
454
|
+
* @example Add custom patterns
|
|
455
|
+
* ```typescript
|
|
456
|
+
* init({
|
|
457
|
+
* service: 'my-app',
|
|
458
|
+
* validation: {
|
|
459
|
+
* sensitivePatterns: [
|
|
460
|
+
* /password/i,
|
|
461
|
+
* /apiKey/i,
|
|
462
|
+
* /customSecret/i // Your custom pattern
|
|
463
|
+
* ]
|
|
464
|
+
* }
|
|
465
|
+
* })
|
|
466
|
+
* ```
|
|
467
|
+
*/
|
|
468
|
+
validation?: Partial<ValidationConfig>;
|
|
469
|
+
/**
|
|
470
|
+
* Debug mode for local span inspection.
|
|
471
|
+
* Enables console output to help you see spans as they're created.
|
|
472
|
+
*
|
|
473
|
+
* When true: Outputs spans to console AND sends to backend (if endpoint/exporter configured)
|
|
474
|
+
* When false/undefined: Sends to backend only (default behavior)
|
|
475
|
+
*
|
|
476
|
+
* Perfect for progressive development:
|
|
477
|
+
* - Start with debug: true (no endpoint) → console-only, see traces immediately
|
|
478
|
+
* - Add endpoint later → console + backend, verify before choosing provider
|
|
479
|
+
* - Remove debug in production → backend only, clean production config
|
|
480
|
+
*
|
|
481
|
+
* Can be overridden with AUTOLEMETRY_DEBUG environment variable.
|
|
482
|
+
*
|
|
483
|
+
* @example Getting started - see spans immediately
|
|
484
|
+
* ```typescript
|
|
485
|
+
* init({
|
|
486
|
+
* service: 'my-app',
|
|
487
|
+
* debug: true // No endpoint yet - console only!
|
|
488
|
+
* })
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @example Testing with local collector
|
|
492
|
+
* ```typescript
|
|
493
|
+
* init({
|
|
494
|
+
* service: 'my-app',
|
|
495
|
+
* debug: true,
|
|
496
|
+
* endpoint: 'http://localhost:4318' // Console + OTLP
|
|
497
|
+
* })
|
|
498
|
+
* ```
|
|
499
|
+
*
|
|
500
|
+
* @example Production debugging
|
|
501
|
+
* ```typescript
|
|
502
|
+
* init({
|
|
503
|
+
* service: 'my-app',
|
|
504
|
+
* debug: true, // See what's being sent
|
|
505
|
+
* endpoint: 'https://api.honeycomb.io'
|
|
506
|
+
* })
|
|
507
|
+
* ```
|
|
508
|
+
*
|
|
509
|
+
* @example Environment variable
|
|
510
|
+
* ```bash
|
|
511
|
+
* AUTOLEMETRY_DEBUG=true node server.js
|
|
512
|
+
* ```
|
|
513
|
+
*/
|
|
514
|
+
debug?: boolean;
|
|
515
|
+
/**
|
|
516
|
+
* OpenLLMetry integration for LLM observability.
|
|
517
|
+
* Requires @traceloop/node-server-sdk as an optional peer dependency.
|
|
518
|
+
*
|
|
519
|
+
* @example Enable OpenLLMetry with default settings
|
|
520
|
+
* ```typescript
|
|
521
|
+
* init({
|
|
522
|
+
* service: 'my-app',
|
|
523
|
+
* openllmetry: { enabled: true }
|
|
524
|
+
* })
|
|
525
|
+
* ```
|
|
526
|
+
*
|
|
527
|
+
* @example Enable with custom options
|
|
528
|
+
* ```typescript
|
|
529
|
+
* init({
|
|
530
|
+
* service: 'my-app',
|
|
531
|
+
* openllmetry: {
|
|
532
|
+
* enabled: true,
|
|
533
|
+
* options: {
|
|
534
|
+
* disableBatch: process.env.NODE_ENV !== 'production',
|
|
535
|
+
* apiKey: process.env.TRACELOOP_API_KEY
|
|
536
|
+
* }
|
|
537
|
+
* }
|
|
538
|
+
* })
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
openllmetry?: {
|
|
542
|
+
enabled: boolean;
|
|
543
|
+
options?: Record<string, unknown>;
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
declare function init(cfg: AutotelConfig): void;
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Span processor that copies baggage entries to span attributes
|
|
550
|
+
*
|
|
551
|
+
* This makes baggage visible in trace UIs without manual attribute setting.
|
|
552
|
+
* Enabled via init({ baggage: true }) or init({ baggage: 'custom-prefix' })
|
|
553
|
+
*/
|
|
554
|
+
|
|
555
|
+
interface BaggageSpanProcessorOptions {
|
|
556
|
+
/**
|
|
557
|
+
* Prefix for baggage attributes
|
|
558
|
+
* @default 'baggage.'
|
|
559
|
+
*/
|
|
560
|
+
prefix?: string;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Span processor that automatically copies baggage entries to span attributes
|
|
564
|
+
*
|
|
565
|
+
* This makes baggage visible in trace UIs (Jaeger, Grafana, DataDog, etc.)
|
|
566
|
+
* without manually calling ctx.setAttribute() for each baggage entry.
|
|
567
|
+
*
|
|
568
|
+
* @example Enable in init()
|
|
569
|
+
* ```typescript
|
|
570
|
+
* init({
|
|
571
|
+
* service: 'my-app',
|
|
572
|
+
* baggage: true // Uses default 'baggage.' prefix
|
|
573
|
+
* });
|
|
574
|
+
*
|
|
575
|
+
* // Now baggage automatically appears as span attributes
|
|
576
|
+
* await withBaggage({
|
|
577
|
+
* baggage: { 'tenant.id': 't1', 'user.id': 'u1' },
|
|
578
|
+
* fn: async () => {
|
|
579
|
+
* // Span has baggage.tenant.id and baggage.user.id attributes!
|
|
580
|
+
* }
|
|
581
|
+
* });
|
|
582
|
+
* ```
|
|
583
|
+
*
|
|
584
|
+
* @example Custom prefix
|
|
585
|
+
* ```typescript
|
|
586
|
+
* init({
|
|
587
|
+
* service: 'my-app',
|
|
588
|
+
* baggage: 'ctx' // Uses 'ctx.' prefix
|
|
589
|
+
* });
|
|
590
|
+
* // Creates attributes: ctx.tenant.id, ctx.user.id
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
declare class BaggageSpanProcessor implements SpanProcessor {
|
|
594
|
+
private readonly prefix;
|
|
595
|
+
constructor(options?: BaggageSpanProcessorOptions);
|
|
596
|
+
onStart(span: Span, parentContext: Context): void;
|
|
597
|
+
onEnd(_span: ReadableSpan): void;
|
|
598
|
+
shutdown(): Promise<void>;
|
|
599
|
+
forceFlush(): Promise<void>;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Operation context tracking using AsyncLocalStorage
|
|
604
|
+
*
|
|
605
|
+
* This module provides a way to track operation names across async boundaries
|
|
606
|
+
* so they can be automatically captured in events events.
|
|
607
|
+
*
|
|
608
|
+
* We cannot read span attributes from OpenTelemetry's API (it's write-only),
|
|
609
|
+
* so we maintain our own async context storage.
|
|
610
|
+
*/
|
|
611
|
+
/**
|
|
612
|
+
* Operation context that flows through async operations
|
|
613
|
+
*/
|
|
614
|
+
interface OperationContext {
|
|
615
|
+
/**
|
|
616
|
+
* The name of the current operation
|
|
617
|
+
* This is set by trace() or span() and can be read by events
|
|
618
|
+
*/
|
|
619
|
+
name: string;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Get the current operation context (if any)
|
|
623
|
+
*
|
|
624
|
+
* @returns The current operation context, or undefined if not in an operation
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```typescript
|
|
628
|
+
* const ctx = getOperationContext();
|
|
629
|
+
* if (ctx) {
|
|
630
|
+
* console.log('Current operation:', ctx.name);
|
|
631
|
+
* }
|
|
632
|
+
* ```
|
|
633
|
+
*/
|
|
634
|
+
declare function getOperationContext(): OperationContext | undefined;
|
|
635
|
+
/**
|
|
636
|
+
* Run a function within an operation context
|
|
637
|
+
*
|
|
638
|
+
* This sets the operation name for the duration of the function execution,
|
|
639
|
+
* including all async operations spawned from it.
|
|
640
|
+
*
|
|
641
|
+
* @param name - The operation name to set
|
|
642
|
+
* @param fn - The function to execute within the context
|
|
643
|
+
* @returns The result of the function
|
|
644
|
+
*
|
|
645
|
+
* @example
|
|
646
|
+
* ```typescript
|
|
647
|
+
* const result = await runInOperationContext('user.create', async () => {
|
|
648
|
+
* // Any events.trackEvent() calls here will automatically capture
|
|
649
|
+
* // 'operation.name': 'user.create'
|
|
650
|
+
* await createUser();
|
|
651
|
+
* return 'success';
|
|
652
|
+
* });
|
|
653
|
+
* ```
|
|
654
|
+
*/
|
|
655
|
+
declare function runInOperationContext<T>(name: string, fn: () => T): T;
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Global track() function for business events
|
|
659
|
+
*
|
|
660
|
+
* Simple, no instantiation needed, auto-attaches trace context
|
|
661
|
+
*/
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Track a business events event
|
|
665
|
+
*
|
|
666
|
+
* Features:
|
|
667
|
+
* - Auto-attaches traceId and spanId if in active span
|
|
668
|
+
* - Batched sending with retry
|
|
669
|
+
* - Type-safe with optional generic
|
|
670
|
+
* - No-op if init() not called or no subscribers configured
|
|
671
|
+
*
|
|
672
|
+
* @example Basic usage
|
|
673
|
+
* ```typescript
|
|
674
|
+
* track('user.signup', { userId: '123', plan: 'pro' })
|
|
675
|
+
* ```
|
|
676
|
+
*
|
|
677
|
+
* @example With type safety
|
|
678
|
+
* ```typescript
|
|
679
|
+
* interface EventDatas {
|
|
680
|
+
* 'user.signup': { userId: string; plan: string }
|
|
681
|
+
* 'plan.upgraded': { userId: string; revenue: number }
|
|
682
|
+
* }
|
|
683
|
+
*
|
|
684
|
+
* track<EventDatas>('user.signup', { userId: '123', plan: 'pro' })
|
|
685
|
+
* ```
|
|
686
|
+
*
|
|
687
|
+
* @example Trace correlation (automatic)
|
|
688
|
+
* ```typescript
|
|
689
|
+
* @Instrumented()
|
|
690
|
+
* class UserService {
|
|
691
|
+
* async createUser(data: CreateUserData) {
|
|
692
|
+
* // This track call automatically includes traceId + spanId
|
|
693
|
+
* track('user.signup', { userId: data.id })
|
|
694
|
+
* }
|
|
695
|
+
* }
|
|
696
|
+
* ```
|
|
697
|
+
*/
|
|
698
|
+
declare function track<Events extends Record<string, any> = Record<string, any>>(event: keyof Events & string, data?: Events[typeof event]): void;
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Graceful shutdown with flush and cleanup
|
|
702
|
+
*/
|
|
703
|
+
/**
|
|
704
|
+
* Flush all pending telemetry
|
|
705
|
+
*
|
|
706
|
+
* Flushes both events events and OpenTelemetry spans to their destinations.
|
|
707
|
+
* Includes timeout protection to prevent hanging in serverless environments.
|
|
708
|
+
*
|
|
709
|
+
* Safe to call multiple times.
|
|
710
|
+
*
|
|
711
|
+
* @param options - Optional configuration
|
|
712
|
+
* @param options.timeout - Timeout in milliseconds (default: 2000ms)
|
|
713
|
+
*
|
|
714
|
+
* @example Manual flush in serverless
|
|
715
|
+
* ```typescript
|
|
716
|
+
* import { flush } from 'autotel';
|
|
717
|
+
*
|
|
718
|
+
* export const handler = async (event) => {
|
|
719
|
+
* // ... process event
|
|
720
|
+
* await flush(); // Flush before function returns
|
|
721
|
+
* return result;
|
|
722
|
+
* };
|
|
723
|
+
* ```
|
|
724
|
+
*
|
|
725
|
+
* @example With custom timeout
|
|
726
|
+
* ```typescript
|
|
727
|
+
* await flush({ timeout: 5000 }); // 5 second timeout
|
|
728
|
+
* ```
|
|
729
|
+
*/
|
|
730
|
+
declare function flush(options?: {
|
|
731
|
+
timeout?: number;
|
|
732
|
+
}): Promise<void>;
|
|
733
|
+
/**
|
|
734
|
+
* Shutdown telemetry and cleanup resources
|
|
735
|
+
*
|
|
736
|
+
* - Flushes all pending data
|
|
737
|
+
* - Shuts down OpenTelemetry SDK
|
|
738
|
+
* - Cleans up resources
|
|
739
|
+
*
|
|
740
|
+
* Call this before process exit.
|
|
741
|
+
*
|
|
742
|
+
* Always performs cleanup even if flush fails, preventing resource leaks
|
|
743
|
+
* in serverless handlers or tests.
|
|
744
|
+
*
|
|
745
|
+
* @example Express server
|
|
746
|
+
* ```typescript
|
|
747
|
+
* const server = app.listen(3000)
|
|
748
|
+
*
|
|
749
|
+
* process.on('SIGTERM', async () => {
|
|
750
|
+
* await server.close()
|
|
751
|
+
* await shutdown()
|
|
752
|
+
* process.exit(0)
|
|
753
|
+
* })
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
756
|
+
declare function shutdown(): Promise<void>;
|
|
757
|
+
|
|
758
|
+
export { type AutotelConfig, BaggageSpanProcessor, type BaggageSpanProcessorOptions, EventSubscriber, type OperationContext, Sampler, flush, getOperationContext, init, runInOperationContext, shutdown, track };
|