@nestjs/observe 0.3.8 → 0.3.9

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.
@@ -5,6 +5,8 @@ import { CALLER_METADATA_KEY, TRACE_REGISTRY_KEY, } from "../observe.constants.j
5
5
  * is grouped under this label.
6
6
  */
7
7
  const STANDALONE_FUNCTION_LABEL = "Function";
8
+ /** Class name for an instance that has no constructor function. */
9
+ const FALLBACK_CLASS_NAME = "Object";
8
10
  export function createInstanceDecorator(als, operationTraceRegistry, options) {
9
11
  /**
10
12
  * Builds the tracing wrapper shared by methods and standalone functions.
@@ -53,13 +55,16 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
53
55
  operationTraceRegistry.internalEndTraceStep(requestId, spanId, className, methodName, newStepId);
54
56
  return res;
55
57
  };
56
- const onError = (err) => {
58
+ const recordError = (err) => {
57
59
  // Before the registry records the step: `internalEndTraceStep` copies
58
60
  // `err.stack` into the span payload (and, on a root span, into the
59
61
  // snapshot that becomes the error group's sample), so relabelling
60
62
  // afterwards would leave that copy carrying this frame's `Proxy.`.
61
63
  relabelProxyFrame(err, className, methodName);
62
64
  operationTraceRegistry.internalEndTraceStep(requestId, spanId, className, methodName, newStepId, err);
65
+ };
66
+ const onError = (err) => {
67
+ recordError(err);
63
68
  // Re-throw the error to maintain original behavior
64
69
  throw err;
65
70
  };
@@ -70,7 +75,32 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
70
75
  try {
71
76
  const result = call(this, args);
72
77
  if (result instanceof Promise) {
73
- return result.then(onReturnValue).catch(onError);
78
+ // Closing the span is a side effect: a registry failure must
79
+ // leave the caller with the value or rejection it would have
80
+ // seen without the span.
81
+ const settled = (value) => {
82
+ ignoreFailure(() => onReturnValue(value));
83
+ return value;
84
+ };
85
+ const failed = (err) => {
86
+ ignoreFailure(() => recordError(err));
87
+ throw err;
88
+ };
89
+ // A plain native promise is swapped for a chained one, just as
90
+ // the caller's own `.then` would: a rejection the caller drops
91
+ // still raises `unhandledRejection`, as without the span.
92
+ if (isPlainPromise(result)) {
93
+ return result.then(settled, failed);
94
+ }
95
+ // A promise carrying more than its value (`json()` on a `got`
96
+ // request, `kill()` on an `execa` child, a subclass and its own
97
+ // members) is handed back as-is, or the caller loses all that.
98
+ // It is watched on a side branch instead, the way
99
+ // `OutgoingSpanRecorder.endWhenSettled` watches outgoing calls.
100
+ // Watching marks it handled, so a rejection the caller drops no
101
+ // longer raises `unhandledRejection`; the span still records it.
102
+ result.then(settled, (err) => ignoreFailure(() => recordError(err)));
103
+ return result;
74
104
  }
75
105
  return onReturnValue(result);
76
106
  }
@@ -176,7 +206,7 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
176
206
  if (!shouldProxy) {
177
207
  return Reflect.get(target, prop);
178
208
  }
179
- const className = target.constructor.name;
209
+ const className = classNameOf(target);
180
210
  const methodName = String(prop);
181
211
  const frameName = `${className}.${methodName}`;
182
212
  // When "originalMethod" is already proxied, return the cached version
@@ -251,6 +281,28 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
251
281
  return instance;
252
282
  };
253
283
  }
284
+ /**
285
+ * Whether a promise is a bare native one: no subclass, no members of its own.
286
+ * Such a promise carries nothing but its value, so a chained promise can stand
287
+ * in for it without the caller noticing.
288
+ *
289
+ * Symbol keys do not count: before Node 24, `AsyncLocalStorage` stamps its
290
+ * own (`async_id_symbol`, `kResourceStore`, ...) on every promise created in
291
+ * a context, which every traced call runs in.
292
+ */
293
+ function isPlainPromise(promise) {
294
+ return (Object.getPrototypeOf(promise) === Promise.prototype &&
295
+ Object.getOwnPropertyNames(promise).length === 0);
296
+ }
297
+ /** Runs span bookkeeping whose failure must not reach the caller. */
298
+ function ignoreFailure(fn) {
299
+ try {
300
+ fn();
301
+ }
302
+ catch {
303
+ // The span is a side effect of the call, not part of its outcome.
304
+ }
305
+ }
254
306
  /**
255
307
  * Renames a function so that V8 prints `name` for the stack frames it appears
256
308
  * in. The `name` property is what the stack serializer reads, so redefining it
@@ -442,6 +494,15 @@ function declaresPrivateMembers(ctor) {
442
494
  return false;
443
495
  }
444
496
  }
497
+ /**
498
+ * The class name that labels an instance's spans. A null-prototype object or
499
+ * an ES module namespace has no `constructor`, and some objects hold a
500
+ * non-function there. Both fall back to `Object`.
501
+ */
502
+ function classNameOf(instance) {
503
+ const ctor = Reflect.get(instance, "constructor");
504
+ return typeof ctor === "function" ? ctor.name : FALLBACK_CLASS_NAME;
505
+ }
445
506
  function generateSpanId(instance, methodKey) {
446
- return `${instance.constructor.name}#${methodKey}`;
507
+ return `${classNameOf(instance)}#${methodKey}`;
447
508
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
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",
@@ -118,9 +118,9 @@
118
118
  "devDependencies": {
119
119
  "@apollo/server": "5.5.1",
120
120
  "@as-integrations/express5": "1.1.2",
121
- "@commitlint/cli": "21.2.2",
122
- "@commitlint/config-angular": "21.2.2",
123
- "@grpc/grpc-js": "1.14.4",
121
+ "@commitlint/cli": "21.2.3",
122
+ "@commitlint/config-angular": "21.2.3",
123
+ "@grpc/grpc-js": "1.14.6",
124
124
  "@grpc/proto-loader": "0.8.1",
125
125
  "@mikro-orm/core": "7.2.0",
126
126
  "@mikro-orm/mysql": "7.2.0",
@@ -152,7 +152,7 @@
152
152
  "graphql": "16.14.2",
153
153
  "husky": "9.1.7",
154
154
  "ioredis": "6.0.0",
155
- "lint-staged": "17.3.0",
155
+ "lint-staged": "17.6.0",
156
156
  "mongodb": "7.6.0",
157
157
  "mongoose": "9.10.1",
158
158
  "mysql2": "3.24.4",