@nestjs/observe 0.3.21 → 0.3.23
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 +100 -14
- package/dist/agent/degraded-ingest.protocol.d.ts +14 -0
- package/dist/agent/degraded-ingest.protocol.js +19 -0
- package/dist/agent/detached-observe-worker.assembly.d.ts +2 -0
- package/dist/agent/detached-observe-worker.assembly.js +5 -1
- package/dist/agent/detached-observe-worker.d.ts +10 -0
- package/dist/agent/detached-observe-worker.js +27 -2
- package/dist/agent/observe-agent.shared-buffer.d.ts +63 -1
- package/dist/agent/observe-agent.shared-buffer.js +117 -2
- package/dist/agent/observe-agent.worker.d.ts +27 -20
- package/dist/agent/observe-agent.worker.js +109 -62
- package/dist/agent/observe-reporting.util.d.ts +35 -0
- package/dist/agent/observe-reporting.util.js +69 -0
- package/dist/interfaces/observe-options.interface.d.ts +100 -8
- package/dist/observe.constants.d.ts +17 -0
- package/dist/observe.constants.js +17 -0
- package/dist/observe.module.js +53 -4
- package/dist/otlp/otlp-encoders.d.ts +26 -0
- package/dist/otlp/otlp-encoders.js +296 -0
- package/dist/otlp/otlp-export-grant.d.ts +34 -0
- package/dist/otlp/otlp-export-grant.js +22 -0
- package/dist/otlp/otlp-options.interface.d.ts +59 -0
- package/dist/otlp/otlp-options.interface.js +1 -0
- package/dist/otlp/otlp-settings.util.d.ts +38 -0
- package/dist/otlp/otlp-settings.util.js +154 -0
- package/dist/otlp/otlp-span.mapper.d.ts +73 -0
- package/dist/otlp/otlp-span.mapper.js +345 -0
- package/dist/otlp/otlp-trace-exporter.service.d.ts +130 -0
- package/dist/otlp/otlp-trace-exporter.service.js +497 -0
- package/dist/outgoing/http.integration.d.ts +36 -3
- package/dist/outgoing/http.integration.js +84 -17
- package/dist/outgoing/outgoing-observe-agent.service.d.ts +14 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +24 -9
- package/dist/outgoing/outgoing-span.recorder.d.ts +11 -1
- package/dist/outgoing/outgoing-span.recorder.js +8 -2
- package/dist/outgoing/sql-statement.util.js +2 -2
- package/dist/protocols/http-observe-agent.service.d.ts +1 -8
- package/dist/protocols/http-observe-agent.service.js +44 -14
- package/dist/protocols/job-trace-runner.d.ts +11 -0
- package/dist/protocols/job-trace-runner.js +42 -8
- package/dist/protocols/rpc-observe-agent.service.d.ts +5 -0
- package/dist/protocols/rpc-observe-agent.service.js +43 -3
- package/dist/services/operation-trace.registry.d.ts +46 -3
- package/dist/services/operation-trace.registry.js +69 -13
- package/dist/services/tracer.service.d.ts +6 -0
- package/dist/services/tracer.service.js +11 -2
- package/dist/utils/default-trace-id-generator.util.d.ts +18 -0
- package/dist/utils/default-trace-id-generator.util.js +39 -2
- package/dist/utils/opentelemetry.util.d.ts +57 -0
- package/dist/utils/opentelemetry.util.js +100 -0
- package/dist/utils/trace-context.util.d.ts +91 -0
- package/dist/utils/trace-context.util.js +219 -0
- package/dist/utils/trace-handoff.util.d.ts +45 -0
- package/dist/utils/trace-handoff.util.js +90 -0
- package/dist/utils/trace-id-generator-origin.util.d.ts +11 -0
- package/dist/utils/trace-id-generator-origin.util.js +26 -0
- package/package.json +26 -13
|
@@ -21,6 +21,7 @@ import { JobSnapshotEncoder, } from "../encoders/job-snapshot.encoder.js";
|
|
|
21
21
|
import { RequestSnapshotEncoder, } from "../encoders/request-snapshot.encoder.js";
|
|
22
22
|
import { RuntimeMetricsEncoder, } from "../encoders/runtime-metrics.encoder.js";
|
|
23
23
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
24
|
+
import { OtlpTraceExporter } from "../otlp/otlp-trace-exporter.service.js";
|
|
24
25
|
import { fallbackOperationId } from "../utils/fallback-operation-id.util.js";
|
|
25
26
|
const SHARED_BUFFER_SIZE = 1024 * 1024 * 16; // 16 MB
|
|
26
27
|
const DEFAULT_MAX_SNAPSHOTS_PER_TRANSACTION = 1000;
|
|
@@ -61,6 +62,7 @@ const MAX_OBJECTIVES_PER_BATCH = 100;
|
|
|
61
62
|
let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSharedBuffer {
|
|
62
63
|
options;
|
|
63
64
|
objectivesRegistry;
|
|
65
|
+
otlpTraceExporter;
|
|
64
66
|
_sharedBuffer = new SharedArrayBuffer(SHARED_BUFFER_SIZE);
|
|
65
67
|
lock = new Int32Array(this._sharedBuffer, 0, 1);
|
|
66
68
|
sharedBytes = new Uint8Array(this._sharedBuffer);
|
|
@@ -82,9 +84,10 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
82
84
|
get sharedBuffer() {
|
|
83
85
|
return this._sharedBuffer;
|
|
84
86
|
}
|
|
85
|
-
constructor(options, objectivesRegistry) {
|
|
87
|
+
constructor(options, objectivesRegistry, otlpTraceExporter) {
|
|
86
88
|
this.options = options;
|
|
87
89
|
this.objectivesRegistry = objectivesRegistry;
|
|
90
|
+
this.otlpTraceExporter = otlpTraceExporter;
|
|
88
91
|
}
|
|
89
92
|
insertRequestSnapshot(snapshot) {
|
|
90
93
|
// A request can end before its route metadata is ever captured - an auth
|
|
@@ -96,6 +99,15 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
96
99
|
// ride in. The URL is folded to a route-like path first, or every query
|
|
97
100
|
// string and id would be a route of its own; `originalUrl` keeps it whole.
|
|
98
101
|
snapshot.operationId ??= fallbackOperationId(snapshot.attributes?.originalUrl);
|
|
102
|
+
// A full grant exports ahead of everything below that can leave the
|
|
103
|
+
// snapshot out: the collector's limits and its answers about this
|
|
104
|
+
// account decide what reaches Observe, not what an OTLP receiver of the
|
|
105
|
+
// application's own is sent. The raw snapshot, which the encoder below
|
|
106
|
+
// only copies. A capped grant exports what the batch keeps, further down.
|
|
107
|
+
const otlpExport = this.otlpExportGrant();
|
|
108
|
+
if (otlpExport === "full") {
|
|
109
|
+
this.otlpTraceExporter?.exportRequestSnapshot(snapshot);
|
|
110
|
+
}
|
|
99
111
|
if (!snapshot.operationId) {
|
|
100
112
|
if (this.options.debug) {
|
|
101
113
|
this.logger.debug(`Snapshot for traceId "${snapshot.traceId}" has no operation id nor URL to fall back on. Ignoring snapshot.`);
|
|
@@ -121,12 +133,17 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
121
133
|
// further down - they are not telemetry.
|
|
122
134
|
if (this.sendsWhileErrorsOnly(encodedSnapshot)) {
|
|
123
135
|
this._mainThreadBuffer.snapshots.push(encodedSnapshot);
|
|
136
|
+
if (otlpExport === "capped") {
|
|
137
|
+
// Sent, and never with its tree.
|
|
138
|
+
this.otlpTraceExporter?.exportRequestSnapshot(snapshot, false);
|
|
139
|
+
}
|
|
124
140
|
}
|
|
125
141
|
else {
|
|
126
142
|
this.withheldSnapshots += 1;
|
|
127
143
|
}
|
|
128
144
|
}
|
|
129
145
|
else {
|
|
146
|
+
let keepsTree = true;
|
|
130
147
|
if (this.isDegraded() && !isNotableSnapshot(encodedSnapshot)) {
|
|
131
148
|
// The collector is discarding these on arrival; shipping them costs
|
|
132
149
|
// the application its own CPU and bandwidth for nothing. The snapshot
|
|
@@ -135,8 +152,12 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
135
152
|
// count goes with it, because it is the only record these existed.
|
|
136
153
|
this.countTruncated(encodedSnapshot.t);
|
|
137
154
|
encodedSnapshot.t = [];
|
|
155
|
+
keepsTree = false;
|
|
138
156
|
}
|
|
139
157
|
this._mainThreadBuffer.snapshots.push(encodedSnapshot);
|
|
158
|
+
if (otlpExport === "capped") {
|
|
159
|
+
this.otlpTraceExporter?.exportRequestSnapshot(snapshot, keepsTree);
|
|
160
|
+
}
|
|
140
161
|
}
|
|
141
162
|
// Read off the raw snapshot: the handler's span carries `Class.method`,
|
|
142
163
|
// which is what `@Objective` declarations are keyed by. Declarations are
|
|
@@ -155,6 +176,12 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
155
176
|
}
|
|
156
177
|
}
|
|
157
178
|
insertJobSnapshot(jobSnapshot) {
|
|
179
|
+
// A full grant exports before the cap below, as for a request; a capped
|
|
180
|
+
// one what the batch keeps.
|
|
181
|
+
const otlpExport = this.otlpExportGrant();
|
|
182
|
+
if (otlpExport === "full") {
|
|
183
|
+
this.otlpTraceExporter?.exportJobSnapshot(jobSnapshot);
|
|
184
|
+
}
|
|
158
185
|
if (!this._mainThreadBuffer) {
|
|
159
186
|
this._mainThreadBuffer = this.createEmptyPayload();
|
|
160
187
|
}
|
|
@@ -178,20 +205,28 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
178
205
|
// makes it a server error - which is what the collector keeps of it too.
|
|
179
206
|
if (this.sendsWhileErrorsOnly(encodedJobSnapshot)) {
|
|
180
207
|
this._mainThreadBuffer.jobs.push(encodedJobSnapshot);
|
|
208
|
+
if (otlpExport === "capped") {
|
|
209
|
+
this.otlpTraceExporter?.exportJobSnapshot(jobSnapshot, false);
|
|
210
|
+
}
|
|
181
211
|
}
|
|
182
212
|
else {
|
|
183
213
|
this.withheldJobs += 1;
|
|
184
214
|
}
|
|
185
215
|
}
|
|
186
216
|
else {
|
|
217
|
+
let keepsTree = true;
|
|
187
218
|
if (this.isDegraded() && !isNotableSnapshot(encodedJobSnapshot)) {
|
|
188
219
|
// A job carries no status code, so the same rule grades it on its
|
|
189
220
|
// error and its duration - which is what the collector does with it
|
|
190
221
|
// too.
|
|
191
222
|
this.countTruncated(encodedJobSnapshot.t);
|
|
192
223
|
encodedJobSnapshot.t = [];
|
|
224
|
+
keepsTree = false;
|
|
193
225
|
}
|
|
194
226
|
this._mainThreadBuffer.jobs.push(encodedJobSnapshot);
|
|
227
|
+
if (otlpExport === "capped") {
|
|
228
|
+
this.otlpTraceExporter?.exportJobSnapshot(jobSnapshot, keepsTree);
|
|
229
|
+
}
|
|
195
230
|
}
|
|
196
231
|
}
|
|
197
232
|
/**
|
|
@@ -209,6 +244,12 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
209
244
|
* not a moment longer.
|
|
210
245
|
*/
|
|
211
246
|
errorsOnly = false;
|
|
247
|
+
/**
|
|
248
|
+
* Whether this app reports to NestJS Observe - see `isIngesting`. False
|
|
249
|
+
* until the collector accepts a first batch: what has not been shown to
|
|
250
|
+
* reach NestJS Observe does not count as reaching it.
|
|
251
|
+
*/
|
|
252
|
+
ingesting = false;
|
|
212
253
|
/**
|
|
213
254
|
* Requests and jobs that errors-only ingest withheld from the batch being
|
|
214
255
|
* built, cleared with it.
|
|
@@ -247,6 +288,78 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
247
288
|
isErrorsOnly() {
|
|
248
289
|
return this.errorsOnly && this.isDegraded();
|
|
249
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Whether this app reports to NestJS Observe: from the first batch the
|
|
293
|
+
* collector accepts, until the collector refuses the app's credentials or
|
|
294
|
+
* the worker stops - and again from the next batch it accepts.
|
|
295
|
+
*
|
|
296
|
+
* A batch that fails to get in for any other reason - a collector that is
|
|
297
|
+
* down or slow to answer, a 5xx, a 429 pause, however long - leaves it as
|
|
298
|
+
* it is: that is an outage at NestJS Observe, and the traces running through
|
|
299
|
+
* the services around this one must not break over it. Refused credentials
|
|
300
|
+
* are a verdict on this app, and a stopped worker sends nothing until a new
|
|
301
|
+
* one gets a batch in.
|
|
302
|
+
*
|
|
303
|
+
* OpenTelemetry interop runs on it (see `openTelemetry`): the agent reads
|
|
304
|
+
* and sends `traceparent`, and follows an OpenTelemetry SDK in the process,
|
|
305
|
+
* only beside reporting to NestJS Observe. So an agent that sends Observe
|
|
306
|
+
* nothing - `enabled: false`, no credentials, credentials the collector
|
|
307
|
+
* refuses - never turns it on.
|
|
308
|
+
*/
|
|
309
|
+
isIngesting() {
|
|
310
|
+
return this.ingesting;
|
|
311
|
+
}
|
|
312
|
+
/** The collector accepted a batch, whatever it then kept of it. */
|
|
313
|
+
recordAccepted() {
|
|
314
|
+
this.ingesting = true;
|
|
315
|
+
}
|
|
316
|
+
/** The collector refused the app's credentials. */
|
|
317
|
+
recordRefused() {
|
|
318
|
+
this.ingesting = false;
|
|
319
|
+
}
|
|
320
|
+
/** The worker stopped; a restarted one has to get a batch in again. */
|
|
321
|
+
recordStopped() {
|
|
322
|
+
this.ingesting = false;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* What the collector's last accepted reply granted `otlp` - see
|
|
326
|
+
* `OtlpExportGrant`. Nothing until one says otherwise: what this agent may
|
|
327
|
+
* export is the collector's to say, for the plan the app's account is on.
|
|
328
|
+
*/
|
|
329
|
+
otlpExport = "off";
|
|
330
|
+
/**
|
|
331
|
+
* Until when a `capped` grant holds (epoch ms), refreshed by every reply
|
|
332
|
+
* that repeats it.
|
|
333
|
+
*
|
|
334
|
+
* A capped export is what NestJS Observe keeps, and the agent learns what
|
|
335
|
+
* that is only from replies. With none coming - an account the collector
|
|
336
|
+
* refuses at its limit, an outage - it would fall back on exporting every
|
|
337
|
+
* span once `isDegraded` lapsed. So the grant lapses first: it is set just
|
|
338
|
+
* before the verdict for the same reply, on the same TTL. A `full` grant
|
|
339
|
+
* holds until a reply says otherwise, and export then runs for as long as
|
|
340
|
+
* `isIngesting` does.
|
|
341
|
+
*/
|
|
342
|
+
otlpExportCappedUntil = 0;
|
|
343
|
+
/** Records what the collector's last accepted reply granted `otlp`. */
|
|
344
|
+
setOtlpExport(grant, now = Date.now()) {
|
|
345
|
+
this.otlpExport = grant;
|
|
346
|
+
this.otlpExportCappedUntil = grant === "capped" ? now + DEGRADED_TTL_MS : 0;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* What `otlp` may export right now: the collector's grant, while the
|
|
350
|
+
* collector takes this app's telemetry (`isIngesting`) - export runs beside
|
|
351
|
+
* reporting to NestJS Observe, never instead of it - and `off` otherwise.
|
|
352
|
+
* Asked once per operation, as it is handed over.
|
|
353
|
+
*/
|
|
354
|
+
otlpExportGrant(now = Date.now()) {
|
|
355
|
+
if (!this.otlpTraceExporter?.enabled || !this.isIngesting()) {
|
|
356
|
+
return "off";
|
|
357
|
+
}
|
|
358
|
+
if (this.otlpExport === "capped" && now >= this.otlpExportCappedUntil) {
|
|
359
|
+
return "off";
|
|
360
|
+
}
|
|
361
|
+
return this.otlpExport;
|
|
362
|
+
}
|
|
250
363
|
/**
|
|
251
364
|
* What errors-only ingest makes of one execution: whether it is sent.
|
|
252
365
|
*
|
|
@@ -538,6 +651,8 @@ ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = __decorate([
|
|
|
538
651
|
Injectable(),
|
|
539
652
|
__param(0, Inject(OBSERVE_OPTIONS)),
|
|
540
653
|
__param(1, Optional()),
|
|
541
|
-
|
|
654
|
+
__param(2, Optional()),
|
|
655
|
+
__metadata("design:paramtypes", [Object, ObjectivesRegistry,
|
|
656
|
+
OtlpTraceExporter])
|
|
542
657
|
], ObserveAgentSharedBuffer);
|
|
543
658
|
export { ObserveAgentSharedBuffer };
|
|
@@ -3,11 +3,13 @@ import { ObserveModuleOptionsWithDefaults } from "../interfaces/observe-options.
|
|
|
3
3
|
import { NodeRuntimeMetricsService } from "../services/node-runtime-metrics.service.js";
|
|
4
4
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
5
5
|
import { ObserveAgentSharedBuffer } from "./observe-agent.shared-buffer.js";
|
|
6
|
+
import { OtlpTraceExporter } from "../otlp/otlp-trace-exporter.service.js";
|
|
6
7
|
export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationShutdown {
|
|
7
8
|
private readonly observeAgentSharedBuffer;
|
|
8
9
|
private readonly options;
|
|
9
10
|
private readonly nodeRuntimeMetricsService;
|
|
10
11
|
private readonly operationTraceRegistry?;
|
|
12
|
+
private readonly otlpTraceExporter?;
|
|
11
13
|
private readonly logger;
|
|
12
14
|
private worker;
|
|
13
15
|
/**
|
|
@@ -33,7 +35,7 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
|
|
|
33
35
|
* flush only.
|
|
34
36
|
*/
|
|
35
37
|
private sendsPausedUntil;
|
|
36
|
-
constructor(observeAgentSharedBuffer: ObserveAgentSharedBuffer, options: ObserveModuleOptionsWithDefaults, nodeRuntimeMetricsService: NodeRuntimeMetricsService, operationTraceRegistry?: OperationTraceRegistry | undefined);
|
|
38
|
+
constructor(observeAgentSharedBuffer: ObserveAgentSharedBuffer, options: ObserveModuleOptionsWithDefaults, nodeRuntimeMetricsService: NodeRuntimeMetricsService, operationTraceRegistry?: OperationTraceRegistry | undefined, otlpTraceExporter?: OtlpTraceExporter | undefined);
|
|
37
39
|
onModuleInit(): void;
|
|
38
40
|
/**
|
|
39
41
|
* Starts the runtime-metrics collector: memory, CPU, GC, event loop.
|
|
@@ -122,25 +124,6 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
|
|
|
122
124
|
* The runtime-metrics window between the last scheduled sample and now.
|
|
123
125
|
*/
|
|
124
126
|
private addFinalRuntimeMetrics;
|
|
125
|
-
private get hasScaffoldCredentials();
|
|
126
|
-
/**
|
|
127
|
-
* Whether credentials are missing and the collector is the hosted one,
|
|
128
|
-
* which answers every batch without them with a 401.
|
|
129
|
-
*
|
|
130
|
-
* The worker leaves both headers off when either half is unset, which a
|
|
131
|
-
* collector in placeholder mode accepts, so a collector of the user's own
|
|
132
|
-
* still gets those batches. The hosted collector refuses them for as long
|
|
133
|
-
* as the process runs: on 2026-10-15 that was 42% of the 1.87M batches it
|
|
134
|
-
* refused, from processes deployed without their environment variables,
|
|
135
|
-
* some of them posting every five seconds for weeks.
|
|
136
|
-
*/
|
|
137
|
-
private get lacksHostedCredentials();
|
|
138
|
-
/**
|
|
139
|
-
* Whether `endpoint` - set, from `OBSERVE_ENDPOINT`, or the default - is the
|
|
140
|
-
* hosted collector. Compared by host, so a trailing slash or a capital
|
|
141
|
-
* letter cannot pass it off as a collector of the user's own.
|
|
142
|
-
*/
|
|
143
|
-
private get isHostedCollector();
|
|
144
127
|
/**
|
|
145
128
|
* Whether `enabled: false` switched the agent off - whatever the
|
|
146
129
|
* credentials, and whichever collector `endpoint` names.
|
|
@@ -164,6 +147,30 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
|
|
|
164
147
|
/** The worker's messages, errors, and the restart when it dies. */
|
|
165
148
|
private watchWorker;
|
|
166
149
|
handleMessage(msg: string): void;
|
|
150
|
+
/**
|
|
151
|
+
* Whether OpenTelemetry interop was last said to be on - see
|
|
152
|
+
* `reportOpenTelemetryInterop`. It starts off, as the buffer's state does.
|
|
153
|
+
*/
|
|
154
|
+
private reportedOpenTelemetry;
|
|
155
|
+
/**
|
|
156
|
+
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
157
|
+
* it runs only beside reporting to NestJS Observe (see
|
|
158
|
+
* `ObserveAgentSharedBuffer.isIngesting`), and a `traceparent` that stops
|
|
159
|
+
* going out with no word would read as a bug in the agent.
|
|
160
|
+
*/
|
|
161
|
+
private reportOpenTelemetryInterop;
|
|
162
|
+
/**
|
|
163
|
+
* The OTLP export grant last said, or null before the first. Kept beside
|
|
164
|
+
* the log so each change is said once: every accepted batch repeats it.
|
|
165
|
+
*/
|
|
166
|
+
private reportedOtlpExport;
|
|
167
|
+
/**
|
|
168
|
+
* Says what the collector granted `otlp` when it changes, the first answer
|
|
169
|
+
* included - an export that stays silent would read as a broken receiver.
|
|
170
|
+
* Only the plan's answer: a pause while the collector is not taking the
|
|
171
|
+
* app's telemetry is said by the interop line above.
|
|
172
|
+
*/
|
|
173
|
+
private reportOtlpExport;
|
|
167
174
|
/** Whether this process has said the collector accepted its first batch. */
|
|
168
175
|
private reportedConnection;
|
|
169
176
|
/**
|
|
@@ -21,22 +21,16 @@ import { ProfileUploader } from "../profiling/profile-uploader.js";
|
|
|
21
21
|
import { setActiveSliceRecorder, SpanSliceRecorder, } from "../profiling/span-slice-recorder.js";
|
|
22
22
|
import { NodeRuntimeMetricsService } from "../services/node-runtime-metrics.service.js";
|
|
23
23
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
24
|
-
import { parseDegradedMessage, parseOveragePauseReason, parsePausedMessage, } from "./degraded-ingest.protocol.js";
|
|
24
|
+
import { parseDegradedMessage, parseNotIngestedMessage, parseOveragePauseReason, parsePausedMessage, } from "./degraded-ingest.protocol.js";
|
|
25
25
|
import { detachedWorkerData, detachedWorkerSource, } from "./detached-observe-worker.assembly.js";
|
|
26
26
|
import { describeMissingCredentials, SIGN_UP_URL, } from "./credentials-notice.js";
|
|
27
27
|
import { ObserveAgentSharedBuffer } from "./observe-agent.shared-buffer.js";
|
|
28
|
+
import { parseOtlpExportMessage, } from "../otlp/otlp-export-grant.js";
|
|
29
|
+
import { OtlpTraceExporter } from "../otlp/otlp-trace-exporter.service.js";
|
|
30
|
+
import { collectorEndpointOf, hasScaffoldCredentials, isHostedCollector, lacksHostedCredentials, sendsToObserve, } from "./observe-reporting.util.js";
|
|
28
31
|
const DEFAULT_RUNTIME_METRICS_INTERVAL = 60000; // 60 seconds
|
|
29
32
|
const MIN_RUNTIME_METRICS_INTERVAL = 30000; // 30 seconds
|
|
30
33
|
const DEFAULT_FLUSH_INTERVAL = 5000; // 5 seconds
|
|
31
|
-
/**
|
|
32
|
-
* The hosted collector. Unlike a collector in placeholder mode, it accepts
|
|
33
|
-
* nothing without credentials.
|
|
34
|
-
*/
|
|
35
|
-
const HOSTED_COLLECTOR_ENDPOINT = "https://observe-api.nestjs.com";
|
|
36
|
-
/**
|
|
37
|
-
* Where signals are posted when `endpoint` is not configured.
|
|
38
|
-
*/
|
|
39
|
-
const DEFAULT_COLLECTOR_ENDPOINT = process.env.OBSERVE_ENDPOINT ?? HOSTED_COLLECTOR_ENDPOINT;
|
|
40
34
|
const MIN_FLUSH_INTERVAL = 1000; // 1 second
|
|
41
35
|
/**
|
|
42
36
|
* How long shutdown waits for buffered telemetry to reach the collector before
|
|
@@ -67,21 +61,13 @@ const FINAL_FLUSH_POLL_MS = 10;
|
|
|
67
61
|
* a pegged CPU and a saturated loop, and mean nothing.
|
|
68
62
|
*/
|
|
69
63
|
const MIN_FINAL_RUNTIME_WINDOW_MS = 10_000;
|
|
70
|
-
/**
|
|
71
|
-
* The credentials `nest new --observe` writes into `AppModule`.
|
|
72
|
-
*
|
|
73
|
-
* A project fresh from the generator has these until someone signs up, so
|
|
74
|
-
* they mean "not connected yet", not "wrong": sending them only buys a 401
|
|
75
|
-
* and an `Error:` line on the very first `npm run start`.
|
|
76
|
-
*/
|
|
77
|
-
const SCAFFOLD_APP_KEY = "YOUR_APP_KEY";
|
|
78
|
-
const SCAFFOLD_APP_SECRET = "YOUR_APP_SECRET";
|
|
79
64
|
const DASHBOARD_URL = `${SIGN_UP_URL}/dashboard`;
|
|
80
65
|
let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
81
66
|
observeAgentSharedBuffer;
|
|
82
67
|
options;
|
|
83
68
|
nodeRuntimeMetricsService;
|
|
84
69
|
operationTraceRegistry;
|
|
70
|
+
otlpTraceExporter;
|
|
85
71
|
logger = new Logger(ObserveAgentWorker_1.name);
|
|
86
72
|
// Null before `initializeWorker` and again after termination, which is what
|
|
87
73
|
// the shutdown path below checks for.
|
|
@@ -109,11 +95,12 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
109
95
|
* flush only.
|
|
110
96
|
*/
|
|
111
97
|
sendsPausedUntil = 0;
|
|
112
|
-
constructor(observeAgentSharedBuffer, options, nodeRuntimeMetricsService, operationTraceRegistry) {
|
|
98
|
+
constructor(observeAgentSharedBuffer, options, nodeRuntimeMetricsService, operationTraceRegistry, otlpTraceExporter) {
|
|
113
99
|
this.observeAgentSharedBuffer = observeAgentSharedBuffer;
|
|
114
100
|
this.options = options;
|
|
115
101
|
this.nodeRuntimeMetricsService = nodeRuntimeMetricsService;
|
|
116
102
|
this.operationTraceRegistry = operationTraceRegistry;
|
|
103
|
+
this.otlpTraceExporter = otlpTraceExporter;
|
|
117
104
|
}
|
|
118
105
|
onModuleInit() {
|
|
119
106
|
// Settled before the worker starts, which backs off from this interval
|
|
@@ -132,12 +119,13 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
132
119
|
// needs no key or secret, so a missing one is not worth a warning.
|
|
133
120
|
this.logger.log("Observe is disabled (enabled: false): nothing is recorded or sent.");
|
|
134
121
|
}
|
|
135
|
-
else if (
|
|
122
|
+
else if (hasScaffoldCredentials(this.options) ||
|
|
123
|
+
lacksHostedCredentials(this.options)) {
|
|
136
124
|
// Nothing is sent, so there is no worker to start. The flush timer below
|
|
137
125
|
// still runs and empties the buffer, which the instrumentation keeps
|
|
138
126
|
// writing into either way.
|
|
139
127
|
const notice = describeMissingCredentials({
|
|
140
|
-
scaffold: this.
|
|
128
|
+
scaffold: hasScaffoldCredentials(this.options),
|
|
141
129
|
unset: !this.options.appKey && !this.options.appSecret
|
|
142
130
|
? "both"
|
|
143
131
|
: !this.options.appKey
|
|
@@ -155,6 +143,10 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
155
143
|
else {
|
|
156
144
|
this.initializeWorker();
|
|
157
145
|
}
|
|
146
|
+
if (this.options.openTelemetry === true && this.sendsNothing) {
|
|
147
|
+
this.logger.warn("openTelemetry is on, but this agent reports nothing to NestJS Observe, so OpenTelemetry interop stays off: " +
|
|
148
|
+
"it runs only beside reporting to Observe.");
|
|
149
|
+
}
|
|
158
150
|
this.flushInterval = setInterval(() => this.flush(), flushIntervalTime);
|
|
159
151
|
if (!this.sendsNothing) {
|
|
160
152
|
this.startRuntimeMetrics();
|
|
@@ -444,40 +436,6 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
444
436
|
}
|
|
445
437
|
}
|
|
446
438
|
}
|
|
447
|
-
get hasScaffoldCredentials() {
|
|
448
|
-
return (this.options.appKey === SCAFFOLD_APP_KEY &&
|
|
449
|
-
this.options.appSecret === SCAFFOLD_APP_SECRET);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Whether credentials are missing and the collector is the hosted one,
|
|
453
|
-
* which answers every batch without them with a 401.
|
|
454
|
-
*
|
|
455
|
-
* The worker leaves both headers off when either half is unset, which a
|
|
456
|
-
* collector in placeholder mode accepts, so a collector of the user's own
|
|
457
|
-
* still gets those batches. The hosted collector refuses them for as long
|
|
458
|
-
* as the process runs: on 2026-10-15 that was 42% of the 1.87M batches it
|
|
459
|
-
* refused, from processes deployed without their environment variables,
|
|
460
|
-
* some of them posting every five seconds for weeks.
|
|
461
|
-
*/
|
|
462
|
-
get lacksHostedCredentials() {
|
|
463
|
-
return ((!this.options.appKey || !this.options.appSecret) &&
|
|
464
|
-
this.isHostedCollector);
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Whether `endpoint` - set, from `OBSERVE_ENDPOINT`, or the default - is the
|
|
468
|
-
* hosted collector. Compared by host, so a trailing slash or a capital
|
|
469
|
-
* letter cannot pass it off as a collector of the user's own.
|
|
470
|
-
*/
|
|
471
|
-
get isHostedCollector() {
|
|
472
|
-
try {
|
|
473
|
-
return (new URL(this.endpoint).hostname ===
|
|
474
|
-
new URL(HOSTED_COLLECTOR_ENDPOINT).hostname);
|
|
475
|
-
}
|
|
476
|
-
catch {
|
|
477
|
-
// Not a URL at all; the worker says so when it tries to send.
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
439
|
/**
|
|
482
440
|
* Whether `enabled: false` switched the agent off - whatever the
|
|
483
441
|
* credentials, and whichever collector `endpoint` names.
|
|
@@ -487,12 +445,10 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
487
445
|
}
|
|
488
446
|
/** Whether nothing will be sent, so there is no worker to feed. */
|
|
489
447
|
get sendsNothing() {
|
|
490
|
-
return (this.
|
|
491
|
-
this.hasScaffoldCredentials ||
|
|
492
|
-
this.lacksHostedCredentials);
|
|
448
|
+
return !sendsToObserve(this.options);
|
|
493
449
|
}
|
|
494
450
|
get endpoint() {
|
|
495
|
-
return this.options
|
|
451
|
+
return collectorEndpointOf(this.options);
|
|
496
452
|
}
|
|
497
453
|
/**
|
|
498
454
|
* Says out loud when credentials are about to cross the network unencrypted.
|
|
@@ -538,6 +494,9 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
538
494
|
appSecret: this.options.appSecret,
|
|
539
495
|
// Clamped by `onModuleInit` before the first worker starts.
|
|
540
496
|
flushInterval: this.options.flushInterval ?? DEFAULT_FLUSH_INTERVAL,
|
|
497
|
+
// Batches ask what the app's plan allows `otlp` to export only when
|
|
498
|
+
// it is set up to export at all.
|
|
499
|
+
asksForOtlpExport: this.otlpTraceExporter?.enabled === true,
|
|
541
500
|
}),
|
|
542
501
|
});
|
|
543
502
|
this.watchWorker(this.worker);
|
|
@@ -552,6 +511,10 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
552
511
|
this.logger.debug("Worker exited on shutdown.");
|
|
553
512
|
return;
|
|
554
513
|
}
|
|
514
|
+
// Whatever comes next, this worker gets nothing in any more: a
|
|
515
|
+
// restarted one has to get a batch in again first.
|
|
516
|
+
this.observeAgentSharedBuffer.recordStopped();
|
|
517
|
+
this.reportOpenTelemetryInterop("stopped");
|
|
555
518
|
if (code !== 0) {
|
|
556
519
|
this.logger.error(`Worker stopped with exit code ${code}. Restarting worker...`);
|
|
557
520
|
this.initializeWorker(); // Restart the worker
|
|
@@ -567,8 +530,31 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
567
530
|
this.sendsPausedUntil = pausedUntil;
|
|
568
531
|
return;
|
|
569
532
|
}
|
|
533
|
+
const notIngested = parseNotIngestedMessage(msg);
|
|
534
|
+
if (notIngested !== null) {
|
|
535
|
+
// Only a refusal of the credentials pauses OpenTelemetry interop. A
|
|
536
|
+
// batch that failed to get in otherwise - the collector down, a 5xx, a
|
|
537
|
+
// timeout, a 429 pause - is an outage at NestJS Observe, and the
|
|
538
|
+
// traces running through the services around this one stay whole
|
|
539
|
+
// through it.
|
|
540
|
+
if (notIngested === "refused") {
|
|
541
|
+
this.observeAgentSharedBuffer.recordRefused();
|
|
542
|
+
this.reportOpenTelemetryInterop("refused");
|
|
543
|
+
}
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
// Ahead of the verdict for the same batch, which the worker posts after
|
|
547
|
+
// it - see `OtlpExportGrant`.
|
|
548
|
+
const otlpExport = parseOtlpExportMessage(msg);
|
|
549
|
+
if (otlpExport !== null) {
|
|
550
|
+
this.observeAgentSharedBuffer.setOtlpExport(otlpExport);
|
|
551
|
+
this.reportOtlpExport(otlpExport);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
570
554
|
const mode = parseDegradedMessage(msg);
|
|
571
555
|
if (mode !== null) {
|
|
556
|
+
this.observeAgentSharedBuffer.recordAccepted();
|
|
557
|
+
this.reportOpenTelemetryInterop();
|
|
572
558
|
this.reportFirstAcceptedBatch();
|
|
573
559
|
this.applyIngestMode(mode, parseOveragePauseReason(msg));
|
|
574
560
|
return;
|
|
@@ -581,6 +567,65 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
581
567
|
this.logger.debug(msg);
|
|
582
568
|
}
|
|
583
569
|
}
|
|
570
|
+
/**
|
|
571
|
+
* Whether OpenTelemetry interop was last said to be on - see
|
|
572
|
+
* `reportOpenTelemetryInterop`. It starts off, as the buffer's state does.
|
|
573
|
+
*/
|
|
574
|
+
reportedOpenTelemetry = false;
|
|
575
|
+
/**
|
|
576
|
+
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
577
|
+
* it runs only beside reporting to NestJS Observe (see
|
|
578
|
+
* `ObserveAgentSharedBuffer.isIngesting`), and a `traceparent` that stops
|
|
579
|
+
* going out with no word would read as a bug in the agent.
|
|
580
|
+
*/
|
|
581
|
+
reportOpenTelemetryInterop(why) {
|
|
582
|
+
if (this.options.openTelemetry !== true) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
const on = this.observeAgentSharedBuffer.isIngesting();
|
|
586
|
+
if (on === this.reportedOpenTelemetry) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
this.reportedOpenTelemetry = on;
|
|
590
|
+
if (on) {
|
|
591
|
+
this.logger.log("OpenTelemetry interop is on: the collector accepted this app's telemetry, so traceparent is read and sent. " +
|
|
592
|
+
"It stays on through collector outages and rate limits, and pauses only if the collector refuses the app's " +
|
|
593
|
+
"credentials or the telemetry worker stops.");
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const reason = why === "refused"
|
|
597
|
+
? "the collector refused this app's credentials"
|
|
598
|
+
: "the telemetry worker stopped";
|
|
599
|
+
this.logger.warn(`OpenTelemetry interop is paused: ${reason}. It resumes with the next batch the collector accepts.`);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* The OTLP export grant last said, or null before the first. Kept beside
|
|
603
|
+
* the log so each change is said once: every accepted batch repeats it.
|
|
604
|
+
*/
|
|
605
|
+
reportedOtlpExport = null;
|
|
606
|
+
/**
|
|
607
|
+
* Says what the collector granted `otlp` when it changes, the first answer
|
|
608
|
+
* included - an export that stays silent would read as a broken receiver.
|
|
609
|
+
* Only the plan's answer: a pause while the collector is not taking the
|
|
610
|
+
* app's telemetry is said by the interop line above.
|
|
611
|
+
*/
|
|
612
|
+
reportOtlpExport(grant) {
|
|
613
|
+
if (grant === this.reportedOtlpExport) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
this.reportedOtlpExport = grant;
|
|
617
|
+
const url = this.otlpTraceExporter?.url ?? "the OTLP receiver";
|
|
618
|
+
if (grant === "full") {
|
|
619
|
+
this.logger.log(`OTLP export is on: every trace this app records goes to ${url}.`);
|
|
620
|
+
}
|
|
621
|
+
else if (grant === "capped") {
|
|
622
|
+
this.logger.log(`OTLP export is on, capped by this app's plan: traces go to ${url} as NestJS Observe keeps them, ` +
|
|
623
|
+
"so an operation it keeps without its spans - past the plan's daily pacing or monthly allowance - is exported without them too.");
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
this.logger.warn(`OTLP export is off: the collector did not grant it for this app's plan, so nothing goes to ${url}.`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
584
629
|
/** Whether this process has said the collector accepted its first batch. */
|
|
585
630
|
reportedConnection = false;
|
|
586
631
|
/**
|
|
@@ -598,7 +643,7 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
598
643
|
return;
|
|
599
644
|
}
|
|
600
645
|
this.reportedConnection = true;
|
|
601
|
-
this.logger.log(this.
|
|
646
|
+
this.logger.log(isHostedCollector(this.endpoint)
|
|
602
647
|
? `Observe is connected: the collector accepted this app's first batch. Requests and jobs appear at ${DASHBOARD_URL} as soon as the app serves them.`
|
|
603
648
|
: `Observe is connected: ${this.endpoint} accepted this app's first batch.`);
|
|
604
649
|
}
|
|
@@ -726,7 +771,9 @@ ObserveAgentWorker = ObserveAgentWorker_1 = __decorate([
|
|
|
726
771
|
Injectable(),
|
|
727
772
|
__param(1, Inject(OBSERVE_OPTIONS)),
|
|
728
773
|
__param(3, Optional()),
|
|
774
|
+
__param(4, Optional()),
|
|
729
775
|
__metadata("design:paramtypes", [ObserveAgentSharedBuffer, Object, NodeRuntimeMetricsService,
|
|
730
|
-
OperationTraceRegistry
|
|
776
|
+
OperationTraceRegistry,
|
|
777
|
+
OtlpTraceExporter])
|
|
731
778
|
], ObserveAgentWorker);
|
|
732
779
|
export { ObserveAgentWorker };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ObserveOptions } from "../interfaces/observe-options.interface.js";
|
|
2
|
+
/** The options that decide whether, and where, the agent reports. */
|
|
3
|
+
type ReportingOptions = Pick<ObserveOptions, "enabled" | "appKey" | "appSecret" | "endpoint">;
|
|
4
|
+
/** The collector the agent posts to: `endpoint`, else the default. */
|
|
5
|
+
export declare function collectorEndpointOf(options: ReportingOptions): string;
|
|
6
|
+
export declare function hasScaffoldCredentials(options: ReportingOptions): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Whether `endpoint` - set, from `OBSERVE_ENDPOINT`, or the default - is the
|
|
9
|
+
* hosted collector. Compared by host, so a trailing slash or a capital
|
|
10
|
+
* letter cannot pass it off as a collector of the user's own.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isHostedCollector(endpoint: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Whether credentials are missing and the collector is the hosted one,
|
|
15
|
+
* which answers every batch without them with a 401.
|
|
16
|
+
*
|
|
17
|
+
* The worker leaves both headers off when either half is unset, which a
|
|
18
|
+
* collector in placeholder mode accepts, so a collector of the user's own
|
|
19
|
+
* still gets those batches. The hosted collector refuses them for as long
|
|
20
|
+
* as the process runs: on 2026-10-15 that was 42% of the 1.87M batches it
|
|
21
|
+
* refused, from processes deployed without their environment variables,
|
|
22
|
+
* some of them posting every five seconds for weeks.
|
|
23
|
+
*/
|
|
24
|
+
export declare function lacksHostedCredentials(options: ReportingOptions): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Whether the agent sends NestJS Observe anything: not with `enabled:
|
|
27
|
+
* false`, not with `nest new`'s placeholders, and not without both
|
|
28
|
+
* credentials when `endpoint` is the hosted collector. Another `endpoint` - a
|
|
29
|
+
* proxy that adds them on the way - is sent to either way.
|
|
30
|
+
*
|
|
31
|
+
* The worker starts only when this holds. So does OTLP export, which runs
|
|
32
|
+
* beside reporting to Observe, never instead of it.
|
|
33
|
+
*/
|
|
34
|
+
export declare function sendsToObserve(options: ReportingOptions): boolean;
|
|
35
|
+
export {};
|