@nestjs/observe 0.3.9 → 0.3.11

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.
@@ -23,14 +23,46 @@ const ANSI_PATTERN = /\x1B\[[0-9;]*[A-Za-z]/g;
23
23
  * Nest's default ConsoleLogger line:
24
24
  * [Nest] 1234 - 07/29/2026, 10:42:18 AM LOG [AppController] message
25
25
  * The context bracket is optional - `logger.log()` without a context omits it.
26
+ * The level is right-aligned in seven columns, so `VERBOSE` follows the
27
+ * timestamp after a single space and every other level after several.
28
+ *
29
+ * Must stay linear in the length of the line: it runs on every line the
30
+ * process writes, synchronously inside `process.stdout.write`, on text a caller
31
+ * can shape. The pattern this replaced read `.+?\s{2,}` here - two quantifiers
32
+ * that could both take the same run of whitespace - and backtracked cubically
33
+ * on a `[Nest]` line that never reached a level: seconds for a few thousand
34
+ * spaces. Here no two neighbouring quantifiers can take the same character.
35
+ * The timestamp opens with a non-space, exactly one whitespace character
36
+ * precedes the level, and nothing after the level can fail.
26
37
  */
27
- const NEST_TEXT_PATTERN = /^\[Nest\]\s+\d+\s+-\s+.+?\s{2,}(FATAL|ERROR|WARN|LOG|DEBUG|VERBOSE)\s+(?:\[([^\]]+)\]\s*)?([\s\S]*)$/;
38
+ const NEST_TEXT_PATTERN = /^\[Nest\]\s+\d+\s+-\s+\S.*?\s(FATAL|ERROR|WARN|LOG|DEBUG|VERBOSE)\s+(?:\[([^\]]+)\]\s*)?([\s\S]*)$/;
28
39
  /**
29
- * Suffix appended by LoggerPatcherService when a trace is in scope. Parsing it
30
- * back off recovers the trace id even for lines whose async context is gone by
31
- * the time the forwarder sees them.
40
+ * Marker LoggerPatcherService puts before the trace id it appends when a trace
41
+ * is in scope. Parsing the id back off recovers it even for lines whose async
42
+ * context is gone by the time the forwarder sees them.
32
43
  */
33
- const TRACE_ID_SUFFIX_PATTERN = /\s*Trace ID:\s*(\S+)\s*$/;
44
+ const TRACE_ID_MARKER = "Trace ID:";
45
+ /** What may follow the marker: one token, and nothing after it. */
46
+ const TRACE_ID_VALUE_PATTERN = /^\s*(\S+)\s*$/;
47
+ /**
48
+ * The levels the collector accepts, which are Nest's own. It refuses a batch
49
+ * carrying any other value whole - every trace and metric in it lost over one
50
+ * log line - so nothing else is ever put in `level`.
51
+ */
52
+ const LOG_LEVELS = new Set([
53
+ "fatal",
54
+ "error",
55
+ "warn",
56
+ "log",
57
+ "debug",
58
+ "verbose",
59
+ ]);
60
+ /** Other loggers' names for a level the collector models. */
61
+ const LEVEL_ALIASES = new Map([
62
+ ["info", "log"],
63
+ ["warning", "warn"],
64
+ ["trace", "verbose"],
65
+ ]);
34
66
  /**
35
67
  * Keys the JSON logger sets that map onto modelled columns; anything else a
36
68
  * service attaches is preserved under `attributes`.
@@ -71,9 +103,13 @@ function parseStructured(line) {
71
103
  if (typeof record.message !== "string") {
72
104
  return null;
73
105
  }
74
- const attributes = Object.fromEntries(Object.entries(record).filter(([key]) => !STRUCTURED_KEYS.has(key)));
106
+ const level = toLogLevel(record.level);
107
+ const attributes = Object.fromEntries(Object.entries(record).filter(
108
+ // A level the collector does not model - pino's numeric ones, a logger's
109
+ // own `notice` - stays an attribute rather than being dropped.
110
+ ([key]) => !STRUCTURED_KEYS.has(key) || (key === "level" && level === undefined)));
75
111
  return {
76
- level: typeof record.level === "string" ? record.level : undefined,
112
+ level,
77
113
  context: typeof record.context === "string" ? record.context : undefined,
78
114
  traceId: typeof record.traceId === "string" ? record.traceId : undefined,
79
115
  message: record.message,
@@ -86,11 +122,38 @@ function parseNestText(line) {
86
122
  return null;
87
123
  }
88
124
  const [, level, context, rest] = match;
89
- const traceIdMatch = TRACE_ID_SUFFIX_PATTERN.exec(rest);
125
+ const { message, traceId } = splitTraceIdSuffix(rest);
90
126
  return {
91
- level: level.toLowerCase(),
127
+ level: toLogLevel(level),
92
128
  context: context || undefined,
93
- traceId: traceIdMatch?.[1],
94
- message: traceIdMatch ? rest.slice(0, traceIdMatch.index).trimEnd() : rest,
129
+ traceId,
130
+ message,
95
131
  };
96
132
  }
133
+ /**
134
+ * Splits the trace id suffix off the end of a message, when there is one.
135
+ *
136
+ * Found with `lastIndexOf` rather than an unanchored `\s*Trace ID:...$`
137
+ * pattern, which retried its leading `\s*` from every offset - quadratic in a
138
+ * long run of whitespace, and that whitespace can come from a caller. Only the
139
+ * last marker can be the suffix anyway: the marker itself contains a space, so
140
+ * an earlier one is always followed by more than one token.
141
+ */
142
+ function splitTraceIdSuffix(rest) {
143
+ const markerIndex = rest.lastIndexOf(TRACE_ID_MARKER);
144
+ if (markerIndex === -1) {
145
+ return { message: rest };
146
+ }
147
+ const value = TRACE_ID_VALUE_PATTERN.exec(rest.slice(markerIndex + TRACE_ID_MARKER.length));
148
+ if (!value) {
149
+ return { message: rest };
150
+ }
151
+ return { message: rest.slice(0, markerIndex).trimEnd(), traceId: value[1] };
152
+ }
153
+ function toLogLevel(value) {
154
+ if (typeof value !== "string") {
155
+ return undefined;
156
+ }
157
+ const lower = value.toLowerCase();
158
+ return LOG_LEVELS.has(lower) ? lower : LEVEL_ALIASES.get(lower);
159
+ }
@@ -84,8 +84,13 @@ function buildDefaultRules(literalValuesOnly = false) {
84
84
  replace: (replacement, prefix) => `${prefix}${replacement}`,
85
85
  },
86
86
  // Bare JWTs - they show up outside any header often enough to matter.
87
+ //
88
+ // Must not follow a token character, rather than `\b`: a hyphen is a word
89
+ // boundary but also a token character, so under `\b` every `eyJ` in a run
90
+ // like `eyJ-eyJ-eyJ-...` started a match that rescanned the rest of the
91
+ // run - quadratic, on a line a caller can fill.
87
92
  {
88
- pattern: /\beyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*/g,
93
+ pattern: /(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*/g,
89
94
  replace: (replacement) => replacement,
90
95
  },
91
96
  // AWS access key ids. Distinctive enough to match on sight; the matching
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
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",