@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
|
@@ -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 RpcObserveAgentService_1;
|
|
14
|
-
import { Inject, Injectable, Logger
|
|
14
|
+
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
15
|
import { ModulesContainer } from "@nestjs/core";
|
|
16
16
|
import { AsyncLocalStorage } from "async_hooks";
|
|
17
17
|
import { ObserveAgentSharedBuffer } from "../agent/observe-agent.shared-buffer.js";
|
|
@@ -21,8 +21,10 @@ import { OBSERVE_OPTIONS, TRACE_CONTEXT_KEY, TRACE_REGISTRY_KEY, } from "../obse
|
|
|
21
21
|
import { handedOnTraceContext } from "../utils/trace-context.util.js";
|
|
22
22
|
import { continueHandedOnTrace } from "../utils/trace-handoff.util.js";
|
|
23
23
|
import { uuidv7 } from "../utils/uuid-v7.util.js";
|
|
24
|
+
import { setAgentHook } from "../utils/shared-hook-slot.util.js";
|
|
24
25
|
import { describePeerLoadError, loadOptionalPeer, } from "../utils/optional-peer.util.js";
|
|
25
26
|
import { deferTraceCompletion, generateTraceIdSafely, getUserIdSafely, setAttributesSafely, shouldIgnoreSafely, } from "./deferred-trace-completion.js";
|
|
27
|
+
import { endOperation, OPERATION_STATE_KEY, openOperation, } from "../utils/operation-state.util.js";
|
|
26
28
|
let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentService {
|
|
27
29
|
asyncLocalStorage;
|
|
28
30
|
options;
|
|
@@ -33,11 +35,22 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
33
35
|
logger = new Logger(RpcObserveAgentService_1.name);
|
|
34
36
|
rpcTargetAddedSubscription;
|
|
35
37
|
/**
|
|
36
|
-
* Assigned in
|
|
38
|
+
* Assigned in the constructor. Every use below is reached only through the
|
|
37
39
|
* hooks registered there, after a successful load - a service without the
|
|
38
40
|
* package has no RPC target to hook.
|
|
39
41
|
*/
|
|
40
42
|
microservices;
|
|
43
|
+
/**
|
|
44
|
+
* The registry key each operation was started under, by the context Nest
|
|
45
|
+
* hands its end hook. That hook cannot ask the async store: Nest runs it
|
|
46
|
+
* from the teardown of the handler's reply stream, in the async context of
|
|
47
|
+
* whatever ended the stream - a TCP socket's `close`, a gRPC cancellation,
|
|
48
|
+
* a subject another message's handler emitted into. As
|
|
49
|
+
* `HttpObserveAgentService` keeps its keys by request.
|
|
50
|
+
*/
|
|
51
|
+
registryKeys = new WeakMap();
|
|
52
|
+
/** The operation each context was started for - see `rememberOperation`. */
|
|
53
|
+
operations = new WeakMap();
|
|
41
54
|
constructor(asyncLocalStorage, options, modulesContainer, operationTraceRegistry, observeAgentSharedBuffer, traceSamplerService) {
|
|
42
55
|
this.asyncLocalStorage = asyncLocalStorage;
|
|
43
56
|
this.options = options;
|
|
@@ -45,15 +58,23 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
45
58
|
this.operationTraceRegistry = operationTraceRegistry;
|
|
46
59
|
this.observeAgentSharedBuffer = observeAgentSharedBuffer;
|
|
47
60
|
this.traceSamplerService = traceSamplerService;
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
// From the constructor, while Nest builds the providers: Nest announces
|
|
62
|
+
// a server when it constructs the microservice around it, inside
|
|
63
|
+
// `createMicroservice()` or `connectMicroservice()`, so the hooks are in
|
|
64
|
+
// place before the application is handed the server and can set hooks
|
|
65
|
+
// of its own on it (`getTransportServer()`), which then run beside them.
|
|
50
66
|
this.microservices = this.loadMicroservices();
|
|
51
67
|
if (!this.microservices) {
|
|
52
68
|
return;
|
|
53
69
|
}
|
|
54
|
-
|
|
55
|
-
.
|
|
56
|
-
|
|
70
|
+
try {
|
|
71
|
+
this.rpcTargetAddedSubscription = this.modulesContainer
|
|
72
|
+
.getRpcTargetRegistry?.()
|
|
73
|
+
.subscribe((target) => this.registerRpcHooks(target));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
this.logger.warn(`RPC operations will not be instrumented: the microservice registry could not be subscribed to: ${describePeerLoadError(error)}`);
|
|
77
|
+
}
|
|
57
78
|
}
|
|
58
79
|
onModuleDestroy() {
|
|
59
80
|
if (this.rpcTargetAddedSubscription) {
|
|
@@ -63,9 +84,9 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
63
84
|
/**
|
|
64
85
|
* Loads `@nestjs/microservices` without a static import, so a service that
|
|
65
86
|
* exposes no microservice need not install the package. Loaded here rather
|
|
66
|
-
* than lazily because the target registry is subscribed from
|
|
67
|
-
* and a dynamic `import()` would resolve after a target may
|
|
68
|
-
* been announced.
|
|
87
|
+
* than lazily because the target registry is subscribed from the
|
|
88
|
+
* constructor, and a dynamic `import()` would resolve after a target may
|
|
89
|
+
* already have been announced.
|
|
69
90
|
*/
|
|
70
91
|
loadMicroservices() {
|
|
71
92
|
const result = loadOptionalPeer("@nestjs/microservices");
|
|
@@ -82,20 +103,24 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
82
103
|
}
|
|
83
104
|
return result.module;
|
|
84
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Sets the agent's processing hooks on a server, beside any the application
|
|
108
|
+
* sets on it - see `composeStartHook`.
|
|
109
|
+
*/
|
|
85
110
|
registerRpcHooks(target) {
|
|
86
111
|
// Return `done()`'s promise, as Nest's default hook does. Nest's servers
|
|
87
112
|
// hand it back from their message handlers: kafkajs waits on it to commit
|
|
88
113
|
// the offset or retry the message, and recent Nest versions log its
|
|
89
114
|
// rejection for most other transports.
|
|
90
|
-
target
|
|
115
|
+
setAgentHook(target, "setOnProcessingStartHook", (transportId, ctx, done) => {
|
|
91
116
|
if (transportId === this.microservices.Transport.GRPC) {
|
|
92
117
|
return this.startGrpcRequestTracing(transportId, ctx, done);
|
|
93
118
|
}
|
|
94
|
-
return this.startRpcRequestTracing(transportId, ctx, done);
|
|
95
|
-
});
|
|
96
|
-
target
|
|
119
|
+
return this.startRpcRequestTracing(transportId, ctx, done, target);
|
|
120
|
+
}, composeStartHook);
|
|
121
|
+
setAgentHook(target, "setOnProcessingEndHook", (transportId, ctx) => {
|
|
97
122
|
this.endRpcRequestTracing(transportId, ctx);
|
|
98
|
-
});
|
|
123
|
+
}, composeEndHook);
|
|
99
124
|
}
|
|
100
125
|
/**
|
|
101
126
|
* Names the transport for the snapshot's `protocol` field.
|
|
@@ -109,7 +134,9 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
109
134
|
? transportId.description ?? "custom"
|
|
110
135
|
: this.microservices.Transport[transportId];
|
|
111
136
|
}
|
|
112
|
-
startRpcRequestTracing(transportId, ctx, done
|
|
137
|
+
startRpcRequestTracing(transportId, ctx, done,
|
|
138
|
+
/** The server the message arrived at. */
|
|
139
|
+
target) {
|
|
113
140
|
// The same map `run` is given, rather than `getStore()` inside the callback:
|
|
114
141
|
// identical object, one lookup fewer, and it is known to exist.
|
|
115
142
|
const store = new Map();
|
|
@@ -120,6 +147,9 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
120
147
|
// execution before option callbacks can reach another operation's trace.
|
|
121
148
|
const registryKey = uuidv7();
|
|
122
149
|
store.set(TRACE_REGISTRY_KEY, registryKey);
|
|
150
|
+
this.rememberRegistryKey(ctx, registryKey);
|
|
151
|
+
// Ended by the end hook, recorded or not: see `liveTraceId`.
|
|
152
|
+
this.rememberOperation(ctx, openOperation(store));
|
|
123
153
|
// With OpenTelemetry interop on, the handler runs in the W3C trace
|
|
124
154
|
// context the message was sent in, for an OpenTelemetry SDK in this
|
|
125
155
|
// process - see `continueHandedOnTrace`. Recorded or not, as an
|
|
@@ -150,7 +180,7 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
150
180
|
}
|
|
151
181
|
this.operationTraceRegistry.startTrace(registryKey, {
|
|
152
182
|
protocol: this.toProtocolName(transportId),
|
|
153
|
-
operationId: this.getOperationIdFromContext(ctx),
|
|
183
|
+
operationId: this.getOperationIdFromContext(ctx, target),
|
|
154
184
|
tags: this.options.rpc?.tags,
|
|
155
185
|
}, traceId, traceContext);
|
|
156
186
|
return handle();
|
|
@@ -170,9 +200,16 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
170
200
|
return this.asyncLocalStorage.run(store, () => {
|
|
171
201
|
const traceId = generateTraceIdSafely(this.options.traceIdGenerator, call, this.options, this.logger);
|
|
172
202
|
store.set(this.options.traceIdKey, traceId);
|
|
173
|
-
// The key must precede callbacks here too
|
|
203
|
+
// The key must precede callbacks here too.
|
|
174
204
|
const registryKey = uuidv7();
|
|
175
205
|
store.set(TRACE_REGISTRY_KEY, registryKey);
|
|
206
|
+
// Nest ends a gRPC call with its request message rather than the
|
|
207
|
+
// context it started it with, so the key is kept by both.
|
|
208
|
+
this.rememberRegistryKey(call, registryKey);
|
|
209
|
+
this.rememberRegistryKey(call?.request, registryKey);
|
|
210
|
+
const operation = openOperation(store);
|
|
211
|
+
this.rememberOperation(call, operation);
|
|
212
|
+
this.rememberOperation(call?.request, operation);
|
|
176
213
|
// As for a message: the call's span goes under the caller's for `otlp`,
|
|
177
214
|
// and the calls the handler makes carry its decision on. Read now, while
|
|
178
215
|
// the span a gRPC instrumentation opened for the call is the active one.
|
|
@@ -185,35 +222,44 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
185
222
|
store.set(TRACE_CONTEXT_KEY, traceContext);
|
|
186
223
|
}
|
|
187
224
|
setAttributesSafely(this.options.grpc?.setAttributes, [call], this.options.grpc, store, this.logger, "grpc");
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return resolve(done());
|
|
200
|
-
}
|
|
201
|
-
this.operationTraceRegistry.startTrace(registryKey, {
|
|
202
|
-
protocol: this.toProtocolName(transportId),
|
|
203
|
-
operationId: call.operationId,
|
|
204
|
-
tags: this.options.grpc?.tags,
|
|
205
|
-
}, traceId, traceContext);
|
|
206
|
-
resolve(done());
|
|
207
|
-
}, 0);
|
|
225
|
+
// `done` runs before the hook returns, as for any other transport. Nest
|
|
226
|
+
// listens for the call's `cancelled` and `end` events inside it, and
|
|
227
|
+
// grpc-js emits `cancelled` once: run from a timer, a call cancelled or
|
|
228
|
+
// past its deadline on arrival had nothing listening, and a
|
|
229
|
+
// client-streaming handler waited for good on a request stream that
|
|
230
|
+
// never ended, its trace held in the registry.
|
|
231
|
+
if (shouldIgnoreSafely(this.options.grpc?.ignore, [call], this.options.grpc, this.logger, "grpc")) {
|
|
232
|
+
return done();
|
|
233
|
+
}
|
|
234
|
+
const shouldCapture = this.traceSamplerService.shouldCapture("grpc", {
|
|
235
|
+
call,
|
|
208
236
|
});
|
|
237
|
+
if (!shouldCapture) {
|
|
238
|
+
return done();
|
|
239
|
+
}
|
|
240
|
+
this.operationTraceRegistry.startTrace(registryKey, {
|
|
241
|
+
protocol: this.toProtocolName(transportId),
|
|
242
|
+
operationId: call.operationId,
|
|
243
|
+
tags: this.options.grpc?.tags,
|
|
244
|
+
}, traceId, traceContext);
|
|
245
|
+
return done();
|
|
209
246
|
});
|
|
210
247
|
}
|
|
211
248
|
endRpcRequestTracing(transportId, ctx) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
249
|
+
// The key this context was started under, wherever its reply stream
|
|
250
|
+
// ended. The store answers only for a context no key could be kept by:
|
|
251
|
+
// a gRPC call with no request message of its own, as a client stream has
|
|
252
|
+
// none.
|
|
253
|
+
endOperation((typeof ctx === "object" && ctx !== null
|
|
254
|
+
? this.operations.get(ctx)
|
|
255
|
+
: undefined) ??
|
|
256
|
+
this.asyncLocalStorage
|
|
257
|
+
.getStore()
|
|
258
|
+
?.get(OPERATION_STATE_KEY));
|
|
259
|
+
const registryKey = (typeof ctx === "object" && ctx !== null
|
|
260
|
+
? this.registryKeys.get(ctx)
|
|
261
|
+
: undefined) ??
|
|
262
|
+
this.asyncLocalStorage.getStore()?.get(TRACE_REGISTRY_KEY);
|
|
217
263
|
if (typeof registryKey !== "string" ||
|
|
218
264
|
!this.operationTraceRegistry.hasTrace(registryKey)) {
|
|
219
265
|
return;
|
|
@@ -237,15 +283,73 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
237
283
|
this.observeAgentSharedBuffer.insertRequestSnapshot(snapshot);
|
|
238
284
|
}, this.logger, isGrpc ? "grpc" : "rpc", () => this.operationTraceRegistry.hasOpenSpans(registryKey));
|
|
239
285
|
}
|
|
240
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Keeps `registryKey` for the end hook to find by `ctx`. A custom
|
|
288
|
+
* transporter delivers a context of its own making, and a gRPC call need not
|
|
289
|
+
* carry a request message: what cannot key a `WeakMap` is left to the store.
|
|
290
|
+
*/
|
|
291
|
+
rememberRegistryKey(ctx, registryKey) {
|
|
292
|
+
if (typeof ctx === "object" && ctx !== null) {
|
|
293
|
+
this.registryKeys.set(ctx, registryKey);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** As `rememberRegistryKey`, for the operation the end hook ends. */
|
|
297
|
+
rememberOperation(ctx, operation) {
|
|
298
|
+
if (typeof ctx === "object" && ctx !== null) {
|
|
299
|
+
this.operations.set(ctx, operation);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* The route a message's handler is registered under, for the pattern or
|
|
304
|
+
* topic as its sender wrote it. Nest parses a pattern that is JSON and finds
|
|
305
|
+
* the handler by the normalised outcome, so one handler answers to any
|
|
306
|
+
* number of texts - other whitespace, an escape, a key given twice. Named by
|
|
307
|
+
* the text, each was an operation of the sender's choosing, and a route an
|
|
308
|
+
* `@Objective` on the handler was declared for anew.
|
|
309
|
+
*
|
|
310
|
+
* The route is taken only where the server finds that route's handler for
|
|
311
|
+
* the text: a handler reached through a wildcard keeps the text it was
|
|
312
|
+
* reached on, and so does anything the server cannot be asked about.
|
|
313
|
+
*/
|
|
314
|
+
toRoute(text, target) {
|
|
315
|
+
if (!target) {
|
|
316
|
+
return text;
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
const handlers = target.getHandlers();
|
|
320
|
+
// What a Nest client sends: the route itself. One lookup, no parse.
|
|
321
|
+
if (handlers.has(text)) {
|
|
322
|
+
return text;
|
|
323
|
+
}
|
|
324
|
+
// Protected in Nest's typings, so read by shape.
|
|
325
|
+
const route = target.getRouteFromPattern?.(text);
|
|
326
|
+
if (typeof route !== "string" || route === text) {
|
|
327
|
+
return text;
|
|
328
|
+
}
|
|
329
|
+
const handler = handlers.get(route);
|
|
330
|
+
return handler !== undefined &&
|
|
331
|
+
handler === target.getHandlerByPattern(text)
|
|
332
|
+
? route
|
|
333
|
+
: text;
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
return text;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Kafka, MQTT, RabbitMQ and TCP find a message's handler by the text its
|
|
341
|
+
* context carries - see `toRoute`. NATS and Redis find it by the
|
|
342
|
+
* subscription the message came in on.
|
|
343
|
+
*/
|
|
344
|
+
getOperationIdFromContext(ctx, target) {
|
|
241
345
|
const { KafkaContext, MqttContext, NatsContext, RedisContext, RmqContext, TcpContext, } = this.microservices;
|
|
242
346
|
switch (true) {
|
|
243
347
|
case ctx instanceof KafkaContext:
|
|
244
348
|
case ctx instanceof MqttContext:
|
|
245
|
-
return ctx.getTopic();
|
|
349
|
+
return this.toRoute(ctx.getTopic(), target);
|
|
246
350
|
case ctx instanceof RmqContext:
|
|
247
351
|
case ctx instanceof TcpContext:
|
|
248
|
-
return ctx.getPattern();
|
|
352
|
+
return this.toRoute(ctx.getPattern(), target);
|
|
249
353
|
case ctx instanceof RedisContext:
|
|
250
354
|
return ctx.getChannel();
|
|
251
355
|
case ctx instanceof NatsContext:
|
|
@@ -256,7 +360,9 @@ let RpcObserveAgentService = RpcObserveAgentService_1 = class RpcObserveAgentSer
|
|
|
256
360
|
// routing key. Throwing here would fail the message before `done()`
|
|
257
361
|
// ever ran, so fall back to a conventional accessor when one exists.
|
|
258
362
|
const pattern = ctx?.getPattern?.();
|
|
259
|
-
return typeof pattern === "string"
|
|
363
|
+
return typeof pattern === "string"
|
|
364
|
+
? this.toRoute(pattern, target)
|
|
365
|
+
: "unknown";
|
|
260
366
|
}
|
|
261
367
|
}
|
|
262
368
|
}
|
|
@@ -270,6 +376,40 @@ RpcObserveAgentService = RpcObserveAgentService_1 = __decorate([
|
|
|
270
376
|
TraceSamplerService])
|
|
271
377
|
], RpcObserveAgentService);
|
|
272
378
|
export { RpcObserveAgentService };
|
|
379
|
+
/**
|
|
380
|
+
* The start hook the server is given: the application's outside, and the
|
|
381
|
+
* agent's inside the `done` it is handed, returning what the application's
|
|
382
|
+
* returns. The application's hook decides whether a message is handled at
|
|
383
|
+
* all, and the agent opens a trace only once it is: one the application
|
|
384
|
+
* refuses by never calling `done` leaves nothing behind, where the other way
|
|
385
|
+
* round each would leave a trace open in the registry.
|
|
386
|
+
*/
|
|
387
|
+
function composeStartHook(application, agent) {
|
|
388
|
+
if (!application) {
|
|
389
|
+
return agent;
|
|
390
|
+
}
|
|
391
|
+
return function (transportId, ctx, done) {
|
|
392
|
+
return application.call(this, transportId, ctx, () => agent.call(this, transportId, ctx, done));
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The end hook the server is given: the agent's first, inside the
|
|
397
|
+
* application's as its start hook is, then the application's whatever the
|
|
398
|
+
* agent's did.
|
|
399
|
+
*/
|
|
400
|
+
function composeEndHook(application, agent) {
|
|
401
|
+
if (!application) {
|
|
402
|
+
return agent;
|
|
403
|
+
}
|
|
404
|
+
return function (transportId, ctx) {
|
|
405
|
+
try {
|
|
406
|
+
agent.call(this, transportId, ctx);
|
|
407
|
+
}
|
|
408
|
+
finally {
|
|
409
|
+
application.call(this, transportId, ctx);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
}
|
|
273
413
|
/**
|
|
274
414
|
* The `traceparent` the sending service's agent put in the packet's metadata
|
|
275
415
|
* beside `x-request-id` - see `traceparentToHandOn` - on a transport whose
|
|
@@ -11,7 +11,8 @@ import { KeyOf } from "../types/key-of.type.js";
|
|
|
11
11
|
* processing hooks, so the one place every `@SubscribeMessage()` handler
|
|
12
12
|
* passes through is patched instead: `WsContextCreator.create`, which builds
|
|
13
13
|
* the guards-pipes-interceptors-handler chain for a message. Wrapping what it
|
|
14
|
-
* returns
|
|
14
|
+
* returns, and subscribing to the stream it resolves to in the same store,
|
|
15
|
+
* puts the whole chain inside the trace, whichever platform adapter -
|
|
15
16
|
* socket.io, ws - delivers the message.
|
|
16
17
|
*/
|
|
17
18
|
export declare class WsObserveAgentService<Store extends Record<string, unknown>> {
|
|
@@ -21,19 +22,68 @@ export declare class WsObserveAgentService<Store extends Record<string, unknown>
|
|
|
21
22
|
private readonly traceSamplerService;
|
|
22
23
|
private readonly asyncLocalStorage;
|
|
23
24
|
private readonly logger;
|
|
25
|
+
private readonly tracesByClient;
|
|
26
|
+
private readonly standIns;
|
|
24
27
|
constructor(observeAgentSharedBuffer: ObserveAgentSharedBuffer, options: ObserveModuleOptionsWithDefaults, operationTraceRegistry: OperationTraceRegistry, traceSamplerService: TraceSamplerService, asyncLocalStorage: AsyncLocalStorage<Map<KeyOf<Store>, any>>);
|
|
25
28
|
private loadContextCreator;
|
|
26
29
|
private patchContextCreator;
|
|
27
30
|
/**
|
|
31
|
+
* The handler Nest is given for a `@SubscribeMessage()` method: the same
|
|
32
|
+
* method, called through the instance, telling the message when it runs.
|
|
33
|
+
*
|
|
28
34
|
* Nest reads a gateway's handlers off the class prototype and applies them
|
|
29
35
|
* to the instance, which steps around the instrumentation proxy - the one
|
|
30
36
|
* method of the whole chain that would go unrecorded is the handler itself,
|
|
31
37
|
* and a handler that throws before calling anything leaves no span at all.
|
|
38
|
+
* The stand-in calls the same method through the instance instead.
|
|
39
|
+
*
|
|
40
|
+
* Behind an interceptor, the chain is cold: Nest's proxy resolves to a
|
|
41
|
+
* stream once the guards have run, and the interceptors, the pipes and the
|
|
42
|
+
* handler run only when the adapter subscribes to it. The stand-in marks
|
|
43
|
+
* the message's state when it is called, so the trace can tell which of
|
|
44
|
+
* the two it was handed, and ends that trace when the handler returns a
|
|
45
|
+
* stream of its own.
|
|
46
|
+
*
|
|
47
|
+
* Guards, pipes, filters and the message pattern are all looked up as
|
|
48
|
+
* metadata on the callback, so that metadata is carried over - once per
|
|
49
|
+
* method, since on a request-scoped path `create` runs for every message.
|
|
50
|
+
*/
|
|
51
|
+
private standInFor;
|
|
52
|
+
/**
|
|
53
|
+
* Drops a message's trace when its client goes away before the handler
|
|
54
|
+
* settles, and returns what stops watching for that once the trace ends.
|
|
32
55
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
56
|
+
* A handler waiting on its client - an acknowledgement without a timeout,
|
|
57
|
+
* the client's next frame - never settles once the client has gone, and
|
|
58
|
+
* nothing else would end its trace: every client that hung up on one left
|
|
59
|
+
* its snapshot registered for the life of the process, and a client that
|
|
60
|
+
* reconnects can leave as many as it likes.
|
|
61
|
+
*
|
|
62
|
+
* One listener per client, however many messages it has in flight, on the
|
|
63
|
+
* events Nest's adapters stop a client's messages at: socket.io's
|
|
64
|
+
* `disconnect`, `ws`' `close`. Dropped at once, as the HTTP agent drops an
|
|
65
|
+
* aborted request: an abandoned trace is never reported, and the registry
|
|
66
|
+
* lets a handler that outlives its client end its spans on a trace that is
|
|
67
|
+
* gone. A client that is no event emitter - a custom adapter's - is not
|
|
68
|
+
* watched.
|
|
36
69
|
*/
|
|
37
|
-
private
|
|
70
|
+
private abandonWhenClientLeaves;
|
|
38
71
|
private traceMessage;
|
|
72
|
+
/**
|
|
73
|
+
* The chain's stream, subscribed in the message's store.
|
|
74
|
+
*
|
|
75
|
+
* Nest's adapters subscribe to what the chain resolves to from the socket's
|
|
76
|
+
* own events, outside the store the message was started in. Behind an
|
|
77
|
+
* interceptor that stream is all of the chain past the guards - the
|
|
78
|
+
* interceptors, the pipes, the handler, the exception filter - and for a
|
|
79
|
+
* handler that returns a stream, the stream and the filter its error goes
|
|
80
|
+
* to. Run without the store, they were missing from the trace, and
|
|
81
|
+
* `TracerService` failed in them.
|
|
82
|
+
*
|
|
83
|
+
* `end` is called at the stream's first value, error or completion, and
|
|
84
|
+
* `abandon` when the subscription is torn down before any: Nest's adapters
|
|
85
|
+
* unsubscribe a client's messages when it disconnects. Both are no-ops once
|
|
86
|
+
* either has run.
|
|
87
|
+
*/
|
|
88
|
+
private subscribeInStore;
|
|
39
89
|
}
|