@nathapp/nestjs-observability 3.3.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 +11 -11
- 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 -15
- 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,109 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import type { ShutdownHandler } from '../../lib/tracing/shutdown';
|
|
3
|
-
|
|
4
|
-
type RegisteredListener = {
|
|
5
|
-
event: NodeJS.Signals | 'beforeExit';
|
|
6
|
-
listener: () => void;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const listeners: Record<string, RegisteredListener['listener'][]> = {};
|
|
10
|
-
|
|
11
|
-
const originalOnce = process.once.bind(process);
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
for (const key of Object.keys(listeners)) delete listeners[key];
|
|
15
|
-
jest.resetModules();
|
|
16
|
-
(process as { once: typeof process.once }).once = (((
|
|
17
|
-
event: string,
|
|
18
|
-
listener: (...args: unknown[]) => void,
|
|
19
|
-
) => {
|
|
20
|
-
if (!listeners[event]) listeners[event] = [];
|
|
21
|
-
listeners[event]!.push(listener as RegisteredListener['listener']);
|
|
22
|
-
}) as unknown) as typeof process.once;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
(process as { once: typeof process.once }).once = originalOnce;
|
|
27
|
-
jest.resetModules();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
const loadRegisterShutdown = (): typeof import('../../lib/tracing/shutdown').registerShutdown => {
|
|
31
|
-
let mod: typeof import('../../lib/tracing/shutdown') | undefined;
|
|
32
|
-
jest.isolateModules(() => {
|
|
33
|
-
mod = require('../../lib/tracing/shutdown');
|
|
34
|
-
});
|
|
35
|
-
if (!mod) throw new Error('shutdown module not loaded');
|
|
36
|
-
return mod.registerShutdown;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
describe('registerShutdown — process-level wiring', () => {
|
|
40
|
-
it('registers one process.once listener for SIGTERM, SIGINT, and beforeExit', () => {
|
|
41
|
-
const registerShutdown = loadRegisterShutdown();
|
|
42
|
-
registerShutdown(() => undefined);
|
|
43
|
-
|
|
44
|
-
expect(listeners.SIGTERM).toHaveLength(1);
|
|
45
|
-
expect(listeners.SIGINT).toHaveLength(1);
|
|
46
|
-
expect(listeners.beforeExit).toHaveLength(1);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('invokes the registered handler exactly once when SIGTERM fires', async () => {
|
|
50
|
-
const registerShutdown = loadRegisterShutdown();
|
|
51
|
-
const handler = jest.fn<ShutdownHandler>();
|
|
52
|
-
registerShutdown(handler);
|
|
53
|
-
|
|
54
|
-
listeners.SIGTERM![0]!();
|
|
55
|
-
await Promise.resolve();
|
|
56
|
-
await Promise.resolve();
|
|
57
|
-
|
|
58
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('invokes the registered handler exactly once across multiple signals (deduplication)', async () => {
|
|
62
|
-
const registerShutdown = loadRegisterShutdown();
|
|
63
|
-
const handler = jest.fn<ShutdownHandler>();
|
|
64
|
-
registerShutdown(handler);
|
|
65
|
-
|
|
66
|
-
listeners.SIGTERM![0]!();
|
|
67
|
-
listeners.SIGINT![0]!();
|
|
68
|
-
listeners.beforeExit![0]!();
|
|
69
|
-
await Promise.resolve();
|
|
70
|
-
await Promise.resolve();
|
|
71
|
-
|
|
72
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('swallows handler errors so a failing handler does not crash the process on exit', async () => {
|
|
76
|
-
const registerShutdown = loadRegisterShutdown();
|
|
77
|
-
registerShutdown(() => {
|
|
78
|
-
return Promise.reject(new Error('flush failed'));
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const unhandled = jest.fn();
|
|
82
|
-
const listener = (reason: unknown): void => {
|
|
83
|
-
unhandled(reason);
|
|
84
|
-
};
|
|
85
|
-
process.on('unhandledRejection', listener);
|
|
86
|
-
|
|
87
|
-
listeners.SIGTERM![0]!();
|
|
88
|
-
await Promise.resolve();
|
|
89
|
-
await Promise.resolve();
|
|
90
|
-
await Promise.resolve();
|
|
91
|
-
|
|
92
|
-
expect(unhandled).not.toHaveBeenCalled();
|
|
93
|
-
|
|
94
|
-
process.off('unhandledRejection', listener);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('invokes async handlers and waits for the returned promise before completing', async () => {
|
|
98
|
-
const registerShutdown = loadRegisterShutdown();
|
|
99
|
-
const handler = jest.fn<ShutdownHandler>(async () => {
|
|
100
|
-
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
101
|
-
});
|
|
102
|
-
registerShutdown(handler);
|
|
103
|
-
|
|
104
|
-
listeners.SIGTERM![0]!();
|
|
105
|
-
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
106
|
-
|
|
107
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { CallHandler, ExecutionContext } from '@nestjs/common';
|
|
3
|
-
import { trace } from '@opentelemetry/api';
|
|
4
|
-
import { RequestContextService } from '@nathapp/nestjs-logging';
|
|
5
|
-
import { of } from 'rxjs';
|
|
6
|
-
import { TraceContextInterceptor } from '../../lib/tracing/trace-context.interceptor';
|
|
7
|
-
|
|
8
|
-
type SpanMock = {
|
|
9
|
-
setAttribute: jest.Mock;
|
|
10
|
-
spanContext: jest.Mock;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const makeSpanContext = (traceId: string, spanId: string) => ({
|
|
14
|
-
traceId,
|
|
15
|
-
spanId,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const makeSpan = (traceId: string, spanId: string): SpanMock => ({
|
|
19
|
-
setAttribute: jest.fn(),
|
|
20
|
-
spanContext: jest.fn().mockReturnValue(makeSpanContext(traceId, spanId)),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const installActiveSpan = (span: SpanMock | null) => {
|
|
24
|
-
return jest.spyOn(trace, 'getActiveSpan').mockReturnValue(
|
|
25
|
-
span as unknown as ReturnType<typeof trace.getActiveSpan>,
|
|
26
|
-
);
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const makeContext = (): ExecutionContext => ({}) as unknown as ExecutionContext;
|
|
30
|
-
|
|
31
|
-
const makeOkHandler = <T>(value: T): CallHandler => ({ handle: () => of(value) });
|
|
32
|
-
|
|
33
|
-
const makeRequestContextStub = (
|
|
34
|
-
requestId: string | undefined,
|
|
35
|
-
): { get: jest.Mock; set: jest.Mock } & Pick<RequestContextService, 'get' | 'set'> => {
|
|
36
|
-
const get = jest.fn((key: string) => (key === 'requestId' ? requestId : undefined)) as jest.Mock;
|
|
37
|
-
const set = jest.fn() as jest.Mock;
|
|
38
|
-
return { get, set } as never;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
describe('TraceContextInterceptor (correlation bridge between ALS request context and active spans)', () => {
|
|
42
|
-
let getActiveSpanSpy: ReturnType<typeof jest.spyOn>;
|
|
43
|
-
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
jest.restoreAllMocks();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
if (getActiveSpanSpy) getActiveSpanSpy.mockRestore();
|
|
50
|
-
jest.restoreAllMocks();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('AC-1: importable and intercept returns Observable emitting the handler value', () => {
|
|
54
|
-
it('is importable from the package tracing barrel and exposes intercept', async () => {
|
|
55
|
-
const mod = await import('../../lib/tracing');
|
|
56
|
-
expect(typeof mod.TraceContextInterceptor).toBe('function');
|
|
57
|
-
|
|
58
|
-
const interceptor = new TraceContextInterceptor(
|
|
59
|
-
makeRequestContextStub(undefined) as unknown as RequestContextService,
|
|
60
|
-
);
|
|
61
|
-
expect(typeof interceptor.intercept).toBe('function');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('returns an Observable that emits the handler value when handler returns of("ok")', async () => {
|
|
65
|
-
const span = makeSpan('trace-1', 'span-1');
|
|
66
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
67
|
-
|
|
68
|
-
const interceptor = new TraceContextInterceptor(
|
|
69
|
-
makeRequestContextStub('corr-1') as unknown as RequestContextService,
|
|
70
|
-
);
|
|
71
|
-
const result = interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
72
|
-
|
|
73
|
-
expect(result).toBeDefined();
|
|
74
|
-
const { firstValueFrom } = await import('rxjs');
|
|
75
|
-
const { toArray } = await import('rxjs/operators');
|
|
76
|
-
const values = await firstValueFrom(result.pipe(toArray()));
|
|
77
|
-
expect(values).toEqual(['ok']);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
describe('AC-2: active span + logging context with requestId → span attribute set', () => {
|
|
82
|
-
it('sets span attribute app.correlation_id to the requestId from logging context', () => {
|
|
83
|
-
const span = makeSpan('trace-A', 'span-A');
|
|
84
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
85
|
-
|
|
86
|
-
const interceptor = new TraceContextInterceptor(
|
|
87
|
-
makeRequestContextStub('corr-xyz') as unknown as RequestContextService,
|
|
88
|
-
);
|
|
89
|
-
interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
90
|
-
|
|
91
|
-
expect(span.setAttribute).toHaveBeenCalledTimes(1);
|
|
92
|
-
expect(span.setAttribute).toHaveBeenCalledWith('app.correlation_id', 'corr-xyz');
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe('AC-3: active span → write trace_id/span_id into RequestContextService', () => {
|
|
97
|
-
it('writes the span traceId and spanId into RequestContextService via set()', () => {
|
|
98
|
-
const span = makeSpan('tr-id-123', 'sp-id-456');
|
|
99
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
100
|
-
|
|
101
|
-
const stub = makeRequestContextStub('corr-1');
|
|
102
|
-
const interceptor = new TraceContextInterceptor(
|
|
103
|
-
stub as unknown as RequestContextService,
|
|
104
|
-
);
|
|
105
|
-
interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
106
|
-
|
|
107
|
-
expect(stub.set).toHaveBeenCalledWith('traceId', 'tr-id-123');
|
|
108
|
-
expect(stub.set).toHaveBeenCalledWith('spanId', 'sp-id-456');
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
describe('AC-4: no active span → no context writes, does not throw', () => {
|
|
113
|
-
it('does not call setAttribute and does not call set() when there is no active span', () => {
|
|
114
|
-
getActiveSpanSpy = installActiveSpan(null);
|
|
115
|
-
|
|
116
|
-
const stub = makeRequestContextStub('corr-1');
|
|
117
|
-
const interceptor = new TraceContextInterceptor(
|
|
118
|
-
stub as unknown as RequestContextService,
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
let thrown: unknown;
|
|
122
|
-
try {
|
|
123
|
-
interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
124
|
-
} catch (err) {
|
|
125
|
-
thrown = err;
|
|
126
|
-
}
|
|
127
|
-
expect(thrown).toBeUndefined();
|
|
128
|
-
|
|
129
|
-
expect(stub.set).not.toHaveBeenCalled();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
describe('AC-5: logging context without requestId → does not set app.correlation_id, does not throw', () => {
|
|
134
|
-
it('does not call setAttribute when requestId is absent, but still writes traceId/spanId', () => {
|
|
135
|
-
const span = makeSpan('trace-B', 'span-B');
|
|
136
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
137
|
-
|
|
138
|
-
const stub = makeRequestContextStub(undefined);
|
|
139
|
-
const interceptor = new TraceContextInterceptor(
|
|
140
|
-
stub as unknown as RequestContextService,
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
let thrown: unknown;
|
|
144
|
-
try {
|
|
145
|
-
interceptor.intercept(makeContext(), makeOkHandler('ok'));
|
|
146
|
-
} catch (err) {
|
|
147
|
-
thrown = err;
|
|
148
|
-
}
|
|
149
|
-
expect(thrown).toBeUndefined();
|
|
150
|
-
|
|
151
|
-
expect(span.setAttribute).not.toHaveBeenCalled();
|
|
152
|
-
expect(stub.set).toHaveBeenCalledWith('traceId', 'trace-B');
|
|
153
|
-
expect(stub.set).toHaveBeenCalledWith('spanId', 'span-B');
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe('AC-6: handler result is returned unchanged', () => {
|
|
158
|
-
it('emits the exact handler value through the returned Observable', async () => {
|
|
159
|
-
const span = makeSpan('trace-C', 'span-C');
|
|
160
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
161
|
-
|
|
162
|
-
const interceptor = new TraceContextInterceptor(
|
|
163
|
-
makeRequestContextStub('corr-1') as unknown as RequestContextService,
|
|
164
|
-
);
|
|
165
|
-
const { firstValueFrom } = await import('rxjs');
|
|
166
|
-
const { toArray } = await import('rxjs/operators');
|
|
167
|
-
|
|
168
|
-
const payload = { id: 7, name: 'preserved' };
|
|
169
|
-
const values = await firstValueFrom(
|
|
170
|
-
interceptor.intercept(makeContext(), makeOkHandler(payload)).pipe(toArray()),
|
|
171
|
-
);
|
|
172
|
-
expect(values).toEqual([payload]);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
describe('best-effort: correlation bridging failures do not break the request', () => {
|
|
177
|
-
it('still returns the handler value when span.setAttribute throws', async () => {
|
|
178
|
-
const span: SpanMock = {
|
|
179
|
-
setAttribute: jest.fn().mockImplementation(() => {
|
|
180
|
-
throw new Error('attribute set failed');
|
|
181
|
-
}),
|
|
182
|
-
spanContext: jest.fn().mockReturnValue(makeSpanContext('tr', 'sp')),
|
|
183
|
-
};
|
|
184
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
185
|
-
|
|
186
|
-
const interceptor = new TraceContextInterceptor(
|
|
187
|
-
makeRequestContextStub('corr-1') as unknown as RequestContextService,
|
|
188
|
-
);
|
|
189
|
-
const { firstValueFrom } = await import('rxjs');
|
|
190
|
-
const { toArray } = await import('rxjs/operators');
|
|
191
|
-
|
|
192
|
-
const values = await firstValueFrom(
|
|
193
|
-
interceptor.intercept(makeContext(), makeOkHandler('still-ok')).pipe(toArray()),
|
|
194
|
-
);
|
|
195
|
-
expect(values).toEqual(['still-ok']);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('still returns the handler value when requestContext.set throws', async () => {
|
|
199
|
-
const span = makeSpan('trace-D', 'span-D');
|
|
200
|
-
getActiveSpanSpy = installActiveSpan(span);
|
|
201
|
-
|
|
202
|
-
const stub = {
|
|
203
|
-
get: jest.fn().mockReturnValue('corr-1'),
|
|
204
|
-
set: jest.fn().mockImplementation(() => {
|
|
205
|
-
throw new Error('context write failed');
|
|
206
|
-
}),
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const interceptor = new TraceContextInterceptor(
|
|
210
|
-
stub as unknown as RequestContextService,
|
|
211
|
-
);
|
|
212
|
-
const { firstValueFrom } = await import('rxjs');
|
|
213
|
-
const { toArray } = await import('rxjs/operators');
|
|
214
|
-
|
|
215
|
-
const values = await firstValueFrom(
|
|
216
|
-
interceptor.intercept(makeContext(), makeOkHandler('still-ok')).pipe(toArray()),
|
|
217
|
-
);
|
|
218
|
-
expect(values).toEqual(['still-ok']);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
});
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { SpanStatusCode, trace } from '@opentelemetry/api';
|
|
3
|
-
import { Trace } from '../../lib/tracing/trace.decorator';
|
|
4
|
-
|
|
5
|
-
type SpanMock = {
|
|
6
|
-
end: jest.Mock;
|
|
7
|
-
recordException: jest.Mock;
|
|
8
|
-
setStatus: jest.Mock;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type TracerMock = {
|
|
12
|
-
startActiveSpan: jest.Mock;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
type MockHandle = {
|
|
16
|
-
tracer: TracerMock;
|
|
17
|
-
getTracerSpy: ReturnType<typeof jest.spyOn>;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const installTracerMock = (): MockHandle => {
|
|
21
|
-
const tracer: TracerMock = {
|
|
22
|
-
startActiveSpan: jest.fn(),
|
|
23
|
-
};
|
|
24
|
-
const getTracerSpy = jest
|
|
25
|
-
.spyOn(trace, 'getTracer')
|
|
26
|
-
.mockReturnValue(tracer as unknown as ReturnType<typeof trace.getTracer>);
|
|
27
|
-
return { tracer, getTracerSpy };
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const makeSpan = (): SpanMock => ({
|
|
31
|
-
end: jest.fn(),
|
|
32
|
-
recordException: jest.fn(),
|
|
33
|
-
setStatus: jest.fn(),
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const syncImpl = (span: SpanMock) =>
|
|
37
|
-
((_name: string, cb: (s: SpanMock) => unknown) => cb(span)) as never;
|
|
38
|
-
|
|
39
|
-
const asyncImpl = (span: SpanMock) =>
|
|
40
|
-
((_name: string, cb: (s: SpanMock) => unknown) => Promise.resolve(cb(span))) as never;
|
|
41
|
-
|
|
42
|
-
const SHARED_FAILURE = new Error('decorator-failure');
|
|
43
|
-
|
|
44
|
-
class SampleService {
|
|
45
|
-
readonly calls: string[] = [];
|
|
46
|
-
|
|
47
|
-
@Trace()
|
|
48
|
-
greet(name: string): string {
|
|
49
|
-
this.calls.push(name);
|
|
50
|
-
return `hello, ${name}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
@Trace('custom.span')
|
|
54
|
-
customMethod(value: number): number {
|
|
55
|
-
return value * 2;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
@Trace()
|
|
59
|
-
async fetchAsync(value: string): Promise<string> {
|
|
60
|
-
return value.toUpperCase();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
@Trace()
|
|
64
|
-
async failingAsync(): Promise<never> {
|
|
65
|
-
throw SHARED_FAILURE;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
describe('@Trace decorator', () => {
|
|
70
|
-
let mock: MockHandle;
|
|
71
|
-
let defaultSpan: SpanMock;
|
|
72
|
-
|
|
73
|
-
beforeEach(() => {
|
|
74
|
-
mock = installTracerMock();
|
|
75
|
-
defaultSpan = makeSpan();
|
|
76
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(defaultSpan));
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
afterEach(() => {
|
|
80
|
-
mock.getTracerSpy.mockRestore();
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('AC-5: importability and callability', () => {
|
|
84
|
-
it('is importable from the package and returns a MethodDecorator', async () => {
|
|
85
|
-
const mod = await import('../../lib/tracing');
|
|
86
|
-
expect(typeof mod.Trace).toBe('function');
|
|
87
|
-
const decorator = mod.Trace();
|
|
88
|
-
expect(typeof decorator).toBe('function');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('keeps the decorated method callable and returns its original result', () => {
|
|
92
|
-
const service = new SampleService();
|
|
93
|
-
const result = service.greet('world');
|
|
94
|
-
expect(result).toBe('hello, world');
|
|
95
|
-
expect(service.calls).toEqual(['world']);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('AC-6: method body executes inside an active span', () => {
|
|
100
|
-
it('runs the original method inside an active span and returns its result', () => {
|
|
101
|
-
const span = makeSpan();
|
|
102
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
103
|
-
|
|
104
|
-
const service = new SampleService();
|
|
105
|
-
const result = service.greet('alice');
|
|
106
|
-
|
|
107
|
-
expect(result).toBe('hello, alice');
|
|
108
|
-
expect(mock.tracer.startActiveSpan).toHaveBeenCalledTimes(1);
|
|
109
|
-
expect(mock.tracer.startActiveSpan).toHaveBeenCalledWith('SampleService.greet', expect.any(Function));
|
|
110
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
111
|
-
expect(span.recordException).not.toHaveBeenCalled();
|
|
112
|
-
expect(span.setStatus).not.toHaveBeenCalled();
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('uses the supplied span name when provided', () => {
|
|
116
|
-
const span = makeSpan();
|
|
117
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
118
|
-
|
|
119
|
-
const service = new SampleService();
|
|
120
|
-
const result = service.customMethod(21);
|
|
121
|
-
|
|
122
|
-
expect(result).toBe(42);
|
|
123
|
-
expect(mock.tracer.startActiveSpan).toHaveBeenCalledWith('custom.span', expect.any(Function));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('supports async methods and awaits their resolved value', async () => {
|
|
127
|
-
const span = makeSpan();
|
|
128
|
-
mock.tracer.startActiveSpan.mockImplementation(asyncImpl(span));
|
|
129
|
-
|
|
130
|
-
const service = new SampleService();
|
|
131
|
-
const result = await service.fetchAsync('hi');
|
|
132
|
-
|
|
133
|
-
expect(result).toBe('HI');
|
|
134
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
135
|
-
expect(span.recordException).not.toHaveBeenCalled();
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('records the exception, sets error status, ends the span, and rethrows when the method rejects', async () => {
|
|
139
|
-
const span = makeSpan();
|
|
140
|
-
mock.tracer.startActiveSpan.mockImplementation(asyncImpl(span));
|
|
141
|
-
|
|
142
|
-
const service = new SampleService();
|
|
143
|
-
await expect(service.failingAsync()).rejects.toBe(SHARED_FAILURE);
|
|
144
|
-
|
|
145
|
-
expect(span.recordException).toHaveBeenCalledWith(SHARED_FAILURE);
|
|
146
|
-
expect(span.setStatus).toHaveBeenCalledWith({
|
|
147
|
-
code: SpanStatusCode.ERROR,
|
|
148
|
-
message: 'decorator-failure',
|
|
149
|
-
});
|
|
150
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
});
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import { SpanStatusCode, trace } from '@opentelemetry/api';
|
|
4
|
-
import { TraceService } from '../../lib/tracing/trace.service';
|
|
5
|
-
|
|
6
|
-
type SpanMock = {
|
|
7
|
-
end: jest.Mock;
|
|
8
|
-
recordException: jest.Mock;
|
|
9
|
-
setStatus: jest.Mock;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type TracerMock = {
|
|
13
|
-
startActiveSpan: jest.Mock;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type MockHandle = {
|
|
17
|
-
tracer: TracerMock;
|
|
18
|
-
getTracerSpy: ReturnType<typeof jest.spyOn>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const installTracerMock = (): MockHandle => {
|
|
22
|
-
const tracer: TracerMock = {
|
|
23
|
-
startActiveSpan: jest.fn(),
|
|
24
|
-
};
|
|
25
|
-
const getTracerSpy = jest
|
|
26
|
-
.spyOn(trace, 'getTracer')
|
|
27
|
-
.mockReturnValue(tracer as unknown as ReturnType<typeof trace.getTracer>);
|
|
28
|
-
return { tracer, getTracerSpy };
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const makeSpan = (): SpanMock => ({
|
|
32
|
-
end: jest.fn(),
|
|
33
|
-
recordException: jest.fn(),
|
|
34
|
-
setStatus: jest.fn(),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const syncImpl = (span: SpanMock) =>
|
|
38
|
-
((_name: string, cb: (s: SpanMock) => unknown) => cb(span)) as never;
|
|
39
|
-
|
|
40
|
-
const asyncImpl = (span: SpanMock) =>
|
|
41
|
-
((_name: string, cb: (s: SpanMock) => unknown) => Promise.resolve(cb(span))) as never;
|
|
42
|
-
|
|
43
|
-
describe('TraceService', () => {
|
|
44
|
-
let mock: MockHandle;
|
|
45
|
-
|
|
46
|
-
beforeEach(() => {
|
|
47
|
-
mock = installTracerMock();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
afterEach(() => {
|
|
51
|
-
mock.getTracerSpy.mockRestore();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('AC-1: NestJS module integration', () => {
|
|
55
|
-
it('is injectable and exposes startActiveSpan', async () => {
|
|
56
|
-
const moduleRef = await Test.createTestingModule({
|
|
57
|
-
providers: [TraceService],
|
|
58
|
-
}).compile();
|
|
59
|
-
|
|
60
|
-
const service = moduleRef.get(TraceService);
|
|
61
|
-
expect(service).toBeInstanceOf(TraceService);
|
|
62
|
-
expect(typeof service.startActiveSpan).toBe('function');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('is importable from the package tracing barrel', async () => {
|
|
66
|
-
const mod = await import('../../lib/tracing');
|
|
67
|
-
expect(typeof mod.TraceService).toBe('function');
|
|
68
|
-
expect(typeof mod.Trace).toBe('function');
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
describe('AC-2: runs fn within an active span and returns its resolved value', () => {
|
|
73
|
-
it('returns the synchronous value from fn', async () => {
|
|
74
|
-
const span = makeSpan();
|
|
75
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
76
|
-
|
|
77
|
-
const service = new TraceService();
|
|
78
|
-
const result = await service.startActiveSpan('op', () => 42);
|
|
79
|
-
|
|
80
|
-
expect(result).toBe(42);
|
|
81
|
-
expect(mock.tracer.startActiveSpan).toHaveBeenCalledTimes(1);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('returns the resolved value when fn returns a Promise', async () => {
|
|
85
|
-
const span = makeSpan();
|
|
86
|
-
mock.tracer.startActiveSpan.mockImplementation(asyncImpl(span));
|
|
87
|
-
|
|
88
|
-
const service = new TraceService();
|
|
89
|
-
const result = await service.startActiveSpan('op', async () => 'hello');
|
|
90
|
-
|
|
91
|
-
expect(result).toBe('hello');
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('uses the span name provided by the caller', async () => {
|
|
95
|
-
const span = makeSpan();
|
|
96
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
97
|
-
|
|
98
|
-
const service = new TraceService();
|
|
99
|
-
await service.startActiveSpan('my.custom.span', () => undefined);
|
|
100
|
-
|
|
101
|
-
expect(mock.tracer.startActiveSpan).toHaveBeenCalledWith('my.custom.span', expect.any(Function));
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe('AC-3: ends the span exactly once on success', () => {
|
|
106
|
-
it('ends the span exactly once when fn resolves synchronously', async () => {
|
|
107
|
-
const span = makeSpan();
|
|
108
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
109
|
-
|
|
110
|
-
const service = new TraceService();
|
|
111
|
-
await service.startActiveSpan('op', () => 'ok');
|
|
112
|
-
|
|
113
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
114
|
-
expect(span.recordException).not.toHaveBeenCalled();
|
|
115
|
-
expect(span.setStatus).not.toHaveBeenCalled();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('ends the span exactly once when fn returns a Promise', async () => {
|
|
119
|
-
const span = makeSpan();
|
|
120
|
-
mock.tracer.startActiveSpan.mockImplementation(asyncImpl(span));
|
|
121
|
-
|
|
122
|
-
const service = new TraceService();
|
|
123
|
-
await service.startActiveSpan('op', async () => 'ok');
|
|
124
|
-
|
|
125
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
126
|
-
expect(span.recordException).not.toHaveBeenCalled();
|
|
127
|
-
expect(span.setStatus).not.toHaveBeenCalled();
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe('AC-4: error handling', () => {
|
|
132
|
-
it('records the exception, sets error status, ends the span, and rethrows when fn throws synchronously', async () => {
|
|
133
|
-
const span = makeSpan();
|
|
134
|
-
const err = new Error('boom-sync');
|
|
135
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
136
|
-
|
|
137
|
-
const service = new TraceService();
|
|
138
|
-
await expect(
|
|
139
|
-
service.startActiveSpan('op', () => {
|
|
140
|
-
throw err;
|
|
141
|
-
}),
|
|
142
|
-
).rejects.toBe(err);
|
|
143
|
-
|
|
144
|
-
expect(span.recordException).toHaveBeenCalledWith(err);
|
|
145
|
-
expect(span.setStatus).toHaveBeenCalledWith({
|
|
146
|
-
code: SpanStatusCode.ERROR,
|
|
147
|
-
message: 'boom-sync',
|
|
148
|
-
});
|
|
149
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('records the exception, sets error status, ends the span, and rethrows when fn rejects', async () => {
|
|
153
|
-
const span = makeSpan();
|
|
154
|
-
const err = new Error('boom-async');
|
|
155
|
-
mock.tracer.startActiveSpan.mockImplementation(asyncImpl(span));
|
|
156
|
-
|
|
157
|
-
const service = new TraceService();
|
|
158
|
-
await expect(
|
|
159
|
-
service.startActiveSpan('op', async () => {
|
|
160
|
-
throw err;
|
|
161
|
-
}),
|
|
162
|
-
).rejects.toBe(err);
|
|
163
|
-
|
|
164
|
-
expect(span.recordException).toHaveBeenCalledWith(err);
|
|
165
|
-
expect(span.setStatus).toHaveBeenCalledWith({
|
|
166
|
-
code: SpanStatusCode.ERROR,
|
|
167
|
-
message: 'boom-async',
|
|
168
|
-
});
|
|
169
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('coerces a non-Error throw value via String(err) for recordException and status message', async () => {
|
|
173
|
-
const span = makeSpan();
|
|
174
|
-
const nonError = 'plain-string-failure';
|
|
175
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
176
|
-
|
|
177
|
-
const service = new TraceService();
|
|
178
|
-
await expect(
|
|
179
|
-
service.startActiveSpan('op', () => {
|
|
180
|
-
throw nonError;
|
|
181
|
-
}),
|
|
182
|
-
).rejects.toBe(nonError);
|
|
183
|
-
|
|
184
|
-
expect(span.recordException).toHaveBeenCalledWith(nonError);
|
|
185
|
-
expect(span.setStatus).toHaveBeenCalledWith({
|
|
186
|
-
code: SpanStatusCode.ERROR,
|
|
187
|
-
message: nonError,
|
|
188
|
-
});
|
|
189
|
-
expect(span.end).toHaveBeenCalledTimes(1);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe('tracer access reuse', () => {
|
|
194
|
-
it('reuses a single tracer acquired from @opentelemetry/api', async () => {
|
|
195
|
-
const span = makeSpan();
|
|
196
|
-
mock.tracer.startActiveSpan.mockImplementation(syncImpl(span));
|
|
197
|
-
|
|
198
|
-
const service = new TraceService();
|
|
199
|
-
await service.startActiveSpan('a', () => 1);
|
|
200
|
-
await service.startActiveSpan('b', () => 2);
|
|
201
|
-
|
|
202
|
-
expect(mock.getTracerSpy).toHaveBeenCalledTimes(1);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
});
|
package/tsconfig.json
DELETED