autotel 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1946 -0
- package/dist/chunk-2LNRY4QK.js +273 -0
- package/dist/chunk-2LNRY4QK.js.map +1 -0
- package/dist/chunk-3HENGDW2.js +587 -0
- package/dist/chunk-3HENGDW2.js.map +1 -0
- package/dist/chunk-4OAT42CA.cjs +73 -0
- package/dist/chunk-4OAT42CA.cjs.map +1 -0
- package/dist/chunk-5GWX5LFW.js +70 -0
- package/dist/chunk-5GWX5LFW.js.map +1 -0
- package/dist/chunk-5R2M36QB.js +195 -0
- package/dist/chunk-5R2M36QB.js.map +1 -0
- package/dist/chunk-5ZN622AO.js +73 -0
- package/dist/chunk-5ZN622AO.js.map +1 -0
- package/dist/chunk-77MSMAUQ.cjs +498 -0
- package/dist/chunk-77MSMAUQ.cjs.map +1 -0
- package/dist/chunk-ABPEQ6RK.cjs +596 -0
- package/dist/chunk-ABPEQ6RK.cjs.map +1 -0
- package/dist/chunk-BWYGJKRB.js +95 -0
- package/dist/chunk-BWYGJKRB.js.map +1 -0
- package/dist/chunk-BZHG5IZ4.js +73 -0
- package/dist/chunk-BZHG5IZ4.js.map +1 -0
- package/dist/chunk-G7VZBCD6.cjs +35 -0
- package/dist/chunk-G7VZBCD6.cjs.map +1 -0
- package/dist/chunk-GVLK7YUU.cjs +30 -0
- package/dist/chunk-GVLK7YUU.cjs.map +1 -0
- package/dist/chunk-HCCXC7XG.js +205 -0
- package/dist/chunk-HCCXC7XG.js.map +1 -0
- package/dist/chunk-HE6T6FIX.cjs +203 -0
- package/dist/chunk-HE6T6FIX.cjs.map +1 -0
- package/dist/chunk-KIXWPOCO.cjs +100 -0
- package/dist/chunk-KIXWPOCO.cjs.map +1 -0
- package/dist/chunk-KVGNW3FC.js +87 -0
- package/dist/chunk-KVGNW3FC.js.map +1 -0
- package/dist/chunk-LITNXTTT.js +3 -0
- package/dist/chunk-LITNXTTT.js.map +1 -0
- package/dist/chunk-M4ANN7RL.js +114 -0
- package/dist/chunk-M4ANN7RL.js.map +1 -0
- package/dist/chunk-NC52UBR2.cjs +32 -0
- package/dist/chunk-NC52UBR2.cjs.map +1 -0
- package/dist/chunk-NHCNRQD3.cjs +212 -0
- package/dist/chunk-NHCNRQD3.cjs.map +1 -0
- package/dist/chunk-NZ72VDNY.cjs +4 -0
- package/dist/chunk-NZ72VDNY.cjs.map +1 -0
- package/dist/chunk-P6JUDYNO.js +57 -0
- package/dist/chunk-P6JUDYNO.js.map +1 -0
- package/dist/chunk-RJYY7BWX.js +1349 -0
- package/dist/chunk-RJYY7BWX.js.map +1 -0
- package/dist/chunk-TRI4V5BF.cjs +126 -0
- package/dist/chunk-TRI4V5BF.cjs.map +1 -0
- package/dist/chunk-UL33I6IS.js +139 -0
- package/dist/chunk-UL33I6IS.js.map +1 -0
- package/dist/chunk-URRW6M2C.cjs +61 -0
- package/dist/chunk-URRW6M2C.cjs.map +1 -0
- package/dist/chunk-UY3UYPBZ.cjs +77 -0
- package/dist/chunk-UY3UYPBZ.cjs.map +1 -0
- package/dist/chunk-W3253FGB.cjs +277 -0
- package/dist/chunk-W3253FGB.cjs.map +1 -0
- package/dist/chunk-W7LHZVQF.js +26 -0
- package/dist/chunk-W7LHZVQF.js.map +1 -0
- package/dist/chunk-WBWNM6LB.cjs +1360 -0
- package/dist/chunk-WBWNM6LB.cjs.map +1 -0
- package/dist/chunk-WFJ7L2RV.js +494 -0
- package/dist/chunk-WFJ7L2RV.js.map +1 -0
- package/dist/chunk-X4RMFFMR.js +28 -0
- package/dist/chunk-X4RMFFMR.js.map +1 -0
- package/dist/chunk-Y4Y2S7BM.cjs +92 -0
- package/dist/chunk-Y4Y2S7BM.cjs.map +1 -0
- package/dist/chunk-YLPNXZFI.cjs +143 -0
- package/dist/chunk-YLPNXZFI.cjs.map +1 -0
- package/dist/chunk-YTXEZ4SD.cjs +77 -0
- package/dist/chunk-YTXEZ4SD.cjs.map +1 -0
- package/dist/chunk-Z6ZWNWWR.js +30 -0
- package/dist/chunk-Z6ZWNWWR.js.map +1 -0
- package/dist/config.cjs +26 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +75 -0
- package/dist/config.d.ts +75 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/db.cjs +233 -0
- package/dist/db.cjs.map +1 -0
- package/dist/db.d.cts +123 -0
- package/dist/db.d.ts +123 -0
- package/dist/db.js +228 -0
- package/dist/db.js.map +1 -0
- package/dist/decorators.cjs +67 -0
- package/dist/decorators.cjs.map +1 -0
- package/dist/decorators.d.cts +91 -0
- package/dist/decorators.d.ts +91 -0
- package/dist/decorators.js +65 -0
- package/dist/decorators.js.map +1 -0
- package/dist/event-subscriber.cjs +6 -0
- package/dist/event-subscriber.cjs.map +1 -0
- package/dist/event-subscriber.d.cts +116 -0
- package/dist/event-subscriber.d.ts +116 -0
- package/dist/event-subscriber.js +3 -0
- package/dist/event-subscriber.js.map +1 -0
- package/dist/event-testing.cjs +21 -0
- package/dist/event-testing.cjs.map +1 -0
- package/dist/event-testing.d.cts +110 -0
- package/dist/event-testing.d.ts +110 -0
- package/dist/event-testing.js +4 -0
- package/dist/event-testing.js.map +1 -0
- package/dist/event.cjs +30 -0
- package/dist/event.cjs.map +1 -0
- package/dist/event.d.cts +282 -0
- package/dist/event.d.ts +282 -0
- package/dist/event.js +13 -0
- package/dist/event.js.map +1 -0
- package/dist/exporters.cjs +17 -0
- package/dist/exporters.cjs.map +1 -0
- package/dist/exporters.d.cts +1 -0
- package/dist/exporters.d.ts +1 -0
- package/dist/exporters.js +4 -0
- package/dist/exporters.js.map +1 -0
- package/dist/functional.cjs +46 -0
- package/dist/functional.cjs.map +1 -0
- package/dist/functional.d.cts +478 -0
- package/dist/functional.d.ts +478 -0
- package/dist/functional.js +13 -0
- package/dist/functional.js.map +1 -0
- package/dist/http.cjs +189 -0
- package/dist/http.cjs.map +1 -0
- package/dist/http.d.cts +169 -0
- package/dist/http.d.ts +169 -0
- package/dist/http.js +184 -0
- package/dist/http.js.map +1 -0
- package/dist/index.cjs +333 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +758 -0
- package/dist/index.d.ts +758 -0
- package/dist/index.js +143 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation.cjs +182 -0
- package/dist/instrumentation.cjs.map +1 -0
- package/dist/instrumentation.d.cts +49 -0
- package/dist/instrumentation.d.ts +49 -0
- package/dist/instrumentation.js +179 -0
- package/dist/instrumentation.js.map +1 -0
- package/dist/logger.cjs +19 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +146 -0
- package/dist/logger.d.ts +146 -0
- package/dist/logger.js +6 -0
- package/dist/logger.js.map +1 -0
- package/dist/metric-helpers.cjs +31 -0
- package/dist/metric-helpers.cjs.map +1 -0
- package/dist/metric-helpers.d.cts +13 -0
- package/dist/metric-helpers.d.ts +13 -0
- package/dist/metric-helpers.js +6 -0
- package/dist/metric-helpers.js.map +1 -0
- package/dist/metric-testing.cjs +21 -0
- package/dist/metric-testing.cjs.map +1 -0
- package/dist/metric-testing.d.cts +110 -0
- package/dist/metric-testing.d.ts +110 -0
- package/dist/metric-testing.js +4 -0
- package/dist/metric-testing.js.map +1 -0
- package/dist/metric.cjs +26 -0
- package/dist/metric.cjs.map +1 -0
- package/dist/metric.d.cts +240 -0
- package/dist/metric.d.ts +240 -0
- package/dist/metric.js +9 -0
- package/dist/metric.js.map +1 -0
- package/dist/processors.cjs +17 -0
- package/dist/processors.cjs.map +1 -0
- package/dist/processors.d.cts +1 -0
- package/dist/processors.d.ts +1 -0
- package/dist/processors.js +4 -0
- package/dist/processors.js.map +1 -0
- package/dist/sampling.cjs +40 -0
- package/dist/sampling.cjs.map +1 -0
- package/dist/sampling.d.cts +260 -0
- package/dist/sampling.d.ts +260 -0
- package/dist/sampling.js +7 -0
- package/dist/sampling.js.map +1 -0
- package/dist/semantic-helpers.cjs +35 -0
- package/dist/semantic-helpers.cjs.map +1 -0
- package/dist/semantic-helpers.d.cts +442 -0
- package/dist/semantic-helpers.d.ts +442 -0
- package/dist/semantic-helpers.js +14 -0
- package/dist/semantic-helpers.js.map +1 -0
- package/dist/tail-sampling-processor.cjs +13 -0
- package/dist/tail-sampling-processor.cjs.map +1 -0
- package/dist/tail-sampling-processor.d.cts +27 -0
- package/dist/tail-sampling-processor.d.ts +27 -0
- package/dist/tail-sampling-processor.js +4 -0
- package/dist/tail-sampling-processor.js.map +1 -0
- package/dist/testing.cjs +286 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +291 -0
- package/dist/testing.d.ts +291 -0
- package/dist/testing.js +263 -0
- package/dist/testing.js.map +1 -0
- package/dist/trace-context-DRZdUvVY.d.cts +181 -0
- package/dist/trace-context-DRZdUvVY.d.ts +181 -0
- package/dist/trace-helpers.cjs +54 -0
- package/dist/trace-helpers.cjs.map +1 -0
- package/dist/trace-helpers.d.cts +524 -0
- package/dist/trace-helpers.d.ts +524 -0
- package/dist/trace-helpers.js +5 -0
- package/dist/trace-helpers.js.map +1 -0
- package/dist/tracer-provider.cjs +21 -0
- package/dist/tracer-provider.cjs.map +1 -0
- package/dist/tracer-provider.d.cts +169 -0
- package/dist/tracer-provider.d.ts +169 -0
- package/dist/tracer-provider.js +4 -0
- package/dist/tracer-provider.js.map +1 -0
- package/package.json +280 -0
- package/src/baggage-span-processor.test.ts +202 -0
- package/src/baggage-span-processor.ts +98 -0
- package/src/circuit-breaker.test.ts +341 -0
- package/src/circuit-breaker.ts +184 -0
- package/src/config.test.ts +94 -0
- package/src/config.ts +169 -0
- package/src/db.test.ts +252 -0
- package/src/db.ts +447 -0
- package/src/decorators.test.ts +203 -0
- package/src/decorators.ts +188 -0
- package/src/env-config.test.ts +246 -0
- package/src/env-config.ts +158 -0
- package/src/event-queue.test.ts +222 -0
- package/src/event-queue.ts +203 -0
- package/src/event-subscriber.ts +136 -0
- package/src/event-testing.ts +197 -0
- package/src/event.test.ts +718 -0
- package/src/event.ts +556 -0
- package/src/exporters.ts +96 -0
- package/src/functional.test.ts +1059 -0
- package/src/functional.ts +2295 -0
- package/src/http.test.ts +487 -0
- package/src/http.ts +424 -0
- package/src/index.ts +158 -0
- package/src/init.customization.test.ts +210 -0
- package/src/init.integrations.test.ts +366 -0
- package/src/init.openllmetry.test.ts +282 -0
- package/src/init.protocol.test.ts +215 -0
- package/src/init.ts +1426 -0
- package/src/instrumentation.test.ts +108 -0
- package/src/instrumentation.ts +308 -0
- package/src/logger.test.ts +117 -0
- package/src/logger.ts +246 -0
- package/src/metric-helpers.ts +47 -0
- package/src/metric-testing.ts +197 -0
- package/src/metric.ts +434 -0
- package/src/metrics.test.ts +205 -0
- package/src/operation-context.ts +93 -0
- package/src/processors.ts +106 -0
- package/src/rate-limiter.test.ts +199 -0
- package/src/rate-limiter.ts +98 -0
- package/src/sampling.test.ts +513 -0
- package/src/sampling.ts +428 -0
- package/src/semantic-helpers.test.ts +311 -0
- package/src/semantic-helpers.ts +584 -0
- package/src/shutdown.test.ts +311 -0
- package/src/shutdown.ts +222 -0
- package/src/stub.integration.test.ts +361 -0
- package/src/tail-sampling-processor.test.ts +226 -0
- package/src/tail-sampling-processor.ts +51 -0
- package/src/testing.ts +670 -0
- package/src/trace-context.ts +470 -0
- package/src/trace-helpers.new.test.ts +278 -0
- package/src/trace-helpers.test.ts +242 -0
- package/src/trace-helpers.ts +690 -0
- package/src/tracer-provider.test.ts +183 -0
- package/src/tracer-provider.ts +266 -0
- package/src/track.test.ts +153 -0
- package/src/track.ts +120 -0
- package/src/validation.test.ts +306 -0
- package/src/validation.ts +239 -0
- package/src/variable-name-inference.test.ts +178 -0
- package/src/variable-name-inference.ts +242 -0
package/src/http.test.ts
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
HttpInstrumented,
|
|
4
|
+
traceHttpRequest,
|
|
5
|
+
injectTraceContext,
|
|
6
|
+
extractTraceContext,
|
|
7
|
+
} from './http';
|
|
8
|
+
import { configure, resetConfig } from './config';
|
|
9
|
+
|
|
10
|
+
describe('HttpInstrumented', () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
resetConfig();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should instrument HTTP methods', async () => {
|
|
16
|
+
const mockSpan = {
|
|
17
|
+
setStatus: vi.fn(),
|
|
18
|
+
setAttributes: vi.fn(),
|
|
19
|
+
setAttribute: vi.fn(),
|
|
20
|
+
end: vi.fn(),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const mockTracer = {
|
|
24
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
configure({
|
|
28
|
+
tracer: mockTracer as any,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
@HttpInstrumented({ serviceName: 'api-client' })
|
|
32
|
+
class ApiClient {
|
|
33
|
+
async getUser(userId: string) {
|
|
34
|
+
return { status: 200, data: { id: userId, name: 'Test' } };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const client = new ApiClient();
|
|
39
|
+
const result = await client.getUser('123');
|
|
40
|
+
|
|
41
|
+
expect(result.data).toEqual({ id: '123', name: 'Test' });
|
|
42
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
43
|
+
'HTTP GET 123',
|
|
44
|
+
expect.any(Function),
|
|
45
|
+
);
|
|
46
|
+
expect(mockSpan.setAttributes).toHaveBeenCalledWith(
|
|
47
|
+
expect.objectContaining({
|
|
48
|
+
'http.method': 'GET',
|
|
49
|
+
'service.name': 'api-client',
|
|
50
|
+
'operation.name': 'api-client.getUser',
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith('http.status_code', 200);
|
|
54
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should infer HTTP methods from method names', async () => {
|
|
58
|
+
const mockSpan = {
|
|
59
|
+
setStatus: vi.fn(),
|
|
60
|
+
setAttributes: vi.fn(),
|
|
61
|
+
setAttribute: vi.fn(),
|
|
62
|
+
end: vi.fn(),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const mockTracer = {
|
|
66
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
configure({
|
|
70
|
+
tracer: mockTracer as any,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
@HttpInstrumented()
|
|
74
|
+
class ApiClient {
|
|
75
|
+
async fetchData() {
|
|
76
|
+
return { status: 200 };
|
|
77
|
+
}
|
|
78
|
+
async createUser() {
|
|
79
|
+
return { status: 201 };
|
|
80
|
+
}
|
|
81
|
+
async updateProfile() {
|
|
82
|
+
return { status: 200 };
|
|
83
|
+
}
|
|
84
|
+
async deleteAccount() {
|
|
85
|
+
return { status: 204 };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const client = new ApiClient();
|
|
90
|
+
|
|
91
|
+
await client.fetchData();
|
|
92
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
93
|
+
'HTTP GET',
|
|
94
|
+
expect.any(Function),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
await client.createUser();
|
|
98
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
99
|
+
'HTTP POST',
|
|
100
|
+
expect.any(Function),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
await client.updateProfile();
|
|
104
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
105
|
+
'HTTP PUT',
|
|
106
|
+
expect.any(Function),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
await client.deleteAccount();
|
|
110
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
111
|
+
'HTTP DELETE',
|
|
112
|
+
expect.any(Function),
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should extract URL and parse it', async () => {
|
|
117
|
+
const mockSpan = {
|
|
118
|
+
setStatus: vi.fn(),
|
|
119
|
+
setAttributes: vi.fn(),
|
|
120
|
+
setAttribute: vi.fn(),
|
|
121
|
+
end: vi.fn(),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const mockTracer = {
|
|
125
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
configure({
|
|
129
|
+
tracer: mockTracer as any,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
@HttpInstrumented()
|
|
133
|
+
class ApiClient {
|
|
134
|
+
async getUser(url: string) {
|
|
135
|
+
return { status: 200, url };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const client = new ApiClient();
|
|
140
|
+
await client.getUser('https://api.example.com/users/123?include=profile');
|
|
141
|
+
|
|
142
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
143
|
+
'HTTP GET /users/123',
|
|
144
|
+
expect.any(Function),
|
|
145
|
+
);
|
|
146
|
+
expect(mockSpan.setAttributes).toHaveBeenCalledWith(
|
|
147
|
+
expect.objectContaining({
|
|
148
|
+
'http.scheme': 'https',
|
|
149
|
+
'http.host': 'api.example.com',
|
|
150
|
+
'http.target': '/users/123?include=profile',
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should mark slow requests', async () => {
|
|
156
|
+
const mockSpan = {
|
|
157
|
+
setStatus: vi.fn(),
|
|
158
|
+
setAttributes: vi.fn(),
|
|
159
|
+
setAttribute: vi.fn(),
|
|
160
|
+
end: vi.fn(),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const mockTracer = {
|
|
164
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
configure({
|
|
168
|
+
tracer: mockTracer as any,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
@HttpInstrumented({ slowRequestThresholdMs: 10 })
|
|
172
|
+
class ApiClient {
|
|
173
|
+
async slowRequest() {
|
|
174
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
175
|
+
return { status: 200 };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const client = new ApiClient();
|
|
180
|
+
await client.slowRequest();
|
|
181
|
+
|
|
182
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
|
|
183
|
+
'http.slow_request',
|
|
184
|
+
true,
|
|
185
|
+
);
|
|
186
|
+
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
|
|
187
|
+
'http.slow_request_threshold_ms',
|
|
188
|
+
10,
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should mark 4xx/5xx as errors', async () => {
|
|
193
|
+
const mockSpan = {
|
|
194
|
+
setStatus: vi.fn(),
|
|
195
|
+
setAttributes: vi.fn(),
|
|
196
|
+
setAttribute: vi.fn(),
|
|
197
|
+
end: vi.fn(),
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const mockTracer = {
|
|
201
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
configure({
|
|
205
|
+
tracer: mockTracer as any,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
@HttpInstrumented()
|
|
209
|
+
class ApiClient {
|
|
210
|
+
async notFound() {
|
|
211
|
+
return { status: 404 };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const client = new ApiClient();
|
|
216
|
+
await client.notFound();
|
|
217
|
+
|
|
218
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({
|
|
219
|
+
code: 2, // SpanStatusCode.ERROR
|
|
220
|
+
message: 'HTTP 404',
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should handle errors correctly', async () => {
|
|
225
|
+
const mockSpan = {
|
|
226
|
+
setStatus: vi.fn(),
|
|
227
|
+
setAttributes: vi.fn(),
|
|
228
|
+
setAttribute: vi.fn(),
|
|
229
|
+
end: vi.fn(),
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const mockTracer = {
|
|
233
|
+
startActiveSpan: vi.fn((name, fn) => fn(mockSpan)),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
configure({
|
|
237
|
+
tracer: mockTracer as any,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
@HttpInstrumented()
|
|
241
|
+
class ApiClient {
|
|
242
|
+
async failingRequest() {
|
|
243
|
+
throw new Error('Network timeout');
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const client = new ApiClient();
|
|
248
|
+
|
|
249
|
+
await expect(client.failingRequest()).rejects.toThrow('Network timeout');
|
|
250
|
+
|
|
251
|
+
expect(mockSpan.setStatus).toHaveBeenCalledWith({
|
|
252
|
+
code: 2, // SpanStatusCode.ERROR
|
|
253
|
+
message: 'Network timeout',
|
|
254
|
+
});
|
|
255
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe('traceHttpRequest', () => {
|
|
260
|
+
beforeEach(() => {
|
|
261
|
+
resetConfig();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('should trace HTTP requests', async () => {
|
|
265
|
+
const mockSpan = {
|
|
266
|
+
setStatus: vi.fn(),
|
|
267
|
+
setAttributes: vi.fn(),
|
|
268
|
+
end: vi.fn(),
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const mockTracer = {
|
|
272
|
+
startActiveSpan: vi.fn((name, optionsOrFn, maybeFn) => {
|
|
273
|
+
const fn = typeof optionsOrFn === 'function' ? optionsOrFn : maybeFn;
|
|
274
|
+
return fn(mockSpan);
|
|
275
|
+
}),
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
configure({
|
|
279
|
+
tracer: mockTracer as any,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const result = await traceHttpRequest(
|
|
283
|
+
'GET /api/users',
|
|
284
|
+
async () => ({ data: [1, 2, 3] }),
|
|
285
|
+
{
|
|
286
|
+
'http.method': 'GET',
|
|
287
|
+
'http.target': '/api/users',
|
|
288
|
+
},
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
expect(result).toEqual({ data: [1, 2, 3] });
|
|
292
|
+
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
|
|
293
|
+
'GET /api/users',
|
|
294
|
+
expect.any(Function),
|
|
295
|
+
);
|
|
296
|
+
expect(mockSpan.setAttributes).toHaveBeenCalledWith({
|
|
297
|
+
'http.method': 'GET',
|
|
298
|
+
'http.target': '/api/users',
|
|
299
|
+
});
|
|
300
|
+
expect(mockSpan.end).toHaveBeenCalled();
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe('injectTraceContext', () => {
|
|
305
|
+
beforeEach(() => {
|
|
306
|
+
resetConfig();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
afterEach(async () => {
|
|
310
|
+
// Clean up any SDK that might have been initialized
|
|
311
|
+
const { shutdown } = await import('./shutdown');
|
|
312
|
+
await shutdown().catch(() => {
|
|
313
|
+
// Ignore errors if SDK wasn't initialized
|
|
314
|
+
});
|
|
315
|
+
resetConfig();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('should inject trace headers using propagation.inject', async () => {
|
|
319
|
+
const { propagation } = await import('@opentelemetry/api');
|
|
320
|
+
|
|
321
|
+
// Mock propagation.inject to simulate W3C trace context injection
|
|
322
|
+
const injectSpy = vi
|
|
323
|
+
.spyOn(propagation, 'inject')
|
|
324
|
+
.mockImplementation((ctx, carrier) => {
|
|
325
|
+
// Simulate W3C trace context propagation
|
|
326
|
+
(carrier as Record<string, string>)['traceparent'] =
|
|
327
|
+
'00-0123456789abcdef0123456789abcdef-0123456789abcdef-01';
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const headers = injectTraceContext({
|
|
331
|
+
'Content-Type': 'application/json',
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
expect(injectSpy).toHaveBeenCalled();
|
|
335
|
+
expect(headers['traceparent']).toBe(
|
|
336
|
+
'00-0123456789abcdef0123456789abcdef-0123456789abcdef-01',
|
|
337
|
+
);
|
|
338
|
+
expect(headers['Content-Type']).toBe('application/json');
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should work with empty headers', async () => {
|
|
342
|
+
const { propagation } = await import('@opentelemetry/api');
|
|
343
|
+
const injectSpy = vi.spyOn(propagation, 'inject');
|
|
344
|
+
|
|
345
|
+
const headers = injectTraceContext();
|
|
346
|
+
|
|
347
|
+
expect(injectSpy).toHaveBeenCalled();
|
|
348
|
+
expect(headers).toBeDefined();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it('should inject baggage header when baggage is present', async () => {
|
|
352
|
+
const { propagation } = await import('@opentelemetry/api');
|
|
353
|
+
|
|
354
|
+
// Mock propagation.inject to simulate baggage propagation
|
|
355
|
+
const injectSpy = vi
|
|
356
|
+
.spyOn(propagation, 'inject')
|
|
357
|
+
.mockImplementation((ctx, carrier) => {
|
|
358
|
+
(carrier as Record<string, string>)['traceparent'] =
|
|
359
|
+
'00-0123456789abcdef0123456789abcdef-0123456789abcdef-01';
|
|
360
|
+
(carrier as Record<string, string>)['baggage'] = 'tenant.id=tenant-123';
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const headers = injectTraceContext({ 'Content-Type': 'application/json' });
|
|
364
|
+
|
|
365
|
+
expect(injectSpy).toHaveBeenCalled();
|
|
366
|
+
expect(headers['Content-Type']).toBe('application/json');
|
|
367
|
+
expect(headers['baggage']).toBe('tenant.id=tenant-123');
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('should inject baggage set via ctx.setBaggage()', async () => {
|
|
371
|
+
const { trace } = await import('./functional');
|
|
372
|
+
const { propagation, context: otelContext } = await import(
|
|
373
|
+
'@opentelemetry/api'
|
|
374
|
+
);
|
|
375
|
+
const { init } = await import('./init');
|
|
376
|
+
const { shutdown } = await import('./shutdown');
|
|
377
|
+
const { InMemorySpanExporter } = await import('./exporters');
|
|
378
|
+
const { SimpleSpanProcessor } = await import('./processors');
|
|
379
|
+
const { AsyncLocalStorageContextManager } = await import(
|
|
380
|
+
'@opentelemetry/context-async-hooks'
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
// Clean up any existing SDK first
|
|
384
|
+
await shutdown().catch(() => {
|
|
385
|
+
// Ignore errors if SDK wasn't initialized
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// Set up proper OpenTelemetry SDK for this test
|
|
389
|
+
const exporter = new InMemorySpanExporter();
|
|
390
|
+
const contextManager = new AsyncLocalStorageContextManager();
|
|
391
|
+
contextManager.enable();
|
|
392
|
+
otelContext.setGlobalContextManager(contextManager);
|
|
393
|
+
|
|
394
|
+
init({
|
|
395
|
+
service: 'test',
|
|
396
|
+
spanProcessors: [new SimpleSpanProcessor(exporter)],
|
|
397
|
+
metrics: false,
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
try {
|
|
401
|
+
// Track what context was passed to propagation.inject
|
|
402
|
+
let injectedContext: unknown = null;
|
|
403
|
+
const injectSpy = vi
|
|
404
|
+
.spyOn(propagation, 'inject')
|
|
405
|
+
.mockImplementation((ctx, carrier) => {
|
|
406
|
+
injectedContext = ctx;
|
|
407
|
+
// Extract baggage from context to verify it's included
|
|
408
|
+
const baggage = propagation.getBaggage(ctx as any);
|
|
409
|
+
if (baggage) {
|
|
410
|
+
const baggageEntries: string[] = [];
|
|
411
|
+
for (const [key, entry] of baggage.getAllEntries()) {
|
|
412
|
+
baggageEntries.push(`${key}=${entry.value}`);
|
|
413
|
+
}
|
|
414
|
+
if (baggageEntries.length > 0) {
|
|
415
|
+
(carrier as Record<string, string>)['baggage'] =
|
|
416
|
+
baggageEntries.join(',');
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const testFn = trace((ctx) => async () => {
|
|
422
|
+
// Set baggage via ctx.setBaggage()
|
|
423
|
+
ctx.setBaggage('tenant.id', 't1');
|
|
424
|
+
ctx.setBaggage('user.id', 'u1');
|
|
425
|
+
|
|
426
|
+
// Inject trace context - should include the baggage we just set
|
|
427
|
+
const headers = injectTraceContext();
|
|
428
|
+
return headers;
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
const headers = await testFn();
|
|
432
|
+
|
|
433
|
+
expect(injectSpy).toHaveBeenCalled();
|
|
434
|
+
expect(injectedContext).toBeTruthy();
|
|
435
|
+
// Verify baggage was included in the injected context
|
|
436
|
+
const baggage = propagation.getBaggage(injectedContext as any);
|
|
437
|
+
expect(baggage).toBeTruthy();
|
|
438
|
+
expect(baggage?.getEntry('tenant.id')?.value).toBe('t1');
|
|
439
|
+
expect(baggage?.getEntry('user.id')?.value).toBe('u1');
|
|
440
|
+
expect(headers['baggage']).toContain('tenant.id=t1');
|
|
441
|
+
expect(headers['baggage']).toContain('user.id=u1');
|
|
442
|
+
|
|
443
|
+
injectSpy.mockRestore();
|
|
444
|
+
} finally {
|
|
445
|
+
// Clean up
|
|
446
|
+
await shutdown().catch(() => {
|
|
447
|
+
// Ignore errors
|
|
448
|
+
});
|
|
449
|
+
// Reset config for other tests
|
|
450
|
+
resetConfig();
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
describe('extractTraceContext', () => {
|
|
456
|
+
it('should extract trace context from headers', () => {
|
|
457
|
+
const headers = {
|
|
458
|
+
traceparent: '00-0123456789abcdef0123456789abcdef-0123456789abcdef-01',
|
|
459
|
+
baggage: 'tenant.id=tenant-123',
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const extractedContext = extractTraceContext(headers);
|
|
463
|
+
|
|
464
|
+
// Context should be extracted (actual validation depends on propagator)
|
|
465
|
+
expect(extractedContext).toBeDefined();
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it('should handle array headers', () => {
|
|
469
|
+
const headers = {
|
|
470
|
+
traceparent: ['00-0123456789abcdef0123456789abcdef-0123456789abcdef-01'],
|
|
471
|
+
baggage: ['tenant.id=tenant-123'],
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
const extractedContext = extractTraceContext(headers);
|
|
475
|
+
|
|
476
|
+
expect(extractedContext).toBeDefined();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('should handle missing headers gracefully', () => {
|
|
480
|
+
const headers = {};
|
|
481
|
+
|
|
482
|
+
const extractedContext = extractTraceContext(headers);
|
|
483
|
+
|
|
484
|
+
// Should return a context even if no trace headers are present
|
|
485
|
+
expect(extractedContext).toBeDefined();
|
|
486
|
+
});
|
|
487
|
+
});
|