autotel 4.0.0 → 4.2.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 (232) hide show
  1. package/README.md +26 -1
  2. package/dist/auto.cjs +2 -2
  3. package/dist/auto.js +1 -1
  4. package/dist/correlation-id.cjs +1 -1
  5. package/dist/correlation-id.js +1 -1
  6. package/dist/decorators.cjs +1 -1
  7. package/dist/decorators.js +1 -1
  8. package/dist/{event-Dlqr4ZNL.cjs → event-BhHREDJk.cjs} +3 -3
  9. package/dist/{event-Dlqr4ZNL.cjs.map → event-BhHREDJk.cjs.map} +1 -1
  10. package/dist/{event-_58ryBjh.js → event-ByBTV9M2.js} +3 -3
  11. package/dist/{event-_58ryBjh.js.map → event-ByBTV9M2.js.map} +1 -1
  12. package/dist/event.cjs +1 -1
  13. package/dist/event.js +1 -1
  14. package/dist/{functional-BGkT8J-h.js → functional-DtI0u4vx.js} +19 -19
  15. package/dist/functional-DtI0u4vx.js.map +1 -0
  16. package/dist/{functional-C4CzoVrX.cjs → functional-zpzNLhky.cjs} +4 -4
  17. package/dist/{functional-C4CzoVrX.cjs.map → functional-zpzNLhky.cjs.map} +1 -1
  18. package/dist/functional.cjs +1 -1
  19. package/dist/functional.js +1 -1
  20. package/dist/http.cjs +1 -1
  21. package/dist/http.js +1 -1
  22. package/dist/index.cjs +5 -5
  23. package/dist/index.d.cts +1 -1
  24. package/dist/index.d.ts +1 -1
  25. package/dist/index.js +5 -5
  26. package/dist/{init-DJQOdVlN.d.ts → init-B7u-DjxM.d.ts} +57 -2
  27. package/dist/init-B7u-DjxM.d.ts.map +1 -0
  28. package/dist/{init-DvapOXCc.cjs → init-BX7AmFRl.cjs} +40 -21
  29. package/dist/init-BX7AmFRl.cjs.map +1 -0
  30. package/dist/{init-Ch6t7MNI.js → init-D-jnNMix.js} +39 -20
  31. package/dist/init-D-jnNMix.js.map +1 -0
  32. package/dist/{init-CNp-ee80.d.cts → init-DSrRmVnz.d.cts} +57 -2
  33. package/dist/init-DSrRmVnz.d.cts.map +1 -0
  34. package/dist/instrumentation.cjs +1 -1
  35. package/dist/instrumentation.js +1 -1
  36. package/dist/logger-D3Ej3DII.js +446 -0
  37. package/dist/logger-D3Ej3DII.js.map +1 -0
  38. package/dist/logger-thMPLpOG.cjs +487 -0
  39. package/dist/logger-thMPLpOG.cjs.map +1 -0
  40. package/dist/logger.cjs +8 -236
  41. package/dist/logger.js +2 -204
  42. package/dist/messaging.cjs +1 -1
  43. package/dist/messaging.js +1 -1
  44. package/dist/semantic-helpers.cjs +1 -1
  45. package/dist/semantic-helpers.js +1 -1
  46. package/dist/{track-3HY4NGV-.cjs → track-D59FfpL0.cjs} +2 -2
  47. package/dist/{track-3HY4NGV-.cjs.map → track-D59FfpL0.cjs.map} +1 -1
  48. package/dist/{track-nsKVy-pj.js → track-wc0HafS_.js} +6 -6
  49. package/dist/track-wc0HafS_.js.map +1 -0
  50. package/dist/webhook.cjs +1 -1
  51. package/dist/webhook.js +1 -1
  52. package/dist/workflow-distributed.cjs +1 -1
  53. package/dist/workflow-distributed.js +1 -1
  54. package/dist/workflow.cjs +1 -1
  55. package/dist/workflow.js +1 -1
  56. package/dist/{yaml-config-B3dQ82GR.cjs → yaml-config-Ck2uB0Dp.cjs} +2 -1
  57. package/dist/yaml-config-Ck2uB0Dp.cjs.map +1 -0
  58. package/dist/yaml-config.cjs +1 -1
  59. package/dist/yaml-config.d.cts +7 -1
  60. package/dist/yaml-config.d.cts.map +1 -1
  61. package/dist/yaml-config.d.ts +7 -1
  62. package/dist/yaml-config.d.ts.map +1 -1
  63. package/dist/yaml-config.js +1 -0
  64. package/dist/yaml-config.js.map +1 -1
  65. package/package.json +1 -2
  66. package/skills/autotel-core/SKILL.md +2 -0
  67. package/skills/autotel-instrumentation/SKILL.md +25 -0
  68. package/skills/debug-missing-spans/SKILL.md +3 -1
  69. package/skills/migrate-to-autotel/SKILL.md +24 -23
  70. package/skills/review-otel-patterns/SKILL.md +5 -4
  71. package/dist/functional-BGkT8J-h.js.map +0 -1
  72. package/dist/init-CNp-ee80.d.cts.map +0 -1
  73. package/dist/init-Ch6t7MNI.js.map +0 -1
  74. package/dist/init-DJQOdVlN.d.ts.map +0 -1
  75. package/dist/init-DvapOXCc.cjs.map +0 -1
  76. package/dist/logger.cjs.map +0 -1
  77. package/dist/logger.js.map +0 -1
  78. package/dist/track-nsKVy-pj.js.map +0 -1
  79. package/dist/yaml-config-B3dQ82GR.cjs.map +0 -1
  80. package/src/attribute-redacting-processor.test.ts +0 -763
  81. package/src/attribute-redacting-processor.ts +0 -621
  82. package/src/attributes/attachers.ts +0 -161
  83. package/src/attributes/builders.ts +0 -529
  84. package/src/attributes/domains.ts +0 -42
  85. package/src/attributes/index.ts +0 -81
  86. package/src/attributes/registry.ts +0 -323
  87. package/src/attributes/types.ts +0 -211
  88. package/src/attributes/utils.ts +0 -64
  89. package/src/attributes/validators.ts +0 -266
  90. package/src/attributes.test.ts +0 -292
  91. package/src/auto.ts +0 -67
  92. package/src/autotel-logger.test.ts +0 -548
  93. package/src/autotel-logger.ts +0 -364
  94. package/src/baggage-span-processor.test.ts +0 -202
  95. package/src/baggage-span-processor.ts +0 -100
  96. package/src/business-baggage.test.ts +0 -500
  97. package/src/business-baggage.ts +0 -669
  98. package/src/circuit-breaker.test.ts +0 -341
  99. package/src/circuit-breaker.ts +0 -184
  100. package/src/config.test.ts +0 -94
  101. package/src/config.ts +0 -172
  102. package/src/correlated-events.test.ts +0 -151
  103. package/src/correlated-events.ts +0 -47
  104. package/src/correlation-id.test.ts +0 -163
  105. package/src/correlation-id.ts +0 -206
  106. package/src/db.test.ts +0 -252
  107. package/src/db.ts +0 -447
  108. package/src/decorators.test.ts +0 -153
  109. package/src/decorators.ts +0 -188
  110. package/src/define-event.test.ts +0 -41
  111. package/src/define-event.ts +0 -58
  112. package/src/devtools.ts +0 -60
  113. package/src/drain-pipeline.test.ts +0 -68
  114. package/src/drain-pipeline.ts +0 -199
  115. package/src/drain-toolkit.test.ts +0 -113
  116. package/src/drain-toolkit.ts +0 -129
  117. package/src/enricher-toolkit.test.ts +0 -67
  118. package/src/enricher-toolkit.ts +0 -79
  119. package/src/enrichers.test.ts +0 -150
  120. package/src/enrichers.ts +0 -145
  121. package/src/env-config.test.ts +0 -323
  122. package/src/env-config.ts +0 -309
  123. package/src/error-catalog.test.ts +0 -133
  124. package/src/error-catalog.ts +0 -262
  125. package/src/event-queue.test.ts +0 -864
  126. package/src/event-queue.ts +0 -699
  127. package/src/event-subscriber.ts +0 -262
  128. package/src/event-testing.ts +0 -197
  129. package/src/event.test.ts +0 -1104
  130. package/src/event.ts +0 -988
  131. package/src/events-config.ts +0 -235
  132. package/src/exporters.ts +0 -165
  133. package/src/filtering-span-processor.test.ts +0 -281
  134. package/src/filtering-span-processor.ts +0 -111
  135. package/src/flatten-attributes.test.ts +0 -76
  136. package/src/flatten-attributes.ts +0 -80
  137. package/src/functional.strict-types.typecheck.ts +0 -53
  138. package/src/functional.test.ts +0 -1464
  139. package/src/functional.ts +0 -2539
  140. package/src/functional.types.test.ts +0 -135
  141. package/src/hook.mjs +0 -15
  142. package/src/http.test.ts +0 -485
  143. package/src/http.ts +0 -424
  144. package/src/index.ts +0 -433
  145. package/src/init-auto-redactor.test.ts +0 -53
  146. package/src/init-redactor.test.ts +0 -8
  147. package/src/init.customization.test.ts +0 -594
  148. package/src/init.integrations.test.ts +0 -399
  149. package/src/init.openllmetry.test.ts +0 -194
  150. package/src/init.protocol.test.ts +0 -215
  151. package/src/init.ts +0 -2312
  152. package/src/instrumentation.test.ts +0 -108
  153. package/src/instrumentation.ts +0 -319
  154. package/src/logger.test.ts +0 -125
  155. package/src/logger.ts +0 -341
  156. package/src/messaging-adapters.test.ts +0 -595
  157. package/src/messaging-adapters.ts +0 -583
  158. package/src/messaging-testing.test.ts +0 -573
  159. package/src/messaging-testing.ts +0 -935
  160. package/src/messaging.test.ts +0 -1646
  161. package/src/messaging.ts +0 -2245
  162. package/src/metric-helpers.ts +0 -47
  163. package/src/metric-testing.ts +0 -197
  164. package/src/metric.ts +0 -446
  165. package/src/metrics.test.ts +0 -241
  166. package/src/node-require.ts +0 -123
  167. package/src/operation-context.ts +0 -93
  168. package/src/parse-error.test.ts +0 -73
  169. package/src/parse-error.ts +0 -112
  170. package/src/posthog-logs.test.ts +0 -115
  171. package/src/posthog-logs.ts +0 -77
  172. package/src/pretty-console-exporter.test.ts +0 -545
  173. package/src/pretty-console-exporter.ts +0 -413
  174. package/src/pretty-log-formatter.test.ts +0 -123
  175. package/src/pretty-log-formatter.ts +0 -210
  176. package/src/processors/canonical-log-line-processor.test.ts +0 -523
  177. package/src/processors/canonical-log-line-processor.ts +0 -396
  178. package/src/processors.ts +0 -152
  179. package/src/rate-limiter.test.ts +0 -199
  180. package/src/rate-limiter.ts +0 -98
  181. package/src/redact-values.test.ts +0 -90
  182. package/src/redact-values.ts +0 -34
  183. package/src/register.ts +0 -37
  184. package/src/request-logger.test.ts +0 -545
  185. package/src/request-logger.ts +0 -342
  186. package/src/sampling.test.ts +0 -1060
  187. package/src/sampling.ts +0 -737
  188. package/src/security-schema.test.ts +0 -45
  189. package/src/security-schema.ts +0 -107
  190. package/src/semantic-conventions.ts +0 -15
  191. package/src/semantic-helpers.test.ts +0 -226
  192. package/src/semantic-helpers.ts +0 -438
  193. package/src/shutdown.test.ts +0 -364
  194. package/src/shutdown.ts +0 -246
  195. package/src/span-name-normalizer.test.ts +0 -377
  196. package/src/span-name-normalizer.ts +0 -213
  197. package/src/stable-hash.ts +0 -27
  198. package/src/structured-error.test.ts +0 -191
  199. package/src/structured-error.ts +0 -157
  200. package/src/stub.integration.test.ts +0 -361
  201. package/src/tail-sampling-processor.test.ts +0 -230
  202. package/src/tail-sampling-processor.ts +0 -55
  203. package/src/test-span-collector.test.ts +0 -234
  204. package/src/test-span-collector.ts +0 -150
  205. package/src/testing.ts +0 -705
  206. package/src/trace-context.test.ts +0 -73
  207. package/src/trace-context.ts +0 -567
  208. package/src/trace-helpers.new.test.ts +0 -278
  209. package/src/trace-helpers.test.ts +0 -290
  210. package/src/trace-helpers.ts +0 -710
  211. package/src/trace-hybrid.test.ts +0 -42
  212. package/src/trace-hybrid.ts +0 -37
  213. package/src/tracer-provider.test.ts +0 -183
  214. package/src/tracer-provider.ts +0 -266
  215. package/src/track.test.ts +0 -154
  216. package/src/track.ts +0 -216
  217. package/src/validate.test.ts +0 -287
  218. package/src/validate.ts +0 -307
  219. package/src/validation-attributes.ts +0 -43
  220. package/src/validation.test.ts +0 -330
  221. package/src/validation.ts +0 -246
  222. package/src/variable-name-inference.test.ts +0 -178
  223. package/src/variable-name-inference.ts +0 -242
  224. package/src/webhook.test.ts +0 -649
  225. package/src/webhook.ts +0 -637
  226. package/src/workflow-distributed.test.ts +0 -786
  227. package/src/workflow-distributed.ts +0 -916
  228. package/src/workflow.async-safety.integration.test.ts +0 -345
  229. package/src/workflow.test.ts +0 -647
  230. package/src/workflow.ts +0 -810
  231. package/src/yaml-config.test.ts +0 -337
  232. package/src/yaml-config.ts +0 -342
@@ -1,594 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from 'vitest';
2
- import type { MetricReader } from '@opentelemetry/sdk-metrics';
3
- import type { NodeSDK } from '@opentelemetry/sdk-node';
4
- import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
5
- import type { LogRecordProcessor } from '@opentelemetry/sdk-logs';
6
- import { mock, mockDeep, type DeepMockProxy } from 'vitest-mock-extended';
7
- import { AlwaysSampler, NeverSampler } from './sampling';
8
-
9
- type SdkRecord = {
10
- options: Record<string, unknown>;
11
- instance: DeepMockProxy<NodeSDK>;
12
- };
13
-
14
- async function loadInitWithMocks() {
15
- const sdkInstances: SdkRecord[] = [];
16
- const traceExporterOptions: Record<string, unknown>[] = [];
17
- const metricExporterOptions: Record<string, unknown>[] = [];
18
- const metricReaderOptions: Record<string, unknown>[] = [];
19
- const logExporterOptions: Record<string, unknown>[] = [];
20
- const logProcessorOptions: Record<string, unknown>[] = [];
21
-
22
- class MockNodeSDK {
23
- constructor(options: Record<string, unknown>) {
24
- const instance = mockDeep<NodeSDK>();
25
- instance.start.mockImplementation(() => {});
26
- instance.shutdown.mockResolvedValue();
27
- sdkInstances.push({ options, instance });
28
- return instance;
29
- }
30
- }
31
-
32
- class MockOTLPTraceExporter {
33
- options: Record<string, unknown>;
34
-
35
- constructor(options: Record<string, unknown>) {
36
- this.options = options;
37
- traceExporterOptions.push(options);
38
- }
39
- }
40
-
41
- class MockOTLPMetricExporter {
42
- options: Record<string, unknown>;
43
-
44
- constructor(options: Record<string, unknown>) {
45
- this.options = options;
46
- metricExporterOptions.push(options);
47
- }
48
- }
49
-
50
- class MockPeriodicExportingMetricReader {
51
- options: Record<string, unknown>;
52
-
53
- constructor(options: Record<string, unknown>) {
54
- this.options = options;
55
- metricReaderOptions.push(options);
56
- }
57
- }
58
-
59
- // Reset modules immediately before mocking to ensure clean state
60
- vi.resetModules();
61
-
62
- vi.doMock('@opentelemetry/sdk-node', () => ({
63
- NodeSDK: MockNodeSDK,
64
- }));
65
-
66
- vi.doMock('@opentelemetry/exporter-trace-otlp-http', () => ({
67
- OTLPTraceExporter: MockOTLPTraceExporter,
68
- }));
69
-
70
- vi.doMock('@opentelemetry/exporter-metrics-otlp-http', () => ({
71
- OTLPMetricExporter: MockOTLPMetricExporter,
72
- }));
73
-
74
- vi.doMock('@opentelemetry/sdk-metrics', () => ({
75
- PeriodicExportingMetricReader: MockPeriodicExportingMetricReader,
76
- }));
77
-
78
- class MockOTLPLogExporter {
79
- options: Record<string, unknown>;
80
-
81
- constructor(options: Record<string, unknown>) {
82
- this.options = options;
83
- logExporterOptions.push(options);
84
- }
85
- }
86
-
87
- class MockBatchLogRecordProcessor {
88
- exporter: unknown;
89
-
90
- constructor(exporter: unknown) {
91
- this.exporter = exporter;
92
- logProcessorOptions.push({ exporter });
93
- }
94
-
95
- onEmit() {}
96
- shutdown() {
97
- return Promise.resolve();
98
- }
99
- forceFlush() {
100
- return Promise.resolve();
101
- }
102
- }
103
-
104
- vi.doMock('@opentelemetry/exporter-logs-otlp-http', () => ({
105
- OTLPLogExporter: MockOTLPLogExporter,
106
- }));
107
-
108
- vi.doMock('@opentelemetry/sdk-logs', () => ({
109
- BatchLogRecordProcessor: MockBatchLogRecordProcessor,
110
- }));
111
-
112
- const mod = await import('./init');
113
-
114
- return {
115
- init: mod.init,
116
- getConfig: mod.getConfig,
117
- getDefaultSampler: mod.getDefaultSampler,
118
- resolveLogsFlag: mod.resolveLogsFlag,
119
- setOptionalRequireForTesting: mod._setOptionalRequireForTesting,
120
- resetOptionalRequireForTesting: mod._resetOptionalRequireForTesting,
121
- getEmbeddedDevtoolsCloseForTesting: mod._getEmbeddedDevtoolsCloseForTesting,
122
- sdkInstances,
123
- traceExporterOptions,
124
- metricExporterOptions,
125
- metricReaderOptions,
126
- logExporterOptions,
127
- logProcessorOptions,
128
- };
129
- }
130
-
131
- describe('init() customization', () => {
132
- afterEach(() => {
133
- vi.restoreAllMocks();
134
- delete process.env.AUTOTEL_METRICS;
135
- delete process.env.AUTOTEL_LOGS;
136
- delete process.env.OTEL_LOGS_EXPORTER;
137
- delete process.env.OTEL_TRACES_SAMPLER;
138
- delete process.env.OTEL_TRACES_SAMPLER_ARG;
139
- delete process.env.NODE_ENV;
140
- });
141
-
142
- it('auto-configures local devtools endpoint and logs when devtools is enabled', async () => {
143
- const {
144
- init,
145
- sdkInstances,
146
- traceExporterOptions,
147
- metricExporterOptions,
148
- logExporterOptions,
149
- } = await loadInitWithMocks();
150
-
151
- init({ service: 'devtools-app', devtools: true });
152
-
153
- expect(traceExporterOptions[0]).toMatchObject({
154
- url: 'http://127.0.0.1:4318/v1/traces',
155
- });
156
- expect(metricExporterOptions[0]).toMatchObject({
157
- url: 'http://127.0.0.1:4318/v1/metrics',
158
- });
159
- expect(logExporterOptions[0]).toMatchObject({
160
- url: 'http://127.0.0.1:4318/v1/logs',
161
- });
162
-
163
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
164
- expect(options.logRecordProcessors).toBeDefined();
165
- });
166
-
167
- it('starts embedded autotel-devtools when requested and installed', async () => {
168
- const {
169
- init,
170
- setOptionalRequireForTesting,
171
- getEmbeddedDevtoolsCloseForTesting,
172
- traceExporterOptions,
173
- logExporterOptions,
174
- } = await loadInitWithMocks();
175
-
176
- const close = vi.fn();
177
-
178
- setOptionalRequireForTesting((id: string) => {
179
- if (id === 'autotel-devtools') {
180
- return {
181
- createDevtools: () => ({
182
- port: 9876,
183
- close,
184
- }),
185
- } as any;
186
- }
187
- return undefined;
188
- });
189
-
190
- init({
191
- service: 'embedded-devtools-app',
192
- devtools: { embedded: true, host: '127.0.0.1', port: 0 },
193
- });
194
-
195
- expect(traceExporterOptions[0]).toMatchObject({
196
- url: 'http://127.0.0.1:9876/v1/traces',
197
- });
198
- expect(logExporterOptions[0]).toMatchObject({
199
- url: 'http://127.0.0.1:9876/v1/logs',
200
- });
201
- expect(getEmbeddedDevtoolsCloseForTesting()).toBe(close);
202
- });
203
-
204
- it('falls back cleanly when embedded devtools is requested but unavailable', async () => {
205
- const { init, setOptionalRequireForTesting, traceExporterOptions } =
206
- await loadInitWithMocks();
207
-
208
- setOptionalRequireForTesting(() => undefined);
209
-
210
- init({
211
- service: 'embedded-devtools-fallback-app',
212
- devtools: { embedded: true },
213
- });
214
-
215
- expect(traceExporterOptions[0]).toMatchObject({
216
- url: 'http://127.0.0.1:4318/v1/traces',
217
- });
218
- });
219
-
220
- it(
221
- 'passes custom instrumentations to the NodeSDK',
222
- { timeout: 10_000 },
223
- async () => {
224
- const { init, sdkInstances } = await loadInitWithMocks();
225
-
226
- const instrumentation = { name: 'http' } as any;
227
-
228
- init({
229
- service: 'instrumented-app',
230
- instrumentations: [instrumentation],
231
- });
232
-
233
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
234
- expect(options.instrumentations).toBeDefined();
235
- expect(options.instrumentations).toContain(instrumentation);
236
- },
237
- );
238
-
239
- it('merges resource attributes with defaults', async () => {
240
- const { init, getConfig, sdkInstances } = await loadInitWithMocks();
241
-
242
- init({
243
- service: 'resource-app',
244
- resourceAttributes: { 'cloud.region': 'eu-central-1' },
245
- });
246
-
247
- const resource = sdkInstances.at(-1)?.options.resource as
248
- | {
249
- attributes?: Record<string, unknown>;
250
- }
251
- | undefined;
252
-
253
- if (resource?.attributes) {
254
- expect(resource.attributes['cloud.region']).toBe('eu-central-1');
255
- expect(resource.attributes['service.name']).toBe('resource-app');
256
- return;
257
- }
258
-
259
- const config = getConfig();
260
- expect(config.service).toBe('resource-app');
261
- expect(config.resourceAttributes).toMatchObject({
262
- 'cloud.region': 'eu-central-1',
263
- });
264
- });
265
-
266
- it('creates a default OTLP metric reader when metrics enabled', async () => {
267
- const { init, metricReaderOptions, metricExporterOptions } =
268
- await loadInitWithMocks();
269
-
270
- init({ service: 'metrics-app', endpoint: 'http://localhost:4318' });
271
-
272
- expect(metricReaderOptions).toHaveLength(1);
273
- expect(metricExporterOptions).toHaveLength(1);
274
- });
275
-
276
- it('skips default metric reader when metrics disabled', async () => {
277
- const { init, metricReaderOptions } = await loadInitWithMocks();
278
-
279
- init({ service: 'no-metrics', metrics: false });
280
-
281
- expect(metricReaderOptions).toHaveLength(0);
282
- });
283
-
284
- it('respects custom metric readers', async () => {
285
- const { init, sdkInstances, metricReaderOptions } =
286
- await loadInitWithMocks();
287
- const customMetricReader = mock<MetricReader>();
288
-
289
- init({ service: 'custom-metrics', metricReaders: [customMetricReader] });
290
-
291
- expect(sdkInstances).toHaveLength(1);
292
- const options = sdkInstances.at(-1)!.options as Record<string, unknown>;
293
- expect(options.metricReaders).toEqual([customMetricReader]);
294
- expect(metricReaderOptions).toHaveLength(0);
295
- });
296
-
297
- it('supports singular metricReader alias', async () => {
298
- const { init, sdkInstances, metricReaderOptions } =
299
- await loadInitWithMocks();
300
- const customMetricReader = mock<MetricReader>();
301
-
302
- init({ service: 'custom-metric-alias', metricReader: customMetricReader });
303
-
304
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
305
- expect(options.metricReaders).toEqual([customMetricReader]);
306
- expect(metricReaderOptions).toHaveLength(0);
307
- });
308
-
309
- it('applies OTLP headers for default exporters', async () => {
310
- const { init, traceExporterOptions, metricExporterOptions } =
311
- await loadInitWithMocks();
312
-
313
- init({
314
- service: 'headers-app',
315
- endpoint: 'http://localhost:4318',
316
- headers: 'Authorization=Basic abc123',
317
- });
318
-
319
- expect(traceExporterOptions[0]).toMatchObject({
320
- headers: { Authorization: 'Basic abc123' },
321
- });
322
-
323
- expect(metricExporterOptions[0]).toMatchObject({
324
- headers: { Authorization: 'Basic abc123' },
325
- });
326
- });
327
-
328
- it('resolves sampling preset shorthand to a sampler instance', async () => {
329
- const { init, getDefaultSampler } = await loadInitWithMocks();
330
-
331
- init({
332
- service: 'sampling-preset-app',
333
- sampling: 'development',
334
- });
335
-
336
- const sampler = getDefaultSampler();
337
- expect(sampler.constructor.name).toBe('AlwaysSampler');
338
- expect(sampler.shouldSample({ operationName: 'test', args: [] })).toBe(
339
- true,
340
- );
341
- });
342
-
343
- it('prefers explicit sampler over sampling preset shorthand', async () => {
344
- const { init, getDefaultSampler } = await loadInitWithMocks();
345
- const explicitSampler = new NeverSampler();
346
-
347
- init({
348
- service: 'sampling-precedence-app',
349
- sampler: explicitSampler,
350
- sampling: 'development',
351
- });
352
-
353
- expect(getDefaultSampler()).toBe(explicitSampler);
354
- });
355
-
356
- it('uses OTEL_TRACES_SAMPLER when no explicit sampling config is provided', async () => {
357
- process.env.OTEL_TRACES_SAMPLER = 'always_off';
358
- const { init, sdkInstances } = await loadInitWithMocks();
359
-
360
- init({
361
- service: 'env-sampler-app',
362
- });
363
-
364
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
365
- expect((options.sampler as { toString(): string }).toString()).toContain(
366
- 'AlwaysOffSampler',
367
- );
368
- });
369
-
370
- it('prefers explicit sampling config over OTEL_TRACES_SAMPLER', async () => {
371
- process.env.OTEL_TRACES_SAMPLER = 'always_off';
372
- const { init, sdkInstances } = await loadInitWithMocks();
373
-
374
- init({
375
- service: 'explicit-over-env-sampler-app',
376
- sampling: 'development',
377
- });
378
-
379
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
380
- expect((options.sampler as { toString(): string }).toString()).toBe(
381
- 'AutotelSamplerAdapter',
382
- );
383
- });
384
-
385
- it('supports sdkFactory overrides', async () => {
386
- const { init, sdkInstances } = await loadInitWithMocks();
387
- const customSdk = mockDeep<NodeSDK>();
388
- customSdk.start.mockImplementation(() => {});
389
- customSdk.shutdown.mockResolvedValue();
390
-
391
- init({
392
- service: 'custom-sdk',
393
- endpoint: 'http://localhost:4318',
394
- metrics: false,
395
- sdkFactory: (defaults) => {
396
- expect(defaults.spanProcessors).toBeDefined();
397
- return customSdk;
398
- },
399
- });
400
-
401
- expect(sdkInstances).toHaveLength(0);
402
- expect(customSdk.start).toHaveBeenCalled();
403
- });
404
-
405
- it('uses provided spanProcessors when supplied', async () => {
406
- const { init, sdkInstances } = await loadInitWithMocks();
407
- const customProcessor = mock<SpanProcessor>();
408
- customProcessor.shutdown.mockResolvedValue();
409
- customProcessor.forceFlush.mockResolvedValue();
410
-
411
- init({ service: 'custom-span', spanProcessors: [customProcessor] });
412
-
413
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
414
- expect(options.spanProcessors).toEqual([customProcessor]);
415
- });
416
-
417
- it('supports singular spanProcessor alias', async () => {
418
- const { init, sdkInstances } = await loadInitWithMocks();
419
- const customProcessor = mock<SpanProcessor>();
420
- customProcessor.shutdown.mockResolvedValue();
421
- customProcessor.forceFlush.mockResolvedValue();
422
-
423
- init({ service: 'custom-span-alias', spanProcessor: customProcessor });
424
-
425
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
426
- expect(options.spanProcessors).toEqual([customProcessor]);
427
- });
428
-
429
- it('prefers plural spanProcessors over singular spanProcessor when both are set', async () => {
430
- const { init, sdkInstances } = await loadInitWithMocks();
431
- const pluralProcessor = mock<SpanProcessor>();
432
- pluralProcessor.shutdown.mockResolvedValue();
433
- pluralProcessor.forceFlush.mockResolvedValue();
434
- const singularProcessor = mock<SpanProcessor>();
435
- singularProcessor.shutdown.mockResolvedValue();
436
- singularProcessor.forceFlush.mockResolvedValue();
437
-
438
- init({
439
- service: 'custom-span-precedence',
440
- spanProcessor: singularProcessor,
441
- spanProcessors: [pluralProcessor],
442
- });
443
-
444
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
445
- expect(options.spanProcessors).toEqual([pluralProcessor]);
446
- });
447
-
448
- it('supports singular spanExporter alias', async () => {
449
- const { init, sdkInstances, traceExporterOptions } =
450
- await loadInitWithMocks();
451
- const customExporter = {
452
- export: vi.fn(),
453
- shutdown: vi.fn().mockResolvedValue(undefined),
454
- forceFlush: vi.fn().mockResolvedValue(undefined),
455
- } as any;
456
-
457
- init({
458
- service: 'custom-exporter-alias',
459
- endpoint: 'http://localhost:4318',
460
- spanExporter: customExporter,
461
- });
462
-
463
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
464
- expect(options.spanProcessors).toBeDefined();
465
- // Custom exporter path should bypass default OTLP trace exporter creation.
466
- expect(traceExporterOptions).toHaveLength(0);
467
- });
468
-
469
- it('auto-configures OTLP log exporter when logs enabled with endpoint', async () => {
470
- const { init, sdkInstances, logExporterOptions } =
471
- await loadInitWithMocks();
472
-
473
- init({
474
- service: 'log-app',
475
- endpoint: 'http://localhost:4318',
476
- logs: true,
477
- });
478
-
479
- expect(logExporterOptions).toHaveLength(1);
480
- expect(logExporterOptions[0]!.url).toBe('http://localhost:4318/v1/logs');
481
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
482
- expect(options.logRecordProcessors).toBeDefined();
483
- expect(
484
- (options.logRecordProcessors as unknown[]).length,
485
- ).toBeGreaterThanOrEqual(1);
486
- });
487
-
488
- it('supports singular logRecordProcessor alias', async () => {
489
- const { init, sdkInstances } = await loadInitWithMocks();
490
- const customLogProcessor = mock<LogRecordProcessor>();
491
- customLogProcessor.shutdown.mockResolvedValue();
492
- customLogProcessor.forceFlush.mockResolvedValue();
493
-
494
- init({
495
- service: 'custom-log-alias',
496
- logRecordProcessor: customLogProcessor,
497
- });
498
-
499
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
500
- expect(options.logRecordProcessors).toEqual([customLogProcessor]);
501
- });
502
-
503
- it('does not auto-configure logs when logRecordProcessors are omitted', async () => {
504
- const { init, sdkInstances, logExporterOptions } =
505
- await loadInitWithMocks();
506
-
507
- init({
508
- service: 'default-logs',
509
- endpoint: 'http://localhost:4318',
510
- });
511
-
512
- expect(logExporterOptions).toHaveLength(0);
513
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
514
- expect(options.logRecordProcessors).toBeUndefined();
515
- });
516
-
517
- it('does not override OTEL_LOGS_EXPORTER env configuration by default', async () => {
518
- const { init, sdkInstances, logExporterOptions } =
519
- await loadInitWithMocks();
520
-
521
- process.env.OTEL_LOGS_EXPORTER = 'none';
522
-
523
- init({
524
- service: 'env-logs',
525
- endpoint: 'http://localhost:4318',
526
- });
527
-
528
- expect(logExporterOptions).toHaveLength(0);
529
- const options = sdkInstances.at(-1)?.options as Record<string, unknown>;
530
- expect(options.logRecordProcessors).toBeUndefined();
531
- });
532
-
533
- it('auto-configures logs when logs: true is set', async () => {
534
- const { init, logExporterOptions } = await loadInitWithMocks();
535
-
536
- init({
537
- service: 'default-logs',
538
- endpoint: 'http://localhost:4318',
539
- logs: true,
540
- });
541
-
542
- expect(logExporterOptions).toHaveLength(1);
543
- });
544
-
545
- it('skips log exporter when logs: false', async () => {
546
- const { init, logExporterOptions } = await loadInitWithMocks();
547
-
548
- init({
549
- service: 'no-logs',
550
- endpoint: 'http://localhost:4318',
551
- logs: false,
552
- });
553
-
554
- expect(logExporterOptions).toHaveLength(0);
555
- });
556
-
557
- it('skips log exporter when no endpoint', async () => {
558
- const { init, logExporterOptions } = await loadInitWithMocks();
559
-
560
- init({ service: 'no-endpoint', logs: true });
561
-
562
- expect(logExporterOptions).toHaveLength(0);
563
- });
564
-
565
- it('respects AUTOTEL_LOGS env var override', async () => {
566
- const { resolveLogsFlag } = await loadInitWithMocks();
567
-
568
- process.env.AUTOTEL_LOGS = 'off';
569
- expect(resolveLogsFlag(true)).toBe(false);
570
-
571
- process.env.AUTOTEL_LOGS = 'on';
572
- expect(resolveLogsFlag(false)).toBe(true);
573
-
574
- delete process.env.AUTOTEL_LOGS;
575
- expect(resolveLogsFlag(true)).toBe(true);
576
- expect(resolveLogsFlag(false)).toBe(false);
577
- });
578
-
579
- it('passes OTLP headers to log exporter', async () => {
580
- const { init, logExporterOptions } = await loadInitWithMocks();
581
-
582
- init({
583
- service: 'headers-logs',
584
- endpoint: 'http://localhost:4318',
585
- logs: true,
586
- headers: { Authorization: 'Bearer token' },
587
- });
588
-
589
- expect(logExporterOptions).toHaveLength(1);
590
- expect(logExporterOptions[0]!.headers).toEqual({
591
- Authorization: 'Bearer token',
592
- });
593
- });
594
- });