@nestjs/observe 0.1.1 → 0.1.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 +4 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/interfaces/observe-options.interface.d.ts +12 -1
- package/dist/observe.module.js +6 -0
- package/dist/protocols/queue-observe-agent.service.d.ts +9 -0
- package/dist/protocols/queue-observe-agent.service.js +29 -1
- package/dist/protocols/rpc-observe-agent.service.d.ts +16 -1
- package/dist/protocols/rpc-observe-agent.service.js +50 -9
- package/dist/protocols/schedule-observe-agent.service.d.ts +54 -0
- package/dist/protocols/schedule-observe-agent.service.js +225 -0
- package/dist/services/operation-trace.registry.d.ts +15 -0
- package/dist/services/operation-trace.registry.js +36 -17
- package/dist/utils/optional-peer.util.d.ts +36 -0
- package/dist/utils/optional-peer.util.js +38 -0
- package/package.json +6 -1
|
@@ -227,15 +227,7 @@ export class OperationTraceRegistry {
|
|
|
227
227
|
spanId: newNodeId,
|
|
228
228
|
};
|
|
229
229
|
let refsByCaller = this.callerIdsByTraceId.get(traceId);
|
|
230
|
-
|
|
231
|
-
const caller = refsByCaller.get(callerId);
|
|
232
|
-
caller.children.push(newNode);
|
|
233
|
-
newNode.parent = caller;
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
// No caller means insert trace at the root level
|
|
237
|
-
snapshot.traces.push(newNode);
|
|
238
|
-
}
|
|
230
|
+
this.attachToCaller(snapshot, refsByCaller, callerId, newNode);
|
|
239
231
|
if (!refsByCaller) {
|
|
240
232
|
refsByCaller = new Map();
|
|
241
233
|
this.callerIdsByTraceId.set(traceId, refsByCaller);
|
|
@@ -243,6 +235,31 @@ export class OperationTraceRegistry {
|
|
|
243
235
|
refsByCaller.set(newNodeId, newNode);
|
|
244
236
|
return newNodeId;
|
|
245
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Nests a freshly opened node under its caller when that caller is still
|
|
240
|
+
* live, and at the trace root otherwise.
|
|
241
|
+
*
|
|
242
|
+
* A caller that has already completed has had its scratch fields stripped
|
|
243
|
+
* (`type`, and `children` when it had none), so there is nothing left to
|
|
244
|
+
* nest under. That happens whenever a callee outlives its caller - most
|
|
245
|
+
* commonly a handler that Nest bound (`AsyncResource.bind` in its
|
|
246
|
+
* interceptors consumer) to the async context of an interceptor whose
|
|
247
|
+
* `intercept()` returned synchronously. Fall back to the root rather than
|
|
248
|
+
* throw into the request: the mirror case, a step ending after its caller
|
|
249
|
+
* did, is already tolerated in `internalEndTraceStep`. Auto and manual spans
|
|
250
|
+
* both attach through here, so they share the tolerance.
|
|
251
|
+
*/
|
|
252
|
+
attachToCaller(snapshot, refsByCaller, callerId, newNode) {
|
|
253
|
+
const caller = callerId ? refsByCaller?.get(callerId) : undefined;
|
|
254
|
+
if (caller?.type === "start") {
|
|
255
|
+
caller.children.push(newNode);
|
|
256
|
+
newNode.parent = caller;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// No (live) caller means insert trace at the root level
|
|
260
|
+
snapshot.traces.push(newNode);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
246
263
|
internalEndTraceStep(traceId,
|
|
247
264
|
/**
|
|
248
265
|
* Diagnostic label only - it names the span in the two warnings below and
|
|
@@ -429,11 +446,15 @@ export class OperationTraceRegistry {
|
|
|
429
446
|
}
|
|
430
447
|
async createManualSpan(traceId, callerId, name, spanFunction) {
|
|
431
448
|
// await new Promise((resolve) => setImmediate(resolve));
|
|
449
|
+
// The caller may already have completed (its entry survives completion in
|
|
450
|
+
// `refsByCaller`); it is still the right source for the class and method
|
|
451
|
+
// labels, which outlive completion. Where the new span *attaches* is
|
|
452
|
+
// decided by `attachToCaller`, which nests only under a live caller.
|
|
432
453
|
const activeSpan = this.getActiveSpan(traceId, callerId);
|
|
433
|
-
|
|
454
|
+
const snapshot = this.traceSnapshots.get(traceId);
|
|
455
|
+
if (!activeSpan || !snapshot) {
|
|
434
456
|
return spanFunction(new TraceSpanDelegate("", name, {}));
|
|
435
457
|
}
|
|
436
|
-
const snapshot = this.traceSnapshots.get(traceId);
|
|
437
458
|
// Book the span in the same ledger auto spans use. Completion goes through
|
|
438
459
|
// internalEndTraceStep, which increments refsMarkedAsComplete - without
|
|
439
460
|
// the matching increment here, every completed manual span pushed the
|
|
@@ -441,9 +462,7 @@ export class OperationTraceRegistry {
|
|
|
441
462
|
// one manual span read as complete: pluckSnapshot then shipped a
|
|
442
463
|
// half-built tree whose ongoing node still held its circular `parent`
|
|
443
464
|
// reference, and JSON.stringify threw away the whole flush window.
|
|
444
|
-
|
|
445
|
-
snapshot.refsCounter += 1;
|
|
446
|
-
}
|
|
465
|
+
snapshot.refsCounter += 1;
|
|
447
466
|
const newNodeId = randomUUID();
|
|
448
467
|
const startTime = performance.now();
|
|
449
468
|
// Same as internalStartTraceStep: without an enter, the exit issued on
|
|
@@ -463,10 +482,10 @@ export class OperationTraceRegistry {
|
|
|
463
482
|
startTime,
|
|
464
483
|
startOffset: this.toStartOffset(snapshot, startTime),
|
|
465
484
|
children: [],
|
|
466
|
-
parent: activeSpan,
|
|
467
485
|
};
|
|
468
|
-
|
|
469
|
-
this.
|
|
486
|
+
const refsByCaller = this.callerIdsByTraceId.get(traceId);
|
|
487
|
+
this.attachToCaller(snapshot, refsByCaller, callerId, manualSpan);
|
|
488
|
+
refsByCaller?.set(newNodeId, manualSpan);
|
|
470
489
|
const onResponse = (res) => {
|
|
471
490
|
// setImmediate
|
|
472
491
|
this.internalEndTraceStep(traceId, newNodeId, manualSpan.className, manualSpan.methodKey, newNodeId);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What loading an optional peer produced.
|
|
3
|
+
*
|
|
4
|
+
* `installed: false` is the normal case for a service that does not use the
|
|
5
|
+
* feature - not a misconfiguration, and callers should stay silent about it.
|
|
6
|
+
* `installed: true` with no `module` means the package is present but the
|
|
7
|
+
* requested entry could not be loaded; `error` carries the cause so the
|
|
8
|
+
* caller can say something truthful instead of guessing.
|
|
9
|
+
*/
|
|
10
|
+
export type OptionalPeerResult<T> = {
|
|
11
|
+
installed: false;
|
|
12
|
+
} | {
|
|
13
|
+
installed: true;
|
|
14
|
+
module: T | undefined;
|
|
15
|
+
error?: unknown;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Loads an optional peer dependency without a static import, so a service
|
|
19
|
+
* that does not use the feature need not install the package.
|
|
20
|
+
*
|
|
21
|
+
* Synchronous on purpose: every caller patches or subscribes during provider
|
|
22
|
+
* instantiation or `onModuleInit`, strictly before the peer's own lifecycle
|
|
23
|
+
* runs, and a dynamic `import()` would resolve too late. All current peers
|
|
24
|
+
* ship CommonJS, so `require` can load them.
|
|
25
|
+
*
|
|
26
|
+
* The package's presence is probed with `require.resolve(packageName)` before
|
|
27
|
+
* `specifier` (which may be a deep path into the package) is required, so
|
|
28
|
+
* "not installed" is kept apart from "installed but broken or reshaped".
|
|
29
|
+
* `createRequire` itself failing - a CJS bundle that erased `import.meta.url`,
|
|
30
|
+
* say - is treated as "not installed": with no resolver there is nothing to
|
|
31
|
+
* load, and crashing the constructor would defeat the point of the peer being
|
|
32
|
+
* optional.
|
|
33
|
+
*/
|
|
34
|
+
/** Names a load failure for a log line: the message for an `Error`, `String` otherwise. */
|
|
35
|
+
export declare function describePeerLoadError(error: unknown): string;
|
|
36
|
+
export declare function loadOptionalPeer<T>(packageName: string, specifier?: string): OptionalPeerResult<T>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createRequire } from "module";
|
|
2
|
+
/**
|
|
3
|
+
* Loads an optional peer dependency without a static import, so a service
|
|
4
|
+
* that does not use the feature need not install the package.
|
|
5
|
+
*
|
|
6
|
+
* Synchronous on purpose: every caller patches or subscribes during provider
|
|
7
|
+
* instantiation or `onModuleInit`, strictly before the peer's own lifecycle
|
|
8
|
+
* runs, and a dynamic `import()` would resolve too late. All current peers
|
|
9
|
+
* ship CommonJS, so `require` can load them.
|
|
10
|
+
*
|
|
11
|
+
* The package's presence is probed with `require.resolve(packageName)` before
|
|
12
|
+
* `specifier` (which may be a deep path into the package) is required, so
|
|
13
|
+
* "not installed" is kept apart from "installed but broken or reshaped".
|
|
14
|
+
* `createRequire` itself failing - a CJS bundle that erased `import.meta.url`,
|
|
15
|
+
* say - is treated as "not installed": with no resolver there is nothing to
|
|
16
|
+
* load, and crashing the constructor would defeat the point of the peer being
|
|
17
|
+
* optional.
|
|
18
|
+
*/
|
|
19
|
+
/** Names a load failure for a log line: the message for an `Error`, `String` otherwise. */
|
|
20
|
+
export function describePeerLoadError(error) {
|
|
21
|
+
return error instanceof Error ? error.message : String(error);
|
|
22
|
+
}
|
|
23
|
+
export function loadOptionalPeer(packageName, specifier = packageName) {
|
|
24
|
+
let require;
|
|
25
|
+
try {
|
|
26
|
+
require = createRequire(import.meta.url);
|
|
27
|
+
require.resolve(packageName);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return { installed: false };
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
return { installed: true, module: require(specifier) };
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return { installed: true, module: undefined, error };
|
|
37
|
+
}
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.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://nestjs.com",
|
|
@@ -68,6 +68,7 @@
|
|
|
68
68
|
"@nestjs/core": "^11.0.0",
|
|
69
69
|
"@nestjs/graphql": "^13.0.0",
|
|
70
70
|
"@nestjs/microservices": "^11.0.0",
|
|
71
|
+
"@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0",
|
|
71
72
|
"bullmq": "^5.0.0 || ^6.0.0",
|
|
72
73
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
73
74
|
"rxjs": "^7.0.0"
|
|
@@ -82,6 +83,9 @@
|
|
|
82
83
|
"@nestjs/microservices": {
|
|
83
84
|
"optional": true
|
|
84
85
|
},
|
|
86
|
+
"@nestjs/schedule": {
|
|
87
|
+
"optional": true
|
|
88
|
+
},
|
|
85
89
|
"bullmq": {
|
|
86
90
|
"optional": true
|
|
87
91
|
}
|
|
@@ -100,6 +104,7 @@
|
|
|
100
104
|
"@nestjs/graphql": "13.4.5",
|
|
101
105
|
"@nestjs/microservices": "11.2.1",
|
|
102
106
|
"@nestjs/platform-express": "11.2.1",
|
|
107
|
+
"@nestjs/schedule": "^6.1.3",
|
|
103
108
|
"@nestjs/testing": "11.2.1",
|
|
104
109
|
"@swc/core": "1.16.1",
|
|
105
110
|
"@types/node": "22.20.1",
|