@nestjs/observe 0.0.1 → 0.1.1
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 +16 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/agent/detached-observe-worker.js +98 -1
- package/dist/agent/observe-agent.shared-buffer.d.ts +82 -0
- package/dist/agent/observe-agent.shared-buffer.js +94 -3
- package/dist/agent/observe-agent.worker.d.ts +28 -0
- package/dist/agent/observe-agent.worker.js +59 -5
- package/dist/custom-metrics/counter.d.ts +124 -0
- package/dist/custom-metrics/counter.js +93 -0
- package/dist/custom-metrics/gauge.d.ts +116 -0
- package/dist/custom-metrics/gauge.js +55 -0
- package/dist/custom-metrics/summary.d.ts +102 -0
- package/dist/custom-metrics/summary.js +100 -0
- package/dist/encoders/custom-metrics.encoder.js +18 -0
- package/dist/encoders/remap-keys.util.d.ts +11 -0
- package/dist/encoders/remap-keys.util.js +15 -0
- package/dist/encoders/trace.encoder.d.ts +7 -0
- package/dist/encoders/trace.encoder.js +12 -0
- package/dist/instrument/create-instance-decorator.instrument.d.ts +8 -0
- package/dist/instrument/create-instance-decorator.instrument.js +67 -0
- package/dist/interfaces/custom-metric.interface.d.ts +74 -0
- package/dist/interfaces/graphql.interfaces.d.ts +16 -0
- package/dist/interfaces/job-snapshot.interface.d.ts +70 -0
- package/dist/interfaces/node-runtime-metrics.interface.d.ts +5 -0
- package/dist/interfaces/observe-options.interface.d.ts +358 -0
- package/dist/interfaces/request-snapshot.interface.d.ts +62 -0
- package/dist/interfaces/trace-events.interfaces.d.ts +51 -0
- package/dist/interfaces/trace-span.interface.d.ts +29 -0
- package/dist/observe.module.js +29 -0
- package/dist/profiling/cpu-profiler.service.d.ts +65 -0
- package/dist/profiling/cpu-profiler.service.js +80 -0
- package/dist/profiling/fold-profile.util.d.ts +20 -0
- package/dist/profiling/fold-profile.util.js +0 -0
- package/dist/profiling/profile-uploader.d.ts +26 -0
- package/dist/profiling/profile-uploader.js +52 -0
- package/dist/profiling/span-slice-recorder.d.ts +51 -0
- package/dist/profiling/span-slice-recorder.js +77 -0
- package/dist/protocols/graphql-observe-agent.service.d.ts +128 -0
- package/dist/protocols/graphql-observe-agent.service.js +166 -0
- package/dist/protocols/graphql-operation-parser.d.ts +26 -0
- package/dist/protocols/graphql-operation-parser.js +66 -0
- package/dist/protocols/http-observe-agent.service.d.ts +19 -0
- package/dist/protocols/http-observe-agent.service.js +34 -0
- package/dist/protocols/queue-observe-agent.service.d.ts +8 -0
- package/dist/protocols/queue-observe-agent.service.js +32 -0
- package/dist/protocols/rpc-observe-agent.service.d.ts +7 -0
- package/dist/protocols/rpc-observe-agent.service.js +12 -0
- package/dist/services/logger-patcher.service.d.ts +4 -0
- package/dist/services/logger-patcher.service.js +13 -1
- package/dist/services/node-runtime-metrics.service.js +24 -8
- package/dist/services/operation-trace.registry.d.ts +56 -1
- package/dist/services/operation-trace.registry.js +145 -6
- package/dist/services/stdout-forwarder.service.d.ts +19 -0
- package/dist/services/stdout-forwarder.service.js +62 -0
- package/dist/services/trace-sampler.service.d.ts +6 -0
- package/dist/services/trace-sampler.service.js +3 -3
- package/dist/services/tracer.service.d.ts +91 -0
- package/dist/services/tracer.service.js +56 -1
- package/dist/trace-span.delegate.d.ts +11 -0
- package/dist/trace-span.delegate.js +11 -0
- package/dist/types/path-value.type.d.ts +4 -0
- package/dist/utils/default-trace-id-generator.util.js +11 -0
- package/dist/utils/log-line.parser.d.ts +14 -0
- package/dist/utils/log-line.parser.js +37 -0
- package/dist/utils/log-redactor.js +65 -0
- package/dist/utils/redact-url-query.d.ts +17 -0
- package/dist/utils/redact-url-query.js +38 -0
- package/dist/utils/source-context.util.d.ts +22 -0
- package/dist/utils/source-context.util.js +60 -0
- package/dist/utils/source-map-resolver.util.d.ts +13 -0
- package/dist/utils/source-map-resolver.util.js +26 -28
- package/package.json +1 -1
|
@@ -1,38 +1,162 @@
|
|
|
1
1
|
import { CustomMetric } from "../interfaces/custom-metric.interface.js";
|
|
2
2
|
declare const DEFAULT_LABEL: "default";
|
|
3
|
+
/**
|
|
4
|
+
* Serialises a label object into the map key that identifies its series.
|
|
5
|
+
*
|
|
6
|
+
* Keys are sorted first: `JSON.stringify` preserves insertion order, so
|
|
7
|
+
* `{ route, method }` and `{ method, route }` would otherwise be two different
|
|
8
|
+
* series for what is the same label set.
|
|
9
|
+
*/
|
|
3
10
|
export declare function stringifyLabel(label: Record<string, string | number>): string;
|
|
11
|
+
/**
|
|
12
|
+
* Distinct label combinations one metric may hold.
|
|
13
|
+
*
|
|
14
|
+
* Counters and gauges are cumulative: a series, once created, is kept and
|
|
15
|
+
* flushed for the life of the process. That is correct for a counter and fatal
|
|
16
|
+
* for one labelled with something unbounded - a user id, a request id, a raw
|
|
17
|
+
* URL - which grows this object forever inside the *host application's* memory
|
|
18
|
+
* and adds a row per series to every flush. Summaries need no such cap; their
|
|
19
|
+
* windows are cleared on each flush.
|
|
20
|
+
*
|
|
21
|
+
* A thousand is far above a sensible label set and far below a leak.
|
|
22
|
+
*/
|
|
4
23
|
export declare const MAX_SERIES_PER_METRIC = 1000;
|
|
24
|
+
/**
|
|
25
|
+
* Whether a metric may record under `key`.
|
|
26
|
+
*
|
|
27
|
+
* Refuses rather than throws. This runs inside a customer's own code path -
|
|
28
|
+
* `counter.increment({ userId })` in the middle of their request handler - and
|
|
29
|
+
* an agent that crashes the application it is measuring is worse than one that
|
|
30
|
+
* drops a series. The first refusal is logged, once per metric, because a
|
|
31
|
+
* silent cap is indistinguishable from a metric that does not work.
|
|
32
|
+
*/
|
|
5
33
|
export declare function admitsSeries(metricName: string, values: Record<string, unknown>, key: string, state: {
|
|
6
34
|
warned: boolean;
|
|
7
35
|
}): boolean;
|
|
8
36
|
export declare class Counter<TLabel extends string = typeof DEFAULT_LABEL> implements CustomMetric<TLabel> {
|
|
37
|
+
/**
|
|
38
|
+
* Whether the series cap has already been reported. An object rather than a
|
|
39
|
+
* bare field so `admitsSeries` can set it - a `private` member is not
|
|
40
|
+
* assignable to the structural type such a helper can name.
|
|
41
|
+
*/
|
|
9
42
|
private readonly seriesLimit;
|
|
43
|
+
/**
|
|
44
|
+
* The name of the counter.
|
|
45
|
+
* This should be a descriptive name that identifies the counter.
|
|
46
|
+
* For example, "user_login_count" or "api_request_count".
|
|
47
|
+
*/
|
|
10
48
|
readonly name: string;
|
|
49
|
+
/**
|
|
50
|
+
* The type of the custom metric.
|
|
51
|
+
* This is always 'counter' for this class.
|
|
52
|
+
* It indicates that this metric is a counter that can be incremented.
|
|
53
|
+
*/
|
|
11
54
|
readonly type: "counter";
|
|
55
|
+
/**
|
|
56
|
+
* A brief description of the counter's purpose.
|
|
57
|
+
* This can be used to provide additional context about what the counter represents.
|
|
58
|
+
* For example, "Number of user logins in the last hour" or "Total API requests received".
|
|
59
|
+
*/
|
|
12
60
|
readonly description?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The value of the counter.
|
|
63
|
+
*/
|
|
13
64
|
readonly value: Record<string, number>;
|
|
65
|
+
/**
|
|
66
|
+
* Optional labels for the counter.
|
|
67
|
+
* Labels are key-value pairs that can be used to add additional context to the counter.
|
|
68
|
+
* For example, you might use labels to indicate the environment (e.g., "production", "development") or the service (e.g., "user-service", "order-service").
|
|
69
|
+
*/
|
|
14
70
|
readonly labels?: TLabel[];
|
|
15
71
|
private _tags?;
|
|
16
72
|
private _onChange?;
|
|
17
73
|
private _initialValue;
|
|
18
74
|
private _lastUpdated;
|
|
19
75
|
private _lastFlushedValue;
|
|
76
|
+
/**
|
|
77
|
+
* How much each label rose since the last successful flush.
|
|
78
|
+
*
|
|
79
|
+
* Reported alongside the cumulative `value` so the backend has an additive
|
|
80
|
+
* quantity to aggregate. Summing cumulative readings double counts, and a
|
|
81
|
+
* process restart resets `value` to zero, which makes a naive
|
|
82
|
+
* difference-on-read undercount - this is measured in-process, so neither
|
|
83
|
+
* problem applies.
|
|
84
|
+
*/
|
|
20
85
|
get increase(): Record<string, number>;
|
|
86
|
+
/**
|
|
87
|
+
* Marks the current cumulative values as reported.
|
|
88
|
+
*
|
|
89
|
+
* Called by the agent only once a flush has actually been written, so a failed
|
|
90
|
+
* flush leaves the baseline untouched and the increase rolls into the next one
|
|
91
|
+
* instead of being lost.
|
|
92
|
+
*/
|
|
21
93
|
markFlushed(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Gets the timestamp of when the counter was last updated.
|
|
96
|
+
* This is a Unix timestamp in milliseconds.
|
|
97
|
+
* @returns The last updated timestamp.
|
|
98
|
+
*/
|
|
22
99
|
get lastUpdated(): number;
|
|
100
|
+
/**
|
|
101
|
+
* Gets tags associated with the counter.
|
|
102
|
+
* Tags are key-value pairs that provide additional context about the counter.
|
|
103
|
+
* @returns An object containing tags, or undefined if no tags are set.
|
|
104
|
+
*/
|
|
23
105
|
get tags(): Record<string, string> | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Sets tags for the counter.
|
|
108
|
+
* Tags are key-value pairs that provide additional context about the counter.
|
|
109
|
+
* @param tags An object containing tags to set.
|
|
110
|
+
*/
|
|
24
111
|
set tags(tags: Record<string, string> | undefined);
|
|
25
112
|
constructor(name: string);
|
|
26
113
|
constructor(name: string, description: string);
|
|
27
114
|
constructor(name: string, description: string, initialValue: number);
|
|
28
115
|
constructor(name: string, description: string, labels: TLabel[]);
|
|
29
116
|
constructor(name: string, description: string, initialValue: number, labels: TLabel[]);
|
|
117
|
+
/**
|
|
118
|
+
* The form `TracerService.counter()` builds when it spreads an attributes
|
|
119
|
+
* bag, where any member may be absent. Listed last so the overloads above
|
|
120
|
+
* stay the ones a hand-written call resolves to.
|
|
121
|
+
*/
|
|
30
122
|
constructor(name: string, description?: string, initialValue?: number, labels?: TLabel[]);
|
|
123
|
+
/**
|
|
124
|
+
* Gets the current value of the counter.
|
|
125
|
+
* If labels are defined, you must specify a label to get the value for that label.
|
|
126
|
+
* If no label is specified, it returns the value for the default label.
|
|
127
|
+
* @returns The current value of the counter.
|
|
128
|
+
*/
|
|
31
129
|
getValue(): number;
|
|
130
|
+
/**
|
|
131
|
+
* Gets the current value of the counter.
|
|
132
|
+
* If labels are defined, you must specify a label to get the value for that label.
|
|
133
|
+
* If no label is specified, it returns the value for the default label.
|
|
134
|
+
* @param label Optional label to get the value for. If not provided, returns the default value.
|
|
135
|
+
* @returns The current value of the counter.
|
|
136
|
+
*/
|
|
32
137
|
getValue(label: Record<TLabel, string | number>): number;
|
|
138
|
+
/**
|
|
139
|
+
* Increments the counter by a specified value.
|
|
140
|
+
* If no value is provided, it defaults to 1.
|
|
141
|
+
*/
|
|
33
142
|
increment(): void;
|
|
143
|
+
/**
|
|
144
|
+
* Increments the counter by a specified value for a specific label.
|
|
145
|
+
* If no value is provided, it defaults to 1.
|
|
146
|
+
* @param label The label to increment the counter for.
|
|
147
|
+
*/
|
|
34
148
|
increment(label: Record<TLabel, string | number>): void;
|
|
149
|
+
/**
|
|
150
|
+
* Increments the counter by a specified value.
|
|
151
|
+
* @param value Value to increment the counter by.
|
|
152
|
+
*/
|
|
35
153
|
increment(value: number): void;
|
|
154
|
+
/**
|
|
155
|
+
* Increments the counter by a specified value for a specific label.
|
|
156
|
+
* If no value is provided, it defaults to 1.
|
|
157
|
+
* @param label The label to increment the counter for.
|
|
158
|
+
* @param value Value to increment the counter by.
|
|
159
|
+
*/
|
|
36
160
|
increment(label: Record<TLabel, string | number>, value: number): void;
|
|
37
161
|
private validateLabels;
|
|
38
162
|
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
const DEFAULT_LABEL = "default";
|
|
2
|
+
/**
|
|
3
|
+
* Serialises a label object into the map key that identifies its series.
|
|
4
|
+
*
|
|
5
|
+
* Keys are sorted first: `JSON.stringify` preserves insertion order, so
|
|
6
|
+
* `{ route, method }` and `{ method, route }` would otherwise be two different
|
|
7
|
+
* series for what is the same label set.
|
|
8
|
+
*/
|
|
2
9
|
export function stringifyLabel(label) {
|
|
3
10
|
const sorted = {};
|
|
4
11
|
for (const key of Object.keys(label).sort()) {
|
|
@@ -6,11 +13,36 @@ export function stringifyLabel(label) {
|
|
|
6
13
|
}
|
|
7
14
|
return JSON.stringify(sorted);
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Distinct label combinations one metric may hold.
|
|
18
|
+
*
|
|
19
|
+
* Counters and gauges are cumulative: a series, once created, is kept and
|
|
20
|
+
* flushed for the life of the process. That is correct for a counter and fatal
|
|
21
|
+
* for one labelled with something unbounded - a user id, a request id, a raw
|
|
22
|
+
* URL - which grows this object forever inside the *host application's* memory
|
|
23
|
+
* and adds a row per series to every flush. Summaries need no such cap; their
|
|
24
|
+
* windows are cleared on each flush.
|
|
25
|
+
*
|
|
26
|
+
* A thousand is far above a sensible label set and far below a leak.
|
|
27
|
+
*/
|
|
9
28
|
export const MAX_SERIES_PER_METRIC = 1_000;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a metric may record under `key`.
|
|
31
|
+
*
|
|
32
|
+
* Refuses rather than throws. This runs inside a customer's own code path -
|
|
33
|
+
* `counter.increment({ userId })` in the middle of their request handler - and
|
|
34
|
+
* an agent that crashes the application it is measuring is worse than one that
|
|
35
|
+
* drops a series. The first refusal is logged, once per metric, because a
|
|
36
|
+
* silent cap is indistinguishable from a metric that does not work.
|
|
37
|
+
*/
|
|
10
38
|
export function admitsSeries(metricName, values, key, state) {
|
|
11
39
|
if (key in values) {
|
|
12
40
|
return true;
|
|
13
41
|
}
|
|
42
|
+
// `warned` doubles as a "cap reached" latch: series are never removed, so
|
|
43
|
+
// once the cap has been observed it cannot un-reach. Counting keys is O(n),
|
|
44
|
+
// and without the latch every rejected call would pay it - and the rejected
|
|
45
|
+
// calls are exactly the unbounded-label hot path this cap exists to survive.
|
|
14
46
|
if (!state.warned) {
|
|
15
47
|
if (Object.keys(values).length < MAX_SERIES_PER_METRIC) {
|
|
16
48
|
return true;
|
|
@@ -23,17 +55,54 @@ export function admitsSeries(metricName, values, key, state) {
|
|
|
23
55
|
return false;
|
|
24
56
|
}
|
|
25
57
|
export class Counter {
|
|
58
|
+
/**
|
|
59
|
+
* Whether the series cap has already been reported. An object rather than a
|
|
60
|
+
* bare field so `admitsSeries` can set it - a `private` member is not
|
|
61
|
+
* assignable to the structural type such a helper can name.
|
|
62
|
+
*/
|
|
26
63
|
seriesLimit = { warned: false };
|
|
64
|
+
/**
|
|
65
|
+
* The name of the counter.
|
|
66
|
+
* This should be a descriptive name that identifies the counter.
|
|
67
|
+
* For example, "user_login_count" or "api_request_count".
|
|
68
|
+
*/
|
|
27
69
|
name;
|
|
70
|
+
/**
|
|
71
|
+
* The type of the custom metric.
|
|
72
|
+
* This is always 'counter' for this class.
|
|
73
|
+
* It indicates that this metric is a counter that can be incremented.
|
|
74
|
+
*/
|
|
28
75
|
type;
|
|
76
|
+
/**
|
|
77
|
+
* A brief description of the counter's purpose.
|
|
78
|
+
* This can be used to provide additional context about what the counter represents.
|
|
79
|
+
* For example, "Number of user logins in the last hour" or "Total API requests received".
|
|
80
|
+
*/
|
|
29
81
|
description;
|
|
82
|
+
/**
|
|
83
|
+
* The value of the counter.
|
|
84
|
+
*/
|
|
30
85
|
value;
|
|
86
|
+
/**
|
|
87
|
+
* Optional labels for the counter.
|
|
88
|
+
* Labels are key-value pairs that can be used to add additional context to the counter.
|
|
89
|
+
* For example, you might use labels to indicate the environment (e.g., "production", "development") or the service (e.g., "user-service", "order-service").
|
|
90
|
+
*/
|
|
31
91
|
labels;
|
|
32
92
|
_tags;
|
|
33
93
|
_onChange;
|
|
34
94
|
_initialValue = 0;
|
|
35
95
|
_lastUpdated = Date.now();
|
|
36
96
|
_lastFlushedValue = {};
|
|
97
|
+
/**
|
|
98
|
+
* How much each label rose since the last successful flush.
|
|
99
|
+
*
|
|
100
|
+
* Reported alongside the cumulative `value` so the backend has an additive
|
|
101
|
+
* quantity to aggregate. Summing cumulative readings double counts, and a
|
|
102
|
+
* process restart resets `value` to zero, which makes a naive
|
|
103
|
+
* difference-on-read undercount - this is measured in-process, so neither
|
|
104
|
+
* problem applies.
|
|
105
|
+
*/
|
|
37
106
|
get increase() {
|
|
38
107
|
const increase = {};
|
|
39
108
|
for (const [label, current] of Object.entries(this.value)) {
|
|
@@ -41,15 +110,37 @@ export class Counter {
|
|
|
41
110
|
}
|
|
42
111
|
return increase;
|
|
43
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Marks the current cumulative values as reported.
|
|
115
|
+
*
|
|
116
|
+
* Called by the agent only once a flush has actually been written, so a failed
|
|
117
|
+
* flush leaves the baseline untouched and the increase rolls into the next one
|
|
118
|
+
* instead of being lost.
|
|
119
|
+
*/
|
|
44
120
|
markFlushed() {
|
|
45
121
|
this._lastFlushedValue = { ...this.value };
|
|
46
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Gets the timestamp of when the counter was last updated.
|
|
125
|
+
* This is a Unix timestamp in milliseconds.
|
|
126
|
+
* @returns The last updated timestamp.
|
|
127
|
+
*/
|
|
47
128
|
get lastUpdated() {
|
|
48
129
|
return this._lastUpdated;
|
|
49
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Gets tags associated with the counter.
|
|
133
|
+
* Tags are key-value pairs that provide additional context about the counter.
|
|
134
|
+
* @returns An object containing tags, or undefined if no tags are set.
|
|
135
|
+
*/
|
|
50
136
|
get tags() {
|
|
51
137
|
return this._tags;
|
|
52
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Sets tags for the counter.
|
|
141
|
+
* Tags are key-value pairs that provide additional context about the counter.
|
|
142
|
+
* @param tags An object containing tags to set.
|
|
143
|
+
*/
|
|
53
144
|
set tags(tags) {
|
|
54
145
|
if (tags && typeof tags !== "object") {
|
|
55
146
|
throw new Error("Tags must be an object with key-value pairs.");
|
|
@@ -83,6 +174,8 @@ export class Counter {
|
|
|
83
174
|
else if (this.labels && label) {
|
|
84
175
|
this.validateLabels(label);
|
|
85
176
|
const stringifiedLabel = stringifyLabel(label);
|
|
177
|
+
// Presence, not truthiness: a counter that has been incremented by 0, or
|
|
178
|
+
// decremented back to it, still exists.
|
|
86
179
|
if (!(stringifiedLabel in this.value)) {
|
|
87
180
|
throw new Error(`Label "${stringifiedLabel}" does not exist in the counter. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
88
181
|
}
|
|
@@ -1,20 +1,72 @@
|
|
|
1
1
|
import { CustomMetric } from "../interfaces/custom-metric.interface.js";
|
|
2
2
|
declare const DEFAULT_LABEL: "default";
|
|
3
|
+
/**
|
|
4
|
+
* Defines the kinds of gauges available.
|
|
5
|
+
* - "additive": A gauge value will be summed up when multiple instances report values.
|
|
6
|
+
* - "ratio": A gauge value represents a ratio and should be averaged across instances.
|
|
7
|
+
* - "peak": A gauge value represents a peak measurement and the highest value should be reported.
|
|
8
|
+
*/
|
|
3
9
|
export type GaugeKind = "additive" | "ratio" | "peak";
|
|
4
10
|
export declare class Gauge<TLabel extends string = typeof DEFAULT_LABEL> implements CustomMetric<TLabel> {
|
|
11
|
+
/**
|
|
12
|
+
* Whether the series cap has already been reported. An object rather than a
|
|
13
|
+
* bare field so `admitsSeries` can set it - a `private` member is not
|
|
14
|
+
* assignable to the structural type such a helper can name.
|
|
15
|
+
*/
|
|
5
16
|
private readonly seriesLimit;
|
|
17
|
+
/**
|
|
18
|
+
* The name of the gauge.
|
|
19
|
+
* This should be a descriptive name that identifies the gauge.
|
|
20
|
+
* For example, "memory_usage" or "active_users".
|
|
21
|
+
*/
|
|
6
22
|
readonly name: string;
|
|
23
|
+
/**
|
|
24
|
+
* The type of the custom metric.
|
|
25
|
+
* This is always 'gauge' for this class.
|
|
26
|
+
* It indicates that this metric is a gauge that can be incremented.
|
|
27
|
+
*/
|
|
7
28
|
readonly type: "gauge";
|
|
29
|
+
/**
|
|
30
|
+
* A brief description of the gauge's purpose.
|
|
31
|
+
* This can be used to provide additional context about what the gauge represents.
|
|
32
|
+
* For example, "Current memory usage in bytes" or "Number of active users in the system".
|
|
33
|
+
*/
|
|
8
34
|
readonly description?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The value of the gauge.
|
|
37
|
+
*/
|
|
9
38
|
readonly value: Record<string, number>;
|
|
39
|
+
/**
|
|
40
|
+
* Optional labels for the gauge.
|
|
41
|
+
* Labels are key-value pairs that can be used to add additional context to the gauge.
|
|
42
|
+
* For example, you might use labels to indicate the environment (e.g., "production", "development") or the service (e.g., "user-service", "order-service").
|
|
43
|
+
*/
|
|
10
44
|
readonly labels?: TLabel[];
|
|
45
|
+
/**
|
|
46
|
+
* Optional kind for gauge metrics.
|
|
47
|
+
*/
|
|
11
48
|
readonly kind?: GaugeKind;
|
|
12
49
|
private _tags?;
|
|
13
50
|
private _onChange?;
|
|
14
51
|
private _initialValue;
|
|
15
52
|
private _lastUpdated;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the timestamp of when the gauge was last updated.
|
|
55
|
+
* This is a Unix timestamp in milliseconds.
|
|
56
|
+
* @returns The last updated timestamp.
|
|
57
|
+
*/
|
|
16
58
|
get lastUpdated(): number;
|
|
59
|
+
/**
|
|
60
|
+
* Gets tags associated with the gauge.
|
|
61
|
+
* Tags are key-value pairs that provide additional context about the gauge.
|
|
62
|
+
* @returns An object containing tags, or undefined if no tags are set.
|
|
63
|
+
*/
|
|
17
64
|
get tags(): Record<string, string> | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Sets tags for the gauge.
|
|
67
|
+
* Tags are key-value pairs that provide additional context about the gauge.
|
|
68
|
+
* @param tags An object containing tags to set.
|
|
69
|
+
*/
|
|
18
70
|
set tags(tags: Record<string, string> | undefined);
|
|
19
71
|
constructor(name: string);
|
|
20
72
|
constructor(name: string, attributes: {
|
|
@@ -23,17 +75,81 @@ export declare class Gauge<TLabel extends string = typeof DEFAULT_LABEL> impleme
|
|
|
23
75
|
labels?: TLabel[];
|
|
24
76
|
kind?: GaugeKind;
|
|
25
77
|
});
|
|
78
|
+
/**
|
|
79
|
+
* Gets the current value of the gauge.
|
|
80
|
+
* If labels are defined, you must specify a label to get the value for that label.
|
|
81
|
+
* If no label is specified, it returns the value for the default label.
|
|
82
|
+
* @returns The current value of the gauge.
|
|
83
|
+
*/
|
|
26
84
|
getValue(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the current value of the gauge.
|
|
87
|
+
* If labels are defined, you must specify a label to get the value for that label.
|
|
88
|
+
* If no label is specified, it returns the value for the default label.
|
|
89
|
+
* @param label Optional label to get the value for. If not provided, returns the default value.
|
|
90
|
+
* @returns The current value of the gauge.
|
|
91
|
+
*/
|
|
27
92
|
getValue(label: Record<TLabel, string | number>): number;
|
|
93
|
+
/**
|
|
94
|
+
* Increments the gauge by a specified value.
|
|
95
|
+
* If no value is provided, it defaults to 1.
|
|
96
|
+
*/
|
|
28
97
|
increment(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Increments the gauge by a specified value for a specific label.
|
|
100
|
+
* If no value is provided, it defaults to 1.
|
|
101
|
+
* @param label The label to increment the gauge for.
|
|
102
|
+
*/
|
|
29
103
|
increment(label: Record<TLabel, string | number>): void;
|
|
104
|
+
/**
|
|
105
|
+
* Increments the gauge by a specified value.
|
|
106
|
+
* @param value Value to increment the gauge by.
|
|
107
|
+
*/
|
|
30
108
|
increment(value: number): void;
|
|
109
|
+
/**
|
|
110
|
+
* Increments the gauge by a specified value for a specific label.
|
|
111
|
+
* If no value is provided, it defaults to 1.
|
|
112
|
+
* @param label The label to increment the gauge for.
|
|
113
|
+
* @param value Value to increment the gauge by.
|
|
114
|
+
*/
|
|
31
115
|
increment(label: Record<TLabel, string | number>, value: number): void;
|
|
116
|
+
/**
|
|
117
|
+
* Decrements the gauge by a specified value.
|
|
118
|
+
* If no value is provided, it defaults to 1.
|
|
119
|
+
*/
|
|
32
120
|
decrement(): void;
|
|
121
|
+
/**
|
|
122
|
+
* Decrements the gauge by a specified value for a specific label.
|
|
123
|
+
* If no value is provided, it defaults to 1.
|
|
124
|
+
* @param label The label to decrement the gauge for.
|
|
125
|
+
*/
|
|
33
126
|
decrement(label: Record<TLabel, string | number>): void;
|
|
127
|
+
/**
|
|
128
|
+
* Decrements the gauge by a specified value.
|
|
129
|
+
* @param value Value to decrement the gauge by.
|
|
130
|
+
*/
|
|
34
131
|
decrement(value: number): void;
|
|
132
|
+
/**
|
|
133
|
+
* Decrements the gauge by a specified value for a specific label.
|
|
134
|
+
* If no value is provided, it defaults to 1.
|
|
135
|
+
* @param label The label to decrement the gauge for.
|
|
136
|
+
* @param value Value to decrement the gauge by.
|
|
137
|
+
*/
|
|
35
138
|
decrement(label: Record<TLabel, string | number>, value: number): void;
|
|
139
|
+
/**
|
|
140
|
+
* Sets the value of the gauge.
|
|
141
|
+
* If labels are defined, you must specify a label to set the value for that label.
|
|
142
|
+
* If no label is specified, it sets the value for the default label.
|
|
143
|
+
* @param value The value to set for the gauge.
|
|
144
|
+
*/
|
|
36
145
|
setValue(value: number): void;
|
|
146
|
+
/**
|
|
147
|
+
* Sets the value of the gauge for a specific label.
|
|
148
|
+
* If labels are defined, you must specify a label to set the value for that label.
|
|
149
|
+
* If no label is specified, it sets the value for the default label.
|
|
150
|
+
* @param label The label to set the value for.
|
|
151
|
+
* @param value The value to set for the gauge.
|
|
152
|
+
*/
|
|
37
153
|
setValue(label: Record<TLabel, string | number>, value: number): void;
|
|
38
154
|
private validateLabels;
|
|
39
155
|
}
|
|
@@ -1,23 +1,69 @@
|
|
|
1
1
|
import { admitsSeries, stringifyLabel } from "./counter.js";
|
|
2
2
|
const DEFAULT_LABEL = "default";
|
|
3
3
|
export class Gauge {
|
|
4
|
+
/**
|
|
5
|
+
* Whether the series cap has already been reported. An object rather than a
|
|
6
|
+
* bare field so `admitsSeries` can set it - a `private` member is not
|
|
7
|
+
* assignable to the structural type such a helper can name.
|
|
8
|
+
*/
|
|
4
9
|
seriesLimit = { warned: false };
|
|
10
|
+
/**
|
|
11
|
+
* The name of the gauge.
|
|
12
|
+
* This should be a descriptive name that identifies the gauge.
|
|
13
|
+
* For example, "memory_usage" or "active_users".
|
|
14
|
+
*/
|
|
5
15
|
name;
|
|
16
|
+
/**
|
|
17
|
+
* The type of the custom metric.
|
|
18
|
+
* This is always 'gauge' for this class.
|
|
19
|
+
* It indicates that this metric is a gauge that can be incremented.
|
|
20
|
+
*/
|
|
6
21
|
type;
|
|
22
|
+
/**
|
|
23
|
+
* A brief description of the gauge's purpose.
|
|
24
|
+
* This can be used to provide additional context about what the gauge represents.
|
|
25
|
+
* For example, "Current memory usage in bytes" or "Number of active users in the system".
|
|
26
|
+
*/
|
|
7
27
|
description;
|
|
28
|
+
/**
|
|
29
|
+
* The value of the gauge.
|
|
30
|
+
*/
|
|
8
31
|
value;
|
|
32
|
+
/**
|
|
33
|
+
* Optional labels for the gauge.
|
|
34
|
+
* Labels are key-value pairs that can be used to add additional context to the gauge.
|
|
35
|
+
* For example, you might use labels to indicate the environment (e.g., "production", "development") or the service (e.g., "user-service", "order-service").
|
|
36
|
+
*/
|
|
9
37
|
labels;
|
|
38
|
+
/**
|
|
39
|
+
* Optional kind for gauge metrics.
|
|
40
|
+
*/
|
|
10
41
|
kind;
|
|
11
42
|
_tags;
|
|
12
43
|
_onChange;
|
|
13
44
|
_initialValue = 0;
|
|
14
45
|
_lastUpdated = Date.now();
|
|
46
|
+
/**
|
|
47
|
+
* Gets the timestamp of when the gauge was last updated.
|
|
48
|
+
* This is a Unix timestamp in milliseconds.
|
|
49
|
+
* @returns The last updated timestamp.
|
|
50
|
+
*/
|
|
15
51
|
get lastUpdated() {
|
|
16
52
|
return this._lastUpdated;
|
|
17
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets tags associated with the gauge.
|
|
56
|
+
* Tags are key-value pairs that provide additional context about the gauge.
|
|
57
|
+
* @returns An object containing tags, or undefined if no tags are set.
|
|
58
|
+
*/
|
|
18
59
|
get tags() {
|
|
19
60
|
return this._tags;
|
|
20
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Sets tags for the gauge.
|
|
64
|
+
* Tags are key-value pairs that provide additional context about the gauge.
|
|
65
|
+
* @param tags An object containing tags to set.
|
|
66
|
+
*/
|
|
21
67
|
set tags(tags) {
|
|
22
68
|
if (tags && typeof tags !== "object") {
|
|
23
69
|
throw new Error("Tags must be an object with key-value pairs.");
|
|
@@ -30,6 +76,9 @@ export class Gauge {
|
|
|
30
76
|
this.kind = attributes?.kind;
|
|
31
77
|
this.type = "gauge";
|
|
32
78
|
this._initialValue = attributes?.initialValue ?? 0;
|
|
79
|
+
// Optional, like every other read of `attributes`. Without this the
|
|
80
|
+
// single-argument overload the class advertises - `new Gauge("name")` -
|
|
81
|
+
// threw "Cannot read properties of undefined".
|
|
33
82
|
this.labels = attributes?.labels;
|
|
34
83
|
this.value = {
|
|
35
84
|
[DEFAULT_LABEL]: this._initialValue,
|
|
@@ -42,6 +91,9 @@ export class Gauge {
|
|
|
42
91
|
else if (this.labels && label) {
|
|
43
92
|
this.validateLabels(label);
|
|
44
93
|
const stringifiedLabel = stringifyLabel(label);
|
|
94
|
+
// Presence, not truthiness: a gauge legitimately sitting at 0 is the most
|
|
95
|
+
// common reading there is, and `!0` made it indistinguishable from a label
|
|
96
|
+
// that was never recorded.
|
|
45
97
|
if (!(stringifiedLabel in this.value)) {
|
|
46
98
|
throw new Error(`Label "${stringifiedLabel}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
47
99
|
}
|
|
@@ -112,6 +164,9 @@ export class Gauge {
|
|
|
112
164
|
this.value[stringifiedLabel] = value ?? this._initialValue;
|
|
113
165
|
}
|
|
114
166
|
else {
|
|
167
|
+
// A gauge starts at 0 by default, so the truthiness check here rejected
|
|
168
|
+
// the very first `setValue` on almost every gauge - with a message that
|
|
169
|
+
// named the label it claimed was missing among the available ones.
|
|
115
170
|
if (!(DEFAULT_LABEL in this.value)) {
|
|
116
171
|
throw new Error(`Default label "${DEFAULT_LABEL}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
117
172
|
}
|