@nestjs/observe 0.3.16 → 0.3.17

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.
@@ -53,10 +53,9 @@ export declare class HttpObserveAgentService<Store extends Record<string, unknow
53
53
  * either plucked the trace or is about to.
54
54
  *
55
55
  * Eviction is deferred, not immediate: Express does not cancel the handler
56
- * on abort, so its spans are usually still closing when `close` fires, and
57
- * each one ending after the snapshot is gone would log an error. The grace
58
- * period lets the handler drain first; `unref` keeps the timer from holding
59
- * a shutting-down process open.
56
+ * on abort, so its spans are usually still closing when `close` fires. The
57
+ * grace period lets the handler drain first; `unref` keeps the timer from
58
+ * holding a shutting-down process open.
60
59
  */
61
60
  private evictTraceOnClientAbort;
62
61
  /**
@@ -94,8 +94,11 @@ let HttpObserveAgentService = HttpObserveAgentService_1 = class HttpObserveAgent
94
94
  httpAdapter.setOnRequestHook((req, res, done) => {
95
95
  this.startHttpRequestTracing(req, res, done);
96
96
  });
97
- httpAdapter.setOnResponseHook((req, res) => {
97
+ // Fastify waits for `done` before it runs the next onResponse hook.
98
+ // Express does not pass one.
99
+ httpAdapter.setOnResponseHook((req, res, done) => {
98
100
  this.endHttpRequestTracing(req, res);
101
+ done?.();
99
102
  });
100
103
  }
101
104
  startHttpRequestTracing(req, res, done) {
@@ -207,10 +210,9 @@ let HttpObserveAgentService = HttpObserveAgentService_1 = class HttpObserveAgent
207
210
  * either plucked the trace or is about to.
208
211
  *
209
212
  * Eviction is deferred, not immediate: Express does not cancel the handler
210
- * on abort, so its spans are usually still closing when `close` fires, and
211
- * each one ending after the snapshot is gone would log an error. The grace
212
- * period lets the handler drain first; `unref` keeps the timer from holding
213
- * a shutting-down process open.
213
+ * on abort, so its spans are usually still closing when `close` fires. The
214
+ * grace period lets the handler drain first; `unref` keeps the timer from
215
+ * holding a shutting-down process open.
214
216
  */
215
217
  evictTraceOnClientAbort(res, traceId) {
216
218
  const response = res;
@@ -195,6 +195,12 @@ export declare class OperationTraceRegistry {
195
195
  private awaitingCompletion;
196
196
  hasSnapshotsAwaitingCompletion(): boolean;
197
197
  pluckSnapshot(traceId: string): Promise<RequestSnapshot | JobSnapshot | undefined>;
198
+ /**
199
+ * One line per trace `pluckSnapshot` gives up on, naming the spans that kept
200
+ * it open. Their late ends are silent: one un-awaited call usually holds a
201
+ * whole chain of spans open, and a line per span said the same thing N times.
202
+ */
203
+ private reportDroppedTrace;
198
204
  /**
199
205
  * Whether `skipSpans` claims a finished operation. Only request snapshots
200
206
  * are asked: a job has no status to be judged by.
@@ -370,7 +370,9 @@ export class OperationTraceRegistry {
370
370
  callerId, error) {
371
371
  const snapshot = this.traceSnapshots.get(traceId);
372
372
  if (!snapshot) {
373
- this.logger.error(`No snapshot found for traceId: "${traceId}". Cannot end trace step for "${className}#${methodKey}".`);
373
+ // The trace was dropped while this span was still open - `pluckSnapshot`
374
+ // gave up on it (and said so, once for the whole trace), or the client
375
+ // aborted. Nothing is left to record it in.
374
376
  return;
375
377
  }
376
378
  snapshot.refsMarkedAsComplete += 1;
@@ -561,6 +563,7 @@ export class OperationTraceRegistry {
561
563
  this.awaitingCompletion -= 1;
562
564
  }
563
565
  if (snapshot.refsCounter > snapshot.refsMarkedAsComplete) {
566
+ this.reportDroppedTrace(traceId, snapshot);
564
567
  cleanup();
565
568
  return undefined;
566
569
  }
@@ -597,6 +600,23 @@ export class OperationTraceRegistry {
597
600
  }
598
601
  return undefined;
599
602
  }
603
+ /**
604
+ * One line per trace `pluckSnapshot` gives up on, naming the spans that kept
605
+ * it open. Their late ends are silent: one un-awaited call usually holds a
606
+ * whole chain of spans open, and a line per span said the same thing N times.
607
+ */
608
+ reportDroppedTrace(traceId, snapshot) {
609
+ const open = [];
610
+ for (const node of this.callerIdsByTraceId.get(traceId)?.values() ?? []) {
611
+ if (node.type === "start") {
612
+ open.push(`"${node.className}#${node.methodKey}"`);
613
+ }
614
+ }
615
+ const operation = ("operationId" in snapshot && snapshot.operationId) ||
616
+ ("name" in snapshot && snapshot.name) ||
617
+ traceId;
618
+ this.logger.warn(`Trace "${traceId}" (${operation}) was not reported: ${open.join(", ") || "a span"} had not ended ${SNAPSHOT_COMPLETION_TIMEOUT_MS}ms after the operation did. Some of your async operations were not awaited and outlived the operation that started them. Ignore if this is the desired behavior.`);
619
+ }
600
620
  /**
601
621
  * Whether `skipSpans` claims a finished operation. Only request snapshots
602
622
  * are asked: a job has no status to be judged by.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.3.16",
3
+ "version": "0.3.17",
4
4
  "description": "Observability and APM agent for NestJS - traces, runtime metrics and custom metrics",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://observe.nestjs.com",