@nestjs/observe 0.3.25 → 0.3.27
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 +55 -2
- 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 +201 -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 +10 -4
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +51 -4
- 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/package-manager.d.ts +6 -0
- package/dist/cli/package-manager.js +60 -3
- package/dist/cli/project-files.d.ts +62 -1
- package/dist/cli/project-files.js +229 -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 +90 -12
- package/dist/instrument/create-instance-decorator.instrument.js +504 -116
- package/dist/instrument/raw-receiver.d.ts +6 -0
- package/dist/instrument/raw-receiver.js +32 -0
- package/dist/interfaces/observe-options.interface.d.ts +17 -6
- 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.d.ts +2 -0
- package/dist/otlp/otlp-span.mapper.js +26 -4
- package/dist/otlp/otlp-trace-exporter.service.d.ts +63 -9
- package/dist/otlp/otlp-trace-exporter.service.js +295 -43
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +49 -17
- 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/mongodb.integration.d.ts +3 -1
- package/dist/outgoing/mongodb.integration.js +93 -18
- package/dist/outgoing/mssql.integration.js +25 -6
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +20 -4
- 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 +139 -21
- 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 +23 -12
- package/dist/protocols/deferred-trace-completion.d.ts +17 -3
- package/dist/protocols/deferred-trace-completion.js +76 -9
- package/dist/protocols/graphql-observe-agent.service.d.ts +97 -6
- package/dist/protocols/graphql-observe-agent.service.js +354 -19
- package/dist/protocols/graphql-operation-parser.js +111 -26
- package/dist/protocols/http-observe-agent.service.d.ts +50 -9
- package/dist/protocols/http-observe-agent.service.js +220 -59
- package/dist/protocols/job-trace-runner.d.ts +42 -3
- package/dist/protocols/job-trace-runner.js +197 -36
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +111 -8
- package/dist/protocols/rpc-observe-agent.service.js +332 -56
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +227 -21
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +16 -7
- package/dist/services/operation-trace.registry.d.ts +58 -1
- package/dist/services/operation-trace.registry.js +576 -63
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +308 -51
- package/dist/services/tracer.service.d.ts +39 -11
- package/dist/services/tracer.service.js +145 -36
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +128 -12
- package/dist/utils/context-accessor.util.d.ts +16 -0
- package/dist/utils/context-accessor.util.js +28 -0
- package/dist/utils/default-trace-id-generator.util.js +5 -7
- package/dist/utils/error-stack.util.d.ts +13 -0
- package/dist/utils/error-stack.util.js +166 -0
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +22 -0
- package/dist/utils/log-line.parser.js +20 -7
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +690 -65
- 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 +37 -4
- package/dist/utils/redact-url-query.js +197 -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 +14 -0
- package/dist/utils/source-context.util.js +142 -43
- package/dist/utils/source-map-resolver.util.js +33 -6
- package/dist/utils/store-copy.util.d.ts +10 -0
- package/dist/utils/store-copy.util.js +43 -0
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -11,6 +11,9 @@ export interface CodeFrame {
|
|
|
11
11
|
/** Source lines, `firstLine` through `firstLine + lines.length - 1`. */
|
|
12
12
|
lines: string[];
|
|
13
13
|
}
|
|
14
|
+
export interface SourceRedactor {
|
|
15
|
+
redactSource(lines: string[]): string[];
|
|
16
|
+
}
|
|
14
17
|
/**
|
|
15
18
|
* Reads source around each in-app frame of a stack trace.
|
|
16
19
|
*
|
|
@@ -36,4 +39,15 @@ export declare function collectCodeFrames(stack: string | undefined, options?: {
|
|
|
36
39
|
* a stack that came off an error.
|
|
37
40
|
*/
|
|
38
41
|
message?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The error's `name`, and its `code` when it has one that is text: with
|
|
44
|
+
* the message, the header the stack opens with. `Error` when absent.
|
|
45
|
+
*/
|
|
46
|
+
name?: string;
|
|
47
|
+
code?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Redacts the source before a frame is cut from it - see
|
|
50
|
+
* `redactedSource`.
|
|
51
|
+
*/
|
|
52
|
+
redactor?: SourceRedactor;
|
|
39
53
|
}): CodeFrame[] | undefined;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync, realpathSync } from "fs";
|
|
2
2
|
import { resolve, sep } from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
3
4
|
import { resolveOriginalPosition } from "./source-map-resolver.util.js";
|
|
4
5
|
const DEFAULT_LINES_OF_CONTEXT = 5;
|
|
5
6
|
/**
|
|
@@ -25,6 +26,31 @@ const MAX_FRAMES = 20;
|
|
|
25
26
|
* deleted file is not retried on every single throw.
|
|
26
27
|
*/
|
|
27
28
|
const sourceCache = new Map();
|
|
29
|
+
/** Each redactor's copy of a cached source, held as long as the source is. */
|
|
30
|
+
const redactedSources = new WeakMap();
|
|
31
|
+
/**
|
|
32
|
+
* The whole file redacted, rather than the frame cut from it. A rule that
|
|
33
|
+
* knows a secret by both its ends - a PEM key's BEGIN and END lines - finds
|
|
34
|
+
* neither in a frame whose edge falls inside the key, and the lines between
|
|
35
|
+
* would ship as they are. Cached against the source's own array, which the
|
|
36
|
+
* file and source-map caches keep stable, so each file is redacted once.
|
|
37
|
+
*/
|
|
38
|
+
function redactedSource(source, redactor) {
|
|
39
|
+
if (!redactor) {
|
|
40
|
+
return source;
|
|
41
|
+
}
|
|
42
|
+
let byRedactor = redactedSources.get(redactor);
|
|
43
|
+
if (!byRedactor) {
|
|
44
|
+
byRedactor = new WeakMap();
|
|
45
|
+
redactedSources.set(redactor, byRedactor);
|
|
46
|
+
}
|
|
47
|
+
let redacted = byRedactor.get(source);
|
|
48
|
+
if (!redacted) {
|
|
49
|
+
redacted = redactor.redactSource(source);
|
|
50
|
+
byRedactor.set(source, redacted);
|
|
51
|
+
}
|
|
52
|
+
return redacted;
|
|
53
|
+
}
|
|
28
54
|
/** What opens every V8 frame line. */
|
|
29
55
|
const FRAME_PREFIX = /^\s*at\s+/;
|
|
30
56
|
/**
|
|
@@ -70,39 +96,31 @@ function isReadableSourcePath(file, root = resolve(process.cwd())) {
|
|
|
70
96
|
* `"\n at f (/app/dist/config.js:1:1)"` - a request parameter interpolated
|
|
71
97
|
* into a validation or driver error is enough - reads as a frame, and sits
|
|
72
98
|
* above the real ones. So the boundary is taken from the error itself: the
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
99
|
+
* stack has to open with exactly the header its name, code and message make,
|
|
100
|
+
* and only the unbroken run of frame lines directly after it counts. That run
|
|
101
|
+
* also ends before anything appended later (`Caused by: ...`), which is one
|
|
102
|
+
* more message.
|
|
103
|
+
*
|
|
104
|
+
* The whole header, not the message wherever it ends the first line: a first
|
|
105
|
+
* line written from some other message can end with this one, and the lines
|
|
106
|
+
* after it are then that message's - a `GraphQLError` carries the stack of
|
|
107
|
+
* the error it wraps, whose message may quote the request and end with the
|
|
108
|
+
* wrapper's own.
|
|
77
109
|
*
|
|
78
|
-
* Fails closed. An error whose message was reassigned after
|
|
79
|
-
*
|
|
80
|
-
*
|
|
110
|
+
* Fails closed. An error whose message was reassigned after its stack was
|
|
111
|
+
* written, or that took over another error's stack, no longer matches it and
|
|
112
|
+
* gets no code frames, which costs a nicety; guessing the boundary instead is
|
|
113
|
+
* what the paragraphs above are about.
|
|
81
114
|
*
|
|
82
115
|
* With `message` undefined the whole string is treated as frames - only for a
|
|
83
116
|
* stack the caller wrote itself.
|
|
84
117
|
*/
|
|
85
|
-
function frameLines(stack, message) {
|
|
118
|
+
function frameLines(stack, message, name = "Error", code) {
|
|
86
119
|
if (message === undefined) {
|
|
87
120
|
return stack.split("\n", MAX_FRAME_LINES);
|
|
88
121
|
}
|
|
89
|
-
|
|
90
|
-
if (
|
|
91
|
-
headerEnd = stack.indexOf("\n");
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
// The last match on the first line that runs to the end of a line: an
|
|
95
|
-
// earlier one can sit inside the name - `UnauthorizedException:
|
|
96
|
-
// Unauthorized` - and a later one ends past the real message, so whatever
|
|
97
|
-
// follows it is still what V8 wrote.
|
|
98
|
-
headerEnd = -1;
|
|
99
|
-
for (let at = stack.indexOf(message); at !== -1 && stack.lastIndexOf("\n", at) === -1; at = stack.indexOf(message, at + 1)) {
|
|
100
|
-
if (stack[at + message.length] === "\n") {
|
|
101
|
-
headerEnd = at + message.length;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (headerEnd === -1 || stack[headerEnd] !== "\n") {
|
|
122
|
+
const headerEnd = headerEndOf(stack, message, name, code);
|
|
123
|
+
if (headerEnd === -1) {
|
|
106
124
|
return [];
|
|
107
125
|
}
|
|
108
126
|
const lines = [];
|
|
@@ -114,6 +132,42 @@ function frameLines(stack, message) {
|
|
|
114
132
|
}
|
|
115
133
|
return lines;
|
|
116
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Where the header a stack opens with ends, when it is the one V8 writes for
|
|
137
|
+
* an error of this name, code and message - at the line break after it - or
|
|
138
|
+
* -1. V8 writes what `Error.prototype.toString` returns: `name: message`, or
|
|
139
|
+
* either alone when the other is empty. Node's own errors put their code in
|
|
140
|
+
* it too, `TypeError [ERR_INVALID_ARG_TYPE]: ...`, and `source-map-support`
|
|
141
|
+
* writes `name: ` for an empty message as well.
|
|
142
|
+
*
|
|
143
|
+
* Compared in place rather than built and compared: neither the message nor
|
|
144
|
+
* the stack is bounded here.
|
|
145
|
+
*/
|
|
146
|
+
function headerEndOf(stack, message, name, code) {
|
|
147
|
+
const endsAt = (at) => (stack[at] === "\n" ? at : -1);
|
|
148
|
+
if (message === "" && stack.startsWith(name)) {
|
|
149
|
+
const end = endsAt(name.length);
|
|
150
|
+
if (end !== -1) {
|
|
151
|
+
return end;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (name === "" && stack.startsWith(message)) {
|
|
155
|
+
const end = endsAt(message.length);
|
|
156
|
+
if (end !== -1) {
|
|
157
|
+
return end;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const prefixes = code === undefined ? [`${name}: `] : [`${name}: `, `${name} [${code}]: `];
|
|
161
|
+
for (const prefix of prefixes) {
|
|
162
|
+
if (stack.startsWith(prefix) && stack.startsWith(message, prefix.length)) {
|
|
163
|
+
const end = endsAt(prefix.length + message.length);
|
|
164
|
+
if (end !== -1) {
|
|
165
|
+
return end;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return -1;
|
|
170
|
+
}
|
|
117
171
|
/**
|
|
118
172
|
* Reads one V8 frame line, in either shape:
|
|
119
173
|
* at fn (/abs/path/file.ts:42:31)
|
|
@@ -150,20 +204,49 @@ function parseFrameLine(raw) {
|
|
|
150
204
|
return undefined;
|
|
151
205
|
}
|
|
152
206
|
return {
|
|
153
|
-
file: location.slice(0, lineAt),
|
|
207
|
+
file: ownCopy(location.slice(0, lineAt)),
|
|
154
208
|
line: Number(line),
|
|
155
209
|
column: Number(column),
|
|
156
210
|
};
|
|
157
211
|
}
|
|
158
|
-
|
|
212
|
+
/**
|
|
213
|
+
* Its own copy of a frame's path. V8 keeps a substring as a slice of the
|
|
214
|
+
* string it was cut from, and the path is cut from the stack, which repeats
|
|
215
|
+
* a message as long as whoever threw made it: a code frame's `file`, kept on
|
|
216
|
+
* an error payload until the flush, held the whole stack alive for a path of
|
|
217
|
+
* a few dozen characters. UTF-16 both ways, so a path with a lone surrogate
|
|
218
|
+
* is copied as it is.
|
|
219
|
+
*/
|
|
220
|
+
function ownCopy(text) {
|
|
221
|
+
return Buffer.from(text, "utf16le").toString("utf16le");
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* A frame's file as a path. V8 names an ES module's file by its URL -
|
|
225
|
+
* `at run (file:///app/dist/main.js:3:9)` - which is the same file as the
|
|
226
|
+
* path a CommonJS frame carries. `undefined` for a URL that names no local
|
|
227
|
+
* file.
|
|
228
|
+
*/
|
|
229
|
+
function toFilePath(file) {
|
|
230
|
+
if (!file.startsWith("file://")) {
|
|
231
|
+
return file;
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
return fileURLToPath(file);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function parseFrames(stack, header) {
|
|
159
241
|
const frames = [];
|
|
160
|
-
for (const raw of frameLines(stack, message)) {
|
|
242
|
+
for (const raw of frameLines(stack, header.message, header.name, header.code)) {
|
|
161
243
|
const parsed = parseFrameLine(raw);
|
|
162
244
|
if (!parsed) {
|
|
163
245
|
continue;
|
|
164
246
|
}
|
|
165
|
-
const {
|
|
166
|
-
|
|
247
|
+
const { line, column } = parsed;
|
|
248
|
+
const file = toFilePath(parsed.file);
|
|
249
|
+
if (file === undefined || isIgnored(file)) {
|
|
167
250
|
continue;
|
|
168
251
|
}
|
|
169
252
|
// Only absolute paths can be read back off disk. Anything else (an eval
|
|
@@ -174,10 +257,20 @@ function parseFrames(stack, message) {
|
|
|
174
257
|
if (!isReadableSourcePath(file)) {
|
|
175
258
|
continue;
|
|
176
259
|
}
|
|
177
|
-
frames.push({ file, line, column });
|
|
260
|
+
frames.push({ file, path: resolve(file), line, column });
|
|
178
261
|
}
|
|
179
262
|
return frames;
|
|
180
263
|
}
|
|
264
|
+
function rememberSource(file, lines) {
|
|
265
|
+
if (sourceCache.size >= MAX_CACHED_FILES) {
|
|
266
|
+
const oldest = sourceCache.keys().next().value;
|
|
267
|
+
if (oldest !== undefined) {
|
|
268
|
+
sourceCache.delete(oldest);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
sourceCache.set(file, lines);
|
|
272
|
+
}
|
|
273
|
+
/** The lines of `file`, a resolved path. */
|
|
181
274
|
function readSource(file) {
|
|
182
275
|
if (sourceCache.has(file)) {
|
|
183
276
|
return sourceCache.get(file);
|
|
@@ -190,8 +283,18 @@ function readSource(file) {
|
|
|
190
283
|
// resolved path has to clear the same gate. The root is resolved too: a
|
|
191
284
|
// working directory reached through a link is otherwise outside itself.
|
|
192
285
|
const real = realpathSync(file);
|
|
193
|
-
if (
|
|
286
|
+
if (real !== file && sourceCache.has(real)) {
|
|
287
|
+
// Another name for a file already read - a directory link inside the
|
|
288
|
+
// application, which can even lead back to where it sits and so spell
|
|
289
|
+
// one file endlessly. It shares that file's lines rather than holding
|
|
290
|
+
// a copy of its own.
|
|
291
|
+
lines = sourceCache.get(real);
|
|
292
|
+
}
|
|
293
|
+
else if (isReadableSourcePath(real, realpathSync(process.cwd()))) {
|
|
194
294
|
lines = readFileSync(real, "utf8").split("\n");
|
|
295
|
+
if (real !== file) {
|
|
296
|
+
rememberSource(real, lines);
|
|
297
|
+
}
|
|
195
298
|
}
|
|
196
299
|
}
|
|
197
300
|
catch {
|
|
@@ -199,13 +302,7 @@ function readSource(file) {
|
|
|
199
302
|
// build. Cached as a miss so it is not retried on the next throw.
|
|
200
303
|
lines = null;
|
|
201
304
|
}
|
|
202
|
-
|
|
203
|
-
const oldest = sourceCache.keys().next().value;
|
|
204
|
-
if (oldest !== undefined) {
|
|
205
|
-
sourceCache.delete(oldest);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
sourceCache.set(file, lines);
|
|
305
|
+
rememberSource(file, lines);
|
|
209
306
|
return lines;
|
|
210
307
|
}
|
|
211
308
|
/**
|
|
@@ -246,14 +343,15 @@ export function collectCodeFrames(stack, options = {}) {
|
|
|
246
343
|
}
|
|
247
344
|
const maxFrames = options.maxFrames ?? 5;
|
|
248
345
|
try {
|
|
249
|
-
const frames = parseFrames(stack, options
|
|
346
|
+
const frames = parseFrames(stack, options).slice(0, maxFrames);
|
|
250
347
|
const codeFrames = [];
|
|
251
348
|
for (const frame of frames) {
|
|
252
349
|
let file = frame.file;
|
|
350
|
+
let path = frame.path;
|
|
253
351
|
let line = frame.line;
|
|
254
352
|
let source = null;
|
|
255
353
|
if (options.sourceMaps) {
|
|
256
|
-
const original = resolveOriginalPosition(frame.
|
|
354
|
+
const original = resolveOriginalPosition(frame.path, frame.line, frame.column);
|
|
257
355
|
if (original) {
|
|
258
356
|
// Re-apply the in-app filter to the mapped path: a frame in
|
|
259
357
|
// dist/main.js can map back into node_modules, and that is still noise.
|
|
@@ -269,13 +367,14 @@ export function collectCodeFrames(stack, options = {}) {
|
|
|
269
367
|
continue;
|
|
270
368
|
}
|
|
271
369
|
file = original.file;
|
|
370
|
+
path = resolve(original.file);
|
|
272
371
|
line = original.line;
|
|
273
372
|
// Prefer the map's embedded copy - it is the source this build was
|
|
274
373
|
// compiled from, and it works when the source tree is not deployed.
|
|
275
374
|
source = original.content ?? null;
|
|
276
375
|
}
|
|
277
376
|
}
|
|
278
|
-
source ??= readSource(
|
|
377
|
+
source ??= readSource(path);
|
|
279
378
|
if (!source) {
|
|
280
379
|
continue;
|
|
281
380
|
}
|
|
@@ -290,7 +389,7 @@ export function collectCodeFrames(stack, options = {}) {
|
|
|
290
389
|
file,
|
|
291
390
|
line,
|
|
292
391
|
firstLine,
|
|
293
|
-
lines: source.slice(firstLine - 1, lastLine),
|
|
392
|
+
lines: redactedSource(source, options.redactor).slice(firstLine - 1, lastLine),
|
|
294
393
|
});
|
|
295
394
|
}
|
|
296
395
|
return codeFrames.length ? codeFrames : undefined;
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* eagerly costs one module parse and keeps this file loadable either way.
|
|
9
9
|
*/
|
|
10
10
|
import { originalPositionFor, sourceContentFor, TraceMap, } from "@jridgewell/trace-mapping";
|
|
11
|
-
import { readFileSync } from "node:fs";
|
|
12
|
-
import { dirname, resolve } from "node:path";
|
|
11
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
12
|
+
import { dirname, resolve, sep } from "node:path";
|
|
13
13
|
const SOURCE_MAPPING_URL_PATTERN = /\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/gm;
|
|
14
14
|
const INLINE_MAP_PREFIX = "data:application/json";
|
|
15
15
|
/**
|
|
@@ -20,10 +20,31 @@ const INLINE_MAP_PREFIX = "data:application/json";
|
|
|
20
20
|
const MAX_CACHED_MAPS = 200;
|
|
21
21
|
/** Parsed maps by compiled-file path; null marks "looked, found none". */
|
|
22
22
|
const mapCache = new Map();
|
|
23
|
+
/**
|
|
24
|
+
* Reads a file that is inside the working directory once links are resolved.
|
|
25
|
+
*
|
|
26
|
+
* Neither file read here is named by this package: the compiled file comes
|
|
27
|
+
* off a stack frame that was checked only as written, and its map is
|
|
28
|
+
* wherever a comment in that file says. What a map embeds ships as the code
|
|
29
|
+
* frame, so both are held to the gate `readSource` holds a source file to -
|
|
30
|
+
* the resolved path under the resolved root. Without it a link under the
|
|
31
|
+
* root, or a `sourceMappingURL` leading out of it, shipped source the
|
|
32
|
+
* application does not own.
|
|
33
|
+
*
|
|
34
|
+
* Throws for a path that leads anywhere else, which the caller reads as "no
|
|
35
|
+
* map".
|
|
36
|
+
*/
|
|
37
|
+
function readFileUnderRoot(path) {
|
|
38
|
+
const real = realpathSync(path);
|
|
39
|
+
if (!real.startsWith(realpathSync(process.cwd()) + sep)) {
|
|
40
|
+
throw new Error(`${path} is outside the working directory`);
|
|
41
|
+
}
|
|
42
|
+
return readFileSync(real, "utf8");
|
|
43
|
+
}
|
|
23
44
|
function readMapFor(file) {
|
|
24
45
|
let compiled;
|
|
25
46
|
try {
|
|
26
|
-
compiled =
|
|
47
|
+
compiled = readFileUnderRoot(file);
|
|
27
48
|
}
|
|
28
49
|
catch {
|
|
29
50
|
return null;
|
|
@@ -50,7 +71,7 @@ function readMapFor(file) {
|
|
|
50
71
|
else {
|
|
51
72
|
const mapPath = resolve(dirname(file), url);
|
|
52
73
|
mapDir = dirname(mapPath);
|
|
53
|
-
rawMap =
|
|
74
|
+
rawMap = readFileUnderRoot(mapPath);
|
|
54
75
|
}
|
|
55
76
|
}
|
|
56
77
|
catch {
|
|
@@ -60,6 +81,7 @@ function readMapFor(file) {
|
|
|
60
81
|
return {
|
|
61
82
|
tracer: new TraceMap(JSON.parse(rawMap)),
|
|
62
83
|
mapDir,
|
|
84
|
+
contentLines: new Map(),
|
|
63
85
|
};
|
|
64
86
|
}
|
|
65
87
|
catch {
|
|
@@ -102,13 +124,18 @@ export function resolveOriginalPosition(file, line, column) {
|
|
|
102
124
|
// `sourcesContent` is the reliable path: the original text is embedded in the
|
|
103
125
|
// map, so it works even when the source tree is absent at runtime (a container
|
|
104
126
|
// shipping only dist, for instance).
|
|
105
|
-
|
|
127
|
+
let content = map.contentLines.get(position.source);
|
|
128
|
+
if (!map.contentLines.has(position.source)) {
|
|
129
|
+
const embedded = sourceContentFor(map.tracer, position.source);
|
|
130
|
+
content = embedded ? embedded.split("\n") : undefined;
|
|
131
|
+
map.contentLines.set(position.source, content);
|
|
132
|
+
}
|
|
106
133
|
return {
|
|
107
134
|
file: position.source.startsWith("/")
|
|
108
135
|
? position.source
|
|
109
136
|
: resolve(map.mapDir, position.source),
|
|
110
137
|
line: position.line,
|
|
111
|
-
content
|
|
138
|
+
content,
|
|
112
139
|
};
|
|
113
140
|
}
|
|
114
141
|
/** Testing seam - the caches are process-lifetime otherwise. */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** A copy of `store` for a span, with `entries` over what it copies. */
|
|
2
|
+
export declare function copyStore<K, V>(store: Map<K, V>, ...entries: Array<[K, V]>): Map<K, V>;
|
|
3
|
+
/** Sets `key` in `store` and in every store it was copied from. */
|
|
4
|
+
export declare function setThroughCopies(store: Map<any, unknown>, key: unknown, value: unknown): void;
|
|
5
|
+
/**
|
|
6
|
+
* `key` as `store` holds it, or else as the nearest store it was copied from
|
|
7
|
+
* holds it: what a span running beside the one that set it - copied before
|
|
8
|
+
* it was set - still finds in the operation's own store.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getThroughCopies(store: Map<any, unknown>, key: unknown): unknown;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A span runs in a copy of its caller's store, so that the copy can name the
|
|
3
|
+
* span as the caller of whatever it calls. An attribute written to the copy
|
|
4
|
+
* alone was gone once the span returned - one a guard, a pipe or a service
|
|
5
|
+
* set never reached the handler - while an operation that is not recorded
|
|
6
|
+
* makes no copies and kept it.
|
|
7
|
+
*
|
|
8
|
+
* So each copy remembers the store it was made from, here rather than in the
|
|
9
|
+
* store: the store is the application's to read and write as well, and an
|
|
10
|
+
* entry of the agent's own would show in every `entries()` it walks. An
|
|
11
|
+
* attribute is written through the copy to every store it came from, so it
|
|
12
|
+
* is there once the span returns, for `getAttribute` and for a plain
|
|
13
|
+
* `store.get` alike.
|
|
14
|
+
*/
|
|
15
|
+
const copiedFrom = new WeakMap();
|
|
16
|
+
/** A copy of `store` for a span, with `entries` over what it copies. */
|
|
17
|
+
export function copyStore(store, ...entries) {
|
|
18
|
+
const copy = new Map(store);
|
|
19
|
+
for (const [key, value] of entries) {
|
|
20
|
+
copy.set(key, value);
|
|
21
|
+
}
|
|
22
|
+
copiedFrom.set(copy, store);
|
|
23
|
+
return copy;
|
|
24
|
+
}
|
|
25
|
+
/** Sets `key` in `store` and in every store it was copied from. */
|
|
26
|
+
export function setThroughCopies(store, key, value) {
|
|
27
|
+
for (let current = store; current; current = copiedFrom.get(current)) {
|
|
28
|
+
current.set(key, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* `key` as `store` holds it, or else as the nearest store it was copied from
|
|
33
|
+
* holds it: what a span running beside the one that set it - copied before
|
|
34
|
+
* it was set - still finds in the operation's own store.
|
|
35
|
+
*/
|
|
36
|
+
export function getThroughCopies(store, key) {
|
|
37
|
+
for (let current = store; current; current = copiedFrom.get(current)) {
|
|
38
|
+
if (current.has(key)) {
|
|
39
|
+
return current.get(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
@@ -48,8 +48,11 @@ export function readTraceContext(headers) {
|
|
|
48
48
|
return undefined;
|
|
49
49
|
}
|
|
50
50
|
const context = {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
// Copied: a capture is a slice of the header, and would keep all of it
|
|
52
|
+
// alive for as long as the context - in the OTLP retry queue, that can
|
|
53
|
+
// be long. A version past 00 may carry anything after its flags.
|
|
54
|
+
traceId: copyOf(traceId),
|
|
55
|
+
parentSpanId: copyOf(parentSpanId),
|
|
53
56
|
sampled: (parseInt(flags, 16) & 0x01) === 0x01,
|
|
54
57
|
};
|
|
55
58
|
const traceState = fitTraceState(headers?.["tracestate"]);
|
|
@@ -72,8 +75,10 @@ function fitTraceState(value) {
|
|
|
72
75
|
if (!trimmed || !TRACE_STATE_PATTERN.test(trimmed)) {
|
|
73
76
|
return undefined;
|
|
74
77
|
}
|
|
78
|
+
// Copied, here and below: what `trim` and `split` give back are slices of
|
|
79
|
+
// the value, however long it was.
|
|
75
80
|
if (trimmed.length <= MAX_TRACE_STATE_LENGTH) {
|
|
76
|
-
return trimmed;
|
|
81
|
+
return copyOf(trimmed);
|
|
77
82
|
}
|
|
78
83
|
let fitted = "";
|
|
79
84
|
for (const entry of trimmed.split(",")) {
|
|
@@ -87,7 +92,15 @@ function fitTraceState(value) {
|
|
|
87
92
|
}
|
|
88
93
|
fitted = next;
|
|
89
94
|
}
|
|
90
|
-
return fitted
|
|
95
|
+
return fitted ? copyOf(fitted) : undefined;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `text`, which is printable ASCII, as a string that shares no memory with
|
|
99
|
+
* what it was cut from. V8 keeps a substring as a slice of its source, so a
|
|
100
|
+
* 32-character trace id would otherwise hold a header of any length.
|
|
101
|
+
*/
|
|
102
|
+
function copyOf(text) {
|
|
103
|
+
return Buffer.from(text, "latin1").toString("latin1");
|
|
91
104
|
}
|
|
92
105
|
/**
|
|
93
106
|
* The trace id a `traceparent` header carries, written as a UUID - the
|
|
@@ -169,8 +182,10 @@ function traceContextOfSpan(spanContext) {
|
|
|
169
182
|
return undefined;
|
|
170
183
|
}
|
|
171
184
|
const context = {
|
|
172
|
-
|
|
173
|
-
|
|
185
|
+
// Copied: the SDK's ids for a span with a remote parent are substrings
|
|
186
|
+
// of the carrier its propagator read, and would keep all of it alive.
|
|
187
|
+
traceId: copyOf(spanContext.traceId),
|
|
188
|
+
parentSpanId: copyOf(spanContext.spanId),
|
|
174
189
|
sampled: (spanContext.traceFlags & 0x01) === 0x01,
|
|
175
190
|
};
|
|
176
191
|
let traceState;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.27",
|
|
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",
|
|
@@ -163,7 +163,7 @@
|
|
|
163
163
|
"@types/supertest": "7.2.1",
|
|
164
164
|
"@types/ws": "8.18.1",
|
|
165
165
|
"@vitest/coverage-v8": "4.1.11",
|
|
166
|
-
"amqp-connection-manager": "5.0.
|
|
166
|
+
"amqp-connection-manager": "5.0.1",
|
|
167
167
|
"amqplib": "2.2.0",
|
|
168
168
|
"bull": "4.16.5",
|
|
169
169
|
"bullmq": "6.2.0",
|