@nestjs/observe 0.3.22 → 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 +71 -14
- package/dist/agent/degraded-ingest.protocol.d.ts +2 -8
- package/dist/agent/degraded-ingest.protocol.js +2 -8
- package/dist/agent/detached-observe-worker.assembly.d.ts +2 -0
- package/dist/agent/detached-observe-worker.assembly.js +3 -0
- package/dist/agent/detached-observe-worker.d.ts +8 -0
- package/dist/agent/detached-observe-worker.js +20 -2
- package/dist/agent/observe-agent.shared-buffer.d.ts +48 -22
- package/dist/agent/observe-agent.shared-buffer.js +94 -34
- package/dist/agent/observe-agent.worker.d.ts +16 -21
- package/dist/agent/observe-agent.worker.js +67 -74
- 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 +67 -13
- package/dist/observe.constants.d.ts +7 -0
- package/dist/observe.constants.js +7 -0
- package/dist/observe.module.js +50 -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 +6 -4
- package/dist/outgoing/http.integration.js +3 -3
- package/dist/outgoing/outgoing-observe-agent.service.d.ts +2 -2
- package/dist/outgoing/outgoing-observe-agent.service.js +1 -1
- 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 +28 -21
- 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 +29 -1
- package/dist/services/operation-trace.registry.js +46 -5
- package/dist/utils/default-trace-id-generator.util.js +6 -2
- package/dist/utils/opentelemetry.util.d.ts +26 -0
- package/dist/utils/opentelemetry.util.js +48 -0
- package/dist/utils/trace-handoff.util.d.ts +45 -0
- package/dist/utils/trace-handoff.util.js +90 -0
- package/package.json +13 -12
|
@@ -11,7 +11,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
var ObserveAgentSharedBuffer_1;
|
|
14
|
-
import { countTraceNodes, DEGRADED_TTL_MS, isNotableSnapshot, isServerErrorSnapshot,
|
|
14
|
+
import { countTraceNodes, DEGRADED_TTL_MS, isNotableSnapshot, isServerErrorSnapshot, } from "./degraded-ingest.protocol.js";
|
|
15
15
|
import { MAX_BATCH_ITEMS } from "./telemetry-wire-contract.js";
|
|
16
16
|
import { Inject, Injectable, Logger, Optional } from "@nestjs/common";
|
|
17
17
|
import { Counter, Summary } from "../custom-metrics/index.js";
|
|
@@ -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
|
/**
|
|
@@ -210,16 +245,11 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
210
245
|
*/
|
|
211
246
|
errorsOnly = false;
|
|
212
247
|
/**
|
|
213
|
-
* Whether
|
|
214
|
-
*
|
|
215
|
-
* NestJS Observe does not count as reaching it.
|
|
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.
|
|
216
251
|
*/
|
|
217
252
|
ingesting = false;
|
|
218
|
-
/**
|
|
219
|
-
* When batches began failing to get in, since the last one that did; zero
|
|
220
|
-
* while they get in.
|
|
221
|
-
*/
|
|
222
|
-
failingSince = 0;
|
|
223
253
|
/**
|
|
224
254
|
* Requests and jobs that errors-only ingest withheld from the batch being
|
|
225
255
|
* built, cleared with it.
|
|
@@ -259,16 +289,22 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
259
289
|
return this.errorsOnly && this.isDegraded();
|
|
260
290
|
}
|
|
261
291
|
/**
|
|
262
|
-
* Whether
|
|
263
|
-
*
|
|
264
|
-
* stops
|
|
265
|
-
*
|
|
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.
|
|
266
302
|
*
|
|
267
303
|
* OpenTelemetry interop runs on it (see `openTelemetry`): the agent reads
|
|
268
304
|
* and sends `traceparent`, and follows an OpenTelemetry SDK in the process,
|
|
269
|
-
* only
|
|
270
|
-
*
|
|
271
|
-
*
|
|
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.
|
|
272
308
|
*/
|
|
273
309
|
isIngesting() {
|
|
274
310
|
return this.ingesting;
|
|
@@ -276,31 +312,53 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
|
|
|
276
312
|
/** The collector accepted a batch, whatever it then kept of it. */
|
|
277
313
|
recordAccepted() {
|
|
278
314
|
this.ingesting = true;
|
|
279
|
-
this.failingSince = 0;
|
|
280
315
|
}
|
|
281
316
|
/** The collector refused the app's credentials. */
|
|
282
317
|
recordRefused() {
|
|
283
318
|
this.ingesting = false;
|
|
284
|
-
|
|
319
|
+
}
|
|
320
|
+
/** The worker stopped; a restarted one has to get a batch in again. */
|
|
321
|
+
recordStopped() {
|
|
322
|
+
this.ingesting = false;
|
|
285
323
|
}
|
|
286
324
|
/**
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
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.
|
|
290
328
|
*/
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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";
|
|
294
357
|
}
|
|
295
|
-
if (
|
|
296
|
-
|
|
297
|
-
this.ingesting = false;
|
|
358
|
+
if (this.otlpExport === "capped" && now >= this.otlpExportCappedUntil) {
|
|
359
|
+
return "off";
|
|
298
360
|
}
|
|
299
|
-
|
|
300
|
-
/** The worker stopped; a restarted one has to get a batch in again. */
|
|
301
|
-
recordStopped() {
|
|
302
|
-
this.ingesting = false;
|
|
303
|
-
this.failingSince = 0;
|
|
361
|
+
return this.otlpExport;
|
|
304
362
|
}
|
|
305
363
|
/**
|
|
306
364
|
* What errors-only ingest makes of one execution: whether it is sent.
|
|
@@ -593,6 +651,8 @@ ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = __decorate([
|
|
|
593
651
|
Injectable(),
|
|
594
652
|
__param(0, Inject(OBSERVE_OPTIONS)),
|
|
595
653
|
__param(1, Optional()),
|
|
596
|
-
|
|
654
|
+
__param(2, Optional()),
|
|
655
|
+
__metadata("design:paramtypes", [Object, ObjectivesRegistry,
|
|
656
|
+
OtlpTraceExporter])
|
|
597
657
|
], ObserveAgentSharedBuffer);
|
|
598
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.
|
|
@@ -171,11 +154,23 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
|
|
|
171
154
|
private reportedOpenTelemetry;
|
|
172
155
|
/**
|
|
173
156
|
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
174
|
-
* it runs only
|
|
157
|
+
* it runs only beside reporting to NestJS Observe (see
|
|
175
158
|
* `ObserveAgentSharedBuffer.isIngesting`), and a `traceparent` that stops
|
|
176
159
|
* going out with no word would read as a bug in the agent.
|
|
177
160
|
*/
|
|
178
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;
|
|
179
174
|
/** Whether this process has said the collector accepted its first batch. */
|
|
180
175
|
private reportedConnection;
|
|
181
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 {
|
|
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
|
|
@@ -448,40 +436,6 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
448
436
|
}
|
|
449
437
|
}
|
|
450
438
|
}
|
|
451
|
-
get hasScaffoldCredentials() {
|
|
452
|
-
return (this.options.appKey === SCAFFOLD_APP_KEY &&
|
|
453
|
-
this.options.appSecret === SCAFFOLD_APP_SECRET);
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Whether credentials are missing and the collector is the hosted one,
|
|
457
|
-
* which answers every batch without them with a 401.
|
|
458
|
-
*
|
|
459
|
-
* The worker leaves both headers off when either half is unset, which a
|
|
460
|
-
* collector in placeholder mode accepts, so a collector of the user's own
|
|
461
|
-
* still gets those batches. The hosted collector refuses them for as long
|
|
462
|
-
* as the process runs: on 2026-10-15 that was 42% of the 1.87M batches it
|
|
463
|
-
* refused, from processes deployed without their environment variables,
|
|
464
|
-
* some of them posting every five seconds for weeks.
|
|
465
|
-
*/
|
|
466
|
-
get lacksHostedCredentials() {
|
|
467
|
-
return ((!this.options.appKey || !this.options.appSecret) &&
|
|
468
|
-
this.isHostedCollector);
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Whether `endpoint` - set, from `OBSERVE_ENDPOINT`, or the default - is the
|
|
472
|
-
* hosted collector. Compared by host, so a trailing slash or a capital
|
|
473
|
-
* letter cannot pass it off as a collector of the user's own.
|
|
474
|
-
*/
|
|
475
|
-
get isHostedCollector() {
|
|
476
|
-
try {
|
|
477
|
-
return (new URL(this.endpoint).hostname ===
|
|
478
|
-
new URL(HOSTED_COLLECTOR_ENDPOINT).hostname);
|
|
479
|
-
}
|
|
480
|
-
catch {
|
|
481
|
-
// Not a URL at all; the worker says so when it tries to send.
|
|
482
|
-
return false;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
439
|
/**
|
|
486
440
|
* Whether `enabled: false` switched the agent off - whatever the
|
|
487
441
|
* credentials, and whichever collector `endpoint` names.
|
|
@@ -491,12 +445,10 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
491
445
|
}
|
|
492
446
|
/** Whether nothing will be sent, so there is no worker to feed. */
|
|
493
447
|
get sendsNothing() {
|
|
494
|
-
return (this.
|
|
495
|
-
this.hasScaffoldCredentials ||
|
|
496
|
-
this.lacksHostedCredentials);
|
|
448
|
+
return !sendsToObserve(this.options);
|
|
497
449
|
}
|
|
498
450
|
get endpoint() {
|
|
499
|
-
return this.options
|
|
451
|
+
return collectorEndpointOf(this.options);
|
|
500
452
|
}
|
|
501
453
|
/**
|
|
502
454
|
* Says out loud when credentials are about to cross the network unencrypted.
|
|
@@ -542,6 +494,9 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
542
494
|
appSecret: this.options.appSecret,
|
|
543
495
|
// Clamped by `onModuleInit` before the first worker starts.
|
|
544
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,
|
|
545
500
|
}),
|
|
546
501
|
});
|
|
547
502
|
this.watchWorker(this.worker);
|
|
@@ -577,14 +532,23 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
577
532
|
}
|
|
578
533
|
const notIngested = parseNotIngestedMessage(msg);
|
|
579
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.
|
|
580
540
|
if (notIngested === "refused") {
|
|
581
541
|
this.observeAgentSharedBuffer.recordRefused();
|
|
542
|
+
this.reportOpenTelemetryInterop("refused");
|
|
582
543
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
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);
|
|
588
552
|
return;
|
|
589
553
|
}
|
|
590
554
|
const mode = parseDegradedMessage(msg);
|
|
@@ -610,7 +574,7 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
610
574
|
reportedOpenTelemetry = false;
|
|
611
575
|
/**
|
|
612
576
|
* Says when OpenTelemetry interop turns on or off, with `openTelemetry` on:
|
|
613
|
-
* it runs only
|
|
577
|
+
* it runs only beside reporting to NestJS Observe (see
|
|
614
578
|
* `ObserveAgentSharedBuffer.isIngesting`), and a `traceparent` that stops
|
|
615
579
|
* going out with no word would read as a bug in the agent.
|
|
616
580
|
*/
|
|
@@ -624,16 +588,43 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
624
588
|
}
|
|
625
589
|
this.reportedOpenTelemetry = on;
|
|
626
590
|
if (on) {
|
|
627
|
-
this.logger.log("OpenTelemetry interop is on: the collector
|
|
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.");
|
|
628
594
|
return;
|
|
629
595
|
}
|
|
630
596
|
const reason = why === "refused"
|
|
631
597
|
? "the collector refused this app's credentials"
|
|
632
|
-
:
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
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
|
+
}
|
|
637
628
|
}
|
|
638
629
|
/** Whether this process has said the collector accepted its first batch. */
|
|
639
630
|
reportedConnection = false;
|
|
@@ -652,7 +643,7 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
|
|
|
652
643
|
return;
|
|
653
644
|
}
|
|
654
645
|
this.reportedConnection = true;
|
|
655
|
-
this.logger.log(this.
|
|
646
|
+
this.logger.log(isHostedCollector(this.endpoint)
|
|
656
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.`
|
|
657
648
|
: `Observe is connected: ${this.endpoint} accepted this app's first batch.`);
|
|
658
649
|
}
|
|
@@ -780,7 +771,9 @@ ObserveAgentWorker = ObserveAgentWorker_1 = __decorate([
|
|
|
780
771
|
Injectable(),
|
|
781
772
|
__param(1, Inject(OBSERVE_OPTIONS)),
|
|
782
773
|
__param(3, Optional()),
|
|
774
|
+
__param(4, Optional()),
|
|
783
775
|
__metadata("design:paramtypes", [ObserveAgentSharedBuffer, Object, NodeRuntimeMetricsService,
|
|
784
|
-
OperationTraceRegistry
|
|
776
|
+
OperationTraceRegistry,
|
|
777
|
+
OtlpTraceExporter])
|
|
785
778
|
], ObserveAgentWorker);
|
|
786
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 {};
|