@nestjs/observe 0.3.25 → 0.3.26
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 +17 -0
- package/dist/agent/detached-observe-worker.js +50 -6
- package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
- package/dist/agent/observe-agent.shared-buffer.js +196 -19
- package/dist/agent/observe-agent.worker.d.ts +22 -2
- package/dist/agent/observe-agent.worker.js +43 -7
- package/dist/agent/telemetry-wire-contract.js +7 -1
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +28 -3
- package/dist/cli/observe-cli.js +1 -19
- package/dist/cli/open-url.d.ts +27 -0
- package/dist/cli/open-url.js +63 -0
- package/dist/cli/project-files.d.ts +42 -1
- package/dist/cli/project-files.js +146 -7
- package/dist/cli/project-setup.d.ts +6 -0
- package/dist/cli/project-setup.js +14 -1
- package/dist/cli/wire-application.js +6 -0
- package/dist/custom-metrics/counter.d.ts +13 -2
- package/dist/custom-metrics/counter.js +33 -16
- package/dist/custom-metrics/finite-value.util.d.ts +14 -0
- package/dist/custom-metrics/finite-value.util.js +33 -1
- package/dist/custom-metrics/gauge.d.ts +2 -0
- package/dist/custom-metrics/gauge.js +41 -17
- package/dist/custom-metrics/label-values.util.d.ts +37 -0
- package/dist/custom-metrics/label-values.util.js +51 -0
- package/dist/custom-metrics/summary.d.ts +23 -0
- package/dist/custom-metrics/summary.js +73 -2
- package/dist/instrument/create-instance-decorator.instrument.js +158 -96
- package/dist/interfaces/observe-options.interface.d.ts +3 -1
- package/dist/objectives/objectives.registry.js +12 -0
- package/dist/observe.module.js +119 -3
- package/dist/otlp/otlp-encoders.js +22 -5
- package/dist/otlp/otlp-span.mapper.js +7 -3
- package/dist/otlp/otlp-trace-exporter.service.js +63 -5
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +13 -6
- package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
- package/dist/outgoing/llm/llm-providers.d.ts +16 -4
- package/dist/outgoing/llm/llm-providers.js +72 -22
- package/dist/outgoing/llm/llm-usage.d.ts +11 -6
- package/dist/outgoing/llm/llm-usage.js +72 -25
- package/dist/outgoing/mssql.integration.js +16 -2
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +15 -3
- package/dist/outgoing/outgoing-span.recorder.js +17 -3
- package/dist/outgoing/pg.integration.d.ts +3 -2
- package/dist/outgoing/pg.integration.js +39 -11
- package/dist/outgoing/sql-statement.util.js +106 -14
- package/dist/outgoing/undici.integration.d.ts +13 -0
- package/dist/outgoing/undici.integration.js +68 -0
- package/dist/protocols/bull-observe-agent.service.js +2 -12
- package/dist/protocols/deferred-trace-completion.d.ts +9 -1
- package/dist/protocols/deferred-trace-completion.js +36 -6
- package/dist/protocols/graphql-observe-agent.service.d.ts +36 -5
- package/dist/protocols/graphql-observe-agent.service.js +166 -5
- package/dist/protocols/graphql-operation-parser.js +72 -18
- package/dist/protocols/http-observe-agent.service.d.ts +43 -9
- package/dist/protocols/http-observe-agent.service.js +177 -59
- package/dist/protocols/job-trace-runner.d.ts +35 -3
- package/dist/protocols/job-trace-runner.js +126 -29
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +50 -8
- package/dist/protocols/rpc-observe-agent.service.js +187 -47
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +226 -20
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +3 -4
- package/dist/services/operation-trace.registry.d.ts +45 -1
- package/dist/services/operation-trace.registry.js +418 -35
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +288 -51
- package/dist/services/tracer.service.d.ts +23 -0
- package/dist/services/tracer.service.js +91 -5
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +100 -12
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +14 -0
- package/dist/utils/log-line.parser.js +7 -3
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +512 -61
- package/dist/utils/operation-state.util.d.ts +39 -0
- package/dist/utils/operation-state.util.js +50 -0
- package/dist/utils/redact-url-query.d.ts +26 -4
- package/dist/utils/redact-url-query.js +184 -32
- package/dist/utils/sensitive-keys.d.ts +4 -1
- package/dist/utils/sensitive-keys.js +48 -10
- package/dist/utils/shared-hook-slot.util.d.ts +48 -0
- package/dist/utils/shared-hook-slot.util.js +103 -0
- package/dist/utils/source-context.util.d.ts +8 -0
- package/dist/utils/source-context.util.js +88 -24
- package/dist/utils/source-map-resolver.util.js +8 -2
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -13,16 +13,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
var WsObserveAgentService_1;
|
|
14
14
|
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
15
|
import { AsyncLocalStorage } from "async_hooks";
|
|
16
|
+
import { isObservable, Observable } from "rxjs";
|
|
16
17
|
import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.js";
|
|
17
18
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
18
19
|
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
19
20
|
import { TraceSamplerService } from "../services/trace-sampler.service.js";
|
|
20
21
|
import { describePeerLoadError, loadOptionalPeer, } from "../utils/optional-peer.util.js";
|
|
22
|
+
import { endOperation, openOperation } from "../utils/operation-state.util.js";
|
|
21
23
|
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
22
24
|
import { deferTraceCompletion, getUserIdSafely, setAttributesSafely, shouldIgnoreSafely, } from "./deferred-trace-completion.js";
|
|
23
25
|
const ORIGINAL_CREATE = Symbol.for("nestjs.observe.ws.create");
|
|
24
26
|
/** `@nestjs/websockets`' MESSAGE_METADATA - what `@SubscribeMessage()` writes. */
|
|
25
27
|
const MESSAGE_METADATA = "message";
|
|
28
|
+
/** `ws`' `WebSocket.CLOSED`. */
|
|
29
|
+
const WS_CLOSED = 3;
|
|
30
|
+
const MESSAGE_STATE = Symbol("nestjs.observe.ws.message");
|
|
31
|
+
const noop = () => undefined;
|
|
26
32
|
/**
|
|
27
33
|
* Gateway message tracing for `@nestjs/websockets`.
|
|
28
34
|
*
|
|
@@ -30,7 +36,8 @@ const MESSAGE_METADATA = "message";
|
|
|
30
36
|
* processing hooks, so the one place every `@SubscribeMessage()` handler
|
|
31
37
|
* passes through is patched instead: `WsContextCreator.create`, which builds
|
|
32
38
|
* the guards-pipes-interceptors-handler chain for a message. Wrapping what it
|
|
33
|
-
* returns
|
|
39
|
+
* returns, and subscribing to the stream it resolves to in the same store,
|
|
40
|
+
* puts the whole chain inside the trace, whichever platform adapter -
|
|
34
41
|
* socket.io, ws - delivers the message.
|
|
35
42
|
*/
|
|
36
43
|
let WsObserveAgentService = WsObserveAgentService_1 = class WsObserveAgentService {
|
|
@@ -40,6 +47,8 @@ let WsObserveAgentService = WsObserveAgentService_1 = class WsObserveAgentServic
|
|
|
40
47
|
traceSamplerService;
|
|
41
48
|
asyncLocalStorage;
|
|
42
49
|
logger = new Logger(WsObserveAgentService_1.name);
|
|
50
|
+
tracesByClient = new WeakMap();
|
|
51
|
+
standIns = new WeakMap();
|
|
43
52
|
constructor(observeAgentSharedBuffer, options, operationTraceRegistry, traceSamplerService, asyncLocalStorage) {
|
|
44
53
|
this.observeAgentSharedBuffer = observeAgentSharedBuffer;
|
|
45
54
|
this.options = options;
|
|
@@ -76,42 +85,128 @@ let WsObserveAgentService = WsObserveAgentService_1 = class WsObserveAgentServic
|
|
|
76
85
|
// wrapping it again.
|
|
77
86
|
prototype[ORIGINAL_CREATE] = originalCreate;
|
|
78
87
|
const trace = this.traceMessage.bind(this);
|
|
79
|
-
const
|
|
88
|
+
const standInFor = this.standInFor.bind(this);
|
|
80
89
|
prototype.create = function (instance, callback, ...rest) {
|
|
81
90
|
const handler = originalCreate.call(this, instance,
|
|
82
91
|
// rest: [moduleKey, methodName, ...]
|
|
83
|
-
|
|
92
|
+
standInFor(instance, callback, rest[1]), ...rest);
|
|
84
93
|
const pattern = Reflect.getMetadata?.(MESSAGE_METADATA, callback);
|
|
85
94
|
const gateway = instance?.constructor?.name || "Gateway";
|
|
86
95
|
return (...args) => trace({ gateway, pattern: String(pattern), client: args[0], data: args[1] }, () => handler(...args));
|
|
87
96
|
};
|
|
88
97
|
}
|
|
89
98
|
/**
|
|
99
|
+
* The handler Nest is given for a `@SubscribeMessage()` method: the same
|
|
100
|
+
* method, called through the instance, telling the message when it runs.
|
|
101
|
+
*
|
|
90
102
|
* Nest reads a gateway's handlers off the class prototype and applies them
|
|
91
103
|
* to the instance, which steps around the instrumentation proxy - the one
|
|
92
104
|
* method of the whole chain that would go unrecorded is the handler itself,
|
|
93
105
|
* and a handler that throws before calling anything leaves no span at all.
|
|
106
|
+
* The stand-in calls the same method through the instance instead.
|
|
107
|
+
*
|
|
108
|
+
* Behind an interceptor, the chain is cold: Nest's proxy resolves to a
|
|
109
|
+
* stream once the guards have run, and the interceptors, the pipes and the
|
|
110
|
+
* handler run only when the adapter subscribes to it. The stand-in marks
|
|
111
|
+
* the message's state when it is called, so the trace can tell which of
|
|
112
|
+
* the two it was handed, and ends that trace when the handler returns a
|
|
113
|
+
* stream of its own.
|
|
94
114
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
115
|
+
* Guards, pipes, filters and the message pattern are all looked up as
|
|
116
|
+
* metadata on the callback, so that metadata is carried over - once per
|
|
117
|
+
* method, since on a request-scoped path `create` runs for every message.
|
|
98
118
|
*/
|
|
99
|
-
|
|
100
|
-
if (typeof
|
|
119
|
+
standInFor(instance, callback, methodName) {
|
|
120
|
+
if (typeof callback !== "function") {
|
|
101
121
|
return callback;
|
|
102
122
|
}
|
|
103
|
-
const instrumented =
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
const instrumented = typeof methodName === "string"
|
|
124
|
+
? instance?.[methodName]
|
|
125
|
+
: undefined;
|
|
126
|
+
const target = (typeof instrumented === "function" ? instrumented : callback);
|
|
127
|
+
const cached = this.standIns.get(target);
|
|
128
|
+
if (cached) {
|
|
129
|
+
return cached;
|
|
106
130
|
}
|
|
107
|
-
const
|
|
108
|
-
|
|
131
|
+
const als = this.asyncLocalStorage;
|
|
132
|
+
const standIn = function (...args) {
|
|
133
|
+
const state = als.getStore()?.get(MESSAGE_STATE);
|
|
134
|
+
if (state) {
|
|
135
|
+
state.handlerCalled = true;
|
|
136
|
+
}
|
|
137
|
+
const result = target.apply(this, args);
|
|
138
|
+
if (state?.onStreamReturned && isObservable(result)) {
|
|
139
|
+
state.onStreamReturned();
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
109
142
|
};
|
|
110
|
-
Object.defineProperty(
|
|
143
|
+
Object.defineProperty(standIn, "name", { value: callback.name });
|
|
111
144
|
for (const key of Reflect.getMetadataKeys?.(callback) ?? []) {
|
|
112
|
-
Reflect.defineMetadata(key, Reflect.getMetadata(key, callback),
|
|
145
|
+
Reflect.defineMetadata(key, Reflect.getMetadata(key, callback), standIn);
|
|
146
|
+
}
|
|
147
|
+
this.standIns.set(target, standIn);
|
|
148
|
+
return standIn;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Drops a message's trace when its client goes away before the handler
|
|
152
|
+
* settles, and returns what stops watching for that once the trace ends.
|
|
153
|
+
*
|
|
154
|
+
* A handler waiting on its client - an acknowledgement without a timeout,
|
|
155
|
+
* the client's next frame - never settles once the client has gone, and
|
|
156
|
+
* nothing else would end its trace: every client that hung up on one left
|
|
157
|
+
* its snapshot registered for the life of the process, and a client that
|
|
158
|
+
* reconnects can leave as many as it likes.
|
|
159
|
+
*
|
|
160
|
+
* One listener per client, however many messages it has in flight, on the
|
|
161
|
+
* events Nest's adapters stop a client's messages at: socket.io's
|
|
162
|
+
* `disconnect`, `ws`' `close`. Dropped at once, as the HTTP agent drops an
|
|
163
|
+
* aborted request: an abandoned trace is never reported, and the registry
|
|
164
|
+
* lets a handler that outlives its client end its spans on a trace that is
|
|
165
|
+
* gone. A client that is no event emitter - a custom adapter's - is not
|
|
166
|
+
* watched.
|
|
167
|
+
*/
|
|
168
|
+
abandonWhenClientLeaves(client, abandon) {
|
|
169
|
+
const socket = client;
|
|
170
|
+
if (typeof socket?.once !== "function") {
|
|
171
|
+
return noop;
|
|
172
|
+
}
|
|
173
|
+
let traces = this.tracesByClient.get(socket);
|
|
174
|
+
if (!traces) {
|
|
175
|
+
const watched = { open: new Set(), gone: false };
|
|
176
|
+
try {
|
|
177
|
+
socket.once(typeof socket.disconnect === "function" ? "disconnect" : "close", () => {
|
|
178
|
+
watched.gone = true;
|
|
179
|
+
// One macrotask later, not at once: a handler that ends its own
|
|
180
|
+
// client's connection - a logout, a kick - is still running when
|
|
181
|
+
// socket.io emits this, from inside `disconnect()`, and would
|
|
182
|
+
// lose the trace it is about to finish. A trace that settles
|
|
183
|
+
// meanwhile has left `open`; the rest are still dropped, a tick
|
|
184
|
+
// after the client left.
|
|
185
|
+
setImmediate(() => {
|
|
186
|
+
for (const abandonTrace of watched.open) {
|
|
187
|
+
abandonTrace();
|
|
188
|
+
}
|
|
189
|
+
watched.open.clear();
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return noop;
|
|
195
|
+
}
|
|
196
|
+
this.tracesByClient.set(socket, watched);
|
|
197
|
+
traces = watched;
|
|
113
198
|
}
|
|
114
|
-
|
|
199
|
+
// Gone before the message was traced: a request-scoped gateway resolves
|
|
200
|
+
// its instance before the chain runs, and the client can leave meanwhile.
|
|
201
|
+
if (traces.gone ||
|
|
202
|
+
socket.disconnected === true ||
|
|
203
|
+
socket.readyState === WS_CLOSED) {
|
|
204
|
+
abandon();
|
|
205
|
+
return noop;
|
|
206
|
+
}
|
|
207
|
+
const open = traces.open;
|
|
208
|
+
open.add(abandon);
|
|
209
|
+
return () => void open.delete(abandon);
|
|
115
210
|
}
|
|
116
211
|
traceMessage(message, invoke) {
|
|
117
212
|
// Always a store of its own: whatever context the socket's events happen
|
|
@@ -121,20 +216,73 @@ let WsObserveAgentService = WsObserveAgentService_1 = class WsObserveAgentServic
|
|
|
121
216
|
return this.asyncLocalStorage.run(store, () => {
|
|
122
217
|
const traceId = uuidv7();
|
|
123
218
|
store.set(this.options.traceIdKey, traceId);
|
|
219
|
+
// Ended with the message's trace, or as an untraced message settles:
|
|
220
|
+
// see `liveTraceId`.
|
|
221
|
+
const operation = openOperation(store);
|
|
124
222
|
setAttributesSafely(this.options.ws?.setAttributes, [message], this.options.ws, store, this.logger, "ws");
|
|
125
223
|
if (shouldIgnoreSafely(this.options.ws?.ignore, [message], this.options.ws, this.logger, "ws") ||
|
|
126
224
|
!this.traceSamplerService.shouldCapture("ws", {
|
|
127
225
|
gateway: message.gateway,
|
|
128
226
|
pattern: message.pattern,
|
|
129
227
|
})) {
|
|
130
|
-
|
|
228
|
+
// No trace to end, but the store - and the trace id it carries for
|
|
229
|
+
// logs - still has to reach a chain the adapter subscribes to later.
|
|
230
|
+
const end = () => endOperation(operation);
|
|
231
|
+
const settleUntraced = (value) => {
|
|
232
|
+
if (!isObservable(value)) {
|
|
233
|
+
end();
|
|
234
|
+
return value;
|
|
235
|
+
}
|
|
236
|
+
return this.subscribeInStore(store, value, end, end);
|
|
237
|
+
};
|
|
238
|
+
let untraced;
|
|
239
|
+
try {
|
|
240
|
+
untraced = invoke();
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
end();
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
return untraced instanceof Promise
|
|
247
|
+
? untraced.then(settleUntraced, (error) => {
|
|
248
|
+
end();
|
|
249
|
+
throw error;
|
|
250
|
+
})
|
|
251
|
+
: settleUntraced(untraced);
|
|
131
252
|
}
|
|
132
253
|
this.operationTraceRegistry.startTrace(traceId, {
|
|
133
254
|
protocol: "ws",
|
|
134
255
|
operationId: `${message.gateway}:${message.pattern}`,
|
|
135
256
|
tags: this.options.ws?.tags,
|
|
136
257
|
});
|
|
258
|
+
const state = { handlerCalled: false };
|
|
259
|
+
store.set(MESSAGE_STATE, state);
|
|
260
|
+
// Ended or dropped once, whichever comes first: the handler settling,
|
|
261
|
+
// or its client leaving. Closing also clears the end the state holds,
|
|
262
|
+
// and with it the message and its client: span copies of the store can
|
|
263
|
+
// outlive the message.
|
|
264
|
+
let open = true;
|
|
265
|
+
let unwatch = noop;
|
|
266
|
+
const close = () => {
|
|
267
|
+
if (!open) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
open = false;
|
|
271
|
+
unwatch();
|
|
272
|
+
state.onStreamReturned = undefined;
|
|
273
|
+
endOperation(operation);
|
|
274
|
+
return true;
|
|
275
|
+
};
|
|
276
|
+
const abandon = () => {
|
|
277
|
+
if (close()) {
|
|
278
|
+
this.operationTraceRegistry.abandonTrace(traceId);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
unwatch = this.abandonWhenClientLeaves(message.client, abandon);
|
|
137
282
|
const endTrace = () => {
|
|
283
|
+
if (!close()) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
138
286
|
deferTraceCompletion(async () => {
|
|
139
287
|
this.operationTraceRegistry.endTrace(traceId, {
|
|
140
288
|
userId: getUserIdSafely(this.options.ws?.getUserId, [message], this.options.ws, this.logger, "ws"),
|
|
@@ -158,11 +306,69 @@ let WsObserveAgentService = WsObserveAgentService_1 = class WsObserveAgentServic
|
|
|
158
306
|
endTrace();
|
|
159
307
|
throw error;
|
|
160
308
|
}
|
|
309
|
+
const settle = (value) => {
|
|
310
|
+
if (!isObservable(value)) {
|
|
311
|
+
endTrace();
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
if (state.handlerCalled || !open) {
|
|
315
|
+
// The handler has run and returned a stream - the trace covers the
|
|
316
|
+
// handler, not the stream - or the trace is already gone.
|
|
317
|
+
endTrace();
|
|
318
|
+
return this.subscribeInStore(store, value);
|
|
319
|
+
}
|
|
320
|
+
// Behind an interceptor only the guards have run, and the rest of the
|
|
321
|
+
// chain runs once the adapter subscribes. The trace ends with the
|
|
322
|
+
// handler's stream, or with the chain's first word; a chain torn
|
|
323
|
+
// down before either was given up on by the adapter.
|
|
324
|
+
state.onStreamReturned = endTrace;
|
|
325
|
+
return this.subscribeInStore(store, value, endTrace, abandon);
|
|
326
|
+
};
|
|
161
327
|
if (result instanceof Promise) {
|
|
162
|
-
return result.
|
|
328
|
+
return result.then(settle, (error) => {
|
|
329
|
+
endTrace();
|
|
330
|
+
throw error;
|
|
331
|
+
});
|
|
163
332
|
}
|
|
164
|
-
|
|
165
|
-
|
|
333
|
+
return settle(result);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* The chain's stream, subscribed in the message's store.
|
|
338
|
+
*
|
|
339
|
+
* Nest's adapters subscribe to what the chain resolves to from the socket's
|
|
340
|
+
* own events, outside the store the message was started in. Behind an
|
|
341
|
+
* interceptor that stream is all of the chain past the guards - the
|
|
342
|
+
* interceptors, the pipes, the handler, the exception filter - and for a
|
|
343
|
+
* handler that returns a stream, the stream and the filter its error goes
|
|
344
|
+
* to. Run without the store, they were missing from the trace, and
|
|
345
|
+
* `TracerService` failed in them.
|
|
346
|
+
*
|
|
347
|
+
* `end` is called at the stream's first value, error or completion, and
|
|
348
|
+
* `abandon` when the subscription is torn down before any: Nest's adapters
|
|
349
|
+
* unsubscribe a client's messages when it disconnects. Both are no-ops once
|
|
350
|
+
* either has run.
|
|
351
|
+
*/
|
|
352
|
+
subscribeInStore(store, chain, end, abandon) {
|
|
353
|
+
return new Observable((subscriber) => {
|
|
354
|
+
const subscription = this.asyncLocalStorage.run(store, () => chain.subscribe({
|
|
355
|
+
next: (value) => {
|
|
356
|
+
end?.();
|
|
357
|
+
subscriber.next(value);
|
|
358
|
+
},
|
|
359
|
+
error: (error) => {
|
|
360
|
+
end?.();
|
|
361
|
+
subscriber.error(error);
|
|
362
|
+
},
|
|
363
|
+
complete: () => {
|
|
364
|
+
end?.();
|
|
365
|
+
subscriber.complete();
|
|
366
|
+
},
|
|
367
|
+
}));
|
|
368
|
+
if (abandon) {
|
|
369
|
+
subscription.add(abandon);
|
|
370
|
+
}
|
|
371
|
+
return subscription;
|
|
166
372
|
});
|
|
167
373
|
}
|
|
168
374
|
};
|
package/dist/services/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./tracer.service.js";
|
|
2
2
|
export { SPAN_COLLAPSED_TAG } from "./collapse-repeated-spans.util.js";
|
|
3
|
-
export { UNFINISHED_SPANS_TAG } from "./operation-trace.registry.js";
|
|
3
|
+
export { UNFINISHED_SPANS_TAG, UNRECORDED_SPANS_TAG, } from "./operation-trace.registry.js";
|
package/dist/services/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./tracer.service.js";
|
|
2
2
|
export { SPAN_COLLAPSED_TAG } from "./collapse-repeated-spans.util.js";
|
|
3
|
-
export { UNFINISHED_SPANS_TAG } from "./operation-trace.registry.js";
|
|
3
|
+
export { UNFINISHED_SPANS_TAG, UNRECORDED_SPANS_TAG, } from "./operation-trace.registry.js";
|
|
@@ -14,6 +14,7 @@ var LoggerPatcherService_1;
|
|
|
14
14
|
import { ConsoleLogger, Inject, Injectable, Logger, } from "@nestjs/common";
|
|
15
15
|
import { AsyncLocalStorage } from "async_hooks";
|
|
16
16
|
import { OBSERVE_OPTIONS } from "../observe.constants.js";
|
|
17
|
+
import { liveTraceId } from "../utils/operation-state.util.js";
|
|
17
18
|
let LoggerPatcherService = LoggerPatcherService_1 = class LoggerPatcherService {
|
|
18
19
|
asyncLocalStorage;
|
|
19
20
|
options;
|
|
@@ -52,8 +53,7 @@ let LoggerPatcherService = LoggerPatcherService_1 = class LoggerPatcherService {
|
|
|
52
53
|
// landed correctly by accident - `message` in the first slot, the options
|
|
53
54
|
// bag in the second - and the third argument was dropped on the floor.
|
|
54
55
|
message, logOptions) {
|
|
55
|
-
const
|
|
56
|
-
const requestId = store?.get(options.traceIdKey);
|
|
56
|
+
const requestId = liveTraceId(asyncLocalStorage.getStore(), options.traceIdKey);
|
|
57
57
|
const jsonLogObject = originalGetJsonLogObject.call(this, message, logOptions);
|
|
58
58
|
if (requestId) {
|
|
59
59
|
// Not a field Nest models on the returned object, which is the whole
|
|
@@ -69,8 +69,7 @@ let LoggerPatcherService = LoggerPatcherService_1 = class LoggerPatcherService {
|
|
|
69
69
|
// every structured param from text-mode output.
|
|
70
70
|
...args) {
|
|
71
71
|
const output = originalFormatMessage.apply(this, args);
|
|
72
|
-
const
|
|
73
|
-
const requestId = store?.get(options.traceIdKey);
|
|
72
|
+
const requestId = liveTraceId(asyncLocalStorage.getStore(), options.traceIdKey);
|
|
74
73
|
if (!requestId) {
|
|
75
74
|
return output;
|
|
76
75
|
}
|
|
@@ -21,6 +21,12 @@ import { SkipSpansPredicate } from "./skip-spans.util.js";
|
|
|
21
21
|
* collector release has to come first. Absent on a snapshot shipped whole.
|
|
22
22
|
*/
|
|
23
23
|
export declare const UNFINISHED_SPANS_TAG = "observe.unfinishedSpans";
|
|
24
|
+
/**
|
|
25
|
+
* Tag under which a snapshot records how many spans were never opened because
|
|
26
|
+
* it had already opened `MAX_SPANS_PER_TRACE`. A tag for the reason
|
|
27
|
+
* `UNFINISHED_SPANS_TAG` is one. Absent on a snapshot that stayed under it.
|
|
28
|
+
*/
|
|
29
|
+
export declare const UNRECORDED_SPANS_TAG = "observe.unrecordedSpans";
|
|
24
30
|
type TraceId = string;
|
|
25
31
|
export declare class OperationTraceRegistry {
|
|
26
32
|
private readonly als;
|
|
@@ -34,6 +40,17 @@ export declare class OperationTraceRegistry {
|
|
|
34
40
|
* would bury everything else in the log. Full text once, one line after.
|
|
35
41
|
*/
|
|
36
42
|
private hasReportedMissingInstrumentation;
|
|
43
|
+
/** See `reportSpanDepthLimit`. */
|
|
44
|
+
private hasReportedSpanDepthLimit;
|
|
45
|
+
/** See `reportCapturedErrorLimit`. */
|
|
46
|
+
private hasReportedCapturedErrorLimit;
|
|
47
|
+
/** See `countUnrecordedSpan`. */
|
|
48
|
+
private hasReportedSpanLimit;
|
|
49
|
+
/**
|
|
50
|
+
* Root spans of outgoing calls - a query, an HTTP call - that ended with
|
|
51
|
+
* an error. See `endTrace` for what they decide.
|
|
52
|
+
*/
|
|
53
|
+
private readonly failedOutgoingRoots;
|
|
37
54
|
/**
|
|
38
55
|
* Applied to every tree as it is plucked. Defaults are live from
|
|
39
56
|
* construction rather than waiting for `configureSpanCollapse`, so a
|
|
@@ -172,15 +189,31 @@ export declare class OperationTraceRegistry {
|
|
|
172
189
|
* ending after its caller did, is already tolerated in
|
|
173
190
|
* `internalEndTraceStep`. Auto and manual spans both attach through here, so
|
|
174
191
|
* they share the tolerance.
|
|
192
|
+
*
|
|
193
|
+
* A node past `MAX_SPAN_DEPTH` is kept out of its caller's children, and so
|
|
194
|
+
* out of the shipped tree, and everything opened under it goes the same
|
|
195
|
+
* way. Nothing else about it changes: it still has its parent, so an error
|
|
196
|
+
* it ends with is not taken for the operation's own, and it is still
|
|
197
|
+
* registered and counted, so the calls under it find their caller and the
|
|
198
|
+
* trace completes when it would have.
|
|
175
199
|
*/
|
|
176
200
|
private attachToCaller;
|
|
201
|
+
/**
|
|
202
|
+
* Once for the process: a chain that deep comes from one recursive method,
|
|
203
|
+
* which recurses that deep on every request it is given the same input.
|
|
204
|
+
* Flagged before the line is written, which an instrumented logger records
|
|
205
|
+
* as one more span of the same chain.
|
|
206
|
+
*/
|
|
207
|
+
private reportSpanDepthLimit;
|
|
177
208
|
internalEndTraceStep(traceId: TraceId,
|
|
178
209
|
/**
|
|
179
210
|
* Diagnostic label only - it names the span in the two warnings below and
|
|
180
211
|
* is read nowhere else. Absent for a step that never opened, which is the
|
|
181
212
|
* same condition `callerId` reports.
|
|
182
213
|
*/
|
|
183
|
-
spanId: string | undefined, className: string, methodKey: string, callerId: string | undefined, error?: Error | string | object
|
|
214
|
+
spanId: string | undefined, className: string, methodKey: string, callerId: string | undefined, error?: Error | string | object,
|
|
215
|
+
/** The span is an outgoing call's, opened by `OutgoingSpanRecorder`. */
|
|
216
|
+
outgoing?: boolean): void;
|
|
184
217
|
addRouteMetadataToTrace(requestId: string, requestMethod: RequestMethod, path: string): void;
|
|
185
218
|
setOperationId(traceId: TraceId, operationId: string): void;
|
|
186
219
|
/**
|
|
@@ -222,6 +255,17 @@ export declare class OperationTraceRegistry {
|
|
|
222
255
|
* reader could look it up.
|
|
223
256
|
*/
|
|
224
257
|
reportedTraceId?: string): void;
|
|
258
|
+
/**
|
|
259
|
+
* Once for the process: the operation that gets here does so on every
|
|
260
|
+
* request it is given the same input.
|
|
261
|
+
*/
|
|
262
|
+
private reportCapturedErrorLimit;
|
|
263
|
+
/**
|
|
264
|
+
* Counts a span left unopened by `MAX_SPANS_PER_TRACE`. Warned once for
|
|
265
|
+
* the process: an operation that gets there does so on every request it is
|
|
266
|
+
* given the same input.
|
|
267
|
+
*/
|
|
268
|
+
private countUnrecordedSpan;
|
|
225
269
|
/**
|
|
226
270
|
* Reports the trace even if it ends with no span recorded under it.
|
|
227
271
|
*
|