@nestjs/observe 0.3.12 → 0.3.13

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.
Files changed (36) hide show
  1. package/README.md +65 -2
  2. package/dist/agent/degraded-ingest.protocol.d.ts +82 -26
  3. package/dist/agent/degraded-ingest.protocol.js +60 -19
  4. package/dist/agent/detached-observe-worker.js +31 -16
  5. package/dist/agent/observe-agent.shared-buffer.d.ts +54 -1
  6. package/dist/agent/observe-agent.shared-buffer.js +127 -22
  7. package/dist/agent/observe-agent.worker.d.ts +16 -0
  8. package/dist/agent/observe-agent.worker.js +57 -10
  9. package/dist/agent/telemetry-wire-contract.js +11 -1
  10. package/dist/encoders/request-snapshot.encoder.d.ts +1 -0
  11. package/dist/encoders/request-snapshot.encoder.js +1 -0
  12. package/dist/interfaces/observe-options.interface.d.ts +13 -1
  13. package/dist/interfaces/request-snapshot.interface.d.ts +13 -0
  14. package/dist/objectives/objectives.registry.d.ts +1 -0
  15. package/dist/objectives/objectives.registry.js +12 -0
  16. package/dist/outgoing/mssql.integration.d.ts +12 -0
  17. package/dist/outgoing/mssql.integration.js +248 -0
  18. package/dist/outgoing/mysql2.integration.js +1 -1
  19. package/dist/outgoing/outgoing-observe-agent.service.js +46 -8
  20. package/dist/outgoing/outgoing-span.recorder.d.ts +7 -0
  21. package/dist/outgoing/outgoing-span.recorder.js +15 -0
  22. package/dist/outgoing/pg.integration.js +1 -1
  23. package/dist/outgoing/sql-statement.util.d.ts +5 -1
  24. package/dist/outgoing/sql-statement.util.js +83 -10
  25. package/dist/protocols/http-observe-agent.service.js +9 -2
  26. package/dist/protocols/job-trace-runner.js +5 -0
  27. package/dist/services/node-runtime-metrics.service.js +1 -1
  28. package/dist/services/operation-trace.registry.d.ts +11 -0
  29. package/dist/services/operation-trace.registry.js +37 -1
  30. package/dist/services/stdout-forwarder.service.js +1 -1
  31. package/dist/services/trace-sampler.service.js +5 -0
  32. package/dist/services/tracer.service.d.ts +19 -0
  33. package/dist/services/tracer.service.js +61 -3
  34. package/dist/utils/ingest-refusal.util.d.ts +7 -5
  35. package/dist/utils/ingest-refusal.util.js +17 -5
  36. package/package.json +9 -4
@@ -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, } from "./degraded-ingest.protocol.js";
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";
@@ -106,23 +106,38 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
106
106
  this._mainThreadBuffer = this.createEmptyPayload();
107
107
  }
108
108
  const maxPerBatch = this.maxOperationsPerBatch;
109
- if (this._mainThreadBuffer.snapshots.length >= maxPerBatch) {
109
+ if (this._mainThreadBuffer.snapshots.length + this.withheldSnapshots >=
110
+ maxPerBatch) {
110
111
  if (this.options.debug) {
111
112
  this.logger.debug(`Max snapshots per transaction reached: ${maxPerBatch}. Ignoring snapshot.`);
112
113
  }
113
114
  return;
114
115
  }
115
116
  const encodedSnapshot = RequestSnapshotEncoder.encode(snapshot);
116
- if (this.isDegraded() && !isNotableSnapshot(encodedSnapshot)) {
117
- // The collector is discarding these on arrival; shipping them costs the
118
- // application its own CPU and bandwidth for nothing. The snapshot still
119
- // goes - the request, its duration and its error are what the charts
120
- // are built from, and those are still being kept - and the count goes
121
- // with it, because it is the only record these existed.
122
- this.countTruncated(encodedSnapshot.t);
123
- encodedSnapshot.t = [];
124
- }
125
- this._mainThreadBuffer.snapshots.push(encodedSnapshot);
117
+ if (this.isErrorsOnly()) {
118
+ // Decided ahead of the rule below, which keeps more than a paused
119
+ // account does: a slow success is notable, and not a server error.
120
+ // Sent or not, the request still brings its handler's objectives due
121
+ // further down - they are not telemetry.
122
+ if (this.sendsWhileErrorsOnly(encodedSnapshot)) {
123
+ this._mainThreadBuffer.snapshots.push(encodedSnapshot);
124
+ }
125
+ else {
126
+ this.withheldSnapshots += 1;
127
+ }
128
+ }
129
+ else {
130
+ if (this.isDegraded() && !isNotableSnapshot(encodedSnapshot)) {
131
+ // The collector is discarding these on arrival; shipping them costs
132
+ // the application its own CPU and bandwidth for nothing. The snapshot
133
+ // still goes - the request, its duration and its error are what the
134
+ // charts are built from, and those are still being kept - and the
135
+ // count goes with it, because it is the only record these existed.
136
+ this.countTruncated(encodedSnapshot.t);
137
+ encodedSnapshot.t = [];
138
+ }
139
+ this._mainThreadBuffer.snapshots.push(encodedSnapshot);
140
+ }
126
141
  // Read off the raw snapshot: the handler's span carries `Class.method`,
127
142
  // which is what `@Objective` declarations are keyed by. Declarations are
128
143
  // not telemetry, so they ride even a degraded batch - but a batch holds
@@ -151,20 +166,33 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
151
166
  // the worker service - the process that sees every queue job - grows this
152
167
  // array for as long as the stall lasts.
153
168
  const maxPerBatch = this.maxOperationsPerBatch;
154
- if (this._mainThreadBuffer.jobs.length >= maxPerBatch) {
169
+ if (this._mainThreadBuffer.jobs.length + this.withheldJobs >= maxPerBatch) {
155
170
  if (this.options.debug) {
156
171
  this.logger.debug(`Max job snapshots per transaction reached: ${maxPerBatch}. Ignoring snapshot.`);
157
172
  }
158
173
  return;
159
174
  }
160
175
  const encodedJobSnapshot = JobSnapshotEncoder.encode(jobSnapshot);
161
- if (this.isDegraded() && !isNotableSnapshot(encodedJobSnapshot)) {
162
- // A job carries no status code, so the same rule grades it on its error
163
- // and its duration - which is what the collector does with it too.
164
- this.countTruncated(encodedJobSnapshot.t);
165
- encodedJobSnapshot.t = [];
176
+ if (this.isErrorsOnly()) {
177
+ // A job carries no status code, so the error it failed with is what
178
+ // makes it a server error - which is what the collector keeps of it too.
179
+ if (this.sendsWhileErrorsOnly(encodedJobSnapshot)) {
180
+ this._mainThreadBuffer.jobs.push(encodedJobSnapshot);
181
+ }
182
+ else {
183
+ this.withheldJobs += 1;
184
+ }
185
+ }
186
+ else {
187
+ if (this.isDegraded() && !isNotableSnapshot(encodedJobSnapshot)) {
188
+ // A job carries no status code, so the same rule grades it on its
189
+ // error and its duration - which is what the collector does with it
190
+ // too.
191
+ this.countTruncated(encodedJobSnapshot.t);
192
+ encodedJobSnapshot.t = [];
193
+ }
194
+ this._mainThreadBuffer.jobs.push(encodedJobSnapshot);
166
195
  }
167
- this._mainThreadBuffer.jobs.push(encodedJobSnapshot);
168
196
  }
169
197
  /**
170
198
  * Until when the collector's last answer says spans are being discarded.
@@ -175,6 +203,24 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
175
203
  * persisting a guess across restarts.
176
204
  */
177
205
  degradedUntil = 0;
206
+ /**
207
+ * Whether that answer was errors-only: the account's overage is paused, and
208
+ * only its server errors are kept. It holds as long as the window above and
209
+ * not a moment longer.
210
+ */
211
+ errorsOnly = false;
212
+ /**
213
+ * Requests and jobs that errors-only ingest withheld from the batch being
214
+ * built, cleared with it.
215
+ *
216
+ * Counted against `maxTracesPerBatch` as though they had been sent, so a
217
+ * pause grades the same executions a batch would carry outside one: what
218
+ * is reported withheld is what the collector would have been sent and
219
+ * billed, and a busy service encodes no more per batch than it does
220
+ * outside a pause. A server error past the cap is lost, as it would be.
221
+ */
222
+ withheldSnapshots = 0;
223
+ withheldJobs = 0;
178
224
  /**
179
225
  * Records what the collector said about the last batch.
180
226
  *
@@ -182,13 +228,44 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
182
228
  * the account stays over its allowance and lapses on its own once it does
183
229
  * not - the collector has no way to announce the end of it, and an upgrade
184
230
  * must not need a restart to take effect.
231
+ *
232
+ * Each reply replaces the last whole: one that is not errors-only ends
233
+ * errors-only ingest at once, whatever it says instead.
185
234
  */
186
- setDegraded(degraded) {
187
- this.degradedUntil = degraded ? Date.now() + DEGRADED_TTL_MS : 0;
235
+ setIngestMode(mode) {
236
+ this.degradedUntil = mode === "full" ? 0 : Date.now() + DEGRADED_TTL_MS;
237
+ this.errorsOnly = mode === "errors-only";
188
238
  }
239
+ /**
240
+ * Whether trees the collector discards are being withheld: in every mode
241
+ * but `full`, until the window lapses.
242
+ */
189
243
  isDegraded() {
190
244
  return this.degradedUntil > Date.now();
191
245
  }
246
+ /** Whether only server errors are being sent, until the same window lapses. */
247
+ isErrorsOnly() {
248
+ return this.errorsOnly && this.isDegraded();
249
+ }
250
+ /**
251
+ * What errors-only ingest makes of one execution: whether it is sent.
252
+ *
253
+ * Its tree never is, server error or not - the collector stores no tree of
254
+ * a paused account's, and a 5xx storm would otherwise ship every failure's
255
+ * whole waterfall - so its nodes are counted and the array emptied, as
256
+ * degraded ingest empties it. One that is not a server error is not sent at
257
+ * all, and counts itself and the error it carried on top: the same events,
258
+ * in the same units, as the collector counts when it filters a batch itself.
259
+ */
260
+ sendsWhileErrorsOnly(execution) {
261
+ this.countTruncated(execution.t);
262
+ execution.t = [];
263
+ if (isServerErrorSnapshot(execution)) {
264
+ return true;
265
+ }
266
+ this.countWithheld(execution.e ? 2 : 1);
267
+ return false;
268
+ }
192
269
  /**
193
270
  * Adds a withheld tree to the batch's reported truncation.
194
271
  *
@@ -198,11 +275,36 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
198
275
  * actually produced.
199
276
  */
200
277
  countTruncated(traces) {
278
+ this.countWithheld(countTraceNodes(traces));
279
+ }
280
+ /** Adds withheld events, in billed units, to the batch's `truncatedSpans`. */
281
+ countWithheld(events) {
201
282
  if (!this._mainThreadBuffer) {
202
283
  return;
203
284
  }
204
285
  this._mainThreadBuffer.truncatedSpans =
205
- (this._mainThreadBuffer.truncatedSpans ?? 0) + countTraceNodes(traces);
286
+ (this._mainThreadBuffer.truncatedSpans ?? 0) + events;
287
+ }
288
+ /**
289
+ * Takes the log records out of the outgoing batch while errors-only ingest
290
+ * holds, one withheld event each - error-level records too: a crash loop
291
+ * logs faster than it fails requests.
292
+ *
293
+ * Done as the batch is written rather than as lines arrive, so a batch
294
+ * counts exactly the records it would have carried - its share of the
295
+ * per-batch cap and of the backlog, as outside a pause - which is what the
296
+ * collector counts when it drops a batch's logs itself. Counted at the door,
297
+ * a chatty service would report lines the cap was always going to drop.
298
+ */
299
+ withholdLogs() {
300
+ const batch = this._mainThreadBuffer;
301
+ if (!batch?.logs || !this.isErrorsOnly()) {
302
+ return;
303
+ }
304
+ if (batch.logs.length > 0) {
305
+ this.countWithheld(batch.logs.length);
306
+ }
307
+ delete batch.logs;
206
308
  }
207
309
  /**
208
310
  * `maxTracesPerBatch`, held to the most the collector accepts in one section
@@ -385,6 +487,7 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
385
487
  TRUNCATION_SUFFIX);
386
488
  }
387
489
  encodeAndWrite() {
490
+ this.withholdLogs();
388
491
  if (this.options.debug) {
389
492
  if (this._mainThreadBuffer?.snapshots) {
390
493
  this.logger.debug(`Encoding and writing ${this._mainThreadBuffer.snapshots.length} snapshots to shared buffer.`);
@@ -416,6 +519,8 @@ let ObserveAgentSharedBuffer = ObserveAgentSharedBuffer_1 = class ObserveAgentSh
416
519
  }
417
520
  resetMainThreadBuffer() {
418
521
  this._mainThreadBuffer = null;
522
+ this.withheldSnapshots = 0;
523
+ this.withheldJobs = 0;
419
524
  }
420
525
  createEmptyPayload() {
421
526
  const payload = {
@@ -141,6 +141,11 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
141
141
  * letter cannot pass it off as a collector of the user's own.
142
142
  */
143
143
  private get isHostedCollector();
144
+ /**
145
+ * Whether `enabled: false` switched the agent off - whatever the
146
+ * credentials, and whichever collector `endpoint` names.
147
+ */
148
+ private get isDisabled();
144
149
  /** Whether nothing will be sent, so there is no worker to feed. */
145
150
  private get sendsNothing();
146
151
  private get endpoint();
@@ -167,6 +172,13 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
167
172
  * per flush would bury the one thing worth reading.
168
173
  */
169
174
  private reportedMode;
175
+ /**
176
+ * Why the account's overage is paused, when the last reply was errors-only
177
+ * and gave a reason this agent can word; null otherwise. Kept beside
178
+ * `reportedMode` for the same reason, and a pause whose reason changes -
179
+ * spike protection's, then a spent budget's - is said again.
180
+ */
181
+ private reportedPauseReason;
170
182
  /**
171
183
  * Stops sending trace trees the collector has told us it is throwing away,
172
184
  * and says so once - and again only when the reason changes.
@@ -176,6 +188,10 @@ export declare class ObserveAgentWorker implements OnModuleInit, OnApplicationSh
176
188
  * that out by opening a trace and seeing nothing has been let down by an
177
189
  * agent that knew. Each line says what is still kept, because the traces
178
190
  * worth opening - server errors, slow requests, failed jobs - still are.
191
+ *
192
+ * Except while the account's overage is paused, when next to nothing is
193
+ * kept and nothing comes back on its own before the month ends: that line
194
+ * says so, and who can change it.
179
195
  */
180
196
  private applyIngestMode;
181
197
  handleError(error: Error): void;
@@ -21,7 +21,7 @@ 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, parsePausedMessage, } from "./degraded-ingest.protocol.js";
24
+ import { parseDegradedMessage, parseOveragePauseReason, parsePausedMessage, } from "./degraded-ingest.protocol.js";
25
25
  import { detachedWorkerData, detachedWorkerSource, } from "./detached-observe-worker.assembly.js";
26
26
  import { ObserveAgentSharedBuffer } from "./observe-agent.shared-buffer.js";
27
27
  const DEFAULT_RUNTIME_METRICS_INTERVAL = 60000; // 60 seconds
@@ -127,7 +127,12 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
127
127
  flushIntervalTime = MIN_FLUSH_INTERVAL;
128
128
  this.options.flushInterval = MIN_FLUSH_INTERVAL;
129
129
  }
130
- if (this.hasScaffoldCredentials) {
130
+ if (this.isDisabled) {
131
+ // Ahead of the credential checks: an agent switched off on purpose
132
+ // needs no key or secret, so a missing one is not worth a warning.
133
+ this.logger.log("Observe is disabled (enabled: false): nothing is recorded or sent.");
134
+ }
135
+ else if (this.hasScaffoldCredentials) {
131
136
  // Nothing is sent, so there is no worker to start. The flush timer below
132
137
  // still runs and empties the buffer, which the instrumentation keeps
133
138
  // writing into either way.
@@ -470,9 +475,18 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
470
475
  return false;
471
476
  }
472
477
  }
478
+ /**
479
+ * Whether `enabled: false` switched the agent off - whatever the
480
+ * credentials, and whichever collector `endpoint` names.
481
+ */
482
+ get isDisabled() {
483
+ return this.options.enabled === false;
484
+ }
473
485
  /** Whether nothing will be sent, so there is no worker to feed. */
474
486
  get sendsNothing() {
475
- return this.hasScaffoldCredentials || this.lacksHostedCredentials;
487
+ return (this.isDisabled ||
488
+ this.hasScaffoldCredentials ||
489
+ this.lacksHostedCredentials);
476
490
  }
477
491
  get endpoint() {
478
492
  return this.options.endpoint ?? DEFAULT_COLLECTOR_ENDPOINT;
@@ -552,7 +566,7 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
552
566
  }
553
567
  const mode = parseDegradedMessage(msg);
554
568
  if (mode !== null) {
555
- this.applyIngestMode(mode);
569
+ this.applyIngestMode(mode, parseOveragePauseReason(msg));
556
570
  return;
557
571
  }
558
572
  if (msg.includes("Error:")) {
@@ -571,6 +585,13 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
571
585
  * per flush would bury the one thing worth reading.
572
586
  */
573
587
  reportedMode = "full";
588
+ /**
589
+ * Why the account's overage is paused, when the last reply was errors-only
590
+ * and gave a reason this agent can word; null otherwise. Kept beside
591
+ * `reportedMode` for the same reason, and a pause whose reason changes -
592
+ * spike protection's, then a spent budget's - is said again.
593
+ */
594
+ reportedPauseReason = null;
574
595
  /**
575
596
  * Stops sending trace trees the collector has told us it is throwing away,
576
597
  * and says so once - and again only when the reason changes.
@@ -580,15 +601,36 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
580
601
  * that out by opening a trace and seeing nothing has been let down by an
581
602
  * agent that knew. Each line says what is still kept, because the traces
582
603
  * worth opening - server errors, slow requests, failed jobs - still are.
604
+ *
605
+ * Except while the account's overage is paused, when next to nothing is
606
+ * kept and nothing comes back on its own before the month ends: that line
607
+ * says so, and who can change it.
583
608
  */
584
- applyIngestMode(mode) {
585
- // Reduced, trimmed and paced withhold the same trees; only the notice
586
- // differs.
587
- this.observeAgentSharedBuffer.setDegraded(mode !== "full");
588
- if (mode === this.reportedMode) {
609
+ applyIngestMode(mode, pauseReason = null) {
610
+ // Reduced, trimmed and paced withhold the same trees, and errors-only
611
+ // everything but server errors; the buffer tells them apart.
612
+ this.observeAgentSharedBuffer.setIngestMode(mode);
613
+ if (mode === this.reportedMode &&
614
+ pauseReason === this.reportedPauseReason) {
589
615
  return;
590
616
  }
591
- if (mode === "reduced") {
617
+ if (mode === "errors-only") {
618
+ // Not "most traces arrive without their spans": requests, jobs and
619
+ // logs stop too, and the operator may have to ask whoever holds the
620
+ // account to resume them.
621
+ const why = pauseReason === "spike"
622
+ ? "spike protection paused this account's overage"
623
+ : pauseReason === "budget"
624
+ ? "this account's monthly overage budget is spent, so its overage is paused"
625
+ : "this account's overage is paused";
626
+ const resume = pauseReason === "budget"
627
+ ? "resumes it or raises the budget"
628
+ : "resumes it";
629
+ this.logger.warn(`Observe: ${why}. Only server errors and failed jobs are sent now, without their spans; ` +
630
+ "every other request and job, and every log line, is held back. Runtime and custom metrics are still recorded. " +
631
+ `Full collection returns when someone ${resume} at /dashboard/billing/usage, or at 00:00 UTC on the 1st.`);
632
+ }
633
+ else if (mode === "reduced") {
592
634
  this.logger.warn("Observe: this account has used its monthly allowance, so most traces now arrive without their spans. " +
593
635
  "Requests, jobs and errors are still recorded, and server errors, slow requests and failed jobs keep their full traces. " +
594
636
  "See /dashboard/billing/usage.");
@@ -605,10 +647,15 @@ let ObserveAgentWorker = ObserveAgentWorker_1 = class ObserveAgentWorker {
605
647
  "Requests, jobs and errors are still recorded, and server errors, slow requests and failed jobs keep their full traces. " +
606
648
  "See /dashboard/billing/usage.");
607
649
  }
650
+ else if (this.reportedMode === "errors-only") {
651
+ // Not only the spans: every request, job and log line is back.
652
+ this.logger.log("Observe: this account's overage is no longer paused, so everything is recorded again.");
653
+ }
608
654
  else {
609
655
  this.logger.log("Observe: spans are being recorded again.");
610
656
  }
611
657
  this.reportedMode = mode;
658
+ this.reportedPauseReason = pauseReason;
612
659
  }
613
660
  handleError(error) {
614
661
  this.logger.error(error);
@@ -58,10 +58,16 @@ const TRACE_NODE = {
58
58
  so: { type: "number" },
59
59
  ch: { type: "object", array: true, recursive: true },
60
60
  };
61
+ /**
62
+ * The API takes the attributes as one object and checks none of its keys,
63
+ * so these are not refused there. They are declared all the same: a repair
64
+ * removes what is undeclared, and `dop` is what marks a request as renamed.
65
+ */
61
66
  const REQUEST_ATTRIBUTES = {
62
67
  m: { type: "string" },
63
68
  sc: { type: "number" },
64
69
  ou: { type: "string" },
70
+ dop: { type: "string" },
65
71
  };
66
72
  /**
67
73
  * An operation's spans. Required - the API refuses an operation without the
@@ -83,7 +89,11 @@ const REQUEST_SNAPSHOT = {
83
89
  ti: { type: "string", required: true, maxLength: 255 },
84
90
  d: { type: "number" },
85
91
  p: { type: "string" },
86
- op: { type: "string" },
92
+ // Required as the API requires it. The application can set it now
93
+ // (`TracerService.setOperationId`), so an unusable one can reach a batch, and
94
+ // a repair that only stripped it would re-send an entry the API refuses
95
+ // again - costing the whole batch rather than the one operation.
96
+ op: { type: "string", required: true },
87
97
  u: { type: "string" },
88
98
  tg: { type: "object" },
89
99
  e: { type: "object" },
@@ -4,6 +4,7 @@ declare const ATTRIBUTES_KEY_MAP: {
4
4
  readonly method: "m";
5
5
  readonly statusCode: "sc";
6
6
  readonly originalUrl: "ou";
7
+ readonly defaultOperationId: "dop";
7
8
  };
8
9
  declare const SNAPSHOT_KEY_MAP: {
9
10
  readonly calledAt: "ct";
@@ -4,6 +4,7 @@ const ATTRIBUTES_KEY_MAP = {
4
4
  method: "m",
5
5
  statusCode: "sc",
6
6
  originalUrl: "ou",
7
+ defaultOperationId: "dop",
7
8
  };
8
9
  const SNAPSHOT_KEY_MAP = {
9
10
  calledAt: "ct",
@@ -134,6 +134,18 @@ export interface CreateObserveModuleOptions {
134
134
  };
135
135
  }
136
136
  export interface ObserveOptions {
137
+ /**
138
+ * Whether the agent runs at all. `false` keeps the module in place - its
139
+ * providers, `TracerService` among them, still resolve - but records and
140
+ * sends nothing: no traces, runtime metrics or forwarded logs, no worker
141
+ * thread, and no request to any collector. It logs one line at startup
142
+ * saying so, and no warning about credentials, which it does not need.
143
+ *
144
+ * For environments that should not report, such as local development:
145
+ * `enabled: process.env.NODE_ENV === "production"`.
146
+ * @default true
147
+ */
148
+ enabled?: boolean;
137
149
  /**
138
150
  * Application key identifying this service to the collector.
139
151
  *
@@ -570,7 +582,7 @@ export interface ObserveOptions {
570
582
  getUserId?: (call: any) => string;
571
583
  };
572
584
  /**
573
- * Spans for what leaves the process: database queries (`pg`, `mysql2`,
585
+ * Spans for what leaves the process: database queries (`pg`, `mysql2`, `mssql`,
574
586
  * `mongodb` - and so every ORM built on them) and outbound HTTP requests
575
587
  * (`fetch`/undici and `node:http`, so axios too). On by default; `false`
576
588
  * switches all of it off.
@@ -89,5 +89,18 @@ export interface RequestSnapshot {
89
89
  * operation id but not a document.
90
90
  */
91
91
  originalUrl?: string;
92
+ /**
93
+ * The operation id a request had before `TracerService.setOperationId`
94
+ * renamed it: its route, its GraphQL root field, its message pattern.
95
+ * Present only on a renamed request. It tells the collector that the
96
+ * operation id is the application's own name, and what the request is
97
+ * grouped under without it.
98
+ *
99
+ * An attribute rather than a field of its own because the collector
100
+ * refuses a snapshot key it does not know but takes the attributes
101
+ * whole, so an agent sending it still reaches a collector that predates
102
+ * it.
103
+ */
104
+ defaultOperationId?: string;
92
105
  };
93
106
  }
@@ -27,6 +27,7 @@ export declare class ObjectivesRegistry implements OnModuleInit {
27
27
  private readonly byHandler;
28
28
  private readonly statedAt;
29
29
  private readonly refusedRoutes;
30
+ private readonly refusedRenamedHandlers;
30
31
  constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner);
31
32
  onModuleInit(): void;
32
33
  /**
@@ -55,6 +55,7 @@ let ObjectivesRegistry = class ObjectivesRegistry {
55
55
  byHandler = new Map();
56
56
  statedAt = new Map();
57
57
  refusedRoutes = new Set();
58
+ refusedRenamedHandlers = new Set();
58
59
  constructor(discoveryService, metadataScanner) {
59
60
  this.discoveryService = discoveryService;
60
61
  this.metadataScanner = metadataScanner;
@@ -152,6 +153,17 @@ let ObjectivesRegistry = class ObjectivesRegistry {
152
153
  if (!handler) {
153
154
  return undefined;
154
155
  }
156
+ // A renamed request reports under the application's own name, and the
157
+ // collector keeps one SLO per handler: declared from requests named per
158
+ // call, that SLO would move to each new name in turn - the reason a
159
+ // handler mounted on several routes is refused at boot.
160
+ if (snapshot.attributes?.defaultOperationId !== undefined) {
161
+ if (!this.refusedRenamedHandlers.has(handler)) {
162
+ this.refusedRenamedHandlers.add(handler);
163
+ this.logger.warn(`@Objective on ${handler} not declared for requests renamed with setOperationId: an SLO watches one route, and each name is a route of its own.`);
164
+ }
165
+ return undefined;
166
+ }
155
167
  const declared = this.byHandler.get(handler);
156
168
  // Routed for one method: that one, whatever the request was - a HEAD
157
169
  // probe of a GET route is still the GET route.
@@ -0,0 +1,12 @@
1
+ import { OutgoingSpanRecorder } from "./outgoing-span.recorder.js";
2
+ interface MssqlModule {
3
+ Request?: {
4
+ prototype?: object;
5
+ };
6
+ }
7
+ /**
8
+ * These internal boundaries follow template expansion and precede pool
9
+ * checkout. The tedious driver's `_batch` delegates to `_query`.
10
+ */
11
+ export declare function patchMssql(mssql: MssqlModule, recorder: OutgoingSpanRecorder): boolean;
12
+ export {};