@nestjs/observe 0.1.8 → 0.1.10
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/dist/.tsbuildinfo +1 -1
- package/dist/agent/detached-observe-worker.d.ts +2 -1
- package/dist/agent/detached-observe-worker.js +15 -2
- package/dist/agent/observe-agent.worker.js +7 -5
- package/dist/instrument/create-instance-decorator.instrument.js +4 -0
- package/dist/interfaces/observe-options.interface.d.ts +3 -2
- package/dist/profiling/profile-uploader.js +5 -1
- package/dist/protocols/queue-observe-agent.service.js +2 -2
- package/dist/protocols/schedule-observe-agent.service.js +3 -3
- package/dist/services/operation-trace.registry.js +3 -3
- package/dist/utils/default-trace-id-generator.util.js +3 -2
- package/dist/utils/ingest-refusal.util.d.ts +21 -0
- package/dist/utils/ingest-refusal.util.js +45 -0
- package/dist/utils/uuid-v7.util.d.ts +16 -0
- package/dist/utils/uuid-v7.util.js +34 -0
- package/package.json +7 -1
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import type { createTelemetrySanitizer } from "./telemetry-wire-contract.js";
|
|
2
|
-
|
|
2
|
+
import type { describeIngestRefusal } from "../utils/ingest-refusal.util.js";
|
|
3
|
+
export declare const detachedObserveWorker: (createSanitizer?: typeof createTelemetrySanitizer, describeRefusal?: typeof describeIngestRefusal) => void;
|
|
@@ -3,7 +3,11 @@ export const detachedObserveWorker = (
|
|
|
3
3
|
// is assembled (see `ObserveAgentWorker.initializeWorker`) - a stringified
|
|
4
4
|
// function cannot import it. Type-only import above; a value import would
|
|
5
5
|
// compile and then throw inside the worker.
|
|
6
|
-
createSanitizer
|
|
6
|
+
createSanitizer,
|
|
7
|
+
// Same arrangement: the helper that turns a 429 body into a sentence
|
|
8
|
+
// travels as source. Optional so a worker assembled without it still
|
|
9
|
+
// reports the refusal, just without the reason.
|
|
10
|
+
describeRefusal) => {
|
|
7
11
|
// This function body is stringified via .toString() and executed as a
|
|
8
12
|
// standalone worker script, so it can't use static ES imports and must
|
|
9
13
|
// require() its dependencies at runtime instead.
|
|
@@ -254,10 +258,19 @@ createSanitizer) => {
|
|
|
254
258
|
if (response.status === 429) {
|
|
255
259
|
const pauseMs = rateLimitPauseMs(response);
|
|
256
260
|
rateLimitedUntil = Date.now() + pauseMs;
|
|
261
|
+
// The body says which refusal this is - a quota, a quota frozen by
|
|
262
|
+
// a declined payment, or a rate cap - and each needs a different
|
|
263
|
+
// fix from the operator. Parsed defensively: a rate-cap body is
|
|
264
|
+
// not the quota shape and an older collector sends none at all,
|
|
265
|
+
// and neither may turn a refusal into an "unreachable" report.
|
|
266
|
+
const body = await response.json().catch(() => undefined);
|
|
267
|
+
const why = typeof describeRefusal === "function"
|
|
268
|
+
? describeRefusal(body)
|
|
269
|
+
: "the account may be out of credits or over budget";
|
|
257
270
|
// One line per pause window, so a long depletion reads as one line
|
|
258
271
|
// every few minutes rather than one per flush. The batch is dropped
|
|
259
272
|
// - `finally` clears the buffer - same as every other failure here.
|
|
260
|
-
parentPort.postMessage(`Error: Telemetry rate-limited (429) -
|
|
273
|
+
parentPort.postMessage(`Error: Telemetry rate-limited (429) - ${why}. ` +
|
|
261
274
|
`Pausing sends for ${Math.round(pauseMs / 60000)} minute(s); batches in the meantime are dropped.`);
|
|
262
275
|
return true;
|
|
263
276
|
}
|
|
@@ -19,6 +19,7 @@ import { CpuProfilerService } from "../profiling/cpu-profiler.service.js";
|
|
|
19
19
|
import { ProfileUploader } from "../profiling/profile-uploader.js";
|
|
20
20
|
import { setActiveSliceRecorder, SpanSliceRecorder, } from "../profiling/span-slice-recorder.js";
|
|
21
21
|
import { NodeRuntimeMetricsService } from "../services/node-runtime-metrics.service.js";
|
|
22
|
+
import { describeIngestRefusal } from "../utils/ingest-refusal.util.js";
|
|
22
23
|
import { detachedObserveWorker } from "./detached-observe-worker.js";
|
|
23
24
|
import { ObserveAgentSharedBuffer } from "./observe-agent.shared-buffer.js";
|
|
24
25
|
import { createTelemetrySanitizer, SECTION_SHAPES, } from "./telemetry-wire-contract.js";
|
|
@@ -188,11 +189,12 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
188
189
|
}
|
|
189
190
|
initializeWorker() {
|
|
190
191
|
this.warnIfCredentialsSentInClear();
|
|
191
|
-
// The sanitizer factory
|
|
192
|
-
// worker is eval'd source with no module
|
|
193
|
-
// travel as source, and data (the wire
|
|
194
|
-
// `workerData`.
|
|
195
|
-
|
|
192
|
+
// The sanitizer factory and the refusal describer are inlined as the
|
|
193
|
+
// worker function's arguments: the worker is eval'd source with no module
|
|
194
|
+
// scope, so code it needs has to travel as source, and data (the wire
|
|
195
|
+
// shapes are plain JSON) as `workerData`. Both are written self-contained
|
|
196
|
+
// for exactly this.
|
|
197
|
+
this.worker = new Worker(`(${detachedObserveWorker.toString()})(${createTelemetrySanitizer.toString()}, ${describeIngestRefusal.toString()})`, {
|
|
196
198
|
eval: true,
|
|
197
199
|
workerData: {
|
|
198
200
|
sharedBuffer: this.observeAgentSharedBuffer.sharedBuffer,
|
|
@@ -224,6 +224,10 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
224
224
|
value: originalMethod.name,
|
|
225
225
|
configurable: true,
|
|
226
226
|
});
|
|
227
|
+
Object.defineProperty(boundFn, "length", {
|
|
228
|
+
value: originalMethod.length,
|
|
229
|
+
configurable: true,
|
|
230
|
+
});
|
|
227
231
|
const metadataKeys = Reflect.getMetadataKeys(originalMethod);
|
|
228
232
|
for (const key of metadataKeys) {
|
|
229
233
|
const metadata = Reflect.getMetadata(key, originalMethod);
|
|
@@ -52,8 +52,9 @@ export interface CreateObserveModuleOptions {
|
|
|
52
52
|
* For HTTP protocols, the request object is passed to the function.
|
|
53
53
|
* For non-HTTP protocols, the request object will be contextual and may not be available.
|
|
54
54
|
* The default implementation uses the 'x-request-id' header if available,
|
|
55
|
-
* or
|
|
56
|
-
*
|
|
55
|
+
* or mints a time-ordered UUID (version 7) if the header is not present -
|
|
56
|
+
* an id the collector can locate in time without a scan.
|
|
57
|
+
* @default (req) => req.headers['x-request-id'] || uuidv7()
|
|
57
58
|
*/
|
|
58
59
|
traceIdGenerator?: (req: unknown) => string;
|
|
59
60
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describeIngestRefusal } from "../utils/ingest-refusal.util.js";
|
|
1
2
|
/**
|
|
2
3
|
* Windows held while uploads are failing.
|
|
3
4
|
*
|
|
@@ -117,7 +118,10 @@ export class ProfileUploader {
|
|
|
117
118
|
// carries the freshest profiles the two-window cap kept.
|
|
118
119
|
const pauseMs = this.rateLimitPauseMs(response);
|
|
119
120
|
this.rateLimitedUntil = Date.now() + pauseMs;
|
|
120
|
-
|
|
121
|
+
// Same reading of the body as the telemetry worker, so the two halves
|
|
122
|
+
// of the agent describe one account state in one way.
|
|
123
|
+
const body = await response.json().catch(() => undefined);
|
|
124
|
+
this.options.onError?.(`profile upload rate-limited (429) - ${describeIngestRefusal(body)}; ` +
|
|
121
125
|
`pausing uploads for ${Math.round(pauseMs / 60000)} minute(s)`);
|
|
122
126
|
return false;
|
|
123
127
|
}
|
|
@@ -13,7 +13,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
var QueueObserveAgentService_1;
|
|
14
14
|
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
15
|
import { AsyncLocalStorage } from "async_hooks";
|
|
16
|
-
import {
|
|
16
|
+
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
17
17
|
import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.js";
|
|
18
18
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
19
19
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
@@ -112,7 +112,7 @@ let QueueObserveAgentService = QueueObserveAgentService_1 = class QueueObserveAg
|
|
|
112
112
|
}
|
|
113
113
|
return processor(job);
|
|
114
114
|
}
|
|
115
|
-
const traceId =
|
|
115
|
+
const traceId = uuidv7();
|
|
116
116
|
store.set(this.options.traceIdKey, traceId);
|
|
117
117
|
if (this.options.jobs?.setAttributes) {
|
|
118
118
|
const attributes = this.options.jobs?.setAttributes?.({
|
|
@@ -13,7 +13,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
var ScheduleObserveAgentService_1;
|
|
14
14
|
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
15
|
import { AsyncLocalStorage } from "async_hooks";
|
|
16
|
-
import {
|
|
16
|
+
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
17
17
|
import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.js";
|
|
18
18
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
19
19
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
@@ -168,10 +168,10 @@ let ScheduleObserveAgentService = ScheduleObserveAgentService_1 = class Schedule
|
|
|
168
168
|
}
|
|
169
169
|
return methodRef.call(instance, ...args);
|
|
170
170
|
}
|
|
171
|
-
const traceId =
|
|
171
|
+
const traceId = uuidv7();
|
|
172
172
|
store.set(this.options.traceIdKey, traceId);
|
|
173
173
|
// Every firing is its own job run, so every firing gets its own id.
|
|
174
|
-
const id =
|
|
174
|
+
const id = uuidv7();
|
|
175
175
|
if (this.options.jobs?.setAttributes) {
|
|
176
176
|
const attributes = this.options.jobs.setAttributes({
|
|
177
177
|
queueName,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IntrinsicException, Logger } from "@nestjs/common";
|
|
2
2
|
import { activeSliceRecorder } from "../profiling/span-slice-recorder.js";
|
|
3
|
-
import {
|
|
3
|
+
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
4
4
|
import { TraceSpanDelegate } from "../trace-span.delegate.js";
|
|
5
5
|
import { collectCodeFrames } from "../utils/source-context.util.js";
|
|
6
6
|
import { CALLER_METADATA_KEY } from "../observe.constants.js";
|
|
@@ -204,7 +204,7 @@ export class OperationTraceRegistry {
|
|
|
204
204
|
return undefined;
|
|
205
205
|
}
|
|
206
206
|
snapshot.refsCounter += 1;
|
|
207
|
-
const newNodeId =
|
|
207
|
+
const newNodeId = uuidv7();
|
|
208
208
|
const startTime = performance.now();
|
|
209
209
|
// Records which span owns the thread from here. A no-op unless continuous
|
|
210
210
|
// profiling is on, and the only way profile samples can be attributed to a
|
|
@@ -463,7 +463,7 @@ export class OperationTraceRegistry {
|
|
|
463
463
|
// half-built tree whose ongoing node still held its circular `parent`
|
|
464
464
|
// reference, and JSON.stringify threw away the whole flush window.
|
|
465
465
|
snapshot.refsCounter += 1;
|
|
466
|
-
const newNodeId =
|
|
466
|
+
const newNodeId = uuidv7();
|
|
467
467
|
const startTime = performance.now();
|
|
468
468
|
// Same as internalStartTraceStep: without an enter, the exit issued on
|
|
469
469
|
// completion no-ops and the span's execution time is silently attributed
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { uuidv7 } from "./uuid-v7.util.js";
|
|
2
2
|
/**
|
|
3
3
|
* What an inbound `x-request-id` must look like to be adopted as the trace id.
|
|
4
4
|
*
|
|
@@ -18,5 +18,6 @@ export function defaultTraceIdGenerator(req) {
|
|
|
18
18
|
return requestId;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
// Minted here, so it can carry its time - see uuidv7.
|
|
22
|
+
return uuidv7();
|
|
22
23
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explains a 429 from the collector in the operator's terms.
|
|
3
|
+
*
|
|
4
|
+
* The collector refuses ingest for three different reasons, and until this
|
|
5
|
+
* existed the agent reported all of them as "the account may be out of
|
|
6
|
+
* credits or over budget" - which sent an operator whose card had been
|
|
7
|
+
* declined to the pricing page, and one whose rate cap had engaged to their
|
|
8
|
+
* invoices. The body tells them apart: a quota refusal carries
|
|
9
|
+
* `code: "USAGE_LIMIT_REACHED"` with the plan and the counts, and a quota
|
|
10
|
+
* refusal caused by a declined payment adds `reason: "payment_failed"`. A
|
|
11
|
+
* rate cap, and any older collector, sends neither and gets the old sentence.
|
|
12
|
+
*
|
|
13
|
+
* Written self-contained on purpose - no imports, no module-scope constants -
|
|
14
|
+
* because the telemetry worker is a stringified function and this travels
|
|
15
|
+
* into it as source (see `ObserveAgentWorker.initializeWorker`). Anything it
|
|
16
|
+
* referenced from this module's scope would be undefined there.
|
|
17
|
+
*
|
|
18
|
+
* @param body the parsed JSON body of the 429, or anything else - a text
|
|
19
|
+
* body, `undefined` from a failed parse - which reads as "no detail".
|
|
20
|
+
*/
|
|
21
|
+
export declare const describeIngestRefusal: (body: unknown) => string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explains a 429 from the collector in the operator's terms.
|
|
3
|
+
*
|
|
4
|
+
* The collector refuses ingest for three different reasons, and until this
|
|
5
|
+
* existed the agent reported all of them as "the account may be out of
|
|
6
|
+
* credits or over budget" - which sent an operator whose card had been
|
|
7
|
+
* declined to the pricing page, and one whose rate cap had engaged to their
|
|
8
|
+
* invoices. The body tells them apart: a quota refusal carries
|
|
9
|
+
* `code: "USAGE_LIMIT_REACHED"` with the plan and the counts, and a quota
|
|
10
|
+
* refusal caused by a declined payment adds `reason: "payment_failed"`. A
|
|
11
|
+
* rate cap, and any older collector, sends neither and gets the old sentence.
|
|
12
|
+
*
|
|
13
|
+
* Written self-contained on purpose - no imports, no module-scope constants -
|
|
14
|
+
* because the telemetry worker is a stringified function and this travels
|
|
15
|
+
* into it as source (see `ObserveAgentWorker.initializeWorker`). Anything it
|
|
16
|
+
* referenced from this module's scope would be undefined there.
|
|
17
|
+
*
|
|
18
|
+
* @param body the parsed JSON body of the 429, or anything else - a text
|
|
19
|
+
* body, `undefined` from a failed parse - which reads as "no detail".
|
|
20
|
+
*/
|
|
21
|
+
export const describeIngestRefusal = (body) => {
|
|
22
|
+
const detail = body !== null && typeof body === "object"
|
|
23
|
+
? body
|
|
24
|
+
: {};
|
|
25
|
+
if (detail.code !== "USAGE_LIMIT_REACHED") {
|
|
26
|
+
return "the account may be out of credits or over budget";
|
|
27
|
+
}
|
|
28
|
+
const plan = typeof detail.plan === "string" ? detail.plan : undefined;
|
|
29
|
+
const counts = typeof detail.used === "number" && typeof detail.included === "number"
|
|
30
|
+
? `${detail.used.toLocaleString("en-US")} of ${detail.included.toLocaleString("en-US")} included events used` +
|
|
31
|
+
(plan ? ` on the ${plan} plan` : "")
|
|
32
|
+
: undefined;
|
|
33
|
+
if (detail.reason === "payment_failed") {
|
|
34
|
+
// The plan has overage; it is switched off because the last payment was
|
|
35
|
+
// declined. The fix is a card, not an upgrade, so say so - and say that
|
|
36
|
+
// it lifts on payment, since the operator will otherwise wait for the
|
|
37
|
+
// period to reset.
|
|
38
|
+
return ("a payment on the account was declined, so events past the included volume are refused until the outstanding invoice is paid" +
|
|
39
|
+
(counts ? ` (${counts})` : "") +
|
|
40
|
+
"; update the payment method in the dashboard to resume");
|
|
41
|
+
}
|
|
42
|
+
return ("the account has used its included events for this period" +
|
|
43
|
+
(counts ? ` (${counts})` : "") +
|
|
44
|
+
"; upgrade the plan or wait for the period to reset");
|
|
45
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A UUID whose first 48 bits are the minting time, milliseconds since the
|
|
3
|
+
* epoch (RFC 9562 version 7).
|
|
4
|
+
*
|
|
5
|
+
* Every id the agent mints itself - a trace id when no request id arrived, a
|
|
6
|
+
* span id, a scheduled run's id - is stored by the collector in time-ordered
|
|
7
|
+
* hypertables and later looked up by that id alone. A random id says nothing
|
|
8
|
+
* about when, so a lookup by it has to consider every chunk the project
|
|
9
|
+
* keeps; a v7 id tells the collector which chunk to open. Ids the agent did
|
|
10
|
+
* not mint - a propagated `x-request-id`, a customer's own trace key - are
|
|
11
|
+
* passed through untouched, and the collector treats those as before.
|
|
12
|
+
*
|
|
13
|
+
* Same layout as the collector's own generator, so ids from either side
|
|
14
|
+
* decode the same way.
|
|
15
|
+
*/
|
|
16
|
+
export declare function uuidv7(now?: number): string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { randomBytes } from "crypto";
|
|
2
|
+
/**
|
|
3
|
+
* A UUID whose first 48 bits are the minting time, milliseconds since the
|
|
4
|
+
* epoch (RFC 9562 version 7).
|
|
5
|
+
*
|
|
6
|
+
* Every id the agent mints itself - a trace id when no request id arrived, a
|
|
7
|
+
* span id, a scheduled run's id - is stored by the collector in time-ordered
|
|
8
|
+
* hypertables and later looked up by that id alone. A random id says nothing
|
|
9
|
+
* about when, so a lookup by it has to consider every chunk the project
|
|
10
|
+
* keeps; a v7 id tells the collector which chunk to open. Ids the agent did
|
|
11
|
+
* not mint - a propagated `x-request-id`, a customer's own trace key - are
|
|
12
|
+
* passed through untouched, and the collector treats those as before.
|
|
13
|
+
*
|
|
14
|
+
* Same layout as the collector's own generator, so ids from either side
|
|
15
|
+
* decode the same way.
|
|
16
|
+
*/
|
|
17
|
+
export function uuidv7(now = Date.now()) {
|
|
18
|
+
// Ten random bytes: 12 bits go into `rand_a`, 62 into `rand_b`. The two
|
|
19
|
+
// version and two variant bits overwrite what they need.
|
|
20
|
+
const rand = randomBytes(10);
|
|
21
|
+
const bytes = Buffer.allocUnsafe(16);
|
|
22
|
+
// 48-bit big-endian timestamp, placed as a 32/16 split so no shift runs
|
|
23
|
+
// past 2^32.
|
|
24
|
+
bytes.writeUInt32BE(Math.floor(now / 0x10000), 0);
|
|
25
|
+
bytes.writeUInt16BE(now % 0x10000, 4);
|
|
26
|
+
// Version 7 in the top nibble, 12 random bits below it.
|
|
27
|
+
bytes[6] = 0x70 | (rand[0] & 0x0f);
|
|
28
|
+
bytes[7] = rand[1];
|
|
29
|
+
// Variant `10` in the top two bits, 62 random bits below.
|
|
30
|
+
bytes[8] = 0x80 | (rand[2] & 0x3f);
|
|
31
|
+
rand.copy(bytes, 9, 3, 10);
|
|
32
|
+
const hex = bytes.toString("hex");
|
|
33
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
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",
|
|
@@ -128,6 +128,12 @@
|
|
|
128
128
|
"unplugin-swc": "1.5.11",
|
|
129
129
|
"vitest": "4.1.11"
|
|
130
130
|
},
|
|
131
|
+
"overrides": {
|
|
132
|
+
"nestjs-cls": {
|
|
133
|
+
"@nestjs/common": "$@nestjs/common",
|
|
134
|
+
"@nestjs/core": "$@nestjs/core"
|
|
135
|
+
}
|
|
136
|
+
},
|
|
131
137
|
"lint-staged": {
|
|
132
138
|
"**/*.{ts,json}": []
|
|
133
139
|
},
|