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,478 @@
1
+ import { Span } from '@opentelemetry/api';
2
+ import { Sampler } from './sampling.cjs';
3
+ import { T as TraceContext } from './trace-context-DRZdUvVY.cjs';
4
+ import './logger.cjs';
5
+ import 'pino';
6
+
7
+ /**
8
+ * Functional API for non-class code
9
+ *
10
+ * Three approaches for different use cases:
11
+ * 1. trace() - Zero-ceremony HOF for single functions
12
+ * 2. withTracing() - Middleware-style composable wrapper
13
+ * 3. instrument() - Batch auto-instrumentation for modules
14
+ *
15
+ * @example trace() - Single function
16
+ * ```typescript
17
+ * export const createUser = trace(ctx => async (data) => {
18
+ * ctx.setAttribute('user.id', data.id)
19
+ * return await db.users.create(data)
20
+ * })
21
+ * ```
22
+ *
23
+ * @example withTracing() - Composable middleware
24
+ * ```typescript
25
+ * export const createUser = withTracing({
26
+ * name: 'user.create'
27
+ * })(ctx => async (data) => {
28
+ * ctx.setAttribute('user.id', data.id)
29
+ * return await db.users.create(data)
30
+ * })
31
+ * ```
32
+ *
33
+ * @example instrument() - Batch instrumentation
34
+ * ```typescript
35
+ * export default instrument({
36
+ * createUser: async (data) => { },
37
+ * updateUser: async (id, data) => { }
38
+ * }, { serviceName: 'user' })
39
+ * ```
40
+ */
41
+
42
+ /**
43
+ * Helper type to extract function signature from factory pattern
44
+ * This helps TypeScript infer types correctly for factory functions
45
+ */
46
+ type ExtractFunctionSignature<T> = T extends (ctx: TraceContext) => infer F ? F extends (...args: infer Args) => infer Return ? (...args: Args) => Return : never : never;
47
+ /**
48
+ * Helper type to exclude functions that return functions from immediate execution overloads
49
+ */
50
+ type ExcludeFactoryReturn<T> = T extends (ctx: TraceContext) => infer F ? F extends (...args: any[]) => any ? never : T : T;
51
+ /**
52
+ * Common options for functional tracing
53
+ */
54
+ interface TracingOptions<TArgs extends unknown[] = unknown[], TReturn = unknown> {
55
+ /**
56
+ * Span name (highest priority)
57
+ * If provided, this is used as the span name
58
+ */
59
+ name?: string;
60
+ /**
61
+ * Service name (used to compose final span name)
62
+ * If name not provided, span name becomes: ${serviceName}.${functionName}
63
+ */
64
+ serviceName?: string;
65
+ /**
66
+ * Sampling strategy
67
+ * @default AlwaysSampler
68
+ */
69
+ sampler?: Sampler;
70
+ /**
71
+ * Enable metrics collection (counter, histogram)
72
+ * @default false
73
+ */
74
+ withMetrics?: boolean;
75
+ /**
76
+ * Extract attributes from function arguments
77
+ */
78
+ attributesFromArgs?: (args: TArgs) => Record<string, unknown>;
79
+ /**
80
+ * Extract attributes from function result
81
+ */
82
+ attributesFromResult?: (result: TReturn) => Record<string, unknown>;
83
+ /**
84
+ * Start a new root span instead of creating a child
85
+ * Useful for serverless entry points
86
+ * @default false
87
+ */
88
+ startNewRoot?: boolean;
89
+ /**
90
+ * Automatically flush events queue when span ends
91
+ * Only flushes on root spans (to avoid excessive flushing)
92
+ * @default true
93
+ */
94
+ autoFlushEvents?: boolean;
95
+ }
96
+ /**
97
+ * Options for instrument() batch instrumentation
98
+ */
99
+ interface InstrumentOptions<T extends Record<string, InstrumentableFunction> = Record<string, InstrumentableFunction>> extends TracingOptions {
100
+ /** Functions to instrument */
101
+ functions: T;
102
+ /**
103
+ * Per-function configuration overrides
104
+ */
105
+ overrides?: Record<string, Partial<TracingOptions>>;
106
+ /**
107
+ * Functions to skip (won't be instrumented)
108
+ * Supports:
109
+ * - String keys: 'functionName'
110
+ * - RegExp: /^_internal/
111
+ * - Predicate: (key, fn) => boolean
112
+ *
113
+ * By default, functions starting with _ are skipped
114
+ */
115
+ skip?: (string | RegExp | ((key: string, fn: Function) => boolean))[];
116
+ }
117
+ type InstrumentableFunction<TArgs extends unknown[] = unknown[], TReturn = unknown> = ((...args: TArgs) => TReturn | Promise<TReturn>) & {
118
+ displayName?: string;
119
+ name?: string;
120
+ };
121
+ /**
122
+ * Context object that lazily evaluates the active span on property access
123
+ *
124
+ * Access trace context directly without function call syntax.
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * import { trace, ctx } from 'autotel'
129
+ *
130
+ * export const createUser = trace(async (data) => {
131
+ * // Direct property access - no function call!
132
+ * if (ctx.traceId) {
133
+ * ctx.setAttribute('user.id', data.id)
134
+ * console.log('Trace:', ctx.traceId)
135
+ * }
136
+ * })
137
+ * ```
138
+ */
139
+ declare const ctx: TraceContext;
140
+ /**
141
+ * Approach 1: trace() - Zero-ceremony HOF
142
+ *
143
+ * Wrap a single function with automatic tracing.
144
+ * The function receives a context object as the first parameter.
145
+ *
146
+ * Supports two patterns:
147
+ * 1. **Factory pattern** - Returns a traced function: `trace(ctx => (...args) => result)`
148
+ * 2. **Immediate execution** - Executes immediately with tracing: `trace(ctx => result)`
149
+ *
150
+ * @example Auto-inferred name - Plain function
151
+ * ```typescript
152
+ * export const createUser = trace(async (data) => {
153
+ * return await db.users.create(data)
154
+ * })
155
+ * // → Traced as "createUser"
156
+ * ```
157
+ *
158
+ * @example Auto-inferred name - Factory pattern (with ctx access)
159
+ * ```typescript
160
+ * export const createUser = trace(ctx => async (data) => {
161
+ * ctx.setAttribute('user.id', data.id)
162
+ * return await db.users.create(data)
163
+ * })
164
+ * // → Traced as "createUser", returns wrapped function
165
+ * ```
166
+ *
167
+ * @example Immediate execution - Execute once with tracing
168
+ * ```typescript
169
+ * // Wraps an existing function and executes immediately
170
+ * function timed<T>(fn: () => Promise<T>): Promise<T> {
171
+ * return trace(async (ctx) => {
172
+ * ctx.setAttribute('operation', 'timed')
173
+ * return await fn()
174
+ * })
175
+ * }
176
+ * // → Executes immediately, returns result directly
177
+ * ```
178
+ *
179
+ * @example Custom name - Plain function
180
+ * ```typescript
181
+ * export const createUser = trace('user.create', async (data) => {
182
+ * return await db.users.create(data)
183
+ * })
184
+ * // → Traced as "user.create"
185
+ * ```
186
+ *
187
+ * @example Custom name - Factory pattern
188
+ * ```typescript
189
+ * export const createUser = trace('user.create', ctx => async (data) => {
190
+ * ctx.setAttribute('user.id', data.id)
191
+ * return await db.users.create(data)
192
+ * })
193
+ * // → Traced as "user.create"
194
+ * ```
195
+ *
196
+ * @example Custom name - Immediate execution
197
+ * ```typescript
198
+ * const result = trace('fetch.user', async (ctx) => {
199
+ * ctx.setAttribute('userId', '123')
200
+ * return await fetchUser('123')
201
+ * })
202
+ * // → Executes immediately with span name "fetch.user"
203
+ * ```
204
+ *
205
+ * @example Full options - Plain function
206
+ * ```typescript
207
+ * export const createUser = trace({
208
+ * name: 'user.create',
209
+ * sampler: new AdaptiveSampler(),
210
+ * withMetrics: true
211
+ * }, async (data) => {
212
+ * return await db.users.create(data)
213
+ * })
214
+ * ```
215
+ *
216
+ * @example Full options - Factory pattern
217
+ * ```typescript
218
+ * export const createUser = trace({
219
+ * name: 'user.create',
220
+ * sampler: new AdaptiveSampler(),
221
+ * withMetrics: true
222
+ * }, ctx => async (data) => {
223
+ * ctx.setAttribute('user.id', data.id)
224
+ * return await db.users.create(data)
225
+ * })
226
+ * ```
227
+ */
228
+ declare function trace<TBaggage extends Record<string, unknown> | undefined = undefined, TReturn = unknown>(fn: (ctx: TraceContext<TBaggage>) => TReturn): TReturn;
229
+ declare function trace<TBaggage extends Record<string, unknown> | undefined = undefined>(fnFactory: (ctx: TraceContext<TBaggage>) => () => unknown): () => unknown;
230
+ declare function trace<TBaggage extends Record<string, unknown> | undefined = undefined, TArgs extends unknown[] = unknown[], TReturn = unknown>(fnFactory: (ctx: TraceContext<TBaggage>) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
231
+ declare function trace<TReturn = unknown>(fnFactory: (ctx: TraceContext) => () => TReturn): () => TReturn;
232
+ declare function trace<TFactory extends (ctx: TraceContext) => (...args: unknown[]) => unknown>(fnFactory: TFactory): ExtractFunctionSignature<TFactory>;
233
+ declare function trace<TArgs extends unknown[], TReturn = unknown>(fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
234
+ declare function trace<TReturn = unknown>(fn: () => TReturn): () => TReturn;
235
+ declare function trace<TArgs extends unknown[], TReturn = unknown>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
236
+ declare function trace<TReturn = unknown>(name: string, fn: ExcludeFactoryReturn<(ctx: TraceContext) => TReturn>): TReturn;
237
+ declare function trace(name: string, fnFactory: (ctx: TraceContext) => () => unknown): () => unknown;
238
+ declare function trace<TArgs extends unknown[], TReturn>(name: string, fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
239
+ declare function trace<TFactory extends (ctx: TraceContext) => (...args: unknown[]) => unknown>(name: string, fnFactory: TFactory): ExtractFunctionSignature<TFactory>;
240
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(name: string, fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
241
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(name: string, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
242
+ declare function trace<TReturn = unknown>(options: TracingOptions<[], TReturn>, fn: (ctx: TraceContext) => TReturn): TReturn;
243
+ declare function trace(options: TracingOptions, fnFactory: (ctx: TraceContext) => () => unknown): () => unknown;
244
+ declare function trace<TArgs extends unknown[], TReturn>(options: TracingOptions<TArgs, TReturn>, fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
245
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(options: TracingOptions<TArgs, TReturn>, fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
246
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(options: TracingOptions<TArgs, TReturn>, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
247
+ declare function trace<TReturn = unknown>(fn: (ctx: TraceContext) => Promise<TReturn>): Promise<TReturn>;
248
+ declare function trace(fnFactory: (ctx: TraceContext) => () => Promise<unknown>): () => Promise<unknown>;
249
+ declare function trace<TArgs extends unknown[], TReturn>(fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
250
+ declare function trace<TReturn = unknown>(fnFactory: (ctx: TraceContext) => () => Promise<TReturn>): () => Promise<TReturn>;
251
+ declare function trace<TFactory extends (ctx: TraceContext) => (...args: unknown[]) => Promise<unknown>>(fnFactory: TFactory): ExtractFunctionSignature<TFactory>;
252
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
253
+ declare function trace<TReturn = unknown>(fn: () => Promise<TReturn>): () => Promise<TReturn>;
254
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(fn: (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
255
+ declare function trace<TReturn = unknown>(name: string, fn: ExcludeFactoryReturn<(ctx: TraceContext) => Promise<TReturn>>): Promise<TReturn>;
256
+ declare function trace(name: string, fnFactory: (ctx: TraceContext) => () => Promise<unknown>): () => Promise<unknown>;
257
+ declare function trace<TArgs extends unknown[], TReturn>(name: string, fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
258
+ declare function trace<TFactory extends (ctx: TraceContext) => (...args: unknown[]) => Promise<unknown>>(name: string, fnFactory: TFactory): ExtractFunctionSignature<TFactory>;
259
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(name: string, fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
260
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(name: string, fn: (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
261
+ declare function trace<TReturn = unknown>(options: TracingOptions<[], TReturn>, fn: (ctx: TraceContext) => Promise<TReturn>): Promise<TReturn>;
262
+ declare function trace(options: TracingOptions, fnFactory: (ctx: TraceContext) => () => Promise<unknown>): () => Promise<unknown>;
263
+ declare function trace<TArgs extends unknown[], TReturn>(options: TracingOptions<TArgs, TReturn>, fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
264
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(options: TracingOptions<TArgs, TReturn>, fnFactory: (ctx: TraceContext) => (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
265
+ declare function trace<TArgs extends unknown[] = unknown[], TReturn = unknown>(options: TracingOptions<TArgs, TReturn>, fn: (...args: TArgs) => Promise<TReturn>): (...args: TArgs) => Promise<TReturn>;
266
+ /**
267
+ * Approach 2: withTracing() - Middleware-style composable wrapper
268
+ *
269
+ * Returns a HOF that wraps functions with tracing.
270
+ * Perfect for composition and reusable configuration.
271
+ *
272
+ * @example Standard usage
273
+ * ```typescript
274
+ * export const createUser = withTracing({
275
+ * name: 'user.create'
276
+ * })(ctx => async (data) => {
277
+ * ctx.setAttribute('user.id', data.id)
278
+ * return await db.users.create(data)
279
+ * })
280
+ * ```
281
+ *
282
+ * @example Composable
283
+ * ```typescript
284
+ * const trace = withTracing({ serviceName: 'user' })
285
+ *
286
+ * export const createUser = trace(ctx => async (data) => { })
287
+ * export const updateUser = trace(ctx => async (id, data) => { })
288
+ * ```
289
+ *
290
+ * @example With other middleware
291
+ * ```typescript
292
+ * export const createUser = compose(
293
+ * withAuth({ role: 'admin' }),
294
+ * withTracing({ name: 'user.create' }),
295
+ * withRateLimit({ max: 100 })
296
+ * )(ctx => async (data) => { })
297
+ * ```
298
+ */
299
+ declare function withTracing<TArgs extends unknown[] = unknown[], TReturn = unknown>(options?: TracingOptions<TArgs, TReturn>): (fnFactory: (ctx: TraceContext) => (...args: TArgs) => TReturn | Promise<TReturn>) => (...args: TArgs) => TReturn | Promise<TReturn>;
300
+ /**
301
+ * Approach 3: instrument() - Batch auto-instrumentation
302
+ *
303
+ * Instrument an entire module/object at once.
304
+ * Closest to @Instrumented decorator pattern.
305
+ *
306
+ * @example Basic usage
307
+ * ```typescript
308
+ * export default instrument({
309
+ * functions: {
310
+ * createUser: async (data) => { },
311
+ * updateUser: async (id, data) => { },
312
+ * deleteUser: async (id) => { }
313
+ * },
314
+ * serviceName: 'user',
315
+ * sampler: new AdaptiveSampler()
316
+ * })
317
+ * // → Traced as "user.createUser", "user.updateUser", "user.deleteUser"
318
+ * ```
319
+ *
320
+ * @example Per-function overrides
321
+ * ```typescript
322
+ * export default instrument({
323
+ * functions: {
324
+ * createUser: async (data) => { },
325
+ * deleteUser: async (id) => { }
326
+ * },
327
+ * serviceName: 'user',
328
+ * overrides: {
329
+ * deleteUser: {
330
+ * sampler: new AlwaysSampler(),
331
+ * withMetrics: true
332
+ * }
333
+ * }
334
+ * })
335
+ * ```
336
+ *
337
+ * @example Skip functions
338
+ * ```typescript
339
+ * export default instrument({
340
+ * functions: {
341
+ * createUser: async (data) => { },
342
+ * _internal: async () => { }, // Auto-skipped (_-prefix)
343
+ * deleteUser: async (id) => { }
344
+ * },
345
+ * serviceName: 'user',
346
+ * skip: [/^test/, (key) => key.includes('debug')]
347
+ * })
348
+ * ```
349
+ */
350
+ declare function instrument<T extends Record<string, InstrumentableFunction>>(options: InstrumentOptions<T>): T;
351
+ /**
352
+ * Options for span() function
353
+ */
354
+ interface SpanOptions {
355
+ /** Span name */
356
+ name: string;
357
+ /** Attributes to set on the span */
358
+ attributes?: Record<string, string | number | boolean>;
359
+ }
360
+ /**
361
+ * Execute a function within a named span
362
+ *
363
+ * Useful for adding tracing to specific code blocks without wrapping
364
+ * the entire function. Supports both synchronous and asynchronous functions.
365
+ *
366
+ * @example
367
+ * ```typescript
368
+ * // Async function
369
+ * async function processOrder(order: Order) {
370
+ * await span({
371
+ * name: 'payment.charge',
372
+ * attributes: { amount: order.total }
373
+ * }, async (span) => {
374
+ * await chargeCustomer(order);
375
+ * })
376
+ * }
377
+ *
378
+ * // Sync function
379
+ * function calculateTotal(items: Item[]) {
380
+ * return span({
381
+ * name: 'calculateTotal',
382
+ * attributes: { itemCount: items.length }
383
+ * }, (span) => {
384
+ * return items.reduce((sum, item) => sum + item.price, 0);
385
+ * })
386
+ * }
387
+ * ```
388
+ */
389
+ declare function span<T = unknown>(options: SpanOptions, fn: (span: Span) => T): T;
390
+ declare function span<T = unknown>(options: SpanOptions, fn: (span: Span) => Promise<T>): Promise<T>;
391
+ /**
392
+ * Options for withNewContext() function
393
+ */
394
+ interface WithNewContextOptions<T = unknown> {
395
+ /** Function to execute in new root context */
396
+ fn: () => Promise<T>;
397
+ }
398
+ /**
399
+ * Execute a function in a new root context (prevents span propagation)
400
+ *
401
+ * Useful when you want to start a completely new trace without
402
+ * parent-child relationships.
403
+ *
404
+ * @example
405
+ * ```typescript
406
+ * async function handleWebhook(payload: WebhookPayload) {
407
+ * // This creates a new root trace, not connected to the HTTP request trace
408
+ * await withNewContext({
409
+ * fn: async () => {
410
+ * await trace(ctx => async () => {
411
+ * await processWebhookPayload(payload)
412
+ * })()
413
+ * }
414
+ * })
415
+ * }
416
+ * ```
417
+ */
418
+ declare function withNewContext<T = unknown>(options: WithNewContextOptions<T>): Promise<T>;
419
+ /**
420
+ * Options for withBaggage() function
421
+ */
422
+ interface WithBaggageOptions<T = unknown> {
423
+ /** Baggage entries to set (key-value pairs) */
424
+ baggage: Record<string, string>;
425
+ /** Function to execute with the updated baggage */
426
+ fn: () => T | Promise<T>;
427
+ }
428
+ /**
429
+ * Execute a function with updated baggage entries
430
+ *
431
+ * Baggage is immutable in OpenTelemetry, so this helper creates a new context
432
+ * with the specified baggage entries and runs the function within that context.
433
+ * All child spans created within the function will inherit the baggage.
434
+ *
435
+ * @example Setting baggage for downstream services
436
+ * ```typescript
437
+ * import { trace, withBaggage } from 'autotel';
438
+ *
439
+ * export const createOrder = trace((ctx) => async (order: Order) => {
440
+ * // Set baggage that will be propagated to downstream HTTP calls
441
+ * return await withBaggage({
442
+ * baggage: {
443
+ * 'tenant.id': order.tenantId,
444
+ * 'user.id': order.userId,
445
+ * },
446
+ * fn: async () => {
447
+ * // This HTTP call will include the baggage in headers
448
+ * await fetch('/api/charge', {
449
+ * method: 'POST',
450
+ * body: JSON.stringify(order),
451
+ * });
452
+ * },
453
+ * });
454
+ * });
455
+ * ```
456
+ *
457
+ * @example Using with existing baggage
458
+ * ```typescript
459
+ * export const processOrder = trace((ctx) => async (order: Order) => {
460
+ * // Read existing baggage
461
+ * const tenantId = ctx.getBaggage('tenant.id');
462
+ *
463
+ * // Add additional baggage entries
464
+ * return await withBaggage({
465
+ * baggage: {
466
+ * 'order.id': order.id,
467
+ * 'order.amount': String(order.amount),
468
+ * },
469
+ * fn: async () => {
470
+ * await charge(order);
471
+ * },
472
+ * });
473
+ * });
474
+ * ```
475
+ */
476
+ declare function withBaggage<T = unknown>(options: WithBaggageOptions<T>): T | Promise<T>;
477
+
478
+ export { type InstrumentOptions, type SpanOptions, TraceContext, type TracingOptions, type WithBaggageOptions, type WithNewContextOptions, ctx, instrument, span, trace, withBaggage, withNewContext, withTracing };