@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.
Files changed (109) hide show
  1. package/README.md +55 -2
  2. package/dist/agent/detached-observe-worker.js +50 -6
  3. package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
  4. package/dist/agent/observe-agent.shared-buffer.js +201 -19
  5. package/dist/agent/observe-agent.worker.d.ts +22 -2
  6. package/dist/agent/observe-agent.worker.js +43 -7
  7. package/dist/agent/telemetry-wire-contract.js +10 -4
  8. package/dist/cli/agent-notes.js +16 -4
  9. package/dist/cli/link.js +51 -4
  10. package/dist/cli/observe-cli.js +1 -19
  11. package/dist/cli/open-url.d.ts +27 -0
  12. package/dist/cli/open-url.js +63 -0
  13. package/dist/cli/package-manager.d.ts +6 -0
  14. package/dist/cli/package-manager.js +60 -3
  15. package/dist/cli/project-files.d.ts +62 -1
  16. package/dist/cli/project-files.js +229 -7
  17. package/dist/cli/project-setup.d.ts +6 -0
  18. package/dist/cli/project-setup.js +14 -1
  19. package/dist/cli/wire-application.js +6 -0
  20. package/dist/custom-metrics/counter.d.ts +13 -2
  21. package/dist/custom-metrics/counter.js +33 -16
  22. package/dist/custom-metrics/finite-value.util.d.ts +14 -0
  23. package/dist/custom-metrics/finite-value.util.js +33 -1
  24. package/dist/custom-metrics/gauge.d.ts +2 -0
  25. package/dist/custom-metrics/gauge.js +41 -17
  26. package/dist/custom-metrics/label-values.util.d.ts +37 -0
  27. package/dist/custom-metrics/label-values.util.js +51 -0
  28. package/dist/custom-metrics/summary.d.ts +23 -0
  29. package/dist/custom-metrics/summary.js +90 -12
  30. package/dist/instrument/create-instance-decorator.instrument.js +504 -116
  31. package/dist/instrument/raw-receiver.d.ts +6 -0
  32. package/dist/instrument/raw-receiver.js +32 -0
  33. package/dist/interfaces/observe-options.interface.d.ts +17 -6
  34. package/dist/objectives/objectives.registry.js +12 -0
  35. package/dist/observe.module.js +119 -3
  36. package/dist/otlp/otlp-encoders.js +22 -5
  37. package/dist/otlp/otlp-span.mapper.d.ts +2 -0
  38. package/dist/otlp/otlp-span.mapper.js +26 -4
  39. package/dist/otlp/otlp-trace-exporter.service.d.ts +63 -9
  40. package/dist/otlp/otlp-trace-exporter.service.js +295 -43
  41. package/dist/outgoing/http.integration.d.ts +8 -4
  42. package/dist/outgoing/http.integration.js +49 -17
  43. package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
  44. package/dist/outgoing/llm/llm-providers.d.ts +16 -4
  45. package/dist/outgoing/llm/llm-providers.js +72 -22
  46. package/dist/outgoing/llm/llm-usage.d.ts +11 -6
  47. package/dist/outgoing/llm/llm-usage.js +72 -25
  48. package/dist/outgoing/mongodb.integration.d.ts +3 -1
  49. package/dist/outgoing/mongodb.integration.js +93 -18
  50. package/dist/outgoing/mssql.integration.js +25 -6
  51. package/dist/outgoing/mysql2.integration.js +17 -1
  52. package/dist/outgoing/outgoing-observe-agent.service.js +20 -4
  53. package/dist/outgoing/outgoing-span.recorder.js +17 -3
  54. package/dist/outgoing/pg.integration.d.ts +3 -2
  55. package/dist/outgoing/pg.integration.js +39 -11
  56. package/dist/outgoing/sql-statement.util.js +139 -21
  57. package/dist/outgoing/undici.integration.d.ts +13 -0
  58. package/dist/outgoing/undici.integration.js +68 -0
  59. package/dist/protocols/bull-observe-agent.service.js +23 -12
  60. package/dist/protocols/deferred-trace-completion.d.ts +17 -3
  61. package/dist/protocols/deferred-trace-completion.js +76 -9
  62. package/dist/protocols/graphql-observe-agent.service.d.ts +97 -6
  63. package/dist/protocols/graphql-observe-agent.service.js +354 -19
  64. package/dist/protocols/graphql-operation-parser.js +111 -26
  65. package/dist/protocols/http-observe-agent.service.d.ts +50 -9
  66. package/dist/protocols/http-observe-agent.service.js +220 -59
  67. package/dist/protocols/job-trace-runner.d.ts +42 -3
  68. package/dist/protocols/job-trace-runner.js +197 -36
  69. package/dist/protocols/queue-observe-agent.service.js +44 -20
  70. package/dist/protocols/rpc-observe-agent.service.d.ts +111 -8
  71. package/dist/protocols/rpc-observe-agent.service.js +332 -56
  72. package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
  73. package/dist/protocols/ws-observe-agent.service.js +227 -21
  74. package/dist/services/index.d.ts +1 -1
  75. package/dist/services/index.js +1 -1
  76. package/dist/services/logger-patcher.service.js +16 -7
  77. package/dist/services/operation-trace.registry.d.ts +58 -1
  78. package/dist/services/operation-trace.registry.js +576 -63
  79. package/dist/services/stdout-forwarder.service.d.ts +69 -0
  80. package/dist/services/stdout-forwarder.service.js +308 -51
  81. package/dist/services/tracer.service.d.ts +39 -11
  82. package/dist/services/tracer.service.js +145 -36
  83. package/dist/utils/capture-request.util.d.ts +3 -1
  84. package/dist/utils/capture-request.util.js +128 -12
  85. package/dist/utils/context-accessor.util.d.ts +16 -0
  86. package/dist/utils/context-accessor.util.js +28 -0
  87. package/dist/utils/default-trace-id-generator.util.js +5 -7
  88. package/dist/utils/error-stack.util.d.ts +13 -0
  89. package/dist/utils/error-stack.util.js +166 -0
  90. package/dist/utils/fit-to-length.util.js +33 -10
  91. package/dist/utils/log-line.parser.d.ts +22 -0
  92. package/dist/utils/log-line.parser.js +20 -7
  93. package/dist/utils/log-redactor.d.ts +14 -0
  94. package/dist/utils/log-redactor.js +690 -65
  95. package/dist/utils/operation-state.util.d.ts +39 -0
  96. package/dist/utils/operation-state.util.js +50 -0
  97. package/dist/utils/redact-url-query.d.ts +37 -4
  98. package/dist/utils/redact-url-query.js +197 -32
  99. package/dist/utils/sensitive-keys.d.ts +4 -1
  100. package/dist/utils/sensitive-keys.js +48 -10
  101. package/dist/utils/shared-hook-slot.util.d.ts +48 -0
  102. package/dist/utils/shared-hook-slot.util.js +103 -0
  103. package/dist/utils/source-context.util.d.ts +14 -0
  104. package/dist/utils/source-context.util.js +142 -43
  105. package/dist/utils/source-map-resolver.util.js +33 -6
  106. package/dist/utils/store-copy.util.d.ts +10 -0
  107. package/dist/utils/store-copy.util.js +43 -0
  108. package/dist/utils/trace-context.util.js +21 -6
  109. package/package.json +2 -2
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The longest series key - the serialised label combination - the collector
3
+ * accepts. A longer one is not truncated there: the whole batch it rides in is
4
+ * refused, logs and spans included. The longest label it accepts, too, which
5
+ * is what a summary keys its series by.
6
+ */
7
+ export declare const MAX_SERIES_KEY_LENGTH = 255;
8
+ /**
9
+ * Whether a series key can be made from a label set's values, checked before
10
+ * it is made.
11
+ *
12
+ * A label value is a string or a number - a boolean or a null keys as well -
13
+ * but a handler that passes a field of a JSON body on unchecked can hand a
14
+ * metric anything JSON parses to. Made into a key, an array nested a few
15
+ * thousand levels deep overflowed the stack and a bigint threw a TypeError,
16
+ * out of the metric call and into the request being measured. An object or
17
+ * an array is refused whatever its depth: no label set is meant to hold one.
18
+ * Refuses rather than throws, for the reason `admitsSeries` gives, and says
19
+ * so once per metric.
20
+ */
21
+ export declare function admitsLabelValues(metricName: string, label: object, state: {
22
+ warned: boolean;
23
+ }): boolean;
24
+ /**
25
+ * Whether the first argument of a counter's or a gauge's call is a label set
26
+ * rather than the value.
27
+ *
28
+ * Not `typeof === "object"` alone, which a `null` passes: what a number field
29
+ * of a JSON body holds when the client sent `null`, or when the sender
30
+ * serialised a NaN. Taken for a label set, it threw from `Object.keys(null)`
31
+ * into the request being measured; taken for the value, it is a missing one,
32
+ * as `undefined` is.
33
+ *
34
+ * Kept out of `counter.ts`, for the reason `finite-value.util.ts` is: whatever
35
+ * that module exports is re-exported from the package root.
36
+ */
37
+ export declare function isLabelSet<TLabels extends object>(labelOrValue: TLabels | number | null | undefined): labelOrValue is TLabels;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The longest series key - the serialised label combination - the collector
3
+ * accepts. A longer one is not truncated there: the whole batch it rides in is
4
+ * refused, logs and spans included. The longest label it accepts, too, which
5
+ * is what a summary keys its series by.
6
+ */
7
+ export const MAX_SERIES_KEY_LENGTH = 255;
8
+ /**
9
+ * Whether a series key can be made from a label set's values, checked before
10
+ * it is made.
11
+ *
12
+ * A label value is a string or a number - a boolean or a null keys as well -
13
+ * but a handler that passes a field of a JSON body on unchecked can hand a
14
+ * metric anything JSON parses to. Made into a key, an array nested a few
15
+ * thousand levels deep overflowed the stack and a bigint threw a TypeError,
16
+ * out of the metric call and into the request being measured. An object or
17
+ * an array is refused whatever its depth: no label set is meant to hold one.
18
+ * Refuses rather than throws, for the reason `admitsSeries` gives, and says
19
+ * so once per metric.
20
+ */
21
+ export function admitsLabelValues(metricName, label, state) {
22
+ for (const name in label) {
23
+ const value = label[name];
24
+ if ((typeof value === "object" && value !== null) ||
25
+ typeof value === "bigint") {
26
+ if (!state.warned) {
27
+ state.warned = true;
28
+ console.warn(`[observe] Metric "${metricName}" ignored a label set holding a value of type ${typeof value}; ` +
29
+ `label values are strings or numbers. Further ones are ignored without another warning.`);
30
+ }
31
+ return false;
32
+ }
33
+ }
34
+ return true;
35
+ }
36
+ /**
37
+ * Whether the first argument of a counter's or a gauge's call is a label set
38
+ * rather than the value.
39
+ *
40
+ * Not `typeof === "object"` alone, which a `null` passes: what a number field
41
+ * of a JSON body holds when the client sent `null`, or when the sender
42
+ * serialised a NaN. Taken for a label set, it threw from `Object.keys(null)`
43
+ * into the request being measured; taken for the value, it is a missing one,
44
+ * as `undefined` is.
45
+ *
46
+ * Kept out of `counter.ts`, for the reason `finite-value.util.ts` is: whatever
47
+ * that module exports is re-exported from the package root.
48
+ */
49
+ export function isLabelSet(labelOrValue) {
50
+ return typeof labelOrValue === "object" && labelOrValue !== null;
51
+ }
@@ -17,6 +17,14 @@ export declare class Summary<TLabel extends string = typeof DEFAULT_LABEL> imple
17
17
  * `admitsFinite` can set it, as with the counter's and gauge's latches.
18
18
  */
19
19
  private readonly nonFinite;
20
+ /**
21
+ * Whether a label past the cap, one too long, or one that is not a string
22
+ * has been reported: once per metric, though the cap is reached again in
23
+ * every window it fills.
24
+ */
25
+ private readonly labelLimit;
26
+ /** How many labels the current window holds. */
27
+ private _windowLabels;
20
28
  /**
21
29
  * The name of the summary.
22
30
  * This should be a descriptive name that identifies the distribution.
@@ -128,6 +136,21 @@ export declare class Summary<TLabel extends string = typeof DEFAULT_LABEL> imple
128
136
  * rather than being thrown away.
129
137
  */
130
138
  markFlushed(): void;
139
+ /**
140
+ * Whether a summary that declared no labels may record under `label`: one
141
+ * this window already holds, or a new one within the cap and short enough
142
+ * for the collector, as a counter's series is.
143
+ *
144
+ * Without it the labels were bounded by nothing but the next flush written,
145
+ * which an agent that sends nothing - switched off, or without credentials -
146
+ * never does: a label taken from a request, a search term say, stayed for
147
+ * the life of the process, at whatever length the request allowed. Nor is
148
+ * a label from a request certain to be a string, and an object or an array
149
+ * that cannot be made one - `{"toString": 1}`, an array nested a few
150
+ * thousand levels deep - threw where it was used as a key. Refuses rather
151
+ * than throws, for the reason `admitsSeries` gives.
152
+ */
153
+ private admitsLabel;
131
154
  /**
132
155
  * Keeps the retained sample uniform over the whole window (Algorithm R).
133
156
  *
@@ -1,4 +1,6 @@
1
- import { admitsFinite } from "./finite-value.util.js";
1
+ import { MAX_SERIES_PER_METRIC } from "./counter.js";
2
+ import { admitsFinite, admitsReading } from "./finite-value.util.js";
3
+ import { MAX_SERIES_KEY_LENGTH } from "./label-values.util.js";
2
4
  const DEFAULT_LABEL = "default";
3
5
  /**
4
6
  * How many observations per label are kept to compute quantiles from.
@@ -8,6 +10,13 @@ const DEFAULT_LABEL = "default";
8
10
  * separately and stay exact however many observations arrive.
9
11
  */
10
12
  const DEFAULT_SAMPLE_SIZE = 2048;
13
+ /**
14
+ * A record keyed by label, with no prototype. A summary that declared no
15
+ * labels takes any, and on a plain object a label such as `constructor` or
16
+ * `toString` found an inherited member: its first observation was added to a
17
+ * function, and refused as a total past the largest number.
18
+ */
19
+ const byLabel = () => Object.create(null);
11
20
  /**
12
21
  * A distribution of observations - request latency being the usual case.
13
22
  *
@@ -25,6 +34,18 @@ export class Summary {
25
34
  * `admitsFinite` can set it, as with the counter's and gauge's latches.
26
35
  */
27
36
  nonFinite = { warned: false };
37
+ /**
38
+ * Whether a label past the cap, one too long, or one that is not a string
39
+ * has been reported: once per metric, though the cap is reached again in
40
+ * every window it fills.
41
+ */
42
+ labelLimit = {
43
+ capWarned: false,
44
+ longWarned: false,
45
+ typeWarned: false,
46
+ };
47
+ /** How many labels the current window holds. */
48
+ _windowLabels = 0;
28
49
  /**
29
50
  * The name of the summary.
30
51
  * This should be a descriptive name that identifies the distribution.
@@ -54,13 +75,13 @@ export class Summary {
54
75
  * Exact per-label aggregates for the current window. Tracked apart from the
55
76
  * sample below so they describe every observation, not just the retained ones.
56
77
  */
57
- _count = {};
58
- _sum = {};
59
- _max = {};
78
+ _count = byLabel();
79
+ _sum = byLabel();
80
+ _max = byLabel();
60
81
  /**
61
82
  * Bounded per-label sample the quantiles are read off.
62
83
  */
63
- _samples = {};
84
+ _samples = byLabel();
64
85
  /**
65
86
  * The medians, doubling as the metric's headline reading.
66
87
  *
@@ -153,9 +174,21 @@ export class Summary {
153
174
  return;
154
175
  }
155
176
  const key = label ?? DEFAULT_LABEL;
177
+ if (!this.labels && !this.admitsLabel(key)) {
178
+ return;
179
+ }
180
+ // Before anything is updated, so the count, maximum and sample keep
181
+ // describing the observations the total does.
182
+ const sum = (this._sum[key] ?? 0) + value;
183
+ if (!admitsReading(this.name, sum, this.nonFinite)) {
184
+ return;
185
+ }
156
186
  const count = (this._count[key] ?? 0) + 1;
187
+ if (count === 1) {
188
+ this._windowLabels += 1;
189
+ }
157
190
  this._count[key] = count;
158
- this._sum[key] = (this._sum[key] ?? 0) + value;
191
+ this._sum[key] = sum;
159
192
  this._max[key] = Math.max(this._max[key] ?? value, value);
160
193
  this.sample(key, value, count);
161
194
  this._lastUpdated = Date.now();
@@ -171,10 +204,55 @@ export class Summary {
171
204
  * rather than being thrown away.
172
205
  */
173
206
  markFlushed() {
174
- this._count = {};
175
- this._sum = {};
176
- this._max = {};
177
- this._samples = {};
207
+ this._windowLabels = 0;
208
+ this._count = byLabel();
209
+ this._sum = byLabel();
210
+ this._max = byLabel();
211
+ this._samples = byLabel();
212
+ }
213
+ /**
214
+ * Whether a summary that declared no labels may record under `label`: one
215
+ * this window already holds, or a new one within the cap and short enough
216
+ * for the collector, as a counter's series is.
217
+ *
218
+ * Without it the labels were bounded by nothing but the next flush written,
219
+ * which an agent that sends nothing - switched off, or without credentials -
220
+ * never does: a label taken from a request, a search term say, stayed for
221
+ * the life of the process, at whatever length the request allowed. Nor is
222
+ * a label from a request certain to be a string, and an object or an array
223
+ * that cannot be made one - `{"toString": 1}`, an array nested a few
224
+ * thousand levels deep - threw where it was used as a key. Refuses rather
225
+ * than throws, for the reason `admitsSeries` gives.
226
+ */
227
+ admitsLabel(label) {
228
+ if (typeof label !== "string") {
229
+ if (!this.labelLimit.typeWarned) {
230
+ this.labelLimit.typeWarned = true;
231
+ console.warn(`[observe] Metric "${this.name}" ignored a label of type ${typeof label}; a summary's labels are strings.`);
232
+ }
233
+ return false;
234
+ }
235
+ if (label in this._count) {
236
+ return true;
237
+ }
238
+ if (label.length > MAX_SERIES_KEY_LENGTH) {
239
+ if (!this.labelLimit.longWarned) {
240
+ this.labelLimit.longWarned = true;
241
+ console.warn(`[observe] Metric "${this.name}" ignored a label of ${label.length} characters; ` +
242
+ `the collector accepts at most ${MAX_SERIES_KEY_LENGTH}. Shorter labels keep recording.`);
243
+ }
244
+ return false;
245
+ }
246
+ if (this._windowLabels < MAX_SERIES_PER_METRIC) {
247
+ return true;
248
+ }
249
+ if (!this.labelLimit.capWarned) {
250
+ this.labelLimit.capWarned = true;
251
+ console.warn(`[observe] Metric "${this.name}" reached ${MAX_SERIES_PER_METRIC} distinct labels in one window; ` +
252
+ `further labels are ignored until the next flush. This usually means a label carries something ` +
253
+ `unbounded (a user id, a search term, a URL). Existing labels keep recording.`);
254
+ }
255
+ return false;
178
256
  }
179
257
  /**
180
258
  * Keeps the retained sample uniform over the whole window (Algorithm R).
@@ -199,7 +277,7 @@ export class Summary {
199
277
  * Nearest-rank quantile per label, read off a sorted copy of the sample.
200
278
  */
201
279
  quantile(percentile) {
202
- const quantiles = {};
280
+ const quantiles = byLabel();
203
281
  for (const [key, samples] of Object.entries(this._samples)) {
204
282
  if (!samples.length) {
205
283
  continue;
@@ -208,6 +286,6 @@ export class Summary {
208
286
  const rank = Math.ceil(percentile * sorted.length) - 1;
209
287
  quantiles[key] = sorted[Math.min(Math.max(rank, 0), sorted.length - 1)];
210
288
  }
211
- return quantiles;
289
+ return { ...quantiles };
212
290
  }
213
291
  }