@nestjs/observe 0.3.25 → 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.
- package/README.md +17 -0
- 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 +196 -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 +7 -1
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +28 -3
- 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/project-files.d.ts +42 -1
- package/dist/cli/project-files.js +146 -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 +73 -2
- package/dist/instrument/create-instance-decorator.instrument.js +158 -96
- package/dist/interfaces/observe-options.interface.d.ts +3 -1
- 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.js +7 -3
- package/dist/otlp/otlp-trace-exporter.service.js +63 -5
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +13 -6
- 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/mssql.integration.js +16 -2
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +15 -3
- 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 +106 -14
- 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 +2 -12
- package/dist/protocols/deferred-trace-completion.d.ts +9 -1
- package/dist/protocols/deferred-trace-completion.js +36 -6
- package/dist/protocols/graphql-observe-agent.service.d.ts +36 -5
- package/dist/protocols/graphql-observe-agent.service.js +166 -5
- package/dist/protocols/graphql-operation-parser.js +72 -18
- package/dist/protocols/http-observe-agent.service.d.ts +43 -9
- package/dist/protocols/http-observe-agent.service.js +177 -59
- package/dist/protocols/job-trace-runner.d.ts +35 -3
- package/dist/protocols/job-trace-runner.js +126 -29
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +50 -8
- package/dist/protocols/rpc-observe-agent.service.js +187 -47
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +226 -20
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +3 -4
- package/dist/services/operation-trace.registry.d.ts +45 -1
- package/dist/services/operation-trace.registry.js +418 -35
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +288 -51
- package/dist/services/tracer.service.d.ts +23 -0
- package/dist/services/tracer.service.js +91 -5
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +100 -12
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +14 -0
- package/dist/utils/log-line.parser.js +7 -3
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +512 -61
- 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 +26 -4
- package/dist/utils/redact-url-query.js +184 -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 +8 -0
- package/dist/utils/source-context.util.js +88 -24
- package/dist/utils/source-map-resolver.util.js +8 -2
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -21,9 +21,41 @@ export function admitsFinite(metricName, value, state) {
|
|
|
21
21
|
}
|
|
22
22
|
if (!state.warned) {
|
|
23
23
|
state.warned = true;
|
|
24
|
-
|
|
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 &&
|
|
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 (
|
|
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
|
-
|
|
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 &&
|
|
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 (
|
|
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
|
-
|
|
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,11 +189,14 @@ export class Gauge {
|
|
|
168
189
|
}
|
|
169
190
|
}
|
|
170
191
|
setValue(labelOrValue, value) {
|
|
171
|
-
if (this.labels &&
|
|
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 (
|
|
195
|
+
else if (isLabelSet(labelOrValue)) {
|
|
175
196
|
this.validateLabels(labelOrValue);
|
|
197
|
+
if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
176
200
|
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
177
201
|
const newValue = value ?? this._initialValue;
|
|
178
202
|
if (!admitsFinite(this.name, newValue, this.nonFinite) ||
|
|
@@ -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 {
|
|
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] =
|
|
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
|
*
|