@nestjs/observe 0.3.1 → 0.3.3
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/README.md +39 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/interfaces/observe-options.interface.d.ts +86 -10
- package/dist/observe.module.js +4 -1
- package/dist/protocols/job-trace-runner.d.ts +15 -3
- package/dist/protocols/job-trace-runner.js +78 -15
- package/dist/protocols/queue-observe-agent.service.d.ts +34 -0
- package/dist/protocols/queue-observe-agent.service.js +92 -7
- package/dist/protocols/rpc-observe-agent.service.d.ts +2 -2
- package/dist/protocols/rpc-observe-agent.service.js +28 -23
- package/dist/protocols/schedule-observe-agent.service.d.ts +1 -4
- package/dist/protocols/schedule-observe-agent.service.js +12 -80
- package/dist/services/operation-trace.registry.d.ts +25 -0
- package/dist/services/operation-trace.registry.js +54 -0
- package/dist/services/skip-spans.util.d.ts +14 -0
- package/dist/services/skip-spans.util.js +20 -0
- package/dist/services/tracer.service.d.ts +3 -1
- package/dist/services/tracer.service.js +10 -1
- package/package.json +5 -1
|
@@ -18,6 +18,7 @@ import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.j
|
|
|
18
18
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
19
19
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
20
20
|
import { describePeerLoadError, loadOptionalPeer, } from "../utils/optional-peer.util.js";
|
|
21
|
+
import { JobTraceRunner } from "./job-trace-runner.js";
|
|
21
22
|
/**
|
|
22
23
|
* `@nestjs/schedule`'s metadata keys and scheduler-type enum, inlined so the
|
|
23
24
|
* package stays an optional peer. `@Cron`/`@Interval`/`@Timeout` stamp all
|
|
@@ -57,16 +58,10 @@ const SCHEDULER_TYPE_LABELS = {
|
|
|
57
58
|
* `emails / send-welcome` in the same view.
|
|
58
59
|
*/
|
|
59
60
|
let ScheduleObserveAgentService = ScheduleObserveAgentService_1 = class ScheduleObserveAgentService {
|
|
60
|
-
observeAgentSharedBuffer;
|
|
61
|
-
options;
|
|
62
|
-
operationTraceRegistry;
|
|
63
|
-
asyncLocalStorage;
|
|
64
61
|
logger = new Logger(ScheduleObserveAgentService_1.name);
|
|
62
|
+
runner;
|
|
65
63
|
constructor(observeAgentSharedBuffer, options, operationTraceRegistry, asyncLocalStorage) {
|
|
66
|
-
this.
|
|
67
|
-
this.options = options;
|
|
68
|
-
this.operationTraceRegistry = operationTraceRegistry;
|
|
69
|
-
this.asyncLocalStorage = asyncLocalStorage;
|
|
64
|
+
this.runner = new JobTraceRunner(observeAgentSharedBuffer, options, operationTraceRegistry, asyncLocalStorage, this.logger);
|
|
70
65
|
this.patchScheduleExplorer();
|
|
71
66
|
}
|
|
72
67
|
/**
|
|
@@ -153,78 +148,15 @@ let ScheduleObserveAgentService = ScheduleObserveAgentService_1 = class Schedule
|
|
|
153
148
|
}
|
|
154
149
|
instrumentHandler(methodRef, instance) {
|
|
155
150
|
const { queueName, name } = this.describeHandler(methodRef, instance);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// report the same work twice.
|
|
166
|
-
if (this.options.debug) {
|
|
167
|
-
this.logger.debug(`Outer context already has a trace ID. Skipping inner context for scheduled job "${name}"`);
|
|
168
|
-
}
|
|
169
|
-
return methodRef.call(instance, ...args);
|
|
170
|
-
}
|
|
171
|
-
const traceId = uuidv7();
|
|
172
|
-
store.set(this.options.traceIdKey, traceId);
|
|
173
|
-
// Every firing is its own job run, so every firing gets its own id.
|
|
174
|
-
const id = uuidv7();
|
|
175
|
-
if (this.options.jobs?.setAttributes) {
|
|
176
|
-
const attributes = this.options.jobs.setAttributes({
|
|
177
|
-
queueName,
|
|
178
|
-
name,
|
|
179
|
-
id,
|
|
180
|
-
});
|
|
181
|
-
if (attributes) {
|
|
182
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
183
|
-
store.set(key, value);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
this.operationTraceRegistry.startTrace(traceId, {
|
|
188
|
-
tags: this.options.jobs?.tags,
|
|
189
|
-
queueName,
|
|
190
|
-
name,
|
|
191
|
-
id,
|
|
192
|
-
});
|
|
193
|
-
const endTrace = (status) => {
|
|
194
|
-
setTimeout(async () => {
|
|
195
|
-
this.operationTraceRegistry.endTrace(traceId, { status });
|
|
196
|
-
const snapshot = (await this.operationTraceRegistry.pluckSnapshot(traceId));
|
|
197
|
-
// Absent when the registry discarded the trace - a handler on a
|
|
198
|
-
// provider the instance decorator never wrapped records no spans,
|
|
199
|
-
// and a trace with no spans is dropped rather than shipped empty.
|
|
200
|
-
if (!snapshot) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
this.observeAgentSharedBuffer.insertJobSnapshot(snapshot);
|
|
204
|
-
}, 0);
|
|
205
|
-
};
|
|
206
|
-
try {
|
|
207
|
-
const returnValue = methodRef.call(instance, ...args);
|
|
208
|
-
if (returnValue instanceof Promise) {
|
|
209
|
-
return returnValue.then((ret) => {
|
|
210
|
-
endTrace("completed");
|
|
211
|
-
return ret;
|
|
212
|
-
}, (error) => {
|
|
213
|
-
endTrace("failed");
|
|
214
|
-
// The explorer's own wrapper is outside this one and logs the
|
|
215
|
-
// rejection as it always did.
|
|
216
|
-
throw error;
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
endTrace("completed");
|
|
220
|
-
return returnValue;
|
|
221
|
-
}
|
|
222
|
-
catch (error) {
|
|
223
|
-
endTrace("failed");
|
|
224
|
-
throw error;
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
};
|
|
151
|
+
// The explorer's own wrapper is outside this one, so a throwing or
|
|
152
|
+
// rejecting handler is still logged by the scheduler as it always was.
|
|
153
|
+
return (...args) => this.runner.run({
|
|
154
|
+
queueName,
|
|
155
|
+
name,
|
|
156
|
+
// Every firing is its own job run, so every firing gets its own id.
|
|
157
|
+
id: uuidv7(),
|
|
158
|
+
metadata: {},
|
|
159
|
+
}, () => methodRef.call(instance, ...args));
|
|
228
160
|
}
|
|
229
161
|
};
|
|
230
162
|
ScheduleObserveAgentService = ScheduleObserveAgentService_1 = __decorate([
|
|
@@ -9,6 +9,7 @@ import { KeyOf } from "../types/key-of.type.js";
|
|
|
9
9
|
import { LogRedactor } from "../utils/log-redactor.js";
|
|
10
10
|
import { CALLER_METADATA_KEY } from "../observe.constants.js";
|
|
11
11
|
import { SpanCollapseSettings } from "./collapse-repeated-spans.util.js";
|
|
12
|
+
import { SkipSpansPredicate } from "./skip-spans.util.js";
|
|
12
13
|
type TraceId = string;
|
|
13
14
|
export declare class OperationTraceRegistry {
|
|
14
15
|
private readonly als;
|
|
@@ -28,6 +29,15 @@ export declare class OperationTraceRegistry {
|
|
|
28
29
|
* registry the module has not (yet) configured still bounds what it ships.
|
|
29
30
|
*/
|
|
30
31
|
private spanCollapse;
|
|
32
|
+
/**
|
|
33
|
+
* `skipSpans`, resolved. Undefined - the default - ships every tree.
|
|
34
|
+
*/
|
|
35
|
+
private skipSpans;
|
|
36
|
+
/**
|
|
37
|
+
* A `skipSpans` function that throws does so on every request, so the
|
|
38
|
+
* failure is reported once rather than once per request.
|
|
39
|
+
*/
|
|
40
|
+
private hasReportedSkipSpansFailure;
|
|
31
41
|
/**
|
|
32
42
|
* Scrubs error messages and stacks before they are attached to a span.
|
|
33
43
|
*
|
|
@@ -49,6 +59,12 @@ export declare class OperationTraceRegistry {
|
|
|
49
59
|
* before the DI container so the instrumentation hook can hold it.
|
|
50
60
|
*/
|
|
51
61
|
configureSpanCollapse(settings: SpanCollapseSettings | undefined): void;
|
|
62
|
+
/**
|
|
63
|
+
* Sets which finished operations ship without their spans, or none with
|
|
64
|
+
* `undefined`. Called by the module alongside `configureSpanCollapse`, for
|
|
65
|
+
* the same reason.
|
|
66
|
+
*/
|
|
67
|
+
configureSkipSpans(predicate: SkipSpansPredicate | undefined): void;
|
|
52
68
|
/**
|
|
53
69
|
* Applies the resolved `redaction` options to error payloads, or switches
|
|
54
70
|
* redaction off with `null`. Called by the module alongside
|
|
@@ -161,6 +177,15 @@ export declare class OperationTraceRegistry {
|
|
|
161
177
|
*/
|
|
162
178
|
abandonTrace(traceId: TraceId): void;
|
|
163
179
|
pluckSnapshot(traceId: string): Promise<RequestSnapshot | JobSnapshot | undefined>;
|
|
180
|
+
/**
|
|
181
|
+
* Whether `skipSpans` claims a finished operation. Only request snapshots
|
|
182
|
+
* are asked: a job has no status to be judged by.
|
|
183
|
+
*
|
|
184
|
+
* Asked once the snapshot is complete, so the rule sees the status and the
|
|
185
|
+
* error the operation is reported with. A rule that throws keeps the spans:
|
|
186
|
+
* a mistake in it must not cost the application its traces.
|
|
187
|
+
*/
|
|
188
|
+
private shouldSkipSpans;
|
|
164
189
|
getActiveSpan(traceId: string, callerId: string | undefined): OngoingTraceEventNode | undefined;
|
|
165
190
|
createManualSpan(traceId: string, callerId: string | undefined, name: string, spanFunction: (span: TraceSpanDelegate) => any): Promise<any>;
|
|
166
191
|
}
|
|
@@ -38,6 +38,15 @@ export class OperationTraceRegistry {
|
|
|
38
38
|
* registry the module has not (yet) configured still bounds what it ships.
|
|
39
39
|
*/
|
|
40
40
|
spanCollapse = DEFAULT_SPAN_COLLAPSE;
|
|
41
|
+
/**
|
|
42
|
+
* `skipSpans`, resolved. Undefined - the default - ships every tree.
|
|
43
|
+
*/
|
|
44
|
+
skipSpans;
|
|
45
|
+
/**
|
|
46
|
+
* A `skipSpans` function that throws does so on every request, so the
|
|
47
|
+
* failure is reported once rather than once per request.
|
|
48
|
+
*/
|
|
49
|
+
hasReportedSkipSpansFailure = false;
|
|
41
50
|
/**
|
|
42
51
|
* Scrubs error messages and stacks before they are attached to a span.
|
|
43
52
|
*
|
|
@@ -62,6 +71,14 @@ export class OperationTraceRegistry {
|
|
|
62
71
|
configureSpanCollapse(settings) {
|
|
63
72
|
this.spanCollapse = settings;
|
|
64
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Sets which finished operations ship without their spans, or none with
|
|
76
|
+
* `undefined`. Called by the module alongside `configureSpanCollapse`, for
|
|
77
|
+
* the same reason.
|
|
78
|
+
*/
|
|
79
|
+
configureSkipSpans(predicate) {
|
|
80
|
+
this.skipSpans = predicate;
|
|
81
|
+
}
|
|
65
82
|
/**
|
|
66
83
|
* Applies the resolved `redaction` options to error payloads, or switches
|
|
67
84
|
* redaction off with `null`. Called by the module alongside
|
|
@@ -511,6 +528,13 @@ export class OperationTraceRegistry {
|
|
|
511
528
|
delete bookkeeping.refsCounter;
|
|
512
529
|
delete bookkeeping.refsMarkedAsComplete;
|
|
513
530
|
delete bookkeeping.errorStatusCode;
|
|
531
|
+
// Emptied rather than removed: the collector requires the field, and an
|
|
532
|
+
// empty tree is what it already stores for a trace it sampled out - the
|
|
533
|
+
// operation keeps its row, its error and every chart it feeds.
|
|
534
|
+
if (this.shouldSkipSpans(snapshot)) {
|
|
535
|
+
snapshot.traces = [];
|
|
536
|
+
return snapshot;
|
|
537
|
+
}
|
|
514
538
|
// Last, on the finished tree: every span has closed by now, so a group
|
|
515
539
|
// is judged on its complete membership and durations, and nothing that
|
|
516
540
|
// reads the tree afterwards - the encoder, the buffer - sees the
|
|
@@ -522,6 +546,36 @@ export class OperationTraceRegistry {
|
|
|
522
546
|
}
|
|
523
547
|
return undefined;
|
|
524
548
|
}
|
|
549
|
+
/**
|
|
550
|
+
* Whether `skipSpans` claims a finished operation. Only request snapshots
|
|
551
|
+
* are asked: a job has no status to be judged by.
|
|
552
|
+
*
|
|
553
|
+
* Asked once the snapshot is complete, so the rule sees the status and the
|
|
554
|
+
* error the operation is reported with. A rule that throws keeps the spans:
|
|
555
|
+
* a mistake in it must not cost the application its traces.
|
|
556
|
+
*/
|
|
557
|
+
shouldSkipSpans(snapshot) {
|
|
558
|
+
if (!this.skipSpans || !("protocol" in snapshot)) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
try {
|
|
562
|
+
return this.skipSpans({
|
|
563
|
+
protocol: snapshot.protocol,
|
|
564
|
+
operationId: snapshot.operationId,
|
|
565
|
+
method: snapshot.attributes?.method,
|
|
566
|
+
statusCode: snapshot.attributes?.statusCode,
|
|
567
|
+
duration: snapshot.duration ?? 0,
|
|
568
|
+
errorClass: snapshot.error?.cls,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
catch (error) {
|
|
572
|
+
if (!this.hasReportedSkipSpansFailure) {
|
|
573
|
+
this.hasReportedSkipSpansFailure = true;
|
|
574
|
+
this.logger.warn(`"skipSpans" threw for "${snapshot.operationId ?? snapshot.protocol}"; keeping its spans. Later failures are not reported: ${error instanceof Error ? error.message : String(error)}`);
|
|
575
|
+
}
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
525
579
|
getActiveSpan(traceId, callerId) {
|
|
526
580
|
const refsByCaller = this.callerIdsByTraceId.get(traceId);
|
|
527
581
|
if (!refsByCaller) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ObserveOptions, SkipSpansContext } from "../interfaces/observe-options.interface.js";
|
|
2
|
+
/** Resolved form of `ObserveOptions.skipSpans`. */
|
|
3
|
+
export type SkipSpansPredicate = (operation: SkipSpansContext) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Turns the user-facing option into the one predicate the registry asks, or
|
|
6
|
+
* `undefined` when nothing is to be skipped - so an application that never
|
|
7
|
+
* set it pays one falsy check per operation and builds no context object.
|
|
8
|
+
*
|
|
9
|
+
* A list matches on the status code alone, by strict equality: a status never
|
|
10
|
+
* reaches the registry as a string, so `"404"` in the list can only be a
|
|
11
|
+
* mistake, and a list that coerced its way into skipping more than it names
|
|
12
|
+
* would be the worse one to get wrong.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveSkipSpans(option: ObserveOptions["skipSpans"]): SkipSpansPredicate | undefined;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns the user-facing option into the one predicate the registry asks, or
|
|
3
|
+
* `undefined` when nothing is to be skipped - so an application that never
|
|
4
|
+
* set it pays one falsy check per operation and builds no context object.
|
|
5
|
+
*
|
|
6
|
+
* A list matches on the status code alone, by strict equality: a status never
|
|
7
|
+
* reaches the registry as a string, so `"404"` in the list can only be a
|
|
8
|
+
* mistake, and a list that coerced its way into skipping more than it names
|
|
9
|
+
* would be the worse one to get wrong.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveSkipSpans(option) {
|
|
12
|
+
if (typeof option === "function") {
|
|
13
|
+
return option;
|
|
14
|
+
}
|
|
15
|
+
if (!Array.isArray(option) || option.length === 0) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const statusCodes = new Set(option);
|
|
19
|
+
return ({ statusCode }) => statusCode !== undefined && statusCodes.has(statusCode);
|
|
20
|
+
}
|
|
@@ -24,7 +24,9 @@ export declare class TracerService<Store extends Record<string | symbol, unknown
|
|
|
24
24
|
/**
|
|
25
25
|
* Retrieves the active span for the current request.
|
|
26
26
|
* This method should be called within an async context where the AsyncLocalStorage is active.
|
|
27
|
-
* If no
|
|
27
|
+
* If the operation is traced but no span is active, an error is thrown. An
|
|
28
|
+
* operation that is not being recorded (ignored or sampled out) gets a
|
|
29
|
+
* detached delegate whose tags go nowhere, as `createSpan` does.
|
|
28
30
|
* @returns {Promise<TraceSpanDelegate>} A promise that resolves to the active span delegate.
|
|
29
31
|
*/
|
|
30
32
|
activeSpan(): Promise<TraceSpanDelegate>;
|
|
@@ -52,7 +52,9 @@ let TracerService = class TracerService {
|
|
|
52
52
|
/**
|
|
53
53
|
* Retrieves the active span for the current request.
|
|
54
54
|
* This method should be called within an async context where the AsyncLocalStorage is active.
|
|
55
|
-
* If no
|
|
55
|
+
* If the operation is traced but no span is active, an error is thrown. An
|
|
56
|
+
* operation that is not being recorded (ignored or sampled out) gets a
|
|
57
|
+
* detached delegate whose tags go nowhere, as `createSpan` does.
|
|
56
58
|
* @returns {Promise<TraceSpanDelegate>} A promise that resolves to the active span delegate.
|
|
57
59
|
*/
|
|
58
60
|
async activeSpan() {
|
|
@@ -64,6 +66,13 @@ let TracerService = class TracerService {
|
|
|
64
66
|
const traceId = this.requireTraceId(store, "activeSpan");
|
|
65
67
|
const callerId = store.get(CALLER_METADATA_KEY);
|
|
66
68
|
const activeOngoingEvent = this.operationTraceRegistry.getActiveSpan(traceId, callerId);
|
|
69
|
+
if (!activeOngoingEvent && !this.operationTraceRegistry.hasTrace(traceId)) {
|
|
70
|
+
// The store carries the id for log correlation, but no trace was opened
|
|
71
|
+
// under it: `http.ignore`, `jobs.ignore`, or the sampler said no. Which
|
|
72
|
+
// operations get recorded is configuration; a handler tagging its span
|
|
73
|
+
// must not start failing because someone changed it.
|
|
74
|
+
return new TraceSpanDelegate("", undefined, {});
|
|
75
|
+
}
|
|
67
76
|
if (!activeOngoingEvent) {
|
|
68
77
|
throw new Error(`No active span found for traceId: ${traceId}. Ensure that a span is created before calling "activeSpan".`);
|
|
69
78
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Observability and APM agent for NestJS - traces, runtime metrics, custom metrics and CPU profiles",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"homepage": "https://observe.nestjs.com",
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"@nestjs/microservices": "^11.0.0 || ^12.0.0",
|
|
79
79
|
"@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^12.0.0",
|
|
80
80
|
"@nestjs/websockets": "^11.0.0 || ^12.0.0",
|
|
81
|
+
"@taskforcesh/bullmq-pro": "^7.0.0",
|
|
81
82
|
"bull": "^3.3.0 || ^4.0.0",
|
|
82
83
|
"bullmq": "^5.0.0 || ^6.0.0",
|
|
83
84
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
@@ -102,6 +103,9 @@
|
|
|
102
103
|
"@nestjs/websockets": {
|
|
103
104
|
"optional": true
|
|
104
105
|
},
|
|
106
|
+
"@taskforcesh/bullmq-pro": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
105
109
|
"bull": {
|
|
106
110
|
"optional": true
|
|
107
111
|
},
|