@nestjs/observe 0.2.2 → 0.3.0

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.
Files changed (66) hide show
  1. package/README.md +38 -0
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/agent/detached-observe-worker.assembly.d.ts +18 -0
  4. package/dist/agent/detached-observe-worker.assembly.js +29 -0
  5. package/dist/agent/detached-observe-worker.d.ts +21 -0
  6. package/dist/agent/detached-observe-worker.js +74 -24
  7. package/dist/agent/observe-agent.shared-buffer.js +7 -5
  8. package/dist/agent/observe-agent.worker.js +17 -21
  9. package/dist/agent/telemetry-wire-contract.js +1 -0
  10. package/dist/encoders/remap-keys.util.js +6 -0
  11. package/dist/encoders/request-snapshot.encoder.d.ts +1 -0
  12. package/dist/encoders/request-snapshot.encoder.js +1 -0
  13. package/dist/instrument/create-instance-decorator.instrument.js +2 -2
  14. package/dist/interfaces/observe-options.interface.d.ts +97 -9
  15. package/dist/interfaces/request-snapshot.interface.d.ts +8 -0
  16. package/dist/observe.constants.d.ts +16 -0
  17. package/dist/observe.constants.js +16 -0
  18. package/dist/observe.module.js +55 -6
  19. package/dist/outgoing/http.integration.d.ts +36 -0
  20. package/dist/outgoing/http.integration.js +127 -0
  21. package/dist/outgoing/mongodb.integration.d.ts +38 -0
  22. package/dist/outgoing/mongodb.integration.js +141 -0
  23. package/dist/outgoing/mysql2.integration.d.ts +20 -0
  24. package/dist/outgoing/mysql2.integration.js +89 -0
  25. package/dist/outgoing/outgoing-observe-agent.service.d.ts +17 -0
  26. package/dist/outgoing/outgoing-observe-agent.service.js +123 -0
  27. package/dist/outgoing/outgoing-span.recorder.d.ts +54 -0
  28. package/dist/outgoing/outgoing-span.recorder.js +94 -0
  29. package/dist/outgoing/patch-method.util.d.ts +15 -0
  30. package/dist/outgoing/patch-method.util.js +29 -0
  31. package/dist/outgoing/pg.integration.d.ts +22 -0
  32. package/dist/outgoing/pg.integration.js +66 -0
  33. package/dist/outgoing/sql-statement.util.d.ts +21 -0
  34. package/dist/outgoing/sql-statement.util.js +48 -0
  35. package/dist/protocols/bull-observe-agent.service.d.ts +28 -0
  36. package/dist/protocols/bull-observe-agent.service.js +130 -0
  37. package/dist/protocols/graphql-observe-agent.service.js +5 -2
  38. package/dist/protocols/graphql-operation-parser.d.ts +6 -4
  39. package/dist/protocols/graphql-operation-parser.js +61 -6
  40. package/dist/protocols/http-observe-agent.service.js +26 -7
  41. package/dist/protocols/job-trace-runner.d.ts +77 -0
  42. package/dist/protocols/job-trace-runner.js +196 -0
  43. package/dist/protocols/queue-observe-agent.service.d.ts +9 -4
  44. package/dist/protocols/queue-observe-agent.service.js +36 -95
  45. package/dist/protocols/ws-observe-agent.service.d.ts +39 -0
  46. package/dist/protocols/ws-observe-agent.service.js +180 -0
  47. package/dist/services/operation-trace.registry.d.ts +35 -1
  48. package/dist/services/operation-trace.registry.js +68 -5
  49. package/dist/services/trace-sampler.service.d.ts +4 -0
  50. package/dist/services/tracer.service.js +5 -3
  51. package/dist/utils/capture-request.util.d.ts +57 -0
  52. package/dist/utils/capture-request.util.js +116 -0
  53. package/dist/utils/default-trace-id-generator.util.d.ts +12 -0
  54. package/dist/utils/default-trace-id-generator.util.js +12 -1
  55. package/dist/utils/log-redactor.d.ts +20 -1
  56. package/dist/utils/log-redactor.js +54 -38
  57. package/dist/utils/optional-peer.util.d.ts +13 -0
  58. package/dist/utils/optional-peer.util.js +30 -0
  59. package/dist/utils/redact-url-query.d.ts +10 -1
  60. package/dist/utils/redact-url-query.js +16 -37
  61. package/dist/utils/sensitive-keys.d.ts +15 -0
  62. package/dist/utils/sensitive-keys.js +76 -0
  63. package/dist/utils/source-context.util.d.ts +6 -0
  64. package/dist/utils/source-context.util.js +121 -15
  65. package/dist/utils/source-map-resolver.util.js +12 -0
  66. package/package.json +35 -2
@@ -1,4 +1,4 @@
1
- import { readFileSync } from "fs";
1
+ import { readFileSync, realpathSync } from "fs";
2
2
  import { resolve, sep } from "path";
3
3
  import { resolveOriginalPosition } from "./source-map-resolver.util.js";
4
4
  const DEFAULT_LINES_OF_CONTEXT = 5;
@@ -25,12 +25,15 @@ const MAX_FRAMES = 20;
25
25
  * deleted file is not retried on every single throw.
26
26
  */
27
27
  const sourceCache = new Map();
28
+ /** What opens every V8 frame line. */
29
+ const FRAME_PREFIX = /^\s*at\s+/;
28
30
  /**
29
- * V8 stack frame lines, both shapes:
30
- * at fn (/abs/path/file.ts:42:31)
31
- * at /abs/path/file.ts:42:31
31
+ * Longest line read as a frame, and most frame lines read from one stack. A
32
+ * real frame is a function name and a path; a real stack is `stackTraceLimit`
33
+ * of them. Anything past either is not something V8 wrote.
32
34
  */
33
- const FRAME_PATTERN = /^\s*at\s+(?:.*?\s+\()?(.+?):(\d+):(\d+)\)?\s*$/;
35
+ const MAX_FRAME_LINE_LENGTH = 1024;
36
+ const MAX_FRAME_LINES = 100;
34
37
  function isIgnored(file) {
35
38
  return IGNORED_FRAME_PATTERNS.some((pattern) => pattern.test(file));
36
39
  }
@@ -52,22 +55,109 @@ const READABLE_EXTENSIONS = /\.(?:js|cjs|mjs|jsx|ts|cts|mts|tsx)$/;
52
55
  * the application's own root, with an extension the runtime can execute, are
53
56
  * eligible - which is also precisely the set of files worth rendering.
54
57
  */
55
- function isReadableSourcePath(file) {
58
+ function isReadableSourcePath(file, root = resolve(process.cwd())) {
56
59
  if (!READABLE_EXTENSIONS.test(file)) {
57
60
  return false;
58
61
  }
59
- const root = resolve(process.cwd());
60
62
  const resolved = resolve(file);
61
63
  return resolved === root || resolved.startsWith(root + sep);
62
64
  }
63
- function parseFrames(stack) {
65
+ /**
66
+ * The lines of a stack that V8 wrote, as opposed to the ones the message did.
67
+ *
68
+ * `Error#stack` is `${name}: ${message}` followed by the frames, and nothing in
69
+ * the string marks where one ends and the other starts: a message carrying
70
+ * `"\n at f (/app/dist/config.js:1:1)"` - a request parameter interpolated
71
+ * into a validation or driver error is enough - reads as a frame, and sits
72
+ * above the real ones. So the boundary is taken from the error itself: the
73
+ * message has to be found on the stack's first line and run to the end of a
74
+ * line, and only the unbroken run of frame lines directly after it counts.
75
+ * That run also ends before anything appended later (`Caused by: ...`), which
76
+ * is one more message.
77
+ *
78
+ * Fails closed. An error whose message was reassigned after construction no
79
+ * longer matches its own stack and gets no code frames, which costs a nicety;
80
+ * guessing the boundary instead is what the paragraph above is about.
81
+ *
82
+ * With `message` undefined the whole string is treated as frames - only for a
83
+ * stack the caller wrote itself.
84
+ */
85
+ function frameLines(stack, message) {
86
+ if (message === undefined) {
87
+ return stack.split("\n", MAX_FRAME_LINES);
88
+ }
89
+ let headerEnd;
90
+ if (message === "") {
91
+ headerEnd = stack.indexOf("\n");
92
+ }
93
+ else {
94
+ const at = stack.indexOf(message);
95
+ if (at === -1 || stack.lastIndexOf("\n", at) !== -1) {
96
+ return [];
97
+ }
98
+ headerEnd = at + message.length;
99
+ }
100
+ if (headerEnd === -1 || stack[headerEnd] !== "\n") {
101
+ return [];
102
+ }
103
+ const lines = [];
104
+ for (const line of stack.slice(headerEnd + 1).split("\n", MAX_FRAME_LINES)) {
105
+ if (!FRAME_PREFIX.test(line)) {
106
+ break;
107
+ }
108
+ lines.push(line);
109
+ }
110
+ return lines;
111
+ }
112
+ /**
113
+ * Reads one V8 frame line, in either shape:
114
+ * at fn (/abs/path/file.ts:42:31)
115
+ * at /abs/path/file.ts:42:31
116
+ *
117
+ * Taken apart by index rather than by one pattern. The pattern this replaces
118
+ * had two lazy groups that could each claim the same text, which made a line
119
+ * with no position quadratic to reject - seconds of event loop for a 100KB
120
+ * line, and the line came from whoever wrote the error message.
121
+ */
122
+ function parseFrameLine(raw) {
123
+ if (raw.length > MAX_FRAME_LINE_LENGTH) {
124
+ return undefined;
125
+ }
126
+ const prefix = FRAME_PREFIX.exec(raw);
127
+ if (!prefix) {
128
+ return undefined;
129
+ }
130
+ let location = raw.slice(prefix[0].length).trimEnd();
131
+ if (location.endsWith(")")) {
132
+ const open = location.indexOf(" (");
133
+ if (open !== -1) {
134
+ location = location.slice(open + 2, -1);
135
+ }
136
+ }
137
+ const columnAt = location.lastIndexOf(":");
138
+ const lineAt = location.lastIndexOf(":", columnAt - 1);
139
+ if (lineAt <= 0) {
140
+ return undefined;
141
+ }
142
+ const line = location.slice(lineAt + 1, columnAt);
143
+ const column = location.slice(columnAt + 1);
144
+ if (!/^\d+$/.test(line) || !/^\d+$/.test(column)) {
145
+ return undefined;
146
+ }
147
+ return {
148
+ file: location.slice(0, lineAt),
149
+ line: Number(line),
150
+ column: Number(column),
151
+ };
152
+ }
153
+ function parseFrames(stack, message) {
64
154
  const frames = [];
65
- for (const raw of stack.split("\n")) {
66
- const match = FRAME_PATTERN.exec(raw);
67
- if (!match) {
155
+ for (const raw of frameLines(stack, message)) {
156
+ const parsed = parseFrameLine(raw);
157
+ if (!parsed) {
68
158
  continue;
69
159
  }
70
- const [, file, line, column] = match;
160
+ const { file, line, column } = parsed;
71
161
  if (isIgnored(file)) {
72
162
  continue;
73
163
  }
@@ -79,7 +169,7 @@ function parseFrames(stack) {
79
169
  if (!isReadableSourcePath(file)) {
80
170
  continue;
81
171
  }
82
- frames.push({ file, line: Number(line), column: Number(column) });
172
+ frames.push({ file, line, column });
83
173
  }
84
174
  return frames;
85
175
  }
@@ -89,7 +179,15 @@ function readSource(file) {
89
179
  }
90
180
  let lines = null;
91
181
  try {
92
- lines = readFileSync(file, "utf8").split("\n");
182
+ // The path was checked as written; what gets opened is where it leads. A
183
+ // symlink under the root - `current -> /releases/42`, or something less
184
+ // innocent - passes a lexical check while pointing anywhere, so the
185
+ // resolved path has to clear the same gate. The root is resolved too: a
186
+ // working directory reached through a link is otherwise outside itself.
187
+ const real = realpathSync(file);
188
+ if (isReadableSourcePath(real, realpathSync(process.cwd()))) {
189
+ lines = readFileSync(real, "utf8").split("\n");
190
+ }
93
191
  }
94
192
  catch {
95
193
  // Unreadable for any reason - bundled away, permissions, deleted since
@@ -143,7 +241,7 @@ export function collectCodeFrames(stack, options = {}) {
143
241
  }
144
242
  const maxFrames = options.maxFrames ?? 5;
145
243
  try {
146
- const frames = parseFrames(stack).slice(0, maxFrames);
244
+ const frames = parseFrames(stack, options.message).slice(0, maxFrames);
147
245
  const codeFrames = [];
148
246
  for (const frame of frames) {
149
247
  let file = frame.file;
@@ -157,6 +255,14 @@ export function collectCodeFrames(stack, options = {}) {
157
255
  if (isIgnored(original.file)) {
158
256
  continue;
159
257
  }
258
+ // And the containment check, when the map's `sources` entry is about
259
+ // to be read from disk rather than taken from `sourcesContent`. The
260
+ // compiled frame passed it; the path the map resolved it to is a
261
+ // different string from a different input, and a map is one more
262
+ // thing on disk that can say `../../../etc/passwd`.
263
+ if (!original.content && !isReadableSourcePath(original.file)) {
264
+ continue;
265
+ }
160
266
  file = original.file;
161
267
  line = original.line;
162
268
  // Prefer the map's embedded copy - it is the source this build was
@@ -12,6 +12,12 @@ import { readFileSync } from "node:fs";
12
12
  import { dirname, resolve } 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
+ /**
16
+ * Cap on distinct compiled files held. A parsed map is the largest thing this
17
+ * package keeps, and the key is a path off a stack trace - so the cache is
18
+ * bounded like the source cache beside it rather than trusted to stay small.
19
+ */
20
+ const MAX_CACHED_MAPS = 200;
15
21
  /** Parsed maps by compiled-file path; null marks "looked, found none". */
16
22
  const mapCache = new Map();
17
23
  function readMapFor(file) {
@@ -65,6 +71,12 @@ function getMap(file) {
65
71
  return mapCache.get(file);
66
72
  }
67
73
  const loaded = readMapFor(file);
74
+ if (mapCache.size >= MAX_CACHED_MAPS) {
75
+ const oldest = mapCache.keys().next().value;
76
+ if (oldest !== undefined) {
77
+ mapCache.delete(oldest);
78
+ }
79
+ }
68
80
  mapCache.set(file, loaded);
69
81
  return loaded;
70
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Observability and APM agent for NestJS - traces, runtime metrics, custom metrics and CPU profiles",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://observe.nestjs.com",
@@ -48,6 +48,7 @@
48
48
  },
49
49
  "scripts": {
50
50
  "build": "rimraf dist && tsc -p tsconfig.build.json",
51
+ "build:into": "tsc -p tsconfig.build.json --watch --preserveWatchOutput --outDir",
51
52
  "format": "prettier --write \"src/**/*.ts\"",
52
53
  "lint": "oxlint --type-aware src",
53
54
  "lint:fix": "oxlint --type-aware --fix src",
@@ -69,17 +70,23 @@
69
70
  "@jridgewell/trace-mapping": "0.3.31"
70
71
  },
71
72
  "peerDependencies": {
73
+ "@nestjs/bull": "^11.0.0 || ^12.0.0",
72
74
  "@nestjs/bullmq": "^11.0.0 || ^12.0.0",
73
75
  "@nestjs/common": "^11.0.0 || ^12.0.0",
74
76
  "@nestjs/core": "^11.0.0 || ^12.0.0",
75
77
  "@nestjs/graphql": "^13.0.0 || ^14.0.0",
76
78
  "@nestjs/microservices": "^11.0.0 || ^12.0.0",
77
79
  "@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^12.0.0",
80
+ "@nestjs/websockets": "^11.0.0 || ^12.0.0",
81
+ "bull": "^3.3.0 || ^4.0.0",
78
82
  "bullmq": "^5.0.0 || ^6.0.0",
79
83
  "reflect-metadata": "^0.1.12 || ^0.2.0",
80
84
  "rxjs": "^7.0.0"
81
85
  },
82
86
  "peerDependenciesMeta": {
87
+ "@nestjs/bull": {
88
+ "optional": true
89
+ },
83
90
  "@nestjs/bullmq": {
84
91
  "optional": true
85
92
  },
@@ -92,6 +99,12 @@
92
99
  "@nestjs/schedule": {
93
100
  "optional": true
94
101
  },
102
+ "@nestjs/websockets": {
103
+ "optional": true
104
+ },
105
+ "bull": {
106
+ "optional": true
107
+ },
95
108
  "bullmq": {
96
109
  "optional": true
97
110
  }
@@ -103,36 +116,56 @@
103
116
  "@commitlint/config-angular": "21.2.2",
104
117
  "@grpc/grpc-js": "1.14.4",
105
118
  "@grpc/proto-loader": "0.8.1",
119
+ "@mikro-orm/core": "7.2.0",
120
+ "@mikro-orm/mysql": "7.2.0",
121
+ "@mikro-orm/postgresql": "7.2.0",
106
122
  "@nestjs/apollo": "14.0.0",
123
+ "@nestjs/bull": "12.0.0",
107
124
  "@nestjs/bullmq": "12.0.0",
108
125
  "@nestjs/common": "12.0.1",
109
126
  "@nestjs/core": "12.0.1",
110
127
  "@nestjs/graphql": "14.0.0",
111
128
  "@nestjs/microservices": "12.0.1",
112
129
  "@nestjs/platform-express": "12.0.1",
130
+ "@nestjs/platform-fastify": "12.0.3",
131
+ "@nestjs/platform-socket.io": "12.0.3",
132
+ "@nestjs/platform-ws": "12.0.3",
113
133
  "@nestjs/schedule": "12.0.1",
114
134
  "@nestjs/testing": "12.0.1",
135
+ "@nestjs/websockets": "12.0.3",
115
136
  "@swc/core": "1.16.1",
116
137
  "@types/node": "22.20.1",
138
+ "@types/pg": "8.23.1",
117
139
  "@types/supertest": "7.2.1",
140
+ "@types/ws": "8.18.1",
118
141
  "@vitest/coverage-v8": "4.1.11",
142
+ "bull": "4.16.5",
119
143
  "bullmq": "6.2.0",
144
+ "drizzle-orm": "0.45.2",
120
145
  "express": "5.2.1",
121
146
  "graphql": "16.14.2",
122
147
  "husky": "9.1.7",
148
+ "ioredis": "6.0.0",
123
149
  "lint-staged": "17.3.0",
150
+ "mongodb": "7.6.0",
151
+ "mongoose": "9.10.1",
152
+ "mysql2": "3.24.4",
124
153
  "nestjs-cls": "6.2.1",
125
154
  "oxlint": "1.79.0",
126
155
  "oxlint-tsgolint": "7.0.2001",
156
+ "pg": "8.23.0",
127
157
  "prettier": "3.2.5",
128
158
  "reflect-metadata": "0.2.2",
129
159
  "release-it": "21.0.2",
130
160
  "rimraf": "6.1.3",
131
161
  "rxjs": "7.8.2",
162
+ "socket.io-client": "4.8.3",
132
163
  "supertest": "7.2.2",
164
+ "typeorm": "1.1.1",
133
165
  "typescript": "5.9.3",
134
166
  "unplugin-swc": "1.5.11",
135
- "vitest": "4.1.11"
167
+ "vitest": "4.1.11",
168
+ "ws": "8.21.3"
136
169
  },
137
170
  "overrides": {
138
171
  "nestjs-cls": {