@nestjs/observe 0.3.25 → 0.3.26
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 +17 -0
- package/dist/agent/detached-observe-worker.js +50 -6
- package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
- package/dist/agent/observe-agent.shared-buffer.js +196 -19
- package/dist/agent/observe-agent.worker.d.ts +22 -2
- package/dist/agent/observe-agent.worker.js +43 -7
- package/dist/agent/telemetry-wire-contract.js +7 -1
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +28 -3
- package/dist/cli/observe-cli.js +1 -19
- package/dist/cli/open-url.d.ts +27 -0
- package/dist/cli/open-url.js +63 -0
- package/dist/cli/project-files.d.ts +42 -1
- package/dist/cli/project-files.js +146 -7
- package/dist/cli/project-setup.d.ts +6 -0
- package/dist/cli/project-setup.js +14 -1
- package/dist/cli/wire-application.js +6 -0
- package/dist/custom-metrics/counter.d.ts +13 -2
- package/dist/custom-metrics/counter.js +33 -16
- package/dist/custom-metrics/finite-value.util.d.ts +14 -0
- package/dist/custom-metrics/finite-value.util.js +33 -1
- package/dist/custom-metrics/gauge.d.ts +2 -0
- package/dist/custom-metrics/gauge.js +41 -17
- package/dist/custom-metrics/label-values.util.d.ts +37 -0
- package/dist/custom-metrics/label-values.util.js +51 -0
- package/dist/custom-metrics/summary.d.ts +23 -0
- package/dist/custom-metrics/summary.js +73 -2
- package/dist/instrument/create-instance-decorator.instrument.js +158 -96
- package/dist/interfaces/observe-options.interface.d.ts +3 -1
- package/dist/objectives/objectives.registry.js +12 -0
- package/dist/observe.module.js +119 -3
- package/dist/otlp/otlp-encoders.js +22 -5
- package/dist/otlp/otlp-span.mapper.js +7 -3
- package/dist/otlp/otlp-trace-exporter.service.js +63 -5
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +13 -6
- package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
- package/dist/outgoing/llm/llm-providers.d.ts +16 -4
- package/dist/outgoing/llm/llm-providers.js +72 -22
- package/dist/outgoing/llm/llm-usage.d.ts +11 -6
- package/dist/outgoing/llm/llm-usage.js +72 -25
- package/dist/outgoing/mssql.integration.js +16 -2
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +15 -3
- package/dist/outgoing/outgoing-span.recorder.js +17 -3
- package/dist/outgoing/pg.integration.d.ts +3 -2
- package/dist/outgoing/pg.integration.js +39 -11
- package/dist/outgoing/sql-statement.util.js +106 -14
- package/dist/outgoing/undici.integration.d.ts +13 -0
- package/dist/outgoing/undici.integration.js +68 -0
- package/dist/protocols/bull-observe-agent.service.js +2 -12
- package/dist/protocols/deferred-trace-completion.d.ts +9 -1
- package/dist/protocols/deferred-trace-completion.js +36 -6
- package/dist/protocols/graphql-observe-agent.service.d.ts +36 -5
- package/dist/protocols/graphql-observe-agent.service.js +166 -5
- package/dist/protocols/graphql-operation-parser.js +72 -18
- package/dist/protocols/http-observe-agent.service.d.ts +43 -9
- package/dist/protocols/http-observe-agent.service.js +177 -59
- package/dist/protocols/job-trace-runner.d.ts +35 -3
- package/dist/protocols/job-trace-runner.js +126 -29
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +50 -8
- package/dist/protocols/rpc-observe-agent.service.js +187 -47
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +226 -20
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +3 -4
- package/dist/services/operation-trace.registry.d.ts +45 -1
- package/dist/services/operation-trace.registry.js +418 -35
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +288 -51
- package/dist/services/tracer.service.d.ts +23 -0
- package/dist/services/tracer.service.js +91 -5
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +100 -12
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +14 -0
- package/dist/utils/log-line.parser.js +7 -3
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +512 -61
- package/dist/utils/operation-state.util.d.ts +39 -0
- package/dist/utils/operation-state.util.js +50 -0
- package/dist/utils/redact-url-query.d.ts +26 -4
- package/dist/utils/redact-url-query.js +184 -32
- package/dist/utils/sensitive-keys.d.ts +4 -1
- package/dist/utils/sensitive-keys.js +48 -10
- package/dist/utils/shared-hook-slot.util.d.ts +48 -0
- package/dist/utils/shared-hook-slot.util.js +103 -0
- package/dist/utils/source-context.util.d.ts +8 -0
- package/dist/utils/source-context.util.js +88 -24
- package/dist/utils/source-map-resolver.util.js +8 -2
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -4,6 +4,48 @@ import { handedOnTraceContext } from "../utils/trace-context.util.js";
|
|
|
4
4
|
import { continueHandedOnTrace, traceparentToHandOn, } from "../utils/trace-handoff.util.js";
|
|
5
5
|
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
6
6
|
import { deferTraceCompletion, setAttributesSafely, shouldIgnoreSafely, } from "./deferred-trace-completion.js";
|
|
7
|
+
import { endOperation, liveTraceId, openOperation, } from "../utils/operation-state.util.js";
|
|
8
|
+
/** The later of the two ends of the range a `Date` can hold, in ms. */
|
|
9
|
+
const MAX_DATE_TIMESTAMP = 8.64e15;
|
|
10
|
+
/**
|
|
11
|
+
* When a job was enqueued, and how long it waited for a worker once it could
|
|
12
|
+
* run - left out when its times are not ones a `Date` can hold.
|
|
13
|
+
*
|
|
14
|
+
* Job fields are read back from Redis, which anything with access to it can
|
|
15
|
+
* write, and BullMQ takes a caller's `timestamp` option unchecked:
|
|
16
|
+
* `new Date(8.64e15 + 1).toISOString()` throws, and adding an object-valued
|
|
17
|
+
* `delay` to a number throws too. Both ran before the processor, so the
|
|
18
|
+
* worker failed a job that completes without the agent.
|
|
19
|
+
*
|
|
20
|
+
* `timestamp` is when the job was created, not when it became runnable, so for
|
|
21
|
+
* a delayed job the gap between the two is a schedule the caller asked for -
|
|
22
|
+
* not a queue that fell behind. Counting it would report a job dated a week
|
|
23
|
+
* out as a week of backlog and drag `job_wait_p95` with it. The delay is read
|
|
24
|
+
* off the options when the job's own reads 0: it is reset as a delayed job is
|
|
25
|
+
* promoted, so by the time a worker holds the job it says 0 whatever was
|
|
26
|
+
* asked for. `||`, not `??`, for that reason.
|
|
27
|
+
*/
|
|
28
|
+
export function readJobTimes(job) {
|
|
29
|
+
const { timestamp } = job;
|
|
30
|
+
if (!isDateTimestamp(timestamp)) {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
// Falling back to now() keeps the measurement honest if it is missing.
|
|
34
|
+
const startedAt = isDateTimestamp(job.processedOn)
|
|
35
|
+
? job.processedOn
|
|
36
|
+
: Date.now();
|
|
37
|
+
const asked = job.delay || job.opts?.delay;
|
|
38
|
+
const delay = typeof asked === "number" && Number.isFinite(asked) ? asked : 0;
|
|
39
|
+
return {
|
|
40
|
+
enqueuedAt: new Date(timestamp).toISOString(),
|
|
41
|
+
waitDuration: Math.max(0, startedAt - (timestamp + delay)),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function isDateTimestamp(value) {
|
|
45
|
+
return (typeof value === "number" &&
|
|
46
|
+
Number.isFinite(value) &&
|
|
47
|
+
Math.abs(value) <= MAX_DATE_TIMESTAMP);
|
|
48
|
+
}
|
|
7
49
|
const ORIGINAL_ADD = Symbol.for("nestjs.observe.queue.add");
|
|
8
50
|
const ORIGINAL_ADD_BULK = Symbol.for("nestjs.observe.queue.addBulk");
|
|
9
51
|
/** Where a run records which job it is running, for a layer nested inside it. */
|
|
@@ -100,8 +142,10 @@ export class JobTraceRunner {
|
|
|
100
142
|
*/
|
|
101
143
|
stampTraceId(opts) {
|
|
102
144
|
const store = this.asyncLocalStorage.getStore();
|
|
103
|
-
|
|
104
|
-
|
|
145
|
+
// A job a finished operation's timer enqueues, past the grace period,
|
|
146
|
+
// is no part of that operation's trace.
|
|
147
|
+
const traceId = liveTraceId(store, this.options.traceIdKey);
|
|
148
|
+
if (traceId === undefined) {
|
|
105
149
|
return undefined;
|
|
106
150
|
}
|
|
107
151
|
if (opts !== undefined && (typeof opts !== "object" || opts === null)) {
|
|
@@ -172,9 +216,10 @@ export class JobTraceRunner {
|
|
|
172
216
|
* Runs one job under a trace.
|
|
173
217
|
*
|
|
174
218
|
* `invoke` is handed `settle` for drivers whose handlers finish through a
|
|
175
|
-
* callback; pass `settlesItself` for those, and
|
|
176
|
-
* read as
|
|
177
|
-
* here either way
|
|
219
|
+
* callback; pass `settlesItself` for those, and what the handler returns,
|
|
220
|
+
* a promise included, is then not read as the outcome. A throwing handler
|
|
221
|
+
* is settled here either way, and a promise goes back to the driver as the
|
|
222
|
+
* handler returned it.
|
|
178
223
|
*/
|
|
179
224
|
run(job, invoke, settlesItself = false) {
|
|
180
225
|
const outerStore = this.asyncLocalStorage.getStore();
|
|
@@ -188,7 +233,14 @@ export class JobTraceRunner {
|
|
|
188
233
|
isSameJob(outerStore?.get(ACTIVE_JOB_KEY), activeJob)) {
|
|
189
234
|
return invoke(() => undefined);
|
|
190
235
|
}
|
|
191
|
-
|
|
236
|
+
// Any other run opens a store and a trace of its own, whatever store it
|
|
237
|
+
// starts in. A trace id there is no proof that an operation is still open
|
|
238
|
+
// around the run: a store outlives its operation in whatever the
|
|
239
|
+
// operation left running - the timer chain of a cron job a request
|
|
240
|
+
// rescheduled, a Redis connection a request opened - and a run read as
|
|
241
|
+
// nested in it used to run in an id-less store, unreported, with
|
|
242
|
+
// `TracerService.createSpan()` throwing in its handler.
|
|
243
|
+
//
|
|
192
244
|
// The same map `run` is given, rather than `getStore()` inside the
|
|
193
245
|
// callback: identical object, one lookup fewer, and it is known to exist.
|
|
194
246
|
const store = new Map();
|
|
@@ -196,14 +248,6 @@ export class JobTraceRunner {
|
|
|
196
248
|
store.set(ACTIVE_JOB_KEY, activeJob);
|
|
197
249
|
}
|
|
198
250
|
return this.asyncLocalStorage.run(store, () => {
|
|
199
|
-
if (hasOuterContext) {
|
|
200
|
-
// If the outer context already has a trace ID
|
|
201
|
-
// ignore the inner context
|
|
202
|
-
if (this.options.debug) {
|
|
203
|
-
this.logger.debug(`Outer context already has a trace ID. Skipping inner context for job "${job.name}" job.id: ${job.id}`);
|
|
204
|
-
}
|
|
205
|
-
return invoke(() => undefined);
|
|
206
|
-
}
|
|
207
251
|
// The registry key is always this run's own. The inherited id may belong
|
|
208
252
|
// to a request still open in this process, and a retry reuses it.
|
|
209
253
|
const registryKey = uuidv7();
|
|
@@ -211,6 +255,8 @@ export class JobTraceRunner {
|
|
|
211
255
|
const traceId = inheritedTraceId ?? registryKey;
|
|
212
256
|
store.set(this.options.traceIdKey, traceId);
|
|
213
257
|
store.set(TRACE_REGISTRY_KEY, registryKey);
|
|
258
|
+
// Ended as the run settles, recorded or not: see `liveTraceId`.
|
|
259
|
+
const operation = openOperation(store);
|
|
214
260
|
// With OpenTelemetry interop on, the job runs in the W3C trace context
|
|
215
261
|
// it was enqueued in, for an OpenTelemetry SDK in this process - see
|
|
216
262
|
// `continueHandedOnTrace`. Recorded or not, as an outbound call carries
|
|
@@ -235,21 +281,48 @@ export class JobTraceRunner {
|
|
|
235
281
|
// The trace id stays in the store so logs and jobs enqueued from here
|
|
236
282
|
// still correlate; nothing is registered under the registry key, so
|
|
237
283
|
// its spans have no trace to join.
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
284
|
+
const end = () => endOperation(operation);
|
|
285
|
+
let result;
|
|
286
|
+
try {
|
|
287
|
+
result =
|
|
288
|
+
handedOn === undefined
|
|
289
|
+
? invoke(end)
|
|
290
|
+
: continueHandedOnTrace(handedOn, traceId, () => invoke(end));
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
end();
|
|
294
|
+
throw error;
|
|
295
|
+
}
|
|
296
|
+
if (!settlesItself) {
|
|
297
|
+
// Its driver takes the promise as the outcome and consumes it, so
|
|
298
|
+
// watching it here changes nothing about how a rejection surfaces.
|
|
299
|
+
if (result instanceof Promise) {
|
|
300
|
+
result.then(end, end);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
end();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
241
307
|
}
|
|
242
308
|
this.operationTraceRegistry.startTrace(registryKey, {
|
|
243
309
|
tags: this.options.jobs?.tags,
|
|
244
310
|
...context,
|
|
245
311
|
...job.metadata,
|
|
246
312
|
}, traceId, traceContext);
|
|
247
|
-
let
|
|
313
|
+
let outcome;
|
|
314
|
+
// The promise a callback-style handler returned, which its driver
|
|
315
|
+
// drops - see where it is handed back, below.
|
|
316
|
+
let dropped;
|
|
248
317
|
const settle = (status) => {
|
|
249
|
-
if (
|
|
318
|
+
if (outcome) {
|
|
250
319
|
return;
|
|
251
320
|
}
|
|
252
|
-
|
|
321
|
+
outcome = status;
|
|
322
|
+
endOperation(operation);
|
|
323
|
+
if (status === "failed") {
|
|
324
|
+
dropped?.then(undefined, ignoreRejection);
|
|
325
|
+
}
|
|
253
326
|
deferTraceCompletion(async () => {
|
|
254
327
|
this.operationTraceRegistry.endTrace(registryKey, { status });
|
|
255
328
|
const snapshot = (await this.operationTraceRegistry.pluckSnapshot(registryKey));
|
|
@@ -272,15 +345,37 @@ export class JobTraceRunner {
|
|
|
272
345
|
? invoke(settle)
|
|
273
346
|
: continueHandedOnTrace(handedOn, traceId, () => invoke(settle));
|
|
274
347
|
if (returnValue instanceof Promise) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
348
|
+
// Handed back as the handler returned it, never as a promise
|
|
349
|
+
// chained onto it: a chained promise has none of the members of the
|
|
350
|
+
// handler's own (Bull calls `cancel()` on it when a job times out,
|
|
351
|
+
// and hands it to `active` listeners), and it rejects on its own.
|
|
352
|
+
if (settlesItself) {
|
|
353
|
+
// Bull drops what a callback-style processor returns, and `done`
|
|
354
|
+
// is the outcome. One that reports its failure through `done` and
|
|
355
|
+
// returns the promise it handled that way -
|
|
356
|
+
// `redis.hincrby(key, field, n, done)` - left a chained rejection
|
|
357
|
+
// nobody listened to, and the `unhandledRejection` ended a worker
|
|
358
|
+
// that carries on without the agent.
|
|
359
|
+
//
|
|
360
|
+
// So it is not watched either: watching marks it handled, and an
|
|
361
|
+
// `async (job, done)` processor that throws before it calls
|
|
362
|
+
// `done` has to raise `unhandledRejection` as it does without the
|
|
363
|
+
// agent. Only once `done` has reported a failure is the rejection
|
|
364
|
+
// marked handled: an instrumented provider's `@Process()` method
|
|
365
|
+
// hands back a promise chained onto its method's, and that one
|
|
366
|
+
// rejects again after the method's own was handled.
|
|
367
|
+
dropped = returnValue;
|
|
368
|
+
if (outcome === "failed") {
|
|
369
|
+
returnValue.then(undefined, ignoreRejection);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
// A driver that takes the promise as the outcome consumes it, so
|
|
374
|
+
// the run is settled from a side branch. Attached before the
|
|
375
|
+
// driver's own reactions, it still settles the run first.
|
|
376
|
+
returnValue.then(() => settle("completed"), () => settle("failed"));
|
|
377
|
+
}
|
|
378
|
+
return returnValue;
|
|
284
379
|
}
|
|
285
380
|
if (!settlesItself) {
|
|
286
381
|
settle("completed");
|
|
@@ -294,3 +389,5 @@ export class JobTraceRunner {
|
|
|
294
389
|
});
|
|
295
390
|
}
|
|
296
391
|
}
|
|
392
|
+
/** Marks a rejection handled, where it has already been reported. */
|
|
393
|
+
function ignoreRejection() { }
|
|
@@ -16,8 +16,15 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
16
16
|
import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.js";
|
|
17
17
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
18
18
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
19
|
-
import { JobTraceRunner } from "./job-trace-runner.js";
|
|
19
|
+
import { JobTraceRunner, readJobTimes, } from "./job-trace-runner.js";
|
|
20
20
|
import { describePeerLoadError, loadOptionalPeer, } from "../utils/optional-peer.util.js";
|
|
21
|
+
/**
|
|
22
|
+
* On the agent's `decorate`: the one it stands in front of, so a second
|
|
23
|
+
* agent wraps that rather than the first agent's. Kept on the wrapper, not
|
|
24
|
+
* the prototype, so an override the application installs after an agent is
|
|
25
|
+
* never mistaken for what that agent found.
|
|
26
|
+
*/
|
|
27
|
+
const DECORATE_IT_WRAPS = Symbol.for("nestjs.observe.bullmq.decorateItWraps");
|
|
21
28
|
const ORIGINAL_CALL_PROCESS_JOB = Symbol.for("nestjs.observe.bullmqPro.callProcessJob");
|
|
22
29
|
/**
|
|
23
30
|
* Job tracing for BullMQ: through `@nestjs/bullmq`'s processor decorator, and
|
|
@@ -49,23 +56,7 @@ let QueueObserveAgentService = QueueObserveAgentService_1 = class QueueObserveAg
|
|
|
49
56
|
* omitted rather than reported as zero.
|
|
50
57
|
*/
|
|
51
58
|
readQueueMetadata(job) {
|
|
52
|
-
const metadata =
|
|
53
|
-
if (typeof job.timestamp === "number") {
|
|
54
|
-
metadata.enqueuedAt = new Date(job.timestamp).toISOString();
|
|
55
|
-
// processedOn is set by the worker immediately before the processor runs.
|
|
56
|
-
// Falling back to now() keeps the measurement honest if it is missing.
|
|
57
|
-
const startedAt = typeof job.processedOn === "number" ? job.processedOn : Date.now();
|
|
58
|
-
// `timestamp` is when the job was created, not when it became runnable, so
|
|
59
|
-
// for a delayed job the gap between the two is a schedule the caller asked
|
|
60
|
-
// for - not a queue that fell behind. Counting it would report a job dated
|
|
61
|
-
// a week out as a week of backlog and drag `job_wait_p95` with it.
|
|
62
|
-
//
|
|
63
|
-
// Read off the options when `job.delay` is zero: BullMQ resets that
|
|
64
|
-
// field as it promotes a delayed job, so by the time a worker holds the
|
|
65
|
-
// job it says 0 whatever was asked for.
|
|
66
|
-
const availableAt = job.timestamp + (job.delay || job.opts?.delay || 0);
|
|
67
|
-
metadata.waitDuration = Math.max(0, startedAt - availableAt);
|
|
68
|
-
}
|
|
59
|
+
const metadata = readJobTimes(job);
|
|
69
60
|
if (typeof job.attemptsMade === "number") {
|
|
70
61
|
metadata.attemptsMade = job.attemptsMade;
|
|
71
62
|
}
|
|
@@ -108,11 +99,44 @@ let QueueObserveAgentService = QueueObserveAgentService_1 = class QueueObserveAg
|
|
|
108
99
|
this.logger.warn("ProcessorDecoratorService is not available. Please, update to the latest version of @nestjs/bullmq. Skipping patching.");
|
|
109
100
|
return;
|
|
110
101
|
}
|
|
102
|
+
// `decorate` is documented as the place to override, and the application
|
|
103
|
+
// may have: a check that refuses a job, a wrapper of its own. What is
|
|
104
|
+
// there is called, as the explorer would call it, and the job is traced
|
|
105
|
+
// around what it returns. An earlier agent's wrapper is seen through, to
|
|
106
|
+
// what that agent found.
|
|
107
|
+
const prototype = ProcessorDecoratorService.prototype;
|
|
108
|
+
const found = prototype.decorate;
|
|
109
|
+
const original = typeof found === "function" &&
|
|
110
|
+
Object.prototype.hasOwnProperty.call(found, DECORATE_IT_WRAPS)
|
|
111
|
+
? found[DECORATE_IT_WRAPS]
|
|
112
|
+
: found;
|
|
113
|
+
const runner = this.runner;
|
|
114
|
+
const describeJob = (job) => this.describeJob(job);
|
|
111
115
|
// The worker calls the processor with `(job, token, signal)`, and all of it
|
|
112
116
|
// is passed on: a processor that moves its own job (`moveToDelayed`,
|
|
113
117
|
// `extendLock`) needs the token to prove it holds the lock.
|
|
114
|
-
|
|
115
|
-
|
|
118
|
+
//
|
|
119
|
+
// And the wrapper declares as many parameters as the processor it stands
|
|
120
|
+
// in for does: the worker builds a job's abort signal only for a
|
|
121
|
+
// processor that declares three, so a rest parameter, whose `length` is
|
|
122
|
+
// 1, left every `process(job, token, signal)` without one and
|
|
123
|
+
// `cancelJob()` with nothing to abort.
|
|
124
|
+
const decorate = function (processor) {
|
|
125
|
+
const decorated = typeof original === "function"
|
|
126
|
+
? original.call(this, processor)
|
|
127
|
+
: processor;
|
|
128
|
+
if (typeof decorated !== "function") {
|
|
129
|
+
return decorated;
|
|
130
|
+
}
|
|
131
|
+
const traced = (job, ...rest) => runner.run(describeJob(job), () => decorated(job, ...rest));
|
|
132
|
+
Object.defineProperty(traced, "length", {
|
|
133
|
+
value: decorated.length,
|
|
134
|
+
configurable: true,
|
|
135
|
+
});
|
|
136
|
+
return traced;
|
|
137
|
+
};
|
|
138
|
+
Object.defineProperty(decorate, DECORATE_IT_WRAPS, { value: original });
|
|
139
|
+
prototype.decorate = decorate;
|
|
116
140
|
this.patchQueue();
|
|
117
141
|
}
|
|
118
142
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OnModuleDestroy
|
|
1
|
+
import { OnModuleDestroy } from "@nestjs/common";
|
|
2
2
|
import { ModulesContainer } from "@nestjs/core";
|
|
3
3
|
import type { BaseRpcContext, Server, Transport } from "@nestjs/microservices";
|
|
4
4
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -12,7 +12,7 @@ interface GrpcCall<TRequest = any, TMetadata = any> {
|
|
|
12
12
|
metadata: TMetadata;
|
|
13
13
|
operationId: string;
|
|
14
14
|
}
|
|
15
|
-
export declare class RpcObserveAgentService<Store extends Record<string, unknown>> implements
|
|
15
|
+
export declare class RpcObserveAgentService<Store extends Record<string, unknown>> implements OnModuleDestroy {
|
|
16
16
|
private readonly asyncLocalStorage;
|
|
17
17
|
private readonly options;
|
|
18
18
|
private readonly modulesContainer;
|
|
@@ -22,22 +22,36 @@ export declare class RpcObserveAgentService<Store extends Record<string, unknown
|
|
|
22
22
|
private readonly logger;
|
|
23
23
|
private rpcTargetAddedSubscription;
|
|
24
24
|
/**
|
|
25
|
-
* Assigned in
|
|
25
|
+
* Assigned in the constructor. Every use below is reached only through the
|
|
26
26
|
* hooks registered there, after a successful load - a service without the
|
|
27
27
|
* package has no RPC target to hook.
|
|
28
28
|
*/
|
|
29
29
|
private microservices;
|
|
30
|
+
/**
|
|
31
|
+
* The registry key each operation was started under, by the context Nest
|
|
32
|
+
* hands its end hook. That hook cannot ask the async store: Nest runs it
|
|
33
|
+
* from the teardown of the handler's reply stream, in the async context of
|
|
34
|
+
* whatever ended the stream - a TCP socket's `close`, a gRPC cancellation,
|
|
35
|
+
* a subject another message's handler emitted into. As
|
|
36
|
+
* `HttpObserveAgentService` keeps its keys by request.
|
|
37
|
+
*/
|
|
38
|
+
private readonly registryKeys;
|
|
39
|
+
/** The operation each context was started for - see `rememberOperation`. */
|
|
40
|
+
private readonly operations;
|
|
30
41
|
constructor(asyncLocalStorage: AsyncLocalStorage<Map<KeyOf<Store>, any>>, options: ObserveModuleOptionsWithDefaults, modulesContainer: ModulesContainer, operationTraceRegistry: OperationTraceRegistry, observeAgentSharedBuffer: ObserveAgentSharedBuffer, traceSamplerService: TraceSamplerService);
|
|
31
|
-
onModuleInit(): void;
|
|
32
42
|
onModuleDestroy(): void;
|
|
33
43
|
/**
|
|
34
44
|
* Loads `@nestjs/microservices` without a static import, so a service that
|
|
35
45
|
* exposes no microservice need not install the package. Loaded here rather
|
|
36
|
-
* than lazily because the target registry is subscribed from
|
|
37
|
-
* and a dynamic `import()` would resolve after a target may
|
|
38
|
-
* been announced.
|
|
46
|
+
* than lazily because the target registry is subscribed from the
|
|
47
|
+
* constructor, and a dynamic `import()` would resolve after a target may
|
|
48
|
+
* already have been announced.
|
|
39
49
|
*/
|
|
40
50
|
private loadMicroservices;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the agent's processing hooks on a server, beside any the application
|
|
53
|
+
* sets on it - see `composeStartHook`.
|
|
54
|
+
*/
|
|
41
55
|
registerRpcHooks(target: Server): void;
|
|
42
56
|
/**
|
|
43
57
|
* Names the transport for the snapshot's `protocol` field.
|
|
@@ -47,7 +61,9 @@ export declare class RpcObserveAgentService<Store extends Record<string, unknown
|
|
|
47
61
|
* transport used to arrive with no protocol at all.
|
|
48
62
|
*/
|
|
49
63
|
private toProtocolName;
|
|
50
|
-
startRpcRequestTracing(transportId: Transport | symbol, ctx: BaseRpcContext, done: () => Promise<any
|
|
64
|
+
startRpcRequestTracing(transportId: Transport | symbol, ctx: BaseRpcContext, done: () => Promise<any>,
|
|
65
|
+
/** The server the message arrived at. */
|
|
66
|
+
target?: Server): Promise<unknown>;
|
|
51
67
|
/**
|
|
52
68
|
* Whether OpenTelemetry interop runs now: `openTelemetry` on, beside
|
|
53
69
|
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.isReporting`.
|
|
@@ -55,6 +71,32 @@ export declare class RpcObserveAgentService<Store extends Record<string, unknown
|
|
|
55
71
|
private openTelemetryInteropOn;
|
|
56
72
|
startGrpcRequestTracing(transportId: Transport | symbol, call: GrpcCall, done: () => Promise<any>): Promise<unknown>;
|
|
57
73
|
endRpcRequestTracing(transportId: Transport | symbol, ctx: BaseRpcContext | GrpcCall): void;
|
|
74
|
+
/**
|
|
75
|
+
* Keeps `registryKey` for the end hook to find by `ctx`. A custom
|
|
76
|
+
* transporter delivers a context of its own making, and a gRPC call need not
|
|
77
|
+
* carry a request message: what cannot key a `WeakMap` is left to the store.
|
|
78
|
+
*/
|
|
79
|
+
private rememberRegistryKey;
|
|
80
|
+
/** As `rememberRegistryKey`, for the operation the end hook ends. */
|
|
81
|
+
private rememberOperation;
|
|
82
|
+
/**
|
|
83
|
+
* The route a message's handler is registered under, for the pattern or
|
|
84
|
+
* topic as its sender wrote it. Nest parses a pattern that is JSON and finds
|
|
85
|
+
* the handler by the normalised outcome, so one handler answers to any
|
|
86
|
+
* number of texts - other whitespace, an escape, a key given twice. Named by
|
|
87
|
+
* the text, each was an operation of the sender's choosing, and a route an
|
|
88
|
+
* `@Objective` on the handler was declared for anew.
|
|
89
|
+
*
|
|
90
|
+
* The route is taken only where the server finds that route's handler for
|
|
91
|
+
* the text: a handler reached through a wildcard keeps the text it was
|
|
92
|
+
* reached on, and so does anything the server cannot be asked about.
|
|
93
|
+
*/
|
|
94
|
+
private toRoute;
|
|
95
|
+
/**
|
|
96
|
+
* Kafka, MQTT, RabbitMQ and TCP find a message's handler by the text its
|
|
97
|
+
* context carries - see `toRoute`. NATS and Redis find it by the
|
|
98
|
+
* subscription the message came in on.
|
|
99
|
+
*/
|
|
58
100
|
private getOperationIdFromContext;
|
|
59
101
|
}
|
|
60
102
|
export {};
|