@nestjs/observe 0.3.23 → 0.3.24
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 +23 -17
- package/dist/agent/degraded-ingest.protocol.d.ts +1 -1
- package/dist/agent/degraded-ingest.protocol.js +1 -1
- package/dist/agent/observe-agent.shared-buffer.d.ts +31 -9
- package/dist/agent/observe-agent.shared-buffer.js +41 -13
- package/dist/agent/observe-agent.worker.d.ts +3 -2
- package/dist/agent/observe-agent.worker.js +15 -7
- package/dist/instrument/create-instance-decorator.instrument.js +34 -2
- package/dist/interfaces/observe-options.interface.d.ts +16 -12
- package/dist/outgoing/http.integration.d.ts +3 -4
- package/dist/outgoing/http.integration.js +1 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +2 -2
- package/dist/protocols/http-observe-agent.service.js +2 -2
- package/dist/protocols/job-trace-runner.d.ts +1 -1
- package/dist/protocols/job-trace-runner.js +2 -2
- package/dist/protocols/rpc-observe-agent.service.d.ts +1 -1
- package/dist/protocols/rpc-observe-agent.service.js +2 -2
- package/dist/utils/trace-handoff.util.d.ts +8 -2
- package/dist/utils/trace-handoff.util.js +11 -3
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -62,23 +62,29 @@ the SDK gave it - also when the request started the trace - and leaves
|
|
|
62
62
|
`traceparent` to the SDK on calls out of the service, so a request has one
|
|
63
63
|
trace id in both tools and every call carries one `traceparent`. The SDK keeps
|
|
64
64
|
its own spans, the agent adds the class-level ones. Queue jobs and microservice
|
|
65
|
-
messages stay in the request's trace in both tools too
|
|
66
|
-
or Bull job,
|
|
67
|
-
12.0.4 and later
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
messages stay in the request's trace in both tools too. A BullMQ (Pro included)
|
|
66
|
+
or Bull job, and a message over a built-in transport - TCP and Redis on
|
|
67
|
+
`@nestjs/microservices` 12.0.4 and later, NATS, Kafka, RabbitMQ, MQTT 5 and
|
|
68
|
+
gRPC on 12.3.3 and later - carries W3C trace context beside its trace id: as
|
|
69
|
+
call metadata over gRPC, as a header or user property over the others, so a
|
|
70
|
+
consumer in another language instrumented with OpenTelemetry continues the
|
|
71
|
+
trace. In a Nest service, the agent that runs the job or handles the message
|
|
72
|
+
hands that context to the SDK there, which has no instrumentation for BullMQ or
|
|
73
|
+
for Nest's TCP and Redis transports. Where an instrumentation does carry it -
|
|
74
|
+
BullMQ's own `bullmq-otel` add-on, the SDK's gRPC instrumentation - the work's
|
|
75
|
+
spans nest under the span it opens. This is tested against the SDK with its
|
|
70
76
|
auto-instrumentations, on Express and Fastify, over `fetch`, `node:http`, `pg`,
|
|
71
|
-
BullMQ
|
|
72
|
-
the agent speaks `x-request-id` alone
|
|
73
|
-
process alone.
|
|
74
|
-
|
|
75
|
-
It works only beside reporting to NestJS Observe. It
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
Observe is down. It pauses - saying so in the log - only while the
|
|
79
|
-
refuses the app's credentials or the agent's worker is down, resuming
|
|
80
|
-
next batch the collector accepts. With `enabled: false`, or without
|
|
81
|
-
for the hosted collector, it never turns on.
|
|
77
|
+
BullMQ with and without `bullmq-otel`, TCP and gRPC microservices and Nest's
|
|
78
|
+
own instrumentation. Without the option, the agent speaks `x-request-id` alone
|
|
79
|
+
and leaves any OpenTelemetry SDK in the process alone.
|
|
80
|
+
|
|
81
|
+
It works only beside reporting to NestJS Observe. It is on from start-up, so a
|
|
82
|
+
new instance's first requests stay in the traces around it, and stays on
|
|
83
|
+
through collector outages and rate limits, so those traces stay whole while
|
|
84
|
+
NestJS Observe is down. It pauses - saying so in the log - only while the
|
|
85
|
+
collector refuses the app's credentials or the agent's worker is down, resuming
|
|
86
|
+
with the next batch the collector accepts. With `enabled: false`, or without
|
|
87
|
+
credentials for the hosted collector, it never turns on.
|
|
82
88
|
|
|
83
89
|
```typescript
|
|
84
90
|
ObserveModule.forRoot({
|
|
@@ -415,7 +421,7 @@ Once the module and the instrument are in place, the agent reports, with no furt
|
|
|
415
421
|
- **Requests, GraphQL operations, RPC messages, WebSocket gateway messages, queue jobs and scheduled jobs**, each with its tree of provider method calls.
|
|
416
422
|
- **Database queries**, as a span under the method that ran them, for `pg`, `mysql2`, `mssql` and `mongodb`. Anything built on those drivers is covered without being known by name: TypeORM, Drizzle, MikroORM and Mongoose are tested in this repository, and others (Knex, Sequelize) go through the same driver calls. SQL Server is the exception: `mssql` is what TypeORM and Prisma's SQL Server adapter run on, but Knex, Sequelize and MikroORM talk to SQL Server through `tedious` directly, which is not covered yet (nor is `mssql/msnodesqlv8`). The statement is recorded with every value removed (`SELECT "o"."id" FROM "orders" "o" WHERE "o"."customer_id" = $1`). Prisma's Rust query engine does not go through these drivers and is not covered yet.
|
|
417
423
|
- **Outbound HTTP calls** made with `fetch`/`undici` or `node:http`/`https` (so Axios too), as `POST api.stripe.com`. The current trace id is forwarded as `x-request-id` - by `fetch` on every supported Node version, by `node:http`/`https` from Node 22.12, the first to let a request's headers still be set at that point - so a service that also runs the agent continues the same trace; a header you set yourself is never overwritten. With `openTelemetry: true`, a W3C `traceparent` goes with it, naming the trace and the call's span, so a service instrumented with OpenTelemetry - in Go, Python, Java or anything else - continues it too, as does a service running the agent behind it. When an OpenTelemetry SDK that propagates W3C Trace Context runs in the same process, `traceparent` is left to it: its instrumentations write the header on the calls they cover, and a request takes only one. `outgoing: { http: { traceContext: false } }` sends `x-request-id` alone even then, and `propagateTraceId: (url) => boolean` keeps both headers away from the URLs you name.
|
|
418
|
-
- **Trace context from OpenTelemetry**, with `openTelemetry: true`: a request arriving with a W3C `traceparent` - from a service instrumented with OpenTelemetry, a mesh, or a load balancer that starts traces - is reported under that trace id, written as a UUID (`4bf92f35-77b3-4da6-a3ce-929d0e0e4736`), so the same id finds it in both tools. With an OpenTelemetry SDK in the same process - or another tracer registered with the OpenTelemetry API, as dd-trace is once its `TracerProvider` is registered - the trace is the one the SDK's server span is in, so a request that starts a trace gets the SDK's id too. That holds when a mesh or router stamped an `x-request-id` of its own on the request too, as Envoy and Heroku's router do: an `x-request-id` is kept only when it names the same trace, which is what another service running the agent sends, so custom ids still carry from one such service to the next. BullMQ (Pro included) and Bull jobs, and messages over the TCP and Redis
|
|
424
|
+
- **Trace context from OpenTelemetry**, with `openTelemetry: true`: a request arriving with a W3C `traceparent` - from a service instrumented with OpenTelemetry, a mesh, or a load balancer that starts traces - is reported under that trace id, written as a UUID (`4bf92f35-77b3-4da6-a3ce-929d0e0e4736`), so the same id finds it in both tools. With an OpenTelemetry SDK in the same process - or another tracer registered with the OpenTelemetry API, as dd-trace is once its `TracerProvider` is registered - the trace is the one the SDK's server span is in, so a request that starts a trace gets the SDK's id too. That holds when a mesh or router stamped an `x-request-id` of its own on the request too, as Envoy and Heroku's router do: an `x-request-id` is kept only when it names the same trace, which is what another service running the agent sends, so custom ids still carry from one such service to the next. BullMQ (Pro included) and Bull jobs, and messages over the built-in transports (TCP and Redis on `@nestjs/microservices` 12.0.4 and later; NATS, Kafka, RabbitMQ, MQTT 5 and gRPC on 12.3.3 and later), carry W3C trace context beside the trace id, so whatever runs the job or handles the message continues the request's trace in OpenTelemetry too - an SDK in a Nest service, or a consumer in another language. Without the option, a request is named by its `x-request-id`, else by a UUIDv7 of its own, and no `traceparent` is read or sent.
|
|
419
425
|
- **Model calls**: a `fetch` to OpenAI, Azure OpenAI, Anthropic, Gemini or Vertex, Bedrock, Mistral, Groq, OpenRouter, Together, DeepSeek, xAI, Cohere or Perplexity also reports the model and the input, output and cached tokens its response states - read as the response passes through to your code, streams included, so it arrives exactly as it would have and a stream you cancel is cancelled at the provider too. That covers the official SDKs, the Vercel AI SDK and LangChain, which all use `fetch`; calls made through `node:http` (axios, the AWS SDK) keep their plain span. Prompts and completions are never reported. A gateway in front of the providers is added with `outgoing: { llm: { hosts: ['llm-gateway.internal:4000'] } }`, and `outgoing: { llm: false }` turns this off and leaves the global `fetch` as it was.
|
|
420
426
|
- **Trace ids across queues**: a BullMQ (Pro included) or Bull job enqueued while handling a request carries that request's trace id, so the request and its job show up as one trace. Repeatable (cron) jobs start their own.
|
|
421
427
|
- **The code around an error's throw site**: for each in-app frame of a captured error, a few lines of source either side, read from the running process, so the dashboard shows the failing code next to the stack. This sends fragments of your application's source to Observe, stored with the error. Frames in `node_modules` and Node internals are never read. `createObserveModule({ sourceContext: false })` turns it off; the [documentation](https://www.observe.nestjs.com/documentation/sdk#error-source-context) covers tuning it instead.
|
|
@@ -112,7 +112,7 @@ export declare function parsePausedMessage(msg: string): number | null;
|
|
|
112
112
|
* says so with its degraded verdict instead.
|
|
113
113
|
*
|
|
114
114
|
* Read for OpenTelemetry interop, which a refusal pauses and any other failure
|
|
115
|
-
* leaves as it is - see `ObserveAgentSharedBuffer.
|
|
115
|
+
* leaves as it is - see `ObserveAgentSharedBuffer.isReporting`.
|
|
116
116
|
*/
|
|
117
117
|
export declare const NOT_INGESTED_MESSAGE_PREFIX = "observe:not-ingested=";
|
|
118
118
|
export type NotIngestedReason = "refused" | "failed";
|
|
@@ -94,7 +94,7 @@ export function parsePausedMessage(msg) {
|
|
|
94
94
|
* says so with its degraded verdict instead.
|
|
95
95
|
*
|
|
96
96
|
* Read for OpenTelemetry interop, which a refusal pauses and any other failure
|
|
97
|
-
* leaves as it is - see `ObserveAgentSharedBuffer.
|
|
97
|
+
* leaves as it is - see `ObserveAgentSharedBuffer.isReporting`.
|
|
98
98
|
*/
|
|
99
99
|
export const NOT_INGESTED_MESSAGE_PREFIX = "observe:not-ingested=";
|
|
100
100
|
/** Why a batch did not get in, or null if this is not that line. */
|
|
@@ -88,11 +88,14 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
88
88
|
*/
|
|
89
89
|
private errorsOnly;
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
91
|
+
* How far this app's reporting to NestJS Observe has got - see
|
|
92
|
+
* `isReporting` and `isIngesting`. `off` until the worker starts with the
|
|
93
|
+
* application, `started` from then until the collector accepts a batch,
|
|
94
|
+
* and `ingesting` from that batch on. Refused credentials and a stopped
|
|
95
|
+
* worker set it back to `off`, and only an accepted batch moves it on
|
|
96
|
+
* from there.
|
|
94
97
|
*/
|
|
95
|
-
private
|
|
98
|
+
private reporting;
|
|
96
99
|
/**
|
|
97
100
|
* Requests and jobs that errors-only ingest withheld from the batch being
|
|
98
101
|
* built, cleared with it.
|
|
@@ -125,9 +128,16 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
125
128
|
/** Whether only server errors are being sent, until the same window lapses. */
|
|
126
129
|
isErrorsOnly(): boolean;
|
|
127
130
|
/**
|
|
128
|
-
* Whether this app reports to NestJS Observe
|
|
129
|
-
*
|
|
130
|
-
* the worker stops - and again from the next batch
|
|
131
|
+
* Whether this app reports to NestJS Observe, as far as the agent can tell:
|
|
132
|
+
* from start-up, once its worker is running, until the collector refuses
|
|
133
|
+
* the app's credentials or the worker stops - and again from the next batch
|
|
134
|
+
* the collector accepts.
|
|
135
|
+
*
|
|
136
|
+
* Nothing has to get in first. A service's first requests are when a new
|
|
137
|
+
* instance joins the traces around it, and an agent set up to report -
|
|
138
|
+
* enabled, with credentials - almost always does. Credentials the collector
|
|
139
|
+
* refuses are learned from the first batch the worker sends, a flush
|
|
140
|
+
* interval into the app's traffic; until then they count as reporting.
|
|
131
141
|
*
|
|
132
142
|
* A batch that fails to get in for any other reason - a collector that is
|
|
133
143
|
* down or slow to answer, a 5xx, a 429 pause, however long - leaves it as
|
|
@@ -139,10 +149,22 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
139
149
|
* OpenTelemetry interop runs on it (see `openTelemetry`): the agent reads
|
|
140
150
|
* and sends `traceparent`, and follows an OpenTelemetry SDK in the process,
|
|
141
151
|
* only beside reporting to NestJS Observe. So an agent that sends Observe
|
|
142
|
-
* nothing - `enabled: false`, no credentials
|
|
143
|
-
|
|
152
|
+
* nothing - `enabled: false`, no credentials - never turns it on.
|
|
153
|
+
*/
|
|
154
|
+
isReporting(): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* Whether the collector takes this app's telemetry: from the first batch
|
|
157
|
+
* it accepts, until it refuses the app's credentials or the worker stops -
|
|
158
|
+
* and again from the next batch it accepts. What its replies grant, an
|
|
159
|
+
* OTLP export say, holds only while it does.
|
|
144
160
|
*/
|
|
145
161
|
isIngesting(): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* The worker started with the application, set up to report: see
|
|
164
|
+
* `isReporting`. A worker restarted after a stop is not this - it has to
|
|
165
|
+
* get a batch in first - and a batch already taken stays taken.
|
|
166
|
+
*/
|
|
167
|
+
recordStarted(): void;
|
|
146
168
|
/** The collector accepted a batch, whatever it then kept of it. */
|
|
147
169
|
recordAccepted(): void;
|
|
148
170
|
/** The collector refused the app's credentials. */
|
|
@@ -245,11 +245,14 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
245
245
|
*/
|
|
246
246
|
errorsOnly = false;
|
|
247
247
|
/**
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
248
|
+
* How far this app's reporting to NestJS Observe has got - see
|
|
249
|
+
* `isReporting` and `isIngesting`. `off` until the worker starts with the
|
|
250
|
+
* application, `started` from then until the collector accepts a batch,
|
|
251
|
+
* and `ingesting` from that batch on. Refused credentials and a stopped
|
|
252
|
+
* worker set it back to `off`, and only an accepted batch moves it on
|
|
253
|
+
* from there.
|
|
251
254
|
*/
|
|
252
|
-
|
|
255
|
+
reporting = "off";
|
|
253
256
|
/**
|
|
254
257
|
* Requests and jobs that errors-only ingest withheld from the batch being
|
|
255
258
|
* built, cleared with it.
|
|
@@ -289,9 +292,16 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
289
292
|
return this.errorsOnly && this.isDegraded();
|
|
290
293
|
}
|
|
291
294
|
/**
|
|
292
|
-
* Whether this app reports to NestJS Observe
|
|
293
|
-
*
|
|
294
|
-
* the worker stops - and again from the next batch
|
|
295
|
+
* Whether this app reports to NestJS Observe, as far as the agent can tell:
|
|
296
|
+
* from start-up, once its worker is running, until the collector refuses
|
|
297
|
+
* the app's credentials or the worker stops - and again from the next batch
|
|
298
|
+
* the collector accepts.
|
|
299
|
+
*
|
|
300
|
+
* Nothing has to get in first. A service's first requests are when a new
|
|
301
|
+
* instance joins the traces around it, and an agent set up to report -
|
|
302
|
+
* enabled, with credentials - almost always does. Credentials the collector
|
|
303
|
+
* refuses are learned from the first batch the worker sends, a flush
|
|
304
|
+
* interval into the app's traffic; until then they count as reporting.
|
|
295
305
|
*
|
|
296
306
|
* A batch that fails to get in for any other reason - a collector that is
|
|
297
307
|
* down or slow to answer, a 5xx, a 429 pause, however long - leaves it as
|
|
@@ -303,23 +313,41 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
303
313
|
* OpenTelemetry interop runs on it (see `openTelemetry`): the agent reads
|
|
304
314
|
* and sends `traceparent`, and follows an OpenTelemetry SDK in the process,
|
|
305
315
|
* only beside reporting to NestJS Observe. So an agent that sends Observe
|
|
306
|
-
* nothing - `enabled: false`, no credentials
|
|
307
|
-
|
|
316
|
+
* nothing - `enabled: false`, no credentials - never turns it on.
|
|
317
|
+
*/
|
|
318
|
+
isReporting() {
|
|
319
|
+
return this.reporting !== "off";
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Whether the collector takes this app's telemetry: from the first batch
|
|
323
|
+
* it accepts, until it refuses the app's credentials or the worker stops -
|
|
324
|
+
* and again from the next batch it accepts. What its replies grant, an
|
|
325
|
+
* OTLP export say, holds only while it does.
|
|
308
326
|
*/
|
|
309
327
|
isIngesting() {
|
|
310
|
-
return this.ingesting;
|
|
328
|
+
return this.reporting === "ingesting";
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* The worker started with the application, set up to report: see
|
|
332
|
+
* `isReporting`. A worker restarted after a stop is not this - it has to
|
|
333
|
+
* get a batch in first - and a batch already taken stays taken.
|
|
334
|
+
*/
|
|
335
|
+
recordStarted() {
|
|
336
|
+
if (this.reporting === "off") {
|
|
337
|
+
this.reporting = "started";
|
|
338
|
+
}
|
|
311
339
|
}
|
|
312
340
|
/** The collector accepted a batch, whatever it then kept of it. */
|
|
313
341
|
recordAccepted() {
|
|
314
|
-
this.
|
|
342
|
+
this.reporting = "ingesting";
|
|
315
343
|
}
|
|
316
344
|
/** The collector refused the app's credentials. */
|
|
317
345
|
recordRefused() {
|
|
318
|
-
this.
|
|
346
|
+
this.reporting = "off";
|
|
319
347
|
}
|
|
320
348
|
/** The worker stopped; a restarted one has to get a batch in again. */
|
|
321
349
|
recordStopped() {
|
|
322
|
-
this.
|
|
350
|
+
this.reporting = "off";
|
|
323
351
|
}
|
|
324
352
|
/**
|
|
325
353
|
* What the collector's last accepted reply granted `otlp` - see
|
|
@@ -155,8 +155,9 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
|
|
|
155
155
|
/**
|
|
156
156
|
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
157
157
|
* it runs only beside reporting to NestJS Observe (see
|
|
158
|
-
* `ObserveAgentSharedBuffer.
|
|
159
|
-
* going out with no word would read as a bug in the agent.
|
|
158
|
+
* `ObserveAgentSharedBuffer.isReporting`), and a `traceparent` that stops
|
|
159
|
+
* going out with no word would read as a bug in the agent. `why` is what
|
|
160
|
+
* just happened, which the line gives as the reason.
|
|
160
161
|
*/
|
|
161
162
|
private reportOpenTelemetryInterop;
|
|
162
163
|
/**
|
|
@@ -142,6 +142,10 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
142
142
|
}
|
|
143
143
|
else {
|
|
144
144
|
this.initializeWorker();
|
|
145
|
+
// Not in `initializeWorker`, which restarts a stopped worker too: that
|
|
146
|
+
// one has to get a batch in first.
|
|
147
|
+
this.observeAgentSharedBuffer.recordStarted();
|
|
148
|
+
this.reportOpenTelemetryInterop("started");
|
|
145
149
|
}
|
|
146
150
|
if (this.options.openTelemetry === true && this.sendsNothing) {
|
|
147
151
|
this.logger.warn("openTelemetry is on, but this agent reports nothing to NestJS Observe, so OpenTelemetry interop stays off: " +
|
|
@@ -554,7 +558,7 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
554
558
|
const mode = parseDegradedMessage(msg);
|
|
555
559
|
if (mode !== null) {
|
|
556
560
|
this.observeAgentSharedBuffer.recordAccepted();
|
|
557
|
-
this.reportOpenTelemetryInterop();
|
|
561
|
+
this.reportOpenTelemetryInterop("accepted");
|
|
558
562
|
this.reportFirstAcceptedBatch();
|
|
559
563
|
this.applyIngestMode(mode, parseOveragePauseReason(msg));
|
|
560
564
|
return;
|
|
@@ -575,22 +579,26 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
575
579
|
/**
|
|
576
580
|
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
577
581
|
* it runs only beside reporting to NestJS Observe (see
|
|
578
|
-
* `ObserveAgentSharedBuffer.
|
|
579
|
-
* going out with no word would read as a bug in the agent.
|
|
582
|
+
* `ObserveAgentSharedBuffer.isReporting`), and a `traceparent` that stops
|
|
583
|
+
* going out with no word would read as a bug in the agent. `why` is what
|
|
584
|
+
* just happened, which the line gives as the reason.
|
|
580
585
|
*/
|
|
581
586
|
reportOpenTelemetryInterop(why) {
|
|
582
587
|
if (this.options.openTelemetry !== true) {
|
|
583
588
|
return;
|
|
584
589
|
}
|
|
585
|
-
const on = this.observeAgentSharedBuffer.
|
|
590
|
+
const on = this.observeAgentSharedBuffer.isReporting();
|
|
586
591
|
if (on === this.reportedOpenTelemetry) {
|
|
587
592
|
return;
|
|
588
593
|
}
|
|
589
594
|
this.reportedOpenTelemetry = on;
|
|
590
595
|
if (on) {
|
|
591
|
-
this.logger.log(
|
|
592
|
-
"
|
|
593
|
-
|
|
596
|
+
this.logger.log(why === "started"
|
|
597
|
+
? "OpenTelemetry interop is on: traceparent is read and sent beside reporting to NestJS Observe. " +
|
|
598
|
+
"It stays on through collector outages and rate limits, and pauses only if the collector refuses the " +
|
|
599
|
+
"app's credentials or the telemetry worker stops."
|
|
600
|
+
: "OpenTelemetry interop is on again: the collector accepted this app's telemetry, so traceparent is read " +
|
|
601
|
+
"and sent.");
|
|
594
602
|
return;
|
|
595
603
|
}
|
|
596
604
|
const reason = why === "refused"
|
|
@@ -572,11 +572,43 @@ function declaresPrivateMembers(ctor) {
|
|
|
572
572
|
/**
|
|
573
573
|
* The class name that labels an instance's spans. A null-prototype object or
|
|
574
574
|
* an ES module namespace has no `constructor`, and some objects hold a
|
|
575
|
-
* non-function there. Both fall back to `Object`.
|
|
575
|
+
* non-function there. Both fall back to `Object`. An `ioredis` client is
|
|
576
|
+
* named for what it is - see `ioredisClassNameOf`.
|
|
576
577
|
*/
|
|
577
578
|
function classNameOf(instance) {
|
|
578
579
|
const ctor = Reflect.get(instance, "constructor");
|
|
579
|
-
|
|
580
|
+
if (typeof ctor !== "function") {
|
|
581
|
+
return FALLBACK_CLASS_NAME;
|
|
582
|
+
}
|
|
583
|
+
return ((ctor.name === "EventEmitter" && ioredisClassNameOf(instance)) || ctor.name);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* `Redis` or `Cluster` for an `ioredis` client, null for anything else.
|
|
587
|
+
*
|
|
588
|
+
* Both classes take `EventEmitter`'s methods by copying the own properties of
|
|
589
|
+
* its prototype onto theirs, `constructor` included, so a client's
|
|
590
|
+
* `constructor` is `EventEmitter` and every command would be labelled
|
|
591
|
+
* `EventEmitter.get`. Both extend ioredis's `Commander`, whose prototype keeps
|
|
592
|
+
* its own `constructor`, and a client has `sendCommand`.
|
|
593
|
+
*
|
|
594
|
+
* Matched by name and shape only, as `isMongoDriverHandle` is: ioredis is an
|
|
595
|
+
* application's dependency, never imported here, and an application may carry
|
|
596
|
+
* more than one copy of it - BullMQ and Bull bring their own.
|
|
597
|
+
*/
|
|
598
|
+
function ioredisClassNameOf(instance) {
|
|
599
|
+
try {
|
|
600
|
+
const candidate = instance;
|
|
601
|
+
const base = Object.getPrototypeOf(Object.getPrototypeOf(instance));
|
|
602
|
+
if (base?.constructor?.name !== "Commander" ||
|
|
603
|
+
typeof candidate.sendCommand !== "function") {
|
|
604
|
+
return null;
|
|
605
|
+
}
|
|
606
|
+
return candidate.isCluster === true ? "Cluster" : "Redis";
|
|
607
|
+
}
|
|
608
|
+
catch {
|
|
609
|
+
// A prototype or a property that cannot be read names nothing.
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
580
612
|
}
|
|
581
613
|
function generateSpanId(instance, methodKey) {
|
|
582
614
|
return `${classNameOf(instance)}#${methodKey}`;
|
|
@@ -663,22 +663,26 @@ export interface ObserveOptions {
|
|
|
663
663
|
* reported under the trace of the server span the SDK opened for it, the
|
|
664
664
|
* request that starts a trace included, and `traceparent` on outbound
|
|
665
665
|
* calls is left to the SDK.
|
|
666
|
-
* - A BullMQ or Bull job, and a message over
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
666
|
+
* - A BullMQ or Bull job, and a message over a built-in transport - TCP and
|
|
667
|
+
* Redis on `@nestjs/microservices` 12.0.4 and later, NATS, Kafka,
|
|
668
|
+
* RabbitMQ, MQTT 5 and gRPC on 12.3.3 and later - carry a W3C
|
|
669
|
+
* `traceparent` beside their trace id, for whatever runs them to
|
|
670
|
+
* continue: an OpenTelemetry SDK in the Nest process that runs one does,
|
|
671
|
+
* in the span an instrumentation such as `bullmq-otel` opens for the
|
|
672
|
+
* work, else in the trace handed on - so its spans there, and the calls
|
|
673
|
+
* they make, stay in the trace that handed the work on.
|
|
670
674
|
*
|
|
671
675
|
* A `traceIdGenerator` passed to `createObserveModule()` still names the
|
|
672
676
|
* trace; the W3C one is carried on beside it.
|
|
673
677
|
*
|
|
674
|
-
* It works only beside reporting to NestJS Observe
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* the log - only while the collector refuses the
|
|
679
|
-
* agent's worker is down, and resumes with the
|
|
680
|
-
* accepts. With `enabled: false`, or without
|
|
681
|
-
* collector, it never turns on.
|
|
678
|
+
* It works only beside reporting to NestJS Observe. It is on from
|
|
679
|
+
* start-up, and stays on through collector outages and rate limits, so
|
|
680
|
+
* traces through the services around this one stay whole from an
|
|
681
|
+
* instance's first request, and while NestJS Observe is down. It pauses -
|
|
682
|
+
* each time with a line in the log - only while the collector refuses the
|
|
683
|
+
* app's credentials or the agent's worker is down, and resumes with the
|
|
684
|
+
* next batch the collector accepts. With `enabled: false`, or without
|
|
685
|
+
* credentials for the hosted collector, it never turns on.
|
|
682
686
|
* @default false
|
|
683
687
|
*/
|
|
684
688
|
openTelemetry?: boolean;
|
|
@@ -29,9 +29,8 @@ export interface OutgoingHttpOptions {
|
|
|
29
29
|
* cover, and a request takes only one.
|
|
30
30
|
*
|
|
31
31
|
* Only with `openTelemetry` on, and only beside reporting to NestJS
|
|
32
|
-
* Observe - from
|
|
33
|
-
*
|
|
34
|
-
* sent.
|
|
32
|
+
* Observe - from start-up, through outages, until the collector refuses
|
|
33
|
+
* the app's credentials; otherwise no `traceparent` is sent.
|
|
35
34
|
* @default true, with `openTelemetry` on
|
|
36
35
|
*/
|
|
37
36
|
traceContext?: boolean;
|
|
@@ -64,6 +63,6 @@ getRedactor?: () => LogRedactor | null,
|
|
|
64
63
|
/**
|
|
65
64
|
* Whether calls carry W3C Trace Context now: with `openTelemetry` on and
|
|
66
65
|
* `traceContext` not off, while the agent reports to NestJS Observe (see
|
|
67
|
-
* `ObserveAgentSharedBuffer.
|
|
66
|
+
* `ObserveAgentSharedBuffer.isReporting`). Off unless given.
|
|
68
67
|
*/
|
|
69
68
|
traceContextOn?: () => boolean): () => void;
|
|
@@ -28,7 +28,7 @@ getRedactor = () => null,
|
|
|
28
28
|
/**
|
|
29
29
|
* Whether calls carry W3C Trace Context now: with `openTelemetry` on and
|
|
30
30
|
* `traceContext` not off, while the agent reports to NestJS Observe (see
|
|
31
|
-
* `ObserveAgentSharedBuffer.
|
|
31
|
+
* `ObserveAgentSharedBuffer.isReporting`). Off unless given.
|
|
32
32
|
*/
|
|
33
33
|
traceContextOn = () => false) {
|
|
34
34
|
const spans = new WeakMap();
|
|
@@ -127,10 +127,10 @@ let OutgoingObserveAgentService = OutgoingObserveAgentService_1 = class Outgoing
|
|
|
127
127
|
}, http, () => operationTraceRegistry.getRedactor(),
|
|
128
128
|
// W3C Trace Context goes out only with `openTelemetry` on, and only
|
|
129
129
|
// while the agent reports to NestJS Observe - see
|
|
130
|
-
// `ObserveAgentSharedBuffer.
|
|
130
|
+
// `ObserveAgentSharedBuffer.isReporting`.
|
|
131
131
|
() => this.options.openTelemetry === true &&
|
|
132
132
|
http.traceContext !== false &&
|
|
133
|
-
this.observeAgentSharedBuffer?.
|
|
133
|
+
this.observeAgentSharedBuffer?.isReporting() === true);
|
|
134
134
|
// Model calls are read off the spans above, so they go with them.
|
|
135
135
|
if (outgoing?.llm !== false) {
|
|
136
136
|
this.deactivateLlm = activateLlmFetchObserver(recorder, typeof outgoing?.llm === "object" ? outgoing.llm : {});
|
|
@@ -110,10 +110,10 @@ let HttpObserveAgentService = HttpObserveAgentService_1 = class HttpObserveAgent
|
|
|
110
110
|
const store = new Map();
|
|
111
111
|
this.asyncLocalStorage.run(store, () => {
|
|
112
112
|
// OpenTelemetry interop runs only beside reporting to NestJS Observe -
|
|
113
|
-
// see `ObserveAgentSharedBuffer.
|
|
113
|
+
// see `ObserveAgentSharedBuffer.isReporting`. Asked once per request,
|
|
114
114
|
// so a request is named and carried on one way throughout.
|
|
115
115
|
const openTelemetry = this.options.openTelemetry === true &&
|
|
116
|
-
this.observeAgentSharedBuffer.
|
|
116
|
+
this.observeAgentSharedBuffer.isReporting();
|
|
117
117
|
const traceId = generateTraceIdSafely(openTelemetry && usesDefaultTraceIdGenerator(this.options)
|
|
118
118
|
? openTelemetryTraceIdGenerator
|
|
119
119
|
: this.options.traceIdGenerator, req, this.options, this.logger);
|
|
@@ -61,7 +61,7 @@ export declare class JobTraceRunner<Store extends Record<string, unknown>> {
|
|
|
61
61
|
private stampTraceId;
|
|
62
62
|
/**
|
|
63
63
|
* Whether OpenTelemetry interop runs now: `openTelemetry` on, beside
|
|
64
|
-
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.
|
|
64
|
+
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.isReporting`.
|
|
65
65
|
* Asked as a job is enqueued and again as it starts, by each side for
|
|
66
66
|
* itself.
|
|
67
67
|
*/
|
|
@@ -122,13 +122,13 @@ export class JobTraceRunner {
|
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
124
124
|
* Whether OpenTelemetry interop runs now: `openTelemetry` on, beside
|
|
125
|
-
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.
|
|
125
|
+
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.isReporting`.
|
|
126
126
|
* Asked as a job is enqueued and again as it starts, by each side for
|
|
127
127
|
* itself.
|
|
128
128
|
*/
|
|
129
129
|
openTelemetryInteropOn() {
|
|
130
130
|
return (this.options.openTelemetry === true &&
|
|
131
|
-
this.observeAgentSharedBuffer.
|
|
131
|
+
this.observeAgentSharedBuffer.isReporting());
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* The id stamped at enqueue time, if it is one this agent would have minted
|
|
@@ -50,7 +50,7 @@ export declare class RpcObserveAgentService<Store extends Record<string, unknown
|
|
|
50
50
|
startRpcRequestTracing(transportId: Transport | symbol, ctx: BaseRpcContext, done: () => Promise<any>): Promise<unknown>;
|
|
51
51
|
/**
|
|
52
52
|
* Whether OpenTelemetry interop runs now: `openTelemetry` on, beside
|
|
53
|
-
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.
|
|
53
|
+
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.isReporting`.
|
|
54
54
|
*/
|
|
55
55
|
private openTelemetryInteropOn;
|
|
56
56
|
startGrpcRequestTracing(transportId: Transport | symbol, call: GrpcCall, done: () => Promise<any>): Promise<unknown>;
|
|
@@ -150,11 +150,11 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
152
152
|
* Whether OpenTelemetry interop runs now: `openTelemetry` on, beside
|
|
153
|
-
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.
|
|
153
|
+
* reporting to NestJS Observe - see `ObserveAgentSharedBuffer.isReporting`.
|
|
154
154
|
*/
|
|
155
155
|
openTelemetryInteropOn() {
|
|
156
156
|
return (this.options.openTelemetry === true &&
|
|
157
|
-
this.observeAgentSharedBuffer.
|
|
157
|
+
this.observeAgentSharedBuffer.isReporting());
|
|
158
158
|
}
|
|
159
159
|
startGrpcRequestTracing(transportId, call, done) {
|
|
160
160
|
// As above: the map `run` is given, not looked back up.
|
|
@@ -24,11 +24,17 @@ export declare function traceparentToHandOn(store: ReadonlyMap<unknown, unknown>
|
|
|
24
24
|
* packet metadata are writable by anything that reaches Redis or the
|
|
25
25
|
* socket, and a span from another trace would put the work under one it
|
|
26
26
|
* never ran beside. Anything else runs `run` as it is.
|
|
27
|
+
*
|
|
28
|
+
* So does work an OpenTelemetry instrumentation has already taken up, with
|
|
29
|
+
* a span of that trace open for it - BullMQ's `bullmq-otel` opens one for
|
|
30
|
+
* each job, and OpenTelemetry's Kafka and RabbitMQ instrumentations one for
|
|
31
|
+
* each message they deliver. The work's spans belong under that span, which
|
|
32
|
+
* the handed-on parent would put them beside.
|
|
27
33
|
*/
|
|
28
34
|
export declare function continueHandedOnTrace<T>(traceparent: unknown, traceId: string, run: () => T): T;
|
|
29
35
|
/**
|
|
30
36
|
* Whether OpenTelemetry interop runs now - `openTelemetry` on, beside
|
|
31
|
-
* reporting to NestJS Observe (see `ObserveAgentSharedBuffer.
|
|
37
|
+
* reporting to NestJS Observe (see `ObserveAgentSharedBuffer.isReporting`) -
|
|
32
38
|
* for what is set up before the module's options resolve: the microservice
|
|
33
39
|
* dispatch hook, which a client is handed as it is instantiated. Off until
|
|
34
40
|
* the module configures it.
|
|
@@ -39,7 +45,7 @@ export declare class OpenTelemetryInteropGate {
|
|
|
39
45
|
configure(options: {
|
|
40
46
|
openTelemetry?: boolean;
|
|
41
47
|
} | undefined, buffer: {
|
|
42
|
-
|
|
48
|
+
isReporting(): boolean;
|
|
43
49
|
} | undefined): this;
|
|
44
50
|
isOn(): boolean;
|
|
45
51
|
}
|
|
@@ -54,12 +54,20 @@ export function traceparentToHandOn(store, traceId) {
|
|
|
54
54
|
* packet metadata are writable by anything that reaches Redis or the
|
|
55
55
|
* socket, and a span from another trace would put the work under one it
|
|
56
56
|
* never ran beside. Anything else runs `run` as it is.
|
|
57
|
+
*
|
|
58
|
+
* So does work an OpenTelemetry instrumentation has already taken up, with
|
|
59
|
+
* a span of that trace open for it - BullMQ's `bullmq-otel` opens one for
|
|
60
|
+
* each job, and OpenTelemetry's Kafka and RabbitMQ instrumentations one for
|
|
61
|
+
* each message they deliver. The work's spans belong under that span, which
|
|
62
|
+
* the handed-on parent would put them beside.
|
|
57
63
|
*/
|
|
58
64
|
export function continueHandedOnTrace(traceparent, traceId, run) {
|
|
59
65
|
const parent = typeof traceparent === "string"
|
|
60
66
|
? readTraceContext({ traceparent })
|
|
61
67
|
: undefined;
|
|
62
|
-
if (!parent ||
|
|
68
|
+
if (!parent ||
|
|
69
|
+
parent.traceId !== w3cTraceIdOf(traceId) ||
|
|
70
|
+
activeOpenTelemetrySpanContext()?.traceId === parent.traceId) {
|
|
63
71
|
return run();
|
|
64
72
|
}
|
|
65
73
|
return withRemoteOpenTelemetryParent({
|
|
@@ -70,7 +78,7 @@ export function continueHandedOnTrace(traceparent, traceId, run) {
|
|
|
70
78
|
}
|
|
71
79
|
/**
|
|
72
80
|
* Whether OpenTelemetry interop runs now - `openTelemetry` on, beside
|
|
73
|
-
* reporting to NestJS Observe (see `ObserveAgentSharedBuffer.
|
|
81
|
+
* reporting to NestJS Observe (see `ObserveAgentSharedBuffer.isReporting`) -
|
|
74
82
|
* for what is set up before the module's options resolve: the microservice
|
|
75
83
|
* dispatch hook, which a client is handed as it is instantiated. Off until
|
|
76
84
|
* the module configures it.
|
|
@@ -85,6 +93,6 @@ export class OpenTelemetryInteropGate {
|
|
|
85
93
|
}
|
|
86
94
|
isOn() {
|
|
87
95
|
return (this.options?.openTelemetry === true &&
|
|
88
|
-
this.buffer?.
|
|
96
|
+
this.buffer?.isReporting() === true);
|
|
89
97
|
}
|
|
90
98
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.24",
|
|
4
4
|
"description": "Observability and APM agent for NestJS - traces, runtime metrics and custom metrics",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"homepage": "https://observe.nestjs.com",
|
|
@@ -165,6 +165,7 @@
|
|
|
165
165
|
"@vitest/coverage-v8": "4.1.11",
|
|
166
166
|
"bull": "4.16.5",
|
|
167
167
|
"bullmq": "6.2.0",
|
|
168
|
+
"bullmq-otel": "2.0.1",
|
|
168
169
|
"dd-trace": "6.23.0",
|
|
169
170
|
"drizzle-orm": "0.45.4",
|
|
170
171
|
"express": "5.2.1",
|