@nestjs/observe 0.3.24 → 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.
Files changed (97) hide show
  1. package/README.md +26 -1
  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 +196 -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 +7 -1
  8. package/dist/cli/agent-notes.js +16 -4
  9. package/dist/cli/link.js +28 -3
  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/project-files.d.ts +42 -1
  14. package/dist/cli/project-files.js +146 -7
  15. package/dist/cli/project-setup.d.ts +6 -0
  16. package/dist/cli/project-setup.js +14 -1
  17. package/dist/cli/wire-application.js +6 -0
  18. package/dist/custom-metrics/counter.d.ts +13 -2
  19. package/dist/custom-metrics/counter.js +33 -16
  20. package/dist/custom-metrics/finite-value.util.d.ts +14 -0
  21. package/dist/custom-metrics/finite-value.util.js +33 -1
  22. package/dist/custom-metrics/gauge.d.ts +2 -0
  23. package/dist/custom-metrics/gauge.js +43 -21
  24. package/dist/custom-metrics/label-values.util.d.ts +37 -0
  25. package/dist/custom-metrics/label-values.util.js +51 -0
  26. package/dist/custom-metrics/summary.d.ts +23 -0
  27. package/dist/custom-metrics/summary.js +73 -2
  28. package/dist/instrument/create-instance-decorator.instrument.js +244 -97
  29. package/dist/interfaces/observe-options.interface.d.ts +3 -1
  30. package/dist/objectives/objectives.registry.js +12 -0
  31. package/dist/observe.module.js +119 -3
  32. package/dist/otlp/otlp-encoders.js +22 -5
  33. package/dist/otlp/otlp-span.mapper.d.ts +4 -3
  34. package/dist/otlp/otlp-span.mapper.js +16 -8
  35. package/dist/otlp/otlp-trace-exporter.service.d.ts +3 -0
  36. package/dist/otlp/otlp-trace-exporter.service.js +71 -6
  37. package/dist/outgoing/http.integration.d.ts +8 -4
  38. package/dist/outgoing/http.integration.js +13 -6
  39. package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
  40. package/dist/outgoing/llm/llm-providers.d.ts +16 -4
  41. package/dist/outgoing/llm/llm-providers.js +72 -22
  42. package/dist/outgoing/llm/llm-usage.d.ts +11 -6
  43. package/dist/outgoing/llm/llm-usage.js +72 -25
  44. package/dist/outgoing/mssql.integration.js +16 -2
  45. package/dist/outgoing/mysql2.integration.js +17 -1
  46. package/dist/outgoing/outgoing-observe-agent.service.js +15 -3
  47. package/dist/outgoing/outgoing-span.recorder.js +17 -3
  48. package/dist/outgoing/pg.integration.d.ts +3 -2
  49. package/dist/outgoing/pg.integration.js +39 -11
  50. package/dist/outgoing/sql-statement.util.js +106 -14
  51. package/dist/outgoing/undici.integration.d.ts +13 -0
  52. package/dist/outgoing/undici.integration.js +68 -0
  53. package/dist/protocols/bull-observe-agent.service.js +2 -12
  54. package/dist/protocols/deferred-trace-completion.d.ts +9 -1
  55. package/dist/protocols/deferred-trace-completion.js +36 -6
  56. package/dist/protocols/graphql-observe-agent.service.d.ts +36 -5
  57. package/dist/protocols/graphql-observe-agent.service.js +166 -5
  58. package/dist/protocols/graphql-operation-parser.js +72 -18
  59. package/dist/protocols/http-observe-agent.service.d.ts +43 -9
  60. package/dist/protocols/http-observe-agent.service.js +177 -59
  61. package/dist/protocols/job-trace-runner.d.ts +35 -3
  62. package/dist/protocols/job-trace-runner.js +136 -31
  63. package/dist/protocols/queue-observe-agent.service.js +44 -20
  64. package/dist/protocols/rpc-observe-agent.service.d.ts +50 -8
  65. package/dist/protocols/rpc-observe-agent.service.js +225 -49
  66. package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
  67. package/dist/protocols/ws-observe-agent.service.js +226 -20
  68. package/dist/services/index.d.ts +1 -1
  69. package/dist/services/index.js +1 -1
  70. package/dist/services/logger-patcher.service.js +3 -4
  71. package/dist/services/operation-trace.registry.d.ts +45 -1
  72. package/dist/services/operation-trace.registry.js +418 -35
  73. package/dist/services/stdout-forwarder.service.d.ts +69 -0
  74. package/dist/services/stdout-forwarder.service.js +288 -51
  75. package/dist/services/tracer.service.d.ts +23 -0
  76. package/dist/services/tracer.service.js +99 -7
  77. package/dist/utils/capture-request.util.d.ts +3 -1
  78. package/dist/utils/capture-request.util.js +100 -12
  79. package/dist/utils/fit-to-length.util.js +33 -10
  80. package/dist/utils/log-line.parser.d.ts +14 -0
  81. package/dist/utils/log-line.parser.js +7 -3
  82. package/dist/utils/log-redactor.d.ts +14 -0
  83. package/dist/utils/log-redactor.js +512 -61
  84. package/dist/utils/operation-state.util.d.ts +39 -0
  85. package/dist/utils/operation-state.util.js +50 -0
  86. package/dist/utils/redact-url-query.d.ts +26 -4
  87. package/dist/utils/redact-url-query.js +184 -32
  88. package/dist/utils/sensitive-keys.d.ts +4 -1
  89. package/dist/utils/sensitive-keys.js +48 -10
  90. package/dist/utils/shared-hook-slot.util.d.ts +48 -0
  91. package/dist/utils/shared-hook-slot.util.js +103 -0
  92. package/dist/utils/source-context.util.d.ts +8 -0
  93. package/dist/utils/source-context.util.js +88 -19
  94. package/dist/utils/source-map-resolver.util.js +8 -2
  95. package/dist/utils/trace-context.util.d.ts +20 -0
  96. package/dist/utils/trace-context.util.js +50 -6
  97. package/package.json +4 -1
@@ -1,4 +1,5 @@
1
- import { admitsFinite } from "./finite-value.util.js";
1
+ import { admitsFinite, admitsReading } from "./finite-value.util.js";
2
+ import { admitsLabelValues, isLabelSet, MAX_SERIES_KEY_LENGTH, } from "./label-values.util.js";
2
3
  const DEFAULT_LABEL = "default";
3
4
  /**
4
5
  * Serialises a label object into the map key that identifies its series.
@@ -21,18 +22,14 @@ export function stringifyLabel(label) {
21
22
  * flushed for the life of the process. That is correct for a counter and fatal
22
23
  * for one labelled with something unbounded - a user id, a request id, a raw
23
24
  * URL - which grows this object forever inside the *host application's* memory
24
- * and adds a row per series to every flush. Summaries need no such cap; their
25
- * windows are cleared on each flush.
25
+ * and adds a row per series to every flush. A summary's labels go when its
26
+ * window does, on each flush written - but a flush that is never written, by
27
+ * an agent switched off or without credentials, never clears one, so a
28
+ * summary that declared no labels is held to the same number per window.
26
29
  *
27
30
  * A thousand is far above a sensible label set and far below a leak.
28
31
  */
29
32
  export const MAX_SERIES_PER_METRIC = 1_000;
30
- /**
31
- * The longest series key - the serialised label combination - the collector
32
- * accepts. A longer one is not truncated there: the whole batch it rides in is
33
- * refused, logs and spans included.
34
- */
35
- const MAX_SERIES_KEY_LENGTH = 255;
36
33
  /**
37
34
  * Whether a metric may record under `key`: a series it already has, or a new
38
35
  * one that is within the cap and short enough for the collector.
@@ -80,6 +77,8 @@ export class Counter {
80
77
  seriesLimit = { warned: false };
81
78
  /** Whether a non-finite value has already been reported, likewise. */
82
79
  nonFinite = { warned: false };
80
+ /** Whether a label value no key can be made from has been reported. */
81
+ labelValues = { warned: false };
83
82
  /**
84
83
  * The name of the counter.
85
84
  * This should be a descriptive name that identifies the counter.
@@ -209,33 +208,51 @@ export class Counter {
209
208
  }
210
209
  }
211
210
  increment(labelOrValue, value) {
212
- if (this.labels && typeof labelOrValue !== "object") {
211
+ if (this.labels && !isLabelSet(labelOrValue)) {
213
212
  throw new Error("Cannot increment a counter with labels without specifying a label. For example, use `counter.increment({ route: '/login' })`.");
214
213
  }
215
- if (typeof labelOrValue === "object") {
214
+ if (isLabelSet(labelOrValue)) {
216
215
  this.validateLabels(labelOrValue);
216
+ if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
217
+ return;
218
+ }
217
219
  const stringifiedLabel = stringifyLabel(labelOrValue);
218
220
  const incrementValue = value ?? 1;
219
- if (!admitsFinite(this.name, incrementValue, this.nonFinite) ||
221
+ if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
222
+ return;
223
+ }
224
+ const reading = (this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
225
+ if (!this.admitsNewReading(stringifiedLabel, reading) ||
220
226
  !admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
221
227
  return;
222
228
  }
223
- this.value[stringifiedLabel] =
224
- (this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
229
+ this.value[stringifiedLabel] = reading;
225
230
  }
226
231
  else {
227
232
  const incrementValue = labelOrValue ?? 1;
228
233
  if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
229
234
  return;
230
235
  }
231
- this.value[DEFAULT_LABEL] =
232
- (this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
236
+ const reading = (this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
237
+ if (!this.admitsNewReading(DEFAULT_LABEL, reading)) {
238
+ return;
239
+ }
240
+ this.value[DEFAULT_LABEL] = reading;
233
241
  }
234
242
  this._lastUpdated = Date.now();
235
243
  if (this._onChange) {
236
244
  this._onChange(this);
237
245
  }
238
246
  }
247
+ /**
248
+ * Whether `key` may move to `reading`. The reading and its increase since
249
+ * the last flush both go on the wire, and the increase can leave the
250
+ * doubles while the reading stays in them: a counter taken down to -1e308,
251
+ * flushed, then brought back up to 1e308 is 2e308 above its baseline.
252
+ */
253
+ admitsNewReading(key, reading) {
254
+ return admitsReading(this.name, reading, this.nonFinite, this._lastFlushedValue[key] ?? 0);
255
+ }
239
256
  validateLabels(labeledValue) {
240
257
  const keys = Object.keys(labeledValue);
241
258
  const declared = this.labels;
@@ -18,3 +18,17 @@
18
18
  export declare function admitsFinite(metricName: string, value: number, state: {
19
19
  warned: boolean;
20
20
  }): boolean;
21
+ /**
22
+ * Whether a metric may move to `reading`, which an admitted value takes it to,
23
+ * when what goes on the wire is its difference from `baseline` - a counter's
24
+ * increase since the last flush - or the reading itself.
25
+ *
26
+ * Asked after `admitsFinite`, about the result rather than the argument: two
27
+ * finite numbers can add up to an Infinity (`1e308 + 1e308`), which is sent
28
+ * as null just the same, and which nothing added later would bring back. The
29
+ * step is ignored and the reading kept, under the same once-per-metric
30
+ * warning as a non-finite argument.
31
+ */
32
+ export declare function admitsReading(metricName: string, reading: number, state: {
33
+ warned: boolean;
34
+ }, baseline?: number): boolean;
@@ -21,9 +21,41 @@ export function admitsFinite(metricName, value, state) {
21
21
  }
22
22
  if (!state.warned) {
23
23
  state.warned = true;
24
- console.warn(`[observe] Metric "${metricName}" ignored ${value}, which is not a finite number. ` +
24
+ // Only what the warning is about - a NaN, an Infinity, a missing value -
25
+ // is printed. Anything else got past the type at run time, a field of a
26
+ // request body say, and need not convert to a string at all: `{"toString":
27
+ // 1}` throws a TypeError and a deeply nested array a RangeError, out of
28
+ // the call this function exists not to fail. Nor is a string any
29
+ // particular length, or free of line breaks.
30
+ const refused = value;
31
+ const given = typeof refused === "number" || refused === undefined || refused === null
32
+ ? String(refused)
33
+ : `a value of type ${typeof refused}`;
34
+ console.warn(`[observe] Metric "${metricName}" ignored ${given}, which is not a finite number. ` +
25
35
  `Further ones are ignored without another warning. This usually means the value is ` +
26
36
  `computed from something that can be zero or missing (a division by a zero total, a failed parse).`);
27
37
  }
28
38
  return false;
29
39
  }
40
+ /**
41
+ * Whether a metric may move to `reading`, which an admitted value takes it to,
42
+ * when what goes on the wire is its difference from `baseline` - a counter's
43
+ * increase since the last flush - or the reading itself.
44
+ *
45
+ * Asked after `admitsFinite`, about the result rather than the argument: two
46
+ * finite numbers can add up to an Infinity (`1e308 + 1e308`), which is sent
47
+ * as null just the same, and which nothing added later would bring back. The
48
+ * step is ignored and the reading kept, under the same once-per-metric
49
+ * warning as a non-finite argument.
50
+ */
51
+ export function admitsReading(metricName, reading, state, baseline = 0) {
52
+ if (Number.isFinite(reading - baseline)) {
53
+ return true;
54
+ }
55
+ if (!state.warned) {
56
+ state.warned = true;
57
+ console.warn(`[observe] Metric "${metricName}" ignored a value that would take it past the largest number it can report ` +
58
+ `(${Number.MAX_VALUE}). Further ones, and non-finite values, are ignored without another warning.`);
59
+ }
60
+ return false;
61
+ }
@@ -16,6 +16,8 @@ export declare class Gauge<TLabel extends string = typeof DEFAULT_LABEL> impleme
16
16
  private readonly seriesLimit;
17
17
  /** Whether a non-finite value has already been reported, likewise. */
18
18
  private readonly nonFinite;
19
+ /** Whether a label value no key can be made from has been reported. */
20
+ private readonly labelValues;
19
21
  /**
20
22
  * The name of the gauge.
21
23
  * This should be a descriptive name that identifies the gauge.
@@ -1,5 +1,6 @@
1
1
  import { admitsSeries, stringifyLabel } from "./counter.js";
2
- import { admitsFinite } from "./finite-value.util.js";
2
+ import { admitsFinite, admitsReading } from "./finite-value.util.js";
3
+ import { admitsLabelValues, isLabelSet } from "./label-values.util.js";
3
4
  const DEFAULT_LABEL = "default";
4
5
  export class Gauge {
5
6
  /**
@@ -10,6 +11,8 @@ export class Gauge {
10
11
  seriesLimit = { warned: false };
11
12
  /** Whether a non-finite value has already been reported, likewise. */
12
13
  nonFinite = { warned: false };
14
+ /** Whether a label value no key can be made from has been reported. */
15
+ labelValues = { warned: false };
13
16
  /**
14
17
  * The name of the gauge.
15
18
  * This should be a descriptive name that identifies the gauge.
@@ -112,27 +115,36 @@ export class Gauge {
112
115
  }
113
116
  }
114
117
  increment(labelOrValue, value) {
115
- if (this.labels && typeof labelOrValue !== "object") {
118
+ if (this.labels && !isLabelSet(labelOrValue)) {
116
119
  throw new Error("Cannot increment a gauge with labels without specifying a label. For example, use `gauge.increment({ route: '/login' })`.");
117
120
  }
118
- if (typeof labelOrValue === "object") {
121
+ if (isLabelSet(labelOrValue)) {
119
122
  this.validateLabels(labelOrValue);
123
+ if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
124
+ return;
125
+ }
120
126
  const stringifiedLabel = stringifyLabel(labelOrValue);
121
127
  const incrementValue = value ?? 1;
122
- if (!admitsFinite(this.name, incrementValue, this.nonFinite) ||
128
+ if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
129
+ return;
130
+ }
131
+ const reading = (this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
132
+ if (!admitsReading(this.name, reading, this.nonFinite) ||
123
133
  !admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
124
134
  return;
125
135
  }
126
- this.value[stringifiedLabel] =
127
- (this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
136
+ this.value[stringifiedLabel] = reading;
128
137
  }
129
138
  else {
130
139
  const incrementValue = labelOrValue ?? 1;
131
140
  if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
132
141
  return;
133
142
  }
134
- this.value[DEFAULT_LABEL] =
135
- (this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
143
+ const reading = (this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
144
+ if (!admitsReading(this.name, reading, this.nonFinite)) {
145
+ return;
146
+ }
147
+ this.value[DEFAULT_LABEL] = reading;
136
148
  }
137
149
  this._lastUpdated = Date.now();
138
150
  if (this._onChange) {
@@ -140,27 +152,36 @@ export class Gauge {
140
152
  }
141
153
  }
142
154
  decrement(labelOrValue, value) {
143
- if (this.labels && typeof labelOrValue !== "object") {
155
+ if (this.labels && !isLabelSet(labelOrValue)) {
144
156
  throw new Error("Cannot decrement a gauge with labels without specifying a label. For example, use `gauge.decrement({ route: '/login' })`.");
145
157
  }
146
- else if (typeof labelOrValue === "object") {
158
+ else if (isLabelSet(labelOrValue)) {
147
159
  this.validateLabels(labelOrValue);
160
+ if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
161
+ return;
162
+ }
148
163
  const stringifiedLabel = stringifyLabel(labelOrValue);
149
164
  const decrementValue = value ?? 1;
150
- if (!admitsFinite(this.name, decrementValue, this.nonFinite) ||
165
+ if (!admitsFinite(this.name, decrementValue, this.nonFinite)) {
166
+ return;
167
+ }
168
+ const reading = (this.value[stringifiedLabel] ?? this._initialValue) - decrementValue;
169
+ if (!admitsReading(this.name, reading, this.nonFinite) ||
151
170
  !admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
152
171
  return;
153
172
  }
154
- this.value[stringifiedLabel] =
155
- (this.value[stringifiedLabel] ?? this._initialValue) - decrementValue;
173
+ this.value[stringifiedLabel] = reading;
156
174
  }
157
175
  else {
158
176
  const decrementValue = labelOrValue ?? 1;
159
177
  if (!admitsFinite(this.name, decrementValue, this.nonFinite)) {
160
178
  return;
161
179
  }
162
- this.value[DEFAULT_LABEL] =
163
- (this.value[DEFAULT_LABEL] ?? this._initialValue) - decrementValue;
180
+ const reading = (this.value[DEFAULT_LABEL] ?? this._initialValue) - decrementValue;
181
+ if (!admitsReading(this.name, reading, this.nonFinite)) {
182
+ return;
183
+ }
184
+ this.value[DEFAULT_LABEL] = reading;
164
185
  }
165
186
  this._lastUpdated = Date.now();
166
187
  if (this._onChange) {
@@ -168,17 +189,18 @@ export class Gauge {
168
189
  }
169
190
  }
170
191
  setValue(labelOrValue, value) {
171
- if (this.labels && typeof labelOrValue !== "object") {
192
+ if (this.labels && !isLabelSet(labelOrValue)) {
172
193
  throw new Error("Cannot set value of a gauge with labels without specifying a label. For example, use `gauge.setValue({ route: '/login' }, 42)`.");
173
194
  }
174
- else if (typeof labelOrValue === "object") {
195
+ else if (isLabelSet(labelOrValue)) {
175
196
  this.validateLabels(labelOrValue);
176
- const stringifiedLabel = stringifyLabel(labelOrValue);
177
- if (!(stringifiedLabel in this.value)) {
178
- throw new Error(`Label "${stringifiedLabel}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
197
+ if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
198
+ return;
179
199
  }
200
+ const stringifiedLabel = stringifyLabel(labelOrValue);
180
201
  const newValue = value ?? this._initialValue;
181
- if (!admitsFinite(this.name, newValue, this.nonFinite)) {
202
+ if (!admitsFinite(this.name, newValue, this.nonFinite) ||
203
+ !admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
182
204
  return;
183
205
  }
184
206
  this.value[stringifiedLabel] = newValue;
@@ -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.
@@ -25,6 +27,18 @@ export class Summary {
25
27
  * `admitsFinite` can set it, as with the counter's and gauge's latches.
26
28
  */
27
29
  nonFinite = { warned: false };
30
+ /**
31
+ * Whether a label past the cap, one too long, or one that is not a string
32
+ * has been reported: once per metric, though the cap is reached again in
33
+ * every window it fills.
34
+ */
35
+ labelLimit = {
36
+ capWarned: false,
37
+ longWarned: false,
38
+ typeWarned: false,
39
+ };
40
+ /** How many labels the current window holds. */
41
+ _windowLabels = 0;
28
42
  /**
29
43
  * The name of the summary.
30
44
  * This should be a descriptive name that identifies the distribution.
@@ -153,9 +167,21 @@ export class Summary {
153
167
  return;
154
168
  }
155
169
  const key = label ?? DEFAULT_LABEL;
170
+ if (!this.labels && !this.admitsLabel(key)) {
171
+ return;
172
+ }
173
+ // Before anything is updated, so the count, maximum and sample keep
174
+ // describing the observations the total does.
175
+ const sum = (this._sum[key] ?? 0) + value;
176
+ if (!admitsReading(this.name, sum, this.nonFinite)) {
177
+ return;
178
+ }
156
179
  const count = (this._count[key] ?? 0) + 1;
180
+ if (count === 1) {
181
+ this._windowLabels += 1;
182
+ }
157
183
  this._count[key] = count;
158
- this._sum[key] = (this._sum[key] ?? 0) + value;
184
+ this._sum[key] = sum;
159
185
  this._max[key] = Math.max(this._max[key] ?? value, value);
160
186
  this.sample(key, value, count);
161
187
  this._lastUpdated = Date.now();
@@ -171,11 +197,56 @@ export class Summary {
171
197
  * rather than being thrown away.
172
198
  */
173
199
  markFlushed() {
200
+ this._windowLabels = 0;
174
201
  this._count = {};
175
202
  this._sum = {};
176
203
  this._max = {};
177
204
  this._samples = {};
178
205
  }
206
+ /**
207
+ * Whether a summary that declared no labels may record under `label`: one
208
+ * this window already holds, or a new one within the cap and short enough
209
+ * for the collector, as a counter's series is.
210
+ *
211
+ * Without it the labels were bounded by nothing but the next flush written,
212
+ * which an agent that sends nothing - switched off, or without credentials -
213
+ * never does: a label taken from a request, a search term say, stayed for
214
+ * the life of the process, at whatever length the request allowed. Nor is
215
+ * a label from a request certain to be a string, and an object or an array
216
+ * that cannot be made one - `{"toString": 1}`, an array nested a few
217
+ * thousand levels deep - threw where it was used as a key. Refuses rather
218
+ * than throws, for the reason `admitsSeries` gives.
219
+ */
220
+ admitsLabel(label) {
221
+ if (typeof label !== "string") {
222
+ if (!this.labelLimit.typeWarned) {
223
+ this.labelLimit.typeWarned = true;
224
+ console.warn(`[observe] Metric "${this.name}" ignored a label of type ${typeof label}; a summary's labels are strings.`);
225
+ }
226
+ return false;
227
+ }
228
+ if (label in this._count) {
229
+ return true;
230
+ }
231
+ if (label.length > MAX_SERIES_KEY_LENGTH) {
232
+ if (!this.labelLimit.longWarned) {
233
+ this.labelLimit.longWarned = true;
234
+ console.warn(`[observe] Metric "${this.name}" ignored a label of ${label.length} characters; ` +
235
+ `the collector accepts at most ${MAX_SERIES_KEY_LENGTH}. Shorter labels keep recording.`);
236
+ }
237
+ return false;
238
+ }
239
+ if (this._windowLabels < MAX_SERIES_PER_METRIC) {
240
+ return true;
241
+ }
242
+ if (!this.labelLimit.capWarned) {
243
+ this.labelLimit.capWarned = true;
244
+ console.warn(`[observe] Metric "${this.name}" reached ${MAX_SERIES_PER_METRIC} distinct labels in one window; ` +
245
+ `further labels are ignored until the next flush. This usually means a label carries something ` +
246
+ `unbounded (a user id, a search term, a URL). Existing labels keep recording.`);
247
+ }
248
+ return false;
249
+ }
179
250
  /**
180
251
  * Keeps the retained sample uniform over the whole window (Algorithm R).
181
252
  *