@nathapp/nestjs-observability 4.0.0 → 4.0.1
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/package.json +6 -6
- package/.eslintrc.js +0 -8
- package/.nax/features/nestjs-observability/.nax-acceptance.test.ts +0 -797
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -43
- package/lib/constant/index.ts +0 -1
- package/lib/constant/observability.constants.ts +0 -21
- package/lib/index.ts +0 -17
- package/lib/interface/index.ts +0 -2
- package/lib/interface/observability-module-options.interface.ts +0 -47
- package/lib/interface/tracing-bootstrap-options.interface.ts +0 -17
- package/lib/metrics/http-metrics.interceptor.ts +0 -174
- package/lib/metrics/index.ts +0 -7
- package/lib/metrics/metrics.providers.ts +0 -134
- package/lib/module/index.ts +0 -1
- package/lib/module/observability.module.ts +0 -236
- package/lib/tracing/index.ts +0 -4
- package/lib/tracing/init-tracing.ts +0 -92
- package/lib/tracing/shutdown.ts +0 -28
- package/lib/tracing/trace-context.interceptor.ts +0 -42
- package/lib/tracing/trace-internal.ts +0 -19
- package/lib/tracing/trace.decorator.ts +0 -63
- package/lib/tracing/trace.service.ts +0 -48
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -28
- package/test/metrics/http-metrics.interceptor.spec.ts +0 -453
- package/test/metrics/metrics.providers.spec.ts +0 -97
- package/test/module/observability.module.spec.ts +0 -229
- package/test/tracing/build-node-sdk.spec.ts +0 -224
- package/test/tracing/init-tracing.idempotency.spec.ts +0 -64
- package/test/tracing/init-tracing.shutdown.spec.ts +0 -88
- package/test/tracing/shutdown.spec.ts +0 -109
- package/test/tracing/trace-context.interceptor.spec.ts +0 -221
- package/test/tracing/trace.decorator.spec.ts +0 -153
- package/test/tracing/trace.service.spec.ts +0 -205
- package/tsconfig.json +0 -9
|
@@ -1,453 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { CallHandler, ExecutionContext, HttpException } from '@nestjs/common';
|
|
3
|
-
import { Observable, of, throwError } from 'rxjs';
|
|
4
|
-
import {
|
|
5
|
-
HttpMetricsInterceptor,
|
|
6
|
-
PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS,
|
|
7
|
-
PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL,
|
|
8
|
-
PROM_METRIC_HTTP_REQUESTS_TOTAL,
|
|
9
|
-
} from '../../lib/metrics';
|
|
10
|
-
|
|
11
|
-
const makeContext = (
|
|
12
|
-
method = 'GET',
|
|
13
|
-
url = '/test-route',
|
|
14
|
-
): ExecutionContext =>
|
|
15
|
-
({
|
|
16
|
-
getType: () => 'http',
|
|
17
|
-
switchToHttp: () => ({
|
|
18
|
-
getRequest: () => ({ method, url, route: { path: url } }),
|
|
19
|
-
getResponse: () => ({ statusCode: 200 }),
|
|
20
|
-
}),
|
|
21
|
-
}) as unknown as ExecutionContext;
|
|
22
|
-
|
|
23
|
-
const makeContextWithRoute = (
|
|
24
|
-
method: string,
|
|
25
|
-
url: string,
|
|
26
|
-
routePath: string | undefined,
|
|
27
|
-
statusCode?: number,
|
|
28
|
-
): ExecutionContext =>
|
|
29
|
-
({
|
|
30
|
-
getType: () => 'http',
|
|
31
|
-
switchToHttp: () => ({
|
|
32
|
-
getRequest: () => ({
|
|
33
|
-
method,
|
|
34
|
-
url,
|
|
35
|
-
route: routePath === undefined ? undefined : { path: routePath },
|
|
36
|
-
}),
|
|
37
|
-
getResponse: () => (statusCode !== undefined ? { statusCode } : {}),
|
|
38
|
-
}),
|
|
39
|
-
}) as unknown as ExecutionContext;
|
|
40
|
-
|
|
41
|
-
const makeOkHandler = <T>(value: T): CallHandler => ({ handle: () => of(value) });
|
|
42
|
-
|
|
43
|
-
const makeThrowingHandler = (err: unknown): CallHandler => ({
|
|
44
|
-
handle: () => throwError(() => err),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
describe('AC-1: HttpMetricsInterceptor instantiation and intercept return value', () => {
|
|
48
|
-
it('is importable from the package and intercept returns an Observable that emits the handler value', async () => {
|
|
49
|
-
const { firstValueFrom } = await import('rxjs');
|
|
50
|
-
const { toArray: toArrayFn } = await import('rxjs/operators');
|
|
51
|
-
|
|
52
|
-
const interceptor = new HttpMetricsInterceptor();
|
|
53
|
-
expect(typeof interceptor.intercept).toBe('function');
|
|
54
|
-
|
|
55
|
-
const result = interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
56
|
-
expect(result).toBeInstanceOf(Observable);
|
|
57
|
-
const values = await firstValueFrom(result.pipe(toArrayFn()));
|
|
58
|
-
expect(values).toEqual(['ok']);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe('HttpMetricsInterceptor metric recording (AC-2, AC-3, AC-4, AC-7)', () => {
|
|
63
|
-
const setupInterceptor = async (
|
|
64
|
-
overrides: {
|
|
65
|
-
requestsInc?: (labels: Record<string, string>) => void;
|
|
66
|
-
errorsInc?: (labels: Record<string, string>) => void;
|
|
67
|
-
durationObserve?: (labels: Record<string, string>, value: number) => void;
|
|
68
|
-
} = {},
|
|
69
|
-
) => {
|
|
70
|
-
const requestsInc = overrides.requestsInc ?? jest.fn();
|
|
71
|
-
const errorsInc = overrides.errorsInc ?? jest.fn();
|
|
72
|
-
const durationObserve = overrides.durationObserve ?? jest.fn();
|
|
73
|
-
|
|
74
|
-
const { Test } = await import('@nestjs/testing');
|
|
75
|
-
const moduleRef = await Test.createTestingModule({
|
|
76
|
-
providers: [
|
|
77
|
-
HttpMetricsInterceptor,
|
|
78
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: { inc: requestsInc } },
|
|
79
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: { inc: errorsInc } },
|
|
80
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: { observe: durationObserve } },
|
|
81
|
-
],
|
|
82
|
-
}).compile();
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
interceptor: moduleRef.get(HttpMetricsInterceptor),
|
|
86
|
-
requestsInc,
|
|
87
|
-
errorsInc,
|
|
88
|
-
durationObserve,
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
it('AC-2: increments the requests counter exactly once with method/route/status labels on success', async () => {
|
|
93
|
-
const { firstValueFrom } = await import('rxjs');
|
|
94
|
-
const { toArray } = await import('rxjs/operators');
|
|
95
|
-
|
|
96
|
-
const { interceptor, requestsInc, errorsInc, durationObserve } = await setupInterceptor();
|
|
97
|
-
await firstValueFrom(
|
|
98
|
-
interceptor.intercept(makeContext('GET', '/users'), makeOkHandler('ok')).pipe(toArray()),
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
expect(requestsInc).toHaveBeenCalledTimes(1);
|
|
102
|
-
expect(requestsInc).toHaveBeenCalledWith({ method: 'GET', route: '/users', status: '200' });
|
|
103
|
-
expect(errorsInc).not.toHaveBeenCalled();
|
|
104
|
-
expect(durationObserve).toHaveBeenCalledTimes(1);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('AC-3: handler throws an error with status >= 500, errors counter increments once, original error is rethrown unchanged', async () => {
|
|
108
|
-
const { firstValueFrom } = await import('rxjs');
|
|
109
|
-
const { toArray } = await import('rxjs/operators');
|
|
110
|
-
|
|
111
|
-
const { interceptor, requestsInc, errorsInc, durationObserve } = await setupInterceptor();
|
|
112
|
-
const original = new HttpException('boom', 500);
|
|
113
|
-
|
|
114
|
-
let caught: unknown;
|
|
115
|
-
try {
|
|
116
|
-
await firstValueFrom(
|
|
117
|
-
interceptor
|
|
118
|
-
.intercept(makeContext('POST', '/orders'), makeThrowingHandler(original))
|
|
119
|
-
.pipe(toArray()),
|
|
120
|
-
);
|
|
121
|
-
} catch (err) {
|
|
122
|
-
caught = err;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
expect(caught).toBe(original);
|
|
126
|
-
expect(errorsInc).toHaveBeenCalledTimes(1);
|
|
127
|
-
expect(errorsInc).toHaveBeenCalledWith({ method: 'POST', route: '/orders', status: '500' });
|
|
128
|
-
expect(requestsInc).toHaveBeenCalledTimes(1);
|
|
129
|
-
expect(durationObserve).toHaveBeenCalledTimes(1);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('AC-3: handler throws an error with status < 500 (404) — errors counter does NOT increment, error is rethrown', async () => {
|
|
133
|
-
const { firstValueFrom } = await import('rxjs');
|
|
134
|
-
const { toArray } = await import('rxjs/operators');
|
|
135
|
-
|
|
136
|
-
const { interceptor, requestsInc, errorsInc, durationObserve } = await setupInterceptor();
|
|
137
|
-
const notFound = new HttpException('not found', 404);
|
|
138
|
-
|
|
139
|
-
let caught: unknown;
|
|
140
|
-
try {
|
|
141
|
-
await firstValueFrom(
|
|
142
|
-
interceptor
|
|
143
|
-
.intercept(makeContext('GET', '/missing'), makeThrowingHandler(notFound))
|
|
144
|
-
.pipe(toArray()),
|
|
145
|
-
);
|
|
146
|
-
} catch (err) {
|
|
147
|
-
caught = err;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
expect(caught).toBe(notFound);
|
|
151
|
-
expect(errorsInc).not.toHaveBeenCalled();
|
|
152
|
-
expect(requestsInc).toHaveBeenCalledTimes(1);
|
|
153
|
-
expect(durationObserve).toHaveBeenCalledTimes(1);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('AC-4: duration histogram observes exactly one sample labelled with method/route/status on success', async () => {
|
|
157
|
-
const { firstValueFrom } = await import('rxjs');
|
|
158
|
-
const { toArray } = await import('rxjs/operators');
|
|
159
|
-
|
|
160
|
-
const { interceptor, durationObserve } = await setupInterceptor();
|
|
161
|
-
await firstValueFrom(
|
|
162
|
-
interceptor.intercept(makeContext('PUT', '/things/1'), makeOkHandler('ok')).pipe(toArray()),
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
expect(durationObserve).toHaveBeenCalledTimes(1);
|
|
166
|
-
expect(durationObserve).toHaveBeenCalledWith(
|
|
167
|
-
{ method: 'PUT', route: '/things/1', status: '200' },
|
|
168
|
-
expect.any(Number),
|
|
169
|
-
);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
describe('AC-5: HttpMetricsInterceptor ignoreRoutes', () => {
|
|
174
|
-
const setupIgnoring = async (ignoreRoutes: string[]) => {
|
|
175
|
-
const requestsInc = jest.fn();
|
|
176
|
-
const errorsInc = jest.fn();
|
|
177
|
-
const durationObserve = jest.fn();
|
|
178
|
-
|
|
179
|
-
const { Test } = await import('@nestjs/testing');
|
|
180
|
-
const moduleRef = await Test.createTestingModule({
|
|
181
|
-
providers: [
|
|
182
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: { inc: requestsInc } },
|
|
183
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: { inc: errorsInc } },
|
|
184
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: { observe: durationObserve } },
|
|
185
|
-
HttpMetricsInterceptor,
|
|
186
|
-
],
|
|
187
|
-
}).compile();
|
|
188
|
-
|
|
189
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
190
|
-
(interceptor as unknown as { setIgnoreRoutes: (r: string[]) => void }).setIgnoreRoutes(
|
|
191
|
-
ignoreRoutes,
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
interceptor,
|
|
196
|
-
requestsInc,
|
|
197
|
-
errorsInc,
|
|
198
|
-
durationObserve,
|
|
199
|
-
};
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
it('leaves all three metrics untouched for a route listed in ignoreRoutes (success and error)', async () => {
|
|
203
|
-
const { firstValueFrom } = await import('rxjs');
|
|
204
|
-
const { toArray } = await import('rxjs/operators');
|
|
205
|
-
|
|
206
|
-
const { interceptor, requestsInc, errorsInc, durationObserve } = await setupIgnoring(['/health']);
|
|
207
|
-
|
|
208
|
-
await firstValueFrom(
|
|
209
|
-
interceptor.intercept(makeContext('GET', '/health'), makeOkHandler('ok')).pipe(toArray()),
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
expect(requestsInc).not.toHaveBeenCalled();
|
|
213
|
-
expect(errorsInc).not.toHaveBeenCalled();
|
|
214
|
-
expect(durationObserve).not.toHaveBeenCalled();
|
|
215
|
-
|
|
216
|
-
const err = new HttpException('boom', 503);
|
|
217
|
-
let caught: unknown;
|
|
218
|
-
try {
|
|
219
|
-
await firstValueFrom(
|
|
220
|
-
interceptor
|
|
221
|
-
.intercept(makeContext('GET', '/health'), makeThrowingHandler(err))
|
|
222
|
-
.pipe(toArray()),
|
|
223
|
-
);
|
|
224
|
-
} catch (e) {
|
|
225
|
-
caught = e;
|
|
226
|
-
}
|
|
227
|
-
expect(caught).toBe(err);
|
|
228
|
-
expect(requestsInc).not.toHaveBeenCalled();
|
|
229
|
-
expect(errorsInc).not.toHaveBeenCalled();
|
|
230
|
-
expect(durationObserve).not.toHaveBeenCalled();
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
describe('AC-7: internal metric-recording failure does not break the request', () => {
|
|
235
|
-
it('observable still emits the handler value when counter.inc() throws', async () => {
|
|
236
|
-
const { firstValueFrom } = await import('rxjs');
|
|
237
|
-
const { toArray } = await import('rxjs/operators');
|
|
238
|
-
|
|
239
|
-
const throwingCounter = {
|
|
240
|
-
inc: jest.fn().mockImplementation(() => {
|
|
241
|
-
throw new Error('metrics down');
|
|
242
|
-
}),
|
|
243
|
-
};
|
|
244
|
-
const noopCounter = { inc: jest.fn() };
|
|
245
|
-
const noopHistogram = { observe: jest.fn() };
|
|
246
|
-
|
|
247
|
-
const { Test } = await import('@nestjs/testing');
|
|
248
|
-
const moduleRef = await Test.createTestingModule({
|
|
249
|
-
providers: [
|
|
250
|
-
HttpMetricsInterceptor,
|
|
251
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: throwingCounter },
|
|
252
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: noopCounter },
|
|
253
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: noopHistogram },
|
|
254
|
-
],
|
|
255
|
-
}).compile();
|
|
256
|
-
|
|
257
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
258
|
-
const values = await firstValueFrom(
|
|
259
|
-
interceptor.intercept(makeContext('GET', '/x'), makeOkHandler('value')).pipe(toArray()),
|
|
260
|
-
);
|
|
261
|
-
expect(values).toEqual(['value']);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('observable still emits the handler value when histogram.observe() throws', async () => {
|
|
265
|
-
const { firstValueFrom } = await import('rxjs');
|
|
266
|
-
const { toArray } = await import('rxjs/operators');
|
|
267
|
-
|
|
268
|
-
const noopCounter = { inc: jest.fn() };
|
|
269
|
-
const throwingHistogram = {
|
|
270
|
-
observe: jest.fn().mockImplementation(() => {
|
|
271
|
-
throw new Error('histogram down');
|
|
272
|
-
}),
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
const { Test } = await import('@nestjs/testing');
|
|
276
|
-
const moduleRef = await Test.createTestingModule({
|
|
277
|
-
providers: [
|
|
278
|
-
HttpMetricsInterceptor,
|
|
279
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: noopCounter },
|
|
280
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: noopCounter },
|
|
281
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: throwingHistogram },
|
|
282
|
-
],
|
|
283
|
-
}).compile();
|
|
284
|
-
|
|
285
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
286
|
-
const values = await firstValueFrom(
|
|
287
|
-
interceptor.intercept(makeContext('GET', '/x'), makeOkHandler('value')).pipe(toArray()),
|
|
288
|
-
);
|
|
289
|
-
expect(values).toEqual(['value']);
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
it('AC-7 (error path): handler throws AND counter.inc() throws inside catchError — original error still propagates', async () => {
|
|
293
|
-
const { firstValueFrom } = await import('rxjs');
|
|
294
|
-
const { toArray } = await import('rxjs/operators');
|
|
295
|
-
|
|
296
|
-
const throwingCounter = {
|
|
297
|
-
inc: jest.fn().mockImplementation(() => {
|
|
298
|
-
throw new Error('metrics down');
|
|
299
|
-
}),
|
|
300
|
-
};
|
|
301
|
-
const noopHistogram = { observe: jest.fn() };
|
|
302
|
-
|
|
303
|
-
const { Test } = await import('@nestjs/testing');
|
|
304
|
-
const moduleRef = await Test.createTestingModule({
|
|
305
|
-
providers: [
|
|
306
|
-
HttpMetricsInterceptor,
|
|
307
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: throwingCounter },
|
|
308
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: throwingCounter },
|
|
309
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: noopHistogram },
|
|
310
|
-
],
|
|
311
|
-
}).compile();
|
|
312
|
-
|
|
313
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
314
|
-
const original = new HttpException('boom', 500);
|
|
315
|
-
|
|
316
|
-
let caught: unknown;
|
|
317
|
-
try {
|
|
318
|
-
await firstValueFrom(
|
|
319
|
-
interceptor
|
|
320
|
-
.intercept(makeContext('POST', '/fail'), makeThrowingHandler(original))
|
|
321
|
-
.pipe(toArray()),
|
|
322
|
-
);
|
|
323
|
-
} catch (err) {
|
|
324
|
-
caught = err;
|
|
325
|
-
}
|
|
326
|
-
expect(caught).toBe(original);
|
|
327
|
-
expect(throwingCounter.inc).toHaveBeenCalled();
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
describe('route label cardinality: route.path vs URL fallback', () => {
|
|
332
|
-
const setupInterceptor = async () => {
|
|
333
|
-
const requestsInc = jest.fn();
|
|
334
|
-
const errorsInc = jest.fn();
|
|
335
|
-
const durationObserve = jest.fn();
|
|
336
|
-
const { Test } = await import('@nestjs/testing');
|
|
337
|
-
const moduleRef = await Test.createTestingModule({
|
|
338
|
-
providers: [
|
|
339
|
-
HttpMetricsInterceptor,
|
|
340
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: { inc: requestsInc } },
|
|
341
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: { inc: errorsInc } },
|
|
342
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: { observe: durationObserve } },
|
|
343
|
-
],
|
|
344
|
-
}).compile();
|
|
345
|
-
return {
|
|
346
|
-
interceptor: moduleRef.get(HttpMetricsInterceptor),
|
|
347
|
-
requestsInc,
|
|
348
|
-
};
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
it('prefers request.route.path over request.url (route label uses the pattern, not the concrete URL)', async () => {
|
|
352
|
-
const { firstValueFrom } = await import('rxjs');
|
|
353
|
-
const { toArray } = await import('rxjs/operators');
|
|
354
|
-
const { interceptor, requestsInc } = await setupInterceptor();
|
|
355
|
-
|
|
356
|
-
const ctx = makeContextWithRoute('GET', '/users/42', '/users/:id');
|
|
357
|
-
await firstValueFrom(
|
|
358
|
-
interceptor.intercept(ctx, makeOkHandler('ok')).pipe(toArray()),
|
|
359
|
-
);
|
|
360
|
-
|
|
361
|
-
expect(requestsInc).toHaveBeenCalledWith({
|
|
362
|
-
method: 'GET',
|
|
363
|
-
route: '/users/:id',
|
|
364
|
-
status: '200',
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('strips the query string from request.url when route.path is absent (cardinality bound)', async () => {
|
|
369
|
-
const { firstValueFrom } = await import('rxjs');
|
|
370
|
-
const { toArray } = await import('rxjs/operators');
|
|
371
|
-
const { interceptor, requestsInc } = await setupInterceptor();
|
|
372
|
-
|
|
373
|
-
const ctx = makeContextWithRoute('GET', '/users?id=42&page=2', undefined);
|
|
374
|
-
await firstValueFrom(
|
|
375
|
-
interceptor.intercept(ctx, makeOkHandler('ok')).pipe(toArray()),
|
|
376
|
-
);
|
|
377
|
-
|
|
378
|
-
expect(requestsInc).toHaveBeenCalledWith({
|
|
379
|
-
method: 'GET',
|
|
380
|
-
route: '/users',
|
|
381
|
-
status: '200',
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
it('setIgnoreRoutes(["/users/:id"]) matches every concrete URL under the template', async () => {
|
|
386
|
-
const { firstValueFrom } = await import('rxjs');
|
|
387
|
-
const { toArray } = await import('rxjs/operators');
|
|
388
|
-
const noopCounter = { inc: jest.fn() };
|
|
389
|
-
const noopHistogram = { observe: jest.fn() };
|
|
390
|
-
const { Test } = await import('@nestjs/testing');
|
|
391
|
-
const moduleRef = await Test.createTestingModule({
|
|
392
|
-
providers: [
|
|
393
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: noopCounter },
|
|
394
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: noopCounter },
|
|
395
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: noopHistogram },
|
|
396
|
-
HttpMetricsInterceptor,
|
|
397
|
-
],
|
|
398
|
-
}).compile();
|
|
399
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
400
|
-
interceptor.setIgnoreRoutes(['/users/:id']);
|
|
401
|
-
|
|
402
|
-
for (const url of ['/users/1', '/users/42', '/users/abc']) {
|
|
403
|
-
await firstValueFrom(
|
|
404
|
-
interceptor.intercept(makeContextWithRoute('GET', url, '/users/:id'), makeOkHandler('ok')).pipe(toArray()),
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
expect(noopCounter.inc).not.toHaveBeenCalled();
|
|
409
|
-
expect(noopHistogram.observe).not.toHaveBeenCalled();
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
describe('Fastify response: statusCode from reply.raw', () => {
|
|
414
|
-
const setupWithRawStatus = async () => {
|
|
415
|
-
const noopCounter = { inc: jest.fn() };
|
|
416
|
-
const noopHistogram = { observe: jest.fn() };
|
|
417
|
-
const { Test } = await import('@nestjs/testing');
|
|
418
|
-
const moduleRef = await Test.createTestingModule({
|
|
419
|
-
providers: [
|
|
420
|
-
HttpMetricsInterceptor,
|
|
421
|
-
{ provide: PROM_METRIC_HTTP_REQUESTS_TOTAL, useValue: noopCounter },
|
|
422
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL, useValue: noopCounter },
|
|
423
|
-
{ provide: PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS, useValue: noopHistogram },
|
|
424
|
-
],
|
|
425
|
-
}).compile();
|
|
426
|
-
const interceptor = moduleRef.get(HttpMetricsInterceptor);
|
|
427
|
-
return { interceptor, noopCounter };
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
it('reads statusCode from response.raw.statusCode when response.statusCode is absent (Fastify)', async () => {
|
|
431
|
-
const { firstValueFrom } = await import('rxjs');
|
|
432
|
-
const { toArray } = await import('rxjs/operators');
|
|
433
|
-
const { interceptor, noopCounter } = await setupWithRawStatus();
|
|
434
|
-
|
|
435
|
-
const fastifyLikeContext = {
|
|
436
|
-
getType: () => 'http',
|
|
437
|
-
switchToHttp: () => ({
|
|
438
|
-
getRequest: () => ({ method: 'POST', url: '/fast', route: { path: '/fast' } }),
|
|
439
|
-
getResponse: () => ({ raw: { statusCode: 201 } }),
|
|
440
|
-
}),
|
|
441
|
-
} as unknown as ExecutionContext;
|
|
442
|
-
|
|
443
|
-
await firstValueFrom(
|
|
444
|
-
interceptor.intercept(fastifyLikeContext, makeOkHandler('ok')).pipe(toArray()),
|
|
445
|
-
);
|
|
446
|
-
|
|
447
|
-
expect(noopCounter.inc).toHaveBeenCalledWith({
|
|
448
|
-
method: 'POST',
|
|
449
|
-
route: '/fast',
|
|
450
|
-
status: '201',
|
|
451
|
-
});
|
|
452
|
-
});
|
|
453
|
-
});
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import * as client from 'prom-client';
|
|
4
|
-
import { buildRedMetricProviders } from '../../lib/metrics/metrics.providers';
|
|
5
|
-
import {
|
|
6
|
-
PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS,
|
|
7
|
-
PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL,
|
|
8
|
-
PROM_METRIC_HTTP_REQUESTS_TOTAL,
|
|
9
|
-
} from '../../lib/metrics';
|
|
10
|
-
|
|
11
|
-
const unique = (): string =>
|
|
12
|
-
`t_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
13
|
-
|
|
14
|
-
interface MetricInternals {
|
|
15
|
-
name: string;
|
|
16
|
-
labelNames: string[];
|
|
17
|
-
upperBounds?: number[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
describe('buildRedMetricProviders', () => {
|
|
21
|
-
it('returns an array of 3 providers: requests, errors, duration histogram', () => {
|
|
22
|
-
const providers = buildRedMetricProviders(unique(), [0.1, 1, 5]);
|
|
23
|
-
expect(providers).toHaveLength(3);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('wires each provider under the PROM_METRIC_HTTP_* token', async () => {
|
|
27
|
-
const providers = buildRedMetricProviders(unique(), [0.1, 1]);
|
|
28
|
-
const moduleRef = await Test.createTestingModule({
|
|
29
|
-
providers: [...providers],
|
|
30
|
-
}).compile();
|
|
31
|
-
|
|
32
|
-
expect(moduleRef.get(PROM_METRIC_HTTP_REQUESTS_TOTAL)).toBeInstanceOf(client.Counter);
|
|
33
|
-
expect(moduleRef.get(PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL)).toBeInstanceOf(client.Counter);
|
|
34
|
-
expect(moduleRef.get(PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS)).toBeInstanceOf(
|
|
35
|
-
client.Histogram,
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('honours a non-empty prefix on every metric name', async () => {
|
|
40
|
-
const providers = buildRedMetricProviders('api_', [0.1, 1]);
|
|
41
|
-
const moduleRef = await Test.createTestingModule({
|
|
42
|
-
providers: [...providers],
|
|
43
|
-
}).compile();
|
|
44
|
-
|
|
45
|
-
const requests = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUESTS_TOTAL) as unknown as MetricInternals;
|
|
46
|
-
const errors = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL) as unknown as MetricInternals;
|
|
47
|
-
const duration = moduleRef.get<client.Histogram<string>>(PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS) as unknown as MetricInternals;
|
|
48
|
-
|
|
49
|
-
expect(requests.name).toBe('api_http_requests_total');
|
|
50
|
-
expect(errors.name).toBe('api_http_request_errors_total');
|
|
51
|
-
expect(duration.name).toBe('api_http_request_duration_seconds');
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('emits no double-prefix when prefix is empty', async () => {
|
|
55
|
-
const providers = buildRedMetricProviders('', [0.1, 1]);
|
|
56
|
-
const moduleRef = await Test.createTestingModule({
|
|
57
|
-
providers: [...providers],
|
|
58
|
-
}).compile();
|
|
59
|
-
|
|
60
|
-
const requests = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUESTS_TOTAL) as unknown as MetricInternals;
|
|
61
|
-
const errors = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL) as unknown as MetricInternals;
|
|
62
|
-
const duration = moduleRef.get<client.Histogram<string>>(PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS) as unknown as MetricInternals;
|
|
63
|
-
|
|
64
|
-
expect(requests.name).toBe('http_requests_total');
|
|
65
|
-
expect(errors.name).toBe('http_request_errors_total');
|
|
66
|
-
expect(duration.name).toBe('http_request_duration_seconds');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('passes through the supplied buckets into the duration histogram provider', async () => {
|
|
70
|
-
const buckets = [0.05, 0.2, 1, 10];
|
|
71
|
-
const providers = buildRedMetricProviders(unique(), buckets);
|
|
72
|
-
const moduleRef = await Test.createTestingModule({
|
|
73
|
-
providers: [...providers],
|
|
74
|
-
}).compile();
|
|
75
|
-
|
|
76
|
-
const histogram = moduleRef.get<client.Histogram<string>>(PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS) as unknown as MetricInternals;
|
|
77
|
-
expect(histogram.upperBounds).toBeDefined();
|
|
78
|
-
for (const b of buckets) {
|
|
79
|
-
expect(histogram.upperBounds).toContain(b);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('configures every metric with method/route/status label names', async () => {
|
|
84
|
-
const providers = buildRedMetricProviders(unique(), [1, 2]);
|
|
85
|
-
const moduleRef = await Test.createTestingModule({
|
|
86
|
-
providers: [...providers],
|
|
87
|
-
}).compile();
|
|
88
|
-
|
|
89
|
-
const requests = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUESTS_TOTAL) as unknown as MetricInternals;
|
|
90
|
-
const errors = moduleRef.get<client.Counter<string>>(PROM_METRIC_HTTP_REQUEST_ERRORS_TOTAL) as unknown as MetricInternals;
|
|
91
|
-
const duration = moduleRef.get<client.Histogram<string>>(PROM_METRIC_HTTP_REQUEST_DURATION_SECONDS) as unknown as MetricInternals;
|
|
92
|
-
|
|
93
|
-
expect([...requests.labelNames].sort()).toEqual(['method', 'route', 'status']);
|
|
94
|
-
expect([...errors.labelNames].sort()).toEqual(['method', 'route', 'status']);
|
|
95
|
-
expect([...duration.labelNames].sort()).toEqual(['method', 'route', 'status']);
|
|
96
|
-
});
|
|
97
|
-
});
|