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.
Files changed (272) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1946 -0
  3. package/dist/chunk-2LNRY4QK.js +273 -0
  4. package/dist/chunk-2LNRY4QK.js.map +1 -0
  5. package/dist/chunk-3HENGDW2.js +587 -0
  6. package/dist/chunk-3HENGDW2.js.map +1 -0
  7. package/dist/chunk-4OAT42CA.cjs +73 -0
  8. package/dist/chunk-4OAT42CA.cjs.map +1 -0
  9. package/dist/chunk-5GWX5LFW.js +70 -0
  10. package/dist/chunk-5GWX5LFW.js.map +1 -0
  11. package/dist/chunk-5R2M36QB.js +195 -0
  12. package/dist/chunk-5R2M36QB.js.map +1 -0
  13. package/dist/chunk-5ZN622AO.js +73 -0
  14. package/dist/chunk-5ZN622AO.js.map +1 -0
  15. package/dist/chunk-77MSMAUQ.cjs +498 -0
  16. package/dist/chunk-77MSMAUQ.cjs.map +1 -0
  17. package/dist/chunk-ABPEQ6RK.cjs +596 -0
  18. package/dist/chunk-ABPEQ6RK.cjs.map +1 -0
  19. package/dist/chunk-BWYGJKRB.js +95 -0
  20. package/dist/chunk-BWYGJKRB.js.map +1 -0
  21. package/dist/chunk-BZHG5IZ4.js +73 -0
  22. package/dist/chunk-BZHG5IZ4.js.map +1 -0
  23. package/dist/chunk-G7VZBCD6.cjs +35 -0
  24. package/dist/chunk-G7VZBCD6.cjs.map +1 -0
  25. package/dist/chunk-GVLK7YUU.cjs +30 -0
  26. package/dist/chunk-GVLK7YUU.cjs.map +1 -0
  27. package/dist/chunk-HCCXC7XG.js +205 -0
  28. package/dist/chunk-HCCXC7XG.js.map +1 -0
  29. package/dist/chunk-HE6T6FIX.cjs +203 -0
  30. package/dist/chunk-HE6T6FIX.cjs.map +1 -0
  31. package/dist/chunk-KIXWPOCO.cjs +100 -0
  32. package/dist/chunk-KIXWPOCO.cjs.map +1 -0
  33. package/dist/chunk-KVGNW3FC.js +87 -0
  34. package/dist/chunk-KVGNW3FC.js.map +1 -0
  35. package/dist/chunk-LITNXTTT.js +3 -0
  36. package/dist/chunk-LITNXTTT.js.map +1 -0
  37. package/dist/chunk-M4ANN7RL.js +114 -0
  38. package/dist/chunk-M4ANN7RL.js.map +1 -0
  39. package/dist/chunk-NC52UBR2.cjs +32 -0
  40. package/dist/chunk-NC52UBR2.cjs.map +1 -0
  41. package/dist/chunk-NHCNRQD3.cjs +212 -0
  42. package/dist/chunk-NHCNRQD3.cjs.map +1 -0
  43. package/dist/chunk-NZ72VDNY.cjs +4 -0
  44. package/dist/chunk-NZ72VDNY.cjs.map +1 -0
  45. package/dist/chunk-P6JUDYNO.js +57 -0
  46. package/dist/chunk-P6JUDYNO.js.map +1 -0
  47. package/dist/chunk-RJYY7BWX.js +1349 -0
  48. package/dist/chunk-RJYY7BWX.js.map +1 -0
  49. package/dist/chunk-TRI4V5BF.cjs +126 -0
  50. package/dist/chunk-TRI4V5BF.cjs.map +1 -0
  51. package/dist/chunk-UL33I6IS.js +139 -0
  52. package/dist/chunk-UL33I6IS.js.map +1 -0
  53. package/dist/chunk-URRW6M2C.cjs +61 -0
  54. package/dist/chunk-URRW6M2C.cjs.map +1 -0
  55. package/dist/chunk-UY3UYPBZ.cjs +77 -0
  56. package/dist/chunk-UY3UYPBZ.cjs.map +1 -0
  57. package/dist/chunk-W3253FGB.cjs +277 -0
  58. package/dist/chunk-W3253FGB.cjs.map +1 -0
  59. package/dist/chunk-W7LHZVQF.js +26 -0
  60. package/dist/chunk-W7LHZVQF.js.map +1 -0
  61. package/dist/chunk-WBWNM6LB.cjs +1360 -0
  62. package/dist/chunk-WBWNM6LB.cjs.map +1 -0
  63. package/dist/chunk-WFJ7L2RV.js +494 -0
  64. package/dist/chunk-WFJ7L2RV.js.map +1 -0
  65. package/dist/chunk-X4RMFFMR.js +28 -0
  66. package/dist/chunk-X4RMFFMR.js.map +1 -0
  67. package/dist/chunk-Y4Y2S7BM.cjs +92 -0
  68. package/dist/chunk-Y4Y2S7BM.cjs.map +1 -0
  69. package/dist/chunk-YLPNXZFI.cjs +143 -0
  70. package/dist/chunk-YLPNXZFI.cjs.map +1 -0
  71. package/dist/chunk-YTXEZ4SD.cjs +77 -0
  72. package/dist/chunk-YTXEZ4SD.cjs.map +1 -0
  73. package/dist/chunk-Z6ZWNWWR.js +30 -0
  74. package/dist/chunk-Z6ZWNWWR.js.map +1 -0
  75. package/dist/config.cjs +26 -0
  76. package/dist/config.cjs.map +1 -0
  77. package/dist/config.d.cts +75 -0
  78. package/dist/config.d.ts +75 -0
  79. package/dist/config.js +5 -0
  80. package/dist/config.js.map +1 -0
  81. package/dist/db.cjs +233 -0
  82. package/dist/db.cjs.map +1 -0
  83. package/dist/db.d.cts +123 -0
  84. package/dist/db.d.ts +123 -0
  85. package/dist/db.js +228 -0
  86. package/dist/db.js.map +1 -0
  87. package/dist/decorators.cjs +67 -0
  88. package/dist/decorators.cjs.map +1 -0
  89. package/dist/decorators.d.cts +91 -0
  90. package/dist/decorators.d.ts +91 -0
  91. package/dist/decorators.js +65 -0
  92. package/dist/decorators.js.map +1 -0
  93. package/dist/event-subscriber.cjs +6 -0
  94. package/dist/event-subscriber.cjs.map +1 -0
  95. package/dist/event-subscriber.d.cts +116 -0
  96. package/dist/event-subscriber.d.ts +116 -0
  97. package/dist/event-subscriber.js +3 -0
  98. package/dist/event-subscriber.js.map +1 -0
  99. package/dist/event-testing.cjs +21 -0
  100. package/dist/event-testing.cjs.map +1 -0
  101. package/dist/event-testing.d.cts +110 -0
  102. package/dist/event-testing.d.ts +110 -0
  103. package/dist/event-testing.js +4 -0
  104. package/dist/event-testing.js.map +1 -0
  105. package/dist/event.cjs +30 -0
  106. package/dist/event.cjs.map +1 -0
  107. package/dist/event.d.cts +282 -0
  108. package/dist/event.d.ts +282 -0
  109. package/dist/event.js +13 -0
  110. package/dist/event.js.map +1 -0
  111. package/dist/exporters.cjs +17 -0
  112. package/dist/exporters.cjs.map +1 -0
  113. package/dist/exporters.d.cts +1 -0
  114. package/dist/exporters.d.ts +1 -0
  115. package/dist/exporters.js +4 -0
  116. package/dist/exporters.js.map +1 -0
  117. package/dist/functional.cjs +46 -0
  118. package/dist/functional.cjs.map +1 -0
  119. package/dist/functional.d.cts +478 -0
  120. package/dist/functional.d.ts +478 -0
  121. package/dist/functional.js +13 -0
  122. package/dist/functional.js.map +1 -0
  123. package/dist/http.cjs +189 -0
  124. package/dist/http.cjs.map +1 -0
  125. package/dist/http.d.cts +169 -0
  126. package/dist/http.d.ts +169 -0
  127. package/dist/http.js +184 -0
  128. package/dist/http.js.map +1 -0
  129. package/dist/index.cjs +333 -0
  130. package/dist/index.cjs.map +1 -0
  131. package/dist/index.d.cts +758 -0
  132. package/dist/index.d.ts +758 -0
  133. package/dist/index.js +143 -0
  134. package/dist/index.js.map +1 -0
  135. package/dist/instrumentation.cjs +182 -0
  136. package/dist/instrumentation.cjs.map +1 -0
  137. package/dist/instrumentation.d.cts +49 -0
  138. package/dist/instrumentation.d.ts +49 -0
  139. package/dist/instrumentation.js +179 -0
  140. package/dist/instrumentation.js.map +1 -0
  141. package/dist/logger.cjs +19 -0
  142. package/dist/logger.cjs.map +1 -0
  143. package/dist/logger.d.cts +146 -0
  144. package/dist/logger.d.ts +146 -0
  145. package/dist/logger.js +6 -0
  146. package/dist/logger.js.map +1 -0
  147. package/dist/metric-helpers.cjs +31 -0
  148. package/dist/metric-helpers.cjs.map +1 -0
  149. package/dist/metric-helpers.d.cts +13 -0
  150. package/dist/metric-helpers.d.ts +13 -0
  151. package/dist/metric-helpers.js +6 -0
  152. package/dist/metric-helpers.js.map +1 -0
  153. package/dist/metric-testing.cjs +21 -0
  154. package/dist/metric-testing.cjs.map +1 -0
  155. package/dist/metric-testing.d.cts +110 -0
  156. package/dist/metric-testing.d.ts +110 -0
  157. package/dist/metric-testing.js +4 -0
  158. package/dist/metric-testing.js.map +1 -0
  159. package/dist/metric.cjs +26 -0
  160. package/dist/metric.cjs.map +1 -0
  161. package/dist/metric.d.cts +240 -0
  162. package/dist/metric.d.ts +240 -0
  163. package/dist/metric.js +9 -0
  164. package/dist/metric.js.map +1 -0
  165. package/dist/processors.cjs +17 -0
  166. package/dist/processors.cjs.map +1 -0
  167. package/dist/processors.d.cts +1 -0
  168. package/dist/processors.d.ts +1 -0
  169. package/dist/processors.js +4 -0
  170. package/dist/processors.js.map +1 -0
  171. package/dist/sampling.cjs +40 -0
  172. package/dist/sampling.cjs.map +1 -0
  173. package/dist/sampling.d.cts +260 -0
  174. package/dist/sampling.d.ts +260 -0
  175. package/dist/sampling.js +7 -0
  176. package/dist/sampling.js.map +1 -0
  177. package/dist/semantic-helpers.cjs +35 -0
  178. package/dist/semantic-helpers.cjs.map +1 -0
  179. package/dist/semantic-helpers.d.cts +442 -0
  180. package/dist/semantic-helpers.d.ts +442 -0
  181. package/dist/semantic-helpers.js +14 -0
  182. package/dist/semantic-helpers.js.map +1 -0
  183. package/dist/tail-sampling-processor.cjs +13 -0
  184. package/dist/tail-sampling-processor.cjs.map +1 -0
  185. package/dist/tail-sampling-processor.d.cts +27 -0
  186. package/dist/tail-sampling-processor.d.ts +27 -0
  187. package/dist/tail-sampling-processor.js +4 -0
  188. package/dist/tail-sampling-processor.js.map +1 -0
  189. package/dist/testing.cjs +286 -0
  190. package/dist/testing.cjs.map +1 -0
  191. package/dist/testing.d.cts +291 -0
  192. package/dist/testing.d.ts +291 -0
  193. package/dist/testing.js +263 -0
  194. package/dist/testing.js.map +1 -0
  195. package/dist/trace-context-DRZdUvVY.d.cts +181 -0
  196. package/dist/trace-context-DRZdUvVY.d.ts +181 -0
  197. package/dist/trace-helpers.cjs +54 -0
  198. package/dist/trace-helpers.cjs.map +1 -0
  199. package/dist/trace-helpers.d.cts +524 -0
  200. package/dist/trace-helpers.d.ts +524 -0
  201. package/dist/trace-helpers.js +5 -0
  202. package/dist/trace-helpers.js.map +1 -0
  203. package/dist/tracer-provider.cjs +21 -0
  204. package/dist/tracer-provider.cjs.map +1 -0
  205. package/dist/tracer-provider.d.cts +169 -0
  206. package/dist/tracer-provider.d.ts +169 -0
  207. package/dist/tracer-provider.js +4 -0
  208. package/dist/tracer-provider.js.map +1 -0
  209. package/package.json +280 -0
  210. package/src/baggage-span-processor.test.ts +202 -0
  211. package/src/baggage-span-processor.ts +98 -0
  212. package/src/circuit-breaker.test.ts +341 -0
  213. package/src/circuit-breaker.ts +184 -0
  214. package/src/config.test.ts +94 -0
  215. package/src/config.ts +169 -0
  216. package/src/db.test.ts +252 -0
  217. package/src/db.ts +447 -0
  218. package/src/decorators.test.ts +203 -0
  219. package/src/decorators.ts +188 -0
  220. package/src/env-config.test.ts +246 -0
  221. package/src/env-config.ts +158 -0
  222. package/src/event-queue.test.ts +222 -0
  223. package/src/event-queue.ts +203 -0
  224. package/src/event-subscriber.ts +136 -0
  225. package/src/event-testing.ts +197 -0
  226. package/src/event.test.ts +718 -0
  227. package/src/event.ts +556 -0
  228. package/src/exporters.ts +96 -0
  229. package/src/functional.test.ts +1059 -0
  230. package/src/functional.ts +2295 -0
  231. package/src/http.test.ts +487 -0
  232. package/src/http.ts +424 -0
  233. package/src/index.ts +158 -0
  234. package/src/init.customization.test.ts +210 -0
  235. package/src/init.integrations.test.ts +366 -0
  236. package/src/init.openllmetry.test.ts +282 -0
  237. package/src/init.protocol.test.ts +215 -0
  238. package/src/init.ts +1426 -0
  239. package/src/instrumentation.test.ts +108 -0
  240. package/src/instrumentation.ts +308 -0
  241. package/src/logger.test.ts +117 -0
  242. package/src/logger.ts +246 -0
  243. package/src/metric-helpers.ts +47 -0
  244. package/src/metric-testing.ts +197 -0
  245. package/src/metric.ts +434 -0
  246. package/src/metrics.test.ts +205 -0
  247. package/src/operation-context.ts +93 -0
  248. package/src/processors.ts +106 -0
  249. package/src/rate-limiter.test.ts +199 -0
  250. package/src/rate-limiter.ts +98 -0
  251. package/src/sampling.test.ts +513 -0
  252. package/src/sampling.ts +428 -0
  253. package/src/semantic-helpers.test.ts +311 -0
  254. package/src/semantic-helpers.ts +584 -0
  255. package/src/shutdown.test.ts +311 -0
  256. package/src/shutdown.ts +222 -0
  257. package/src/stub.integration.test.ts +361 -0
  258. package/src/tail-sampling-processor.test.ts +226 -0
  259. package/src/tail-sampling-processor.ts +51 -0
  260. package/src/testing.ts +670 -0
  261. package/src/trace-context.ts +470 -0
  262. package/src/trace-helpers.new.test.ts +278 -0
  263. package/src/trace-helpers.test.ts +242 -0
  264. package/src/trace-helpers.ts +690 -0
  265. package/src/tracer-provider.test.ts +183 -0
  266. package/src/tracer-provider.ts +266 -0
  267. package/src/track.test.ts +153 -0
  268. package/src/track.ts +120 -0
  269. package/src/validation.test.ts +306 -0
  270. package/src/validation.ts +239 -0
  271. package/src/variable-name-inference.test.ts +178 -0
  272. package/src/variable-name-inference.ts +242 -0
@@ -0,0 +1,524 @@
1
+ import { Tracer, Span, Context } from '@opentelemetry/api';
2
+
3
+ /**
4
+ * Trace context helpers - Core primitives for trace correlation
5
+ *
6
+ * These are the building blocks that allow users to bring their own logger
7
+ * (bunyan, log4js, custom, etc.) and add trace correlation.
8
+ *
9
+ * @example Using with bunyan
10
+ * ```typescript
11
+ * import bunyan from 'bunyan';
12
+ * import { enrichWithTraceContext } from 'autotel/trace-helpers';
13
+ *
14
+ * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });
15
+ *
16
+ * const logger = {
17
+ * info: (msg: string, extra?: object) => {
18
+ * bunyanLogger.info(enrichWithTraceContext(extra || {}), msg);
19
+ * }
20
+ * };
21
+ * ```
22
+ *
23
+ * @example Using with log4js
24
+ * ```typescript
25
+ * import log4js from 'log4js';
26
+ * import { getTraceContext } from 'autotel/trace-helpers';
27
+ *
28
+ * const log4jsLogger = log4js.getLogger();
29
+ *
30
+ * function logWithTrace(level: string, msg: string, extra?: object) {
31
+ * const context = getTraceContext();
32
+ * log4jsLogger[level](msg, { ...extra, ...context });
33
+ * }
34
+ * ```
35
+ */
36
+
37
+ /**
38
+ * Store span name for a given span
39
+ * Called internally when spans are created
40
+ */
41
+ declare function setSpanName(span: Span, name: string): void;
42
+ /**
43
+ * Trace context extracted from active span
44
+ */
45
+ interface TraceContext {
46
+ /** Full 32-character hex trace ID */
47
+ traceId: string;
48
+ /** 16-character hex span ID */
49
+ spanId: string;
50
+ /** First 16 characters of trace ID (for log grouping/correlation) */
51
+ correlationId: string;
52
+ /** Function/operation name (OpenTelemetry semantic convention: code.function) */
53
+ 'code.function'?: string;
54
+ /** Datadog trace ID in decimal format (lower 64 bits) for log-trace correlation */
55
+ 'dd.trace_id'?: string;
56
+ /** Datadog span ID in decimal format for log-trace correlation */
57
+ 'dd.span_id'?: string;
58
+ }
59
+ /**
60
+ * Get current trace context from active span
61
+ *
62
+ * Returns null if no span is active (e.g., outside of trace operation)
63
+ *
64
+ * Includes both OpenTelemetry standard fields (hex) and Datadog-specific
65
+ * fields (decimal) for maximum compatibility.
66
+ *
67
+ * @returns Trace context with traceId, spanId, correlationId, and Datadog decimal IDs, or null
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * import { getTraceContext } from 'autotel/trace-helpers';
72
+ *
73
+ * const context = getTraceContext();
74
+ * if (context) {
75
+ * console.log('Current trace:', context.traceId);
76
+ * // Current trace: 4bf92f3577b34da6a3ce929d0e0e4736
77
+ * console.log('Datadog trace ID:', context['dd.trace_id']);
78
+ * // Datadog trace ID: 12007117331170166582 (decimal for log correlation)
79
+ * }
80
+ * ```
81
+ */
82
+ declare function getTraceContext(): TraceContext | null;
83
+ /**
84
+ * Enrich object with trace context (traceId, spanId, correlationId, and Datadog fields)
85
+ *
86
+ * If no span is active, returns the object unchanged.
87
+ * This prevents "undefined" or "null" values in logs.
88
+ *
89
+ * Automatically adds both OpenTelemetry standard fields (hex) and Datadog-specific
90
+ * fields (decimal) for maximum compatibility with observability backends.
91
+ *
92
+ * @param obj - Object to enrich (e.g., log metadata)
93
+ * @returns Object with trace context merged in, or unchanged if no active span
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * import { enrichWithTraceContext } from 'autotel/trace-helpers';
98
+ *
99
+ * // Inside a trace operation:
100
+ * const enriched = enrichWithTraceContext({ userId: '123' });
101
+ * // {
102
+ * // userId: '123',
103
+ * // traceId: '4bf92f3577b34da6a3ce929d0e0e4736',
104
+ * // spanId: '00f067aa0ba902b7',
105
+ * // correlationId: '4bf92f3577b34da6',
106
+ * // 'dd.trace_id': '12007117331170166582', // Datadog decimal format
107
+ * // 'dd.span_id': '67667974448284583' // Datadog decimal format
108
+ * // }
109
+ *
110
+ * // Outside trace operation:
111
+ * const unchanged = enrichWithTraceContext({ userId: '123' });
112
+ * // { userId: '123' } - no trace fields added
113
+ * ```
114
+ */
115
+ declare function enrichWithTraceContext<T extends Record<string, unknown>>(obj: T): T;
116
+ /**
117
+ * Check if currently in a trace context
118
+ *
119
+ * Useful for conditional logic based on trace presence
120
+ *
121
+ * @returns true if active span exists, false otherwise
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * import { isTracing } from 'autotel/trace-helpers';
126
+ *
127
+ * if (isTracing()) {
128
+ * // Add expensive debug metadata only when tracing
129
+ * logger.debug('Detailed context', expensiveDebugData());
130
+ * }
131
+ * ```
132
+ */
133
+ declare function isTracing(): boolean;
134
+ /**
135
+ * Get a tracer instance for creating custom spans
136
+ *
137
+ * Use this when you need low-level control over span lifecycle.
138
+ * For most use cases, prefer trace(), span(), or instrument() instead.
139
+ *
140
+ * @param name - Tracer name (usually your service or module name)
141
+ * @param version - Optional version string
142
+ * @returns OpenTelemetry Tracer instance
143
+ *
144
+ * @example Basic usage
145
+ * ```typescript
146
+ * import { getTracer } from 'autotel';
147
+ *
148
+ * const tracer = getTracer('my-service');
149
+ * const span = tracer.startSpan('custom.operation');
150
+ * try {
151
+ * // Your logic
152
+ * span.setAttribute('key', 'value');
153
+ * } finally {
154
+ * span.end();
155
+ * }
156
+ * ```
157
+ *
158
+ * @example With AI SDK
159
+ * ```typescript
160
+ * import { getTracer } from 'autotel';
161
+ * import { generateText } from 'ai';
162
+ *
163
+ * const tracer = getTracer('ai-agent');
164
+ * const result = await generateText({
165
+ * model: myModel,
166
+ * prompt: 'Hello',
167
+ * experimental_telemetry: {
168
+ * isEnabled: true,
169
+ * tracer,
170
+ * },
171
+ * });
172
+ * ```
173
+ */
174
+ declare function getTracer(name: string, version?: string): Tracer;
175
+ /**
176
+ * Get the currently active span
177
+ *
178
+ * Returns undefined if no span is currently active.
179
+ * Useful for adding attributes or events to the current span.
180
+ *
181
+ * @returns Active span or undefined
182
+ *
183
+ * @example Adding attributes to active span
184
+ * ```typescript
185
+ * import { getActiveSpan } from 'autotel';
186
+ *
187
+ * const span = getActiveSpan();
188
+ * if (span) {
189
+ * span.setAttribute('user.id', userId);
190
+ * span.addEvent('User action', { action: 'click' });
191
+ * }
192
+ * ```
193
+ *
194
+ * @example Checking span status
195
+ * ```typescript
196
+ * import { getActiveSpan, SpanStatusCode } from 'autotel';
197
+ *
198
+ * const span = getActiveSpan();
199
+ * if (span?.isRecording()) {
200
+ * span.setStatus({ code: SpanStatusCode.OK });
201
+ * }
202
+ * ```
203
+ */
204
+ declare function getActiveSpan(): Span | undefined;
205
+ /**
206
+ * Get the currently active OpenTelemetry context
207
+ *
208
+ * The context contains the active span and any baggage.
209
+ * Useful for context propagation and custom instrumentation.
210
+ *
211
+ * @returns Current active context
212
+ *
213
+ * @example Propagating context
214
+ * ```typescript
215
+ * import { getActiveContext } from 'autotel';
216
+ *
217
+ * const currentContext = getActiveContext();
218
+ * // Pass context to another function or service
219
+ * ```
220
+ *
221
+ * @example With context injection
222
+ * ```typescript
223
+ * import { getActiveContext, injectTraceContext } from 'autotel';
224
+ *
225
+ * const headers = {};
226
+ * injectTraceContext(headers);
227
+ * // Headers now contain trace propagation data
228
+ * ```
229
+ */
230
+ declare function getActiveContext(): Context;
231
+ /**
232
+ * Run a function with a specific span set as active
233
+ *
234
+ * This is a convenience wrapper around the two-step process of
235
+ * setting a span in context and running code within that context.
236
+ *
237
+ * @param span - The span to set as active
238
+ * @param fn - Function to execute with the span active
239
+ * @returns The return value of the function
240
+ *
241
+ * @example Running code with a custom span
242
+ * ```typescript
243
+ * import { getTracer, runWithSpan } from 'autotel';
244
+ *
245
+ * const tracer = getTracer('my-service');
246
+ * const span = tracer.startSpan('background.job');
247
+ *
248
+ * try {
249
+ * const result = await runWithSpan(span, async () => {
250
+ * // Any spans created here will be children of 'background.job'
251
+ * await processData();
252
+ * return { success: true };
253
+ * });
254
+ * console.log(result);
255
+ * } finally {
256
+ * span.end();
257
+ * }
258
+ * ```
259
+ *
260
+ * @example Testing with custom spans
261
+ * ```typescript
262
+ * import { runWithSpan, otelTrace } from 'autotel';
263
+ *
264
+ * const tracer = otelTrace.getTracer('test');
265
+ * const span = tracer.startSpan('test.operation');
266
+ *
267
+ * const result = runWithSpan(span, () => {
268
+ * // Code under test runs with this span as active
269
+ * return myFunction();
270
+ * });
271
+ *
272
+ * span.end();
273
+ * ```
274
+ */
275
+ declare function runWithSpan<T>(span: Span, fn: () => T): T;
276
+ /**
277
+ * Finalize a span with appropriate status and optional error recording
278
+ *
279
+ * This is a convenience function that:
280
+ * - Records exceptions if an error is provided
281
+ * - Sets span status to ERROR if error exists, OK otherwise
282
+ * - Ends the span
283
+ *
284
+ * @param span - The span to finalize
285
+ * @param error - Optional error to record
286
+ *
287
+ * @example Without error (success case)
288
+ * ```typescript
289
+ * import { getTracer, finalizeSpan } from 'autotel';
290
+ *
291
+ * const tracer = getTracer('my-service');
292
+ * const span = tracer.startSpan('operation');
293
+ *
294
+ * try {
295
+ * await doWork();
296
+ * finalizeSpan(span);
297
+ * } catch (error) {
298
+ * finalizeSpan(span, error);
299
+ * throw error;
300
+ * }
301
+ * ```
302
+ *
303
+ * @example With error
304
+ * ```typescript
305
+ * import { getTracer, finalizeSpan } from 'autotel';
306
+ *
307
+ * const tracer = getTracer('my-service');
308
+ * const span = tracer.startSpan('operation');
309
+ *
310
+ * try {
311
+ * await riskyOperation();
312
+ * finalizeSpan(span);
313
+ * } catch (error) {
314
+ * finalizeSpan(span, error); // Records exception and sets ERROR status
315
+ * throw error;
316
+ * }
317
+ * ```
318
+ *
319
+ * @example In instrumentation
320
+ * ```typescript
321
+ * import { getTracer, runWithSpan, finalizeSpan } from 'autotel';
322
+ *
323
+ * function instrumentedQuery(query: string) {
324
+ * const tracer = getTracer('db');
325
+ * const span = tracer.startSpan('db.query');
326
+ *
327
+ * return runWithSpan(span, () => {
328
+ * try {
329
+ * const result = executeQuery(query);
330
+ * finalizeSpan(span);
331
+ * return result;
332
+ * } catch (error) {
333
+ * finalizeSpan(span, error);
334
+ * throw error;
335
+ * }
336
+ * });
337
+ * }
338
+ * ```
339
+ */
340
+ declare function finalizeSpan(span: Span, error?: unknown): void;
341
+ /**
342
+ * Creates a deterministic trace ID from a seed string.
343
+ *
344
+ * Generates a consistent 128-bit trace ID (32 hex characters) from an input seed
345
+ * using SHA-256 hashing. Useful for correlating external system IDs (request IDs,
346
+ * order IDs, session IDs) with OpenTelemetry trace IDs.
347
+ *
348
+ * **Use Cases:**
349
+ * - Correlate external request IDs with traces
350
+ * - Link customer support tickets to trace data
351
+ * - Associate business entities (orders, sessions) with observability data
352
+ * - Debug specific user flows by deterministic trace lookup
353
+ *
354
+ * **Important:** Only use this when you need deterministic trace IDs for correlation.
355
+ * For normal tracing, let OpenTelemetry generate random trace IDs automatically.
356
+ *
357
+ * **Runtime Support:**
358
+ * - Node.js 15+ (native crypto.subtle)
359
+ * - All modern browsers
360
+ * - Edge runtimes (Cloudflare Workers, Deno, etc.)
361
+ *
362
+ * @param seed - Input string to generate trace ID from (e.g., request ID, order ID)
363
+ * @returns Promise resolving to a 32-character hex trace ID (128 bits)
364
+ *
365
+ * @example Correlate external request ID with trace
366
+ * ```typescript
367
+ * import { createDeterministicTraceId } from 'autotel/trace-helpers'
368
+ * import { trace, context } from '@opentelemetry/api'
369
+ *
370
+ * // In middleware or request handler
371
+ * const requestId = req.headers['x-request-id']
372
+ * const traceId = await createDeterministicTraceId(requestId)
373
+ *
374
+ * // Use with manual span creation (advanced - not needed with trace/span functions)
375
+ * const tracer = trace.getTracer('my-service')
376
+ * const spanContext = {
377
+ * traceId,
378
+ * spanId: '0123456789abcdef', // Still random
379
+ * traceFlags: 1
380
+ * }
381
+ * ```
382
+ *
383
+ * @example Link customer support tickets to traces
384
+ * ```typescript
385
+ * import { createDeterministicTraceId } from 'autotel/trace-helpers'
386
+ *
387
+ * // Support dashboard integration
388
+ * const ticketId = 'TICKET-12345'
389
+ * const traceId = await createDeterministicTraceId(ticketId)
390
+ *
391
+ * // Generate direct link to traces in observability backend
392
+ * const traceUrl = `https://your-otel-backend.com/traces/${traceId}`
393
+ * console.log(`View related traces: ${traceUrl}`)
394
+ * ```
395
+ *
396
+ * @example Session-based correlation
397
+ * ```typescript
398
+ * import { createDeterministicTraceId } from 'autotel/trace-helpers'
399
+ *
400
+ * // Track all operations for a user session
401
+ * const sessionId = req.session.id
402
+ * const traceId = await createDeterministicTraceId(sessionId)
403
+ *
404
+ * // All operations in this session share the same trace ID
405
+ * // Makes it easy to find all activity for a specific session
406
+ * ```
407
+ *
408
+ * @public
409
+ */
410
+ declare function createDeterministicTraceId(seed: string): Promise<string>;
411
+ /**
412
+ * Flattens nested metadata objects into dot-notation span attributes.
413
+ *
414
+ * Converts complex nested objects into flat key-value pairs suitable for
415
+ * OpenTelemetry span attributes. Non-string values are JSON serialized.
416
+ * Handles serialization failures gracefully with a fallback value.
417
+ *
418
+ * **Use Cases:**
419
+ * - Structured metadata with nested objects
420
+ * - User context with multiple properties
421
+ * - Request/response metadata
422
+ * - Business entity attributes
423
+ *
424
+ * **Note:** Filters out null/undefined values automatically to keep spans clean.
425
+ *
426
+ * @param metadata - Nested metadata object to flatten
427
+ * @param prefix - Prefix for all attribute keys (default: 'metadata')
428
+ * @returns Flattened attributes as { [key: string]: string }
429
+ *
430
+ * @example Basic metadata flattening
431
+ * ```typescript
432
+ * import { flattenMetadata } from 'autotel/trace-helpers'
433
+ * import { trace } from 'autotel'
434
+ *
435
+ * export const processOrder = trace(ctx => async (orderId: string) => {
436
+ * const order = await getOrder(orderId)
437
+ *
438
+ * // Flatten complex order metadata
439
+ * const flattened = flattenMetadata({
440
+ * user: { id: order.userId, tier: 'premium' },
441
+ * payment: { method: 'card', processor: 'stripe' },
442
+ * items: order.items.length
443
+ * })
444
+ *
445
+ * ctx.setAttributes(flattened)
446
+ * // Results in:
447
+ * // {
448
+ * // 'metadata.user.id': 'user-123',
449
+ * // 'metadata.user.tier': 'premium',
450
+ * // 'metadata.payment.method': 'card',
451
+ * // 'metadata.payment.processor': 'stripe',
452
+ * // 'metadata.items': '5'
453
+ * // }
454
+ * })
455
+ * ```
456
+ *
457
+ * @example Custom prefix for semantic conventions
458
+ * ```typescript
459
+ * import { flattenMetadata } from 'autotel/trace-helpers'
460
+ * import { trace } from 'autotel'
461
+ *
462
+ * export const fetchUser = trace(ctx => async (userId: string) => {
463
+ * const user = await db.users.findOne({ id: userId })
464
+ *
465
+ * // Use semantic convention prefix
466
+ * const userAttrs = flattenMetadata(
467
+ * {
468
+ * id: user.id,
469
+ * email: user.email,
470
+ * plan: user.subscription.plan
471
+ * },
472
+ * 'user' // Custom prefix
473
+ * )
474
+ *
475
+ * ctx.setAttributes(userAttrs)
476
+ * // Results in:
477
+ * // {
478
+ * // 'user.id': 'user-123',
479
+ * // 'user.email': 'user@example.com',
480
+ * // 'user.plan': 'enterprise'
481
+ * // }
482
+ * })
483
+ * ```
484
+ *
485
+ * @example With complex objects (auto-serialized)
486
+ * ```typescript
487
+ * import { flattenMetadata } from 'autotel/trace-helpers'
488
+ * import { trace } from 'autotel'
489
+ *
490
+ * export const analyzeRequest = trace(ctx => async (req: Request) => {
491
+ * const metadata = flattenMetadata({
492
+ * headers: req.headers, // Object - will be JSON serialized
493
+ * query: req.query, // Object - will be JSON serialized
494
+ * timestamp: new Date() // Non-string - will be JSON serialized
495
+ * })
496
+ *
497
+ * ctx.setAttributes(metadata)
498
+ * // Results in:
499
+ * // {
500
+ * // 'metadata.headers': '{"accept":"application/json",...}',
501
+ * // 'metadata.query': '{"page":"1","limit":"10"}',
502
+ * // 'metadata.timestamp': '"2024-01-15T12:00:00.000Z"'
503
+ * // }
504
+ * })
505
+ * ```
506
+ *
507
+ * @example Error handling
508
+ * ```typescript
509
+ * import { flattenMetadata } from 'autotel/trace-helpers'
510
+ *
511
+ * // Objects with circular references are handled gracefully
512
+ * const circular: any = { a: 1 }
513
+ * circular.self = circular
514
+ *
515
+ * const flattened = flattenMetadata({ data: circular })
516
+ * // Results in:
517
+ * // { 'metadata.data': '<serialization-failed>' }
518
+ * ```
519
+ *
520
+ * @public
521
+ */
522
+ declare function flattenMetadata(metadata: Record<string, unknown>, prefix?: string): Record<string, string>;
523
+
524
+ export { type TraceContext, createDeterministicTraceId, enrichWithTraceContext, finalizeSpan, flattenMetadata, getActiveContext, getActiveSpan, getTraceContext, getTracer, isTracing, runWithSpan, setSpanName };