@nestjs/observe 0.0.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/LICENSE +23 -0
- package/README.md +135 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/agent/detached-observe-worker.d.ts +1 -0
- package/dist/agent/detached-observe-worker.js +104 -0
- package/dist/agent/observe-agent.shared-buffer.d.ts +42 -0
- package/dist/agent/observe-agent.shared-buffer.js +211 -0
- package/dist/agent/observe-agent.worker.d.ts +25 -0
- package/dist/agent/observe-agent.worker.js +213 -0
- package/dist/custom-metrics/counter.d.ts +39 -0
- package/dist/custom-metrics/counter.js +128 -0
- package/dist/custom-metrics/gauge.d.ts +40 -0
- package/dist/custom-metrics/gauge.js +134 -0
- package/dist/custom-metrics/index.d.ts +3 -0
- package/dist/custom-metrics/index.js +3 -0
- package/dist/custom-metrics/summary.d.ts +38 -0
- package/dist/custom-metrics/summary.js +106 -0
- package/dist/encoders/custom-metrics.encoder.d.ts +25 -0
- package/dist/encoders/custom-metrics.encoder.js +50 -0
- package/dist/encoders/job-snapshot.encoder.d.ts +28 -0
- package/dist/encoders/job-snapshot.encoder.js +28 -0
- package/dist/encoders/remap-keys.util.d.ts +1 -0
- package/dist/encoders/remap-keys.util.js +10 -0
- package/dist/encoders/request-snapshot.encoder.d.ts +33 -0
- package/dist/encoders/request-snapshot.encoder.js +32 -0
- package/dist/encoders/runtime-metrics.encoder.d.ts +63 -0
- package/dist/encoders/runtime-metrics.encoder.js +55 -0
- package/dist/encoders/trace.encoder.d.ts +19 -0
- package/dist/encoders/trace.encoder.js +20 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/instrument/create-instance-decorator.instrument.d.ts +7 -0
- package/dist/instrument/create-instance-decorator.instrument.js +165 -0
- package/dist/interfaces/custom-metric.interface.d.ts +18 -0
- package/dist/interfaces/custom-metric.interface.js +1 -0
- package/dist/interfaces/graphql.interfaces.d.ts +12 -0
- package/dist/interfaces/graphql.interfaces.js +1 -0
- package/dist/interfaces/index.d.ts +7 -0
- package/dist/interfaces/index.js +7 -0
- package/dist/interfaces/job-snapshot.interface.d.ts +23 -0
- package/dist/interfaces/job-snapshot.interface.js +1 -0
- package/dist/interfaces/node-runtime-metrics.interface.d.ts +37 -0
- package/dist/interfaces/node-runtime-metrics.interface.js +1 -0
- package/dist/interfaces/observe-options.interface.d.ts +93 -0
- package/dist/interfaces/observe-options.interface.js +1 -0
- package/dist/interfaces/request-snapshot.interface.d.ts +23 -0
- package/dist/interfaces/request-snapshot.interface.js +1 -0
- package/dist/interfaces/trace-events.interfaces.d.ts +17 -0
- package/dist/interfaces/trace-events.interfaces.js +1 -0
- package/dist/interfaces/trace-span.interface.d.ts +12 -0
- package/dist/interfaces/trace-span.interface.js +1 -0
- package/dist/observe.constants.d.ts +2 -0
- package/dist/observe.constants.js +2 -0
- package/dist/observe.module.d.ts +20 -0
- package/dist/observe.module.js +181 -0
- package/dist/profiling/cpu-profiler.service.d.ts +45 -0
- package/dist/profiling/cpu-profiler.service.js +146 -0
- package/dist/profiling/fold-profile.util.d.ts +28 -0
- package/dist/profiling/fold-profile.util.js +0 -0
- package/dist/profiling/profile-uploader.d.ts +19 -0
- package/dist/profiling/profile-uploader.js +88 -0
- package/dist/profiling/span-slice-recorder.d.ts +18 -0
- package/dist/profiling/span-slice-recorder.js +78 -0
- package/dist/protocols/graphql-observe-agent.service.d.ts +58 -0
- package/dist/protocols/graphql-observe-agent.service.js +261 -0
- package/dist/protocols/graphql-operation-parser.d.ts +10 -0
- package/dist/protocols/graphql-operation-parser.js +174 -0
- package/dist/protocols/http-observe-agent.service.d.ts +31 -0
- package/dist/protocols/http-observe-agent.service.js +186 -0
- package/dist/protocols/queue-observe-agent.service.d.ts +15 -0
- package/dist/protocols/queue-observe-agent.service.js +132 -0
- package/dist/protocols/rpc-observe-agent.service.d.ts +33 -0
- package/dist/protocols/rpc-observe-agent.service.js +183 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/logger-patcher.service.d.ts +12 -0
- package/dist/services/logger-patcher.service.js +76 -0
- package/dist/services/node-runtime-metrics.service.d.ts +23 -0
- package/dist/services/node-runtime-metrics.service.js +156 -0
- package/dist/services/operation-trace.registry.d.ts +49 -0
- package/dist/services/operation-trace.registry.js +364 -0
- package/dist/services/stdout-forwarder.service.d.ts +21 -0
- package/dist/services/stdout-forwarder.service.js +128 -0
- package/dist/services/trace-sampler.service.d.ts +19 -0
- package/dist/services/trace-sampler.service.js +38 -0
- package/dist/services/tracer.service.d.ts +48 -0
- package/dist/services/tracer.service.js +186 -0
- package/dist/trace-span.delegate.d.ts +10 -0
- package/dist/trace-span.delegate.js +24 -0
- package/dist/types/key-of.type.d.ts +1 -0
- package/dist/types/key-of.type.js +1 -0
- package/dist/types/path-value.type.d.ts +6 -0
- package/dist/types/path-value.type.js +1 -0
- package/dist/utils/assert-module-options.util.d.ts +2 -0
- package/dist/utils/assert-module-options.util.js +8 -0
- package/dist/utils/default-trace-id-generator.util.d.ts +1 -0
- package/dist/utils/default-trace-id-generator.util.js +11 -0
- package/dist/utils/log-line.parser.d.ts +9 -0
- package/dist/utils/log-line.parser.js +59 -0
- package/dist/utils/log-redactor.d.ts +11 -0
- package/dist/utils/log-redactor.js +150 -0
- package/dist/utils/redact-url-query.d.ts +2 -0
- package/dist/utils/redact-url-query.js +51 -0
- package/dist/utils/source-context.util.d.ts +11 -0
- package/dist/utils/source-context.util.js +130 -0
- package/dist/utils/source-map-resolver.util.d.ts +7 -0
- package/dist/utils/source-map-resolver.util.js +107 -0
- package/package.json +129 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const DEFAULT_LABEL = "default";
|
|
2
|
+
export function stringifyLabel(label) {
|
|
3
|
+
const sorted = {};
|
|
4
|
+
for (const key of Object.keys(label).sort()) {
|
|
5
|
+
sorted[key] = label[key];
|
|
6
|
+
}
|
|
7
|
+
return JSON.stringify(sorted);
|
|
8
|
+
}
|
|
9
|
+
export const MAX_SERIES_PER_METRIC = 1_000;
|
|
10
|
+
export function admitsSeries(metricName, values, key, state) {
|
|
11
|
+
if (key in values) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (!state.warned) {
|
|
15
|
+
if (Object.keys(values).length < MAX_SERIES_PER_METRIC) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
state.warned = true;
|
|
19
|
+
console.warn(`[observe] Metric "${metricName}" reached ${MAX_SERIES_PER_METRIC} distinct label combinations; ` +
|
|
20
|
+
`further label values are being ignored. This usually means a label carries something unbounded ` +
|
|
21
|
+
`(a user id, a request id, a URL). Existing series keep recording.`);
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
export class Counter {
|
|
26
|
+
seriesLimit = { warned: false };
|
|
27
|
+
name;
|
|
28
|
+
type;
|
|
29
|
+
description;
|
|
30
|
+
value;
|
|
31
|
+
labels;
|
|
32
|
+
_tags;
|
|
33
|
+
_onChange;
|
|
34
|
+
_initialValue = 0;
|
|
35
|
+
_lastUpdated = Date.now();
|
|
36
|
+
_lastFlushedValue = {};
|
|
37
|
+
get increase() {
|
|
38
|
+
const increase = {};
|
|
39
|
+
for (const [label, current] of Object.entries(this.value)) {
|
|
40
|
+
increase[label] = current - (this._lastFlushedValue[label] ?? 0);
|
|
41
|
+
}
|
|
42
|
+
return increase;
|
|
43
|
+
}
|
|
44
|
+
markFlushed() {
|
|
45
|
+
this._lastFlushedValue = { ...this.value };
|
|
46
|
+
}
|
|
47
|
+
get lastUpdated() {
|
|
48
|
+
return this._lastUpdated;
|
|
49
|
+
}
|
|
50
|
+
get tags() {
|
|
51
|
+
return this._tags;
|
|
52
|
+
}
|
|
53
|
+
set tags(tags) {
|
|
54
|
+
if (tags && typeof tags !== "object") {
|
|
55
|
+
throw new Error("Tags must be an object with key-value pairs.");
|
|
56
|
+
}
|
|
57
|
+
this._tags = tags;
|
|
58
|
+
}
|
|
59
|
+
constructor(name, description, labelsOrInitialValue, labels) {
|
|
60
|
+
this.name = name;
|
|
61
|
+
this.description = description;
|
|
62
|
+
this.type = "counter";
|
|
63
|
+
this._initialValue =
|
|
64
|
+
typeof labelsOrInitialValue === "number" ? labelsOrInitialValue : 0;
|
|
65
|
+
if (Array.isArray(labelsOrInitialValue)) {
|
|
66
|
+
this.labels = labelsOrInitialValue;
|
|
67
|
+
this.value = {};
|
|
68
|
+
}
|
|
69
|
+
else if (labels) {
|
|
70
|
+
this.labels = labels;
|
|
71
|
+
this.value = {};
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.value = {
|
|
75
|
+
[DEFAULT_LABEL]: this._initialValue,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
getValue(label) {
|
|
80
|
+
if (this.labels && !label) {
|
|
81
|
+
throw new Error("Cannot get value of a counter with labels without specifying a label. For example, use `counter.getValue({ route: '/login' })`.");
|
|
82
|
+
}
|
|
83
|
+
else if (this.labels && label) {
|
|
84
|
+
this.validateLabels(label);
|
|
85
|
+
const stringifiedLabel = stringifyLabel(label);
|
|
86
|
+
if (!(stringifiedLabel in this.value)) {
|
|
87
|
+
throw new Error(`Label "${stringifiedLabel}" does not exist in the counter. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
88
|
+
}
|
|
89
|
+
return this.value[stringifiedLabel];
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return this.value[DEFAULT_LABEL];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
increment(labelOrValue, value) {
|
|
96
|
+
if (this.labels && typeof labelOrValue !== "object") {
|
|
97
|
+
throw new Error("Cannot increment a counter with labels without specifying a label. For example, use `counter.increment({ route: '/login' })`.");
|
|
98
|
+
}
|
|
99
|
+
if (typeof labelOrValue === "object") {
|
|
100
|
+
this.validateLabels(labelOrValue);
|
|
101
|
+
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
102
|
+
if (!admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const incrementValue = value ?? 1;
|
|
106
|
+
this.value[stringifiedLabel] =
|
|
107
|
+
(this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const incrementValue = labelOrValue ?? 1;
|
|
111
|
+
this.value[DEFAULT_LABEL] =
|
|
112
|
+
(this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
|
|
113
|
+
}
|
|
114
|
+
this._lastUpdated = Date.now();
|
|
115
|
+
if (this._onChange) {
|
|
116
|
+
this._onChange(this);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
validateLabels(labeledValue) {
|
|
120
|
+
const keys = Object.keys(labeledValue);
|
|
121
|
+
const declared = this.labels;
|
|
122
|
+
declared?.forEach((label) => {
|
|
123
|
+
if (!keys.includes(label)) {
|
|
124
|
+
throw new Error(`Label "${label}" is not defined in the counter. Available labels: ${declared.join(", ")}.`);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CustomMetric } from "../interfaces/custom-metric.interface.js";
|
|
2
|
+
declare const DEFAULT_LABEL: "default";
|
|
3
|
+
export type GaugeKind = "additive" | "ratio" | "peak";
|
|
4
|
+
export declare class Gauge<TLabel extends string = typeof DEFAULT_LABEL> implements CustomMetric<TLabel> {
|
|
5
|
+
private readonly seriesLimit;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly type: "gauge";
|
|
8
|
+
readonly description?: string;
|
|
9
|
+
readonly value: Record<string, number>;
|
|
10
|
+
readonly labels?: TLabel[];
|
|
11
|
+
readonly kind?: GaugeKind;
|
|
12
|
+
private _tags?;
|
|
13
|
+
private _onChange?;
|
|
14
|
+
private _initialValue;
|
|
15
|
+
private _lastUpdated;
|
|
16
|
+
get lastUpdated(): number;
|
|
17
|
+
get tags(): Record<string, string> | undefined;
|
|
18
|
+
set tags(tags: Record<string, string> | undefined);
|
|
19
|
+
constructor(name: string);
|
|
20
|
+
constructor(name: string, attributes: {
|
|
21
|
+
description?: string;
|
|
22
|
+
initialValue?: number;
|
|
23
|
+
labels?: TLabel[];
|
|
24
|
+
kind?: GaugeKind;
|
|
25
|
+
});
|
|
26
|
+
getValue(): number;
|
|
27
|
+
getValue(label: Record<TLabel, string | number>): number;
|
|
28
|
+
increment(): void;
|
|
29
|
+
increment(label: Record<TLabel, string | number>): void;
|
|
30
|
+
increment(value: number): void;
|
|
31
|
+
increment(label: Record<TLabel, string | number>, value: number): void;
|
|
32
|
+
decrement(): void;
|
|
33
|
+
decrement(label: Record<TLabel, string | number>): void;
|
|
34
|
+
decrement(value: number): void;
|
|
35
|
+
decrement(label: Record<TLabel, string | number>, value: number): void;
|
|
36
|
+
setValue(value: number): void;
|
|
37
|
+
setValue(label: Record<TLabel, string | number>, value: number): void;
|
|
38
|
+
private validateLabels;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { admitsSeries, stringifyLabel } from "./counter.js";
|
|
2
|
+
const DEFAULT_LABEL = "default";
|
|
3
|
+
export class Gauge {
|
|
4
|
+
seriesLimit = { warned: false };
|
|
5
|
+
name;
|
|
6
|
+
type;
|
|
7
|
+
description;
|
|
8
|
+
value;
|
|
9
|
+
labels;
|
|
10
|
+
kind;
|
|
11
|
+
_tags;
|
|
12
|
+
_onChange;
|
|
13
|
+
_initialValue = 0;
|
|
14
|
+
_lastUpdated = Date.now();
|
|
15
|
+
get lastUpdated() {
|
|
16
|
+
return this._lastUpdated;
|
|
17
|
+
}
|
|
18
|
+
get tags() {
|
|
19
|
+
return this._tags;
|
|
20
|
+
}
|
|
21
|
+
set tags(tags) {
|
|
22
|
+
if (tags && typeof tags !== "object") {
|
|
23
|
+
throw new Error("Tags must be an object with key-value pairs.");
|
|
24
|
+
}
|
|
25
|
+
this._tags = tags;
|
|
26
|
+
}
|
|
27
|
+
constructor(name, attributes) {
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.description = attributes?.description;
|
|
30
|
+
this.kind = attributes?.kind;
|
|
31
|
+
this.type = "gauge";
|
|
32
|
+
this._initialValue = attributes?.initialValue ?? 0;
|
|
33
|
+
this.labels = attributes?.labels;
|
|
34
|
+
this.value = {
|
|
35
|
+
[DEFAULT_LABEL]: this._initialValue,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
getValue(label) {
|
|
39
|
+
if (this.labels && !label) {
|
|
40
|
+
throw new Error("Cannot get value of a gauge with labels without specifying a label. For example, use `gauge.getValue({ route: '/login' })`.");
|
|
41
|
+
}
|
|
42
|
+
else if (this.labels && label) {
|
|
43
|
+
this.validateLabels(label);
|
|
44
|
+
const stringifiedLabel = stringifyLabel(label);
|
|
45
|
+
if (!(stringifiedLabel in this.value)) {
|
|
46
|
+
throw new Error(`Label "${stringifiedLabel}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
47
|
+
}
|
|
48
|
+
return this.value[stringifiedLabel];
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return this.value[DEFAULT_LABEL];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
increment(labelOrValue, value) {
|
|
55
|
+
if (this.labels && typeof labelOrValue !== "object") {
|
|
56
|
+
throw new Error("Cannot increment a gauge with labels without specifying a label. For example, use `gauge.increment({ route: '/login' })`.");
|
|
57
|
+
}
|
|
58
|
+
if (typeof labelOrValue === "object") {
|
|
59
|
+
this.validateLabels(labelOrValue);
|
|
60
|
+
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
61
|
+
if (!admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const incrementValue = value ?? 1;
|
|
65
|
+
this.value[stringifiedLabel] =
|
|
66
|
+
(this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const incrementValue = labelOrValue ?? 1;
|
|
70
|
+
this.value[DEFAULT_LABEL] =
|
|
71
|
+
(this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
|
|
72
|
+
}
|
|
73
|
+
this._lastUpdated = Date.now();
|
|
74
|
+
if (this._onChange) {
|
|
75
|
+
this._onChange(this);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
decrement(labelOrValue, value) {
|
|
79
|
+
if (this.labels && typeof labelOrValue !== "object") {
|
|
80
|
+
throw new Error("Cannot decrement a gauge with labels without specifying a label. For example, use `gauge.decrement({ route: '/login' })`.");
|
|
81
|
+
}
|
|
82
|
+
else if (typeof labelOrValue === "object") {
|
|
83
|
+
this.validateLabels(labelOrValue);
|
|
84
|
+
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
85
|
+
if (!admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const decrementValue = value ?? 1;
|
|
89
|
+
this.value[stringifiedLabel] =
|
|
90
|
+
(this.value[stringifiedLabel] ?? this._initialValue) - decrementValue;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const decrementValue = labelOrValue ?? 1;
|
|
94
|
+
this.value[DEFAULT_LABEL] =
|
|
95
|
+
(this.value[DEFAULT_LABEL] ?? this._initialValue) - decrementValue;
|
|
96
|
+
}
|
|
97
|
+
this._lastUpdated = Date.now();
|
|
98
|
+
if (this._onChange) {
|
|
99
|
+
this._onChange(this);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
setValue(labelOrValue, value) {
|
|
103
|
+
if (this.labels && typeof labelOrValue !== "object") {
|
|
104
|
+
throw new Error("Cannot set value of a gauge with labels without specifying a label. For example, use `gauge.setValue({ route: '/login' }, 42)`.");
|
|
105
|
+
}
|
|
106
|
+
else if (typeof labelOrValue === "object") {
|
|
107
|
+
this.validateLabels(labelOrValue);
|
|
108
|
+
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
109
|
+
if (!(stringifiedLabel in this.value)) {
|
|
110
|
+
throw new Error(`Label "${stringifiedLabel}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
111
|
+
}
|
|
112
|
+
this.value[stringifiedLabel] = value ?? this._initialValue;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
if (!(DEFAULT_LABEL in this.value)) {
|
|
116
|
+
throw new Error(`Default label "${DEFAULT_LABEL}" does not exist in the gauge. Available labels: ${Object.keys(this.value).join(", ")}.`);
|
|
117
|
+
}
|
|
118
|
+
this.value[DEFAULT_LABEL] = labelOrValue ?? this._initialValue;
|
|
119
|
+
}
|
|
120
|
+
this._lastUpdated = Date.now();
|
|
121
|
+
if (this._onChange) {
|
|
122
|
+
this._onChange(this);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
validateLabels(labeledValue) {
|
|
126
|
+
const keys = Object.keys(labeledValue);
|
|
127
|
+
const declared = this.labels;
|
|
128
|
+
declared?.forEach((label) => {
|
|
129
|
+
if (!keys.includes(label)) {
|
|
130
|
+
throw new Error(`Label "${label}" is not defined in the gauge. Available labels: ${declared.join(", ")}.`);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CustomMetric } from "../interfaces/custom-metric.interface.js";
|
|
2
|
+
declare const DEFAULT_LABEL: "default";
|
|
3
|
+
export declare class Summary<TLabel extends string = typeof DEFAULT_LABEL> implements CustomMetric<TLabel> {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly type: "summary";
|
|
6
|
+
readonly description?: string;
|
|
7
|
+
readonly labels?: TLabel[];
|
|
8
|
+
private _tags?;
|
|
9
|
+
private _onChange?;
|
|
10
|
+
private _lastUpdated;
|
|
11
|
+
private readonly _sampleSize;
|
|
12
|
+
private _count;
|
|
13
|
+
private _sum;
|
|
14
|
+
private _max;
|
|
15
|
+
private _samples;
|
|
16
|
+
get value(): Record<string, number>;
|
|
17
|
+
get p50(): Record<string, number>;
|
|
18
|
+
get p95(): Record<string, number>;
|
|
19
|
+
get p99(): Record<string, number>;
|
|
20
|
+
get observations(): Record<string, number>;
|
|
21
|
+
get total(): Record<string, number>;
|
|
22
|
+
get maximum(): Record<string, number>;
|
|
23
|
+
get lastUpdated(): number;
|
|
24
|
+
get tags(): Record<string, string> | undefined;
|
|
25
|
+
set tags(tags: Record<string, string> | undefined);
|
|
26
|
+
constructor(name: string);
|
|
27
|
+
constructor(name: string, attributes: {
|
|
28
|
+
description?: string;
|
|
29
|
+
labels?: TLabel[];
|
|
30
|
+
sampleSize?: number;
|
|
31
|
+
});
|
|
32
|
+
observe(value: number): void;
|
|
33
|
+
observe(value: number, label: TLabel): void;
|
|
34
|
+
markFlushed(): void;
|
|
35
|
+
private sample;
|
|
36
|
+
private quantile;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
const DEFAULT_LABEL = "default";
|
|
2
|
+
const DEFAULT_SAMPLE_SIZE = 2048;
|
|
3
|
+
export class Summary {
|
|
4
|
+
name;
|
|
5
|
+
type;
|
|
6
|
+
description;
|
|
7
|
+
labels;
|
|
8
|
+
_tags;
|
|
9
|
+
_onChange;
|
|
10
|
+
_lastUpdated = Date.now();
|
|
11
|
+
_sampleSize;
|
|
12
|
+
_count = {};
|
|
13
|
+
_sum = {};
|
|
14
|
+
_max = {};
|
|
15
|
+
_samples = {};
|
|
16
|
+
get value() {
|
|
17
|
+
return this.quantile(0.5);
|
|
18
|
+
}
|
|
19
|
+
get p50() {
|
|
20
|
+
return this.quantile(0.5);
|
|
21
|
+
}
|
|
22
|
+
get p95() {
|
|
23
|
+
return this.quantile(0.95);
|
|
24
|
+
}
|
|
25
|
+
get p99() {
|
|
26
|
+
return this.quantile(0.99);
|
|
27
|
+
}
|
|
28
|
+
get observations() {
|
|
29
|
+
return { ...this._count };
|
|
30
|
+
}
|
|
31
|
+
get total() {
|
|
32
|
+
return { ...this._sum };
|
|
33
|
+
}
|
|
34
|
+
get maximum() {
|
|
35
|
+
return { ...this._max };
|
|
36
|
+
}
|
|
37
|
+
get lastUpdated() {
|
|
38
|
+
return this._lastUpdated;
|
|
39
|
+
}
|
|
40
|
+
get tags() {
|
|
41
|
+
return this._tags;
|
|
42
|
+
}
|
|
43
|
+
set tags(tags) {
|
|
44
|
+
if (tags && typeof tags !== "object") {
|
|
45
|
+
throw new Error("Tags must be an object with key-value pairs.");
|
|
46
|
+
}
|
|
47
|
+
this._tags = tags;
|
|
48
|
+
}
|
|
49
|
+
constructor(name, attributes) {
|
|
50
|
+
this.name = name;
|
|
51
|
+
this.description = attributes?.description;
|
|
52
|
+
this.labels = attributes?.labels;
|
|
53
|
+
this.type = "summary";
|
|
54
|
+
this._sampleSize = attributes?.sampleSize ?? DEFAULT_SAMPLE_SIZE;
|
|
55
|
+
}
|
|
56
|
+
observe(value, label) {
|
|
57
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
58
|
+
throw new Error(`Cannot observe a non-finite value on summary "${this.name}".`);
|
|
59
|
+
}
|
|
60
|
+
if (this.labels && !label) {
|
|
61
|
+
throw new Error(`Cannot observe on a summary with labels without specifying one. For example, use \`summary.observe(12, "${this.labels[0]}")\`.`);
|
|
62
|
+
}
|
|
63
|
+
if (label && this.labels && !this.labels.includes(label)) {
|
|
64
|
+
throw new Error(`Label "${label}" is not defined in the summary. Available labels: ${this.labels.join(", ")}.`);
|
|
65
|
+
}
|
|
66
|
+
const key = label ?? DEFAULT_LABEL;
|
|
67
|
+
const count = (this._count[key] ?? 0) + 1;
|
|
68
|
+
this._count[key] = count;
|
|
69
|
+
this._sum[key] = (this._sum[key] ?? 0) + value;
|
|
70
|
+
this._max[key] = Math.max(this._max[key] ?? value, value);
|
|
71
|
+
this.sample(key, value, count);
|
|
72
|
+
this._lastUpdated = Date.now();
|
|
73
|
+
if (this._onChange) {
|
|
74
|
+
this._onChange(this);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
markFlushed() {
|
|
78
|
+
this._count = {};
|
|
79
|
+
this._sum = {};
|
|
80
|
+
this._max = {};
|
|
81
|
+
this._samples = {};
|
|
82
|
+
}
|
|
83
|
+
sample(key, value, count) {
|
|
84
|
+
const samples = (this._samples[key] ??= []);
|
|
85
|
+
if (samples.length < this._sampleSize) {
|
|
86
|
+
samples.push(value);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const slot = Math.floor(Math.random() * count);
|
|
90
|
+
if (slot < this._sampleSize) {
|
|
91
|
+
samples[slot] = value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
quantile(percentile) {
|
|
95
|
+
const quantiles = {};
|
|
96
|
+
for (const [key, samples] of Object.entries(this._samples)) {
|
|
97
|
+
if (!samples.length) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
101
|
+
const rank = Math.ceil(percentile * sorted.length) - 1;
|
|
102
|
+
quantiles[key] = sorted[Math.min(Math.max(rank, 0), sorted.length - 1)];
|
|
103
|
+
}
|
|
104
|
+
return quantiles;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { CustomMetric } from "../interfaces/index.js";
|
|
2
|
+
declare const CUSTOM_METRICS_KEY_MAP: {
|
|
3
|
+
readonly name: "n";
|
|
4
|
+
readonly type: "t";
|
|
5
|
+
readonly value: "v";
|
|
6
|
+
readonly tags: "tg";
|
|
7
|
+
readonly description: "d";
|
|
8
|
+
readonly labels: "l";
|
|
9
|
+
readonly lastUpdated: "lu";
|
|
10
|
+
readonly kind: "k";
|
|
11
|
+
readonly increase: "iv";
|
|
12
|
+
readonly p50: "q50";
|
|
13
|
+
readonly p95: "q95";
|
|
14
|
+
readonly p99: "q99";
|
|
15
|
+
readonly observations: "ct";
|
|
16
|
+
readonly total: "sm";
|
|
17
|
+
readonly maximum: "mx";
|
|
18
|
+
};
|
|
19
|
+
export type EncodedCustomMetric = {
|
|
20
|
+
[K in keyof CustomMetric as K extends keyof typeof CUSTOM_METRICS_KEY_MAP ? (typeof CUSTOM_METRICS_KEY_MAP)[K] : never]: CustomMetric[K];
|
|
21
|
+
};
|
|
22
|
+
export declare class CustomMetricsEncoder {
|
|
23
|
+
static encode(metric: CustomMetric): EncodedCustomMetric;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { remapKeys } from "./remap-keys.util.js";
|
|
2
|
+
const CUSTOM_METRICS_KEY_MAP = {
|
|
3
|
+
name: "n",
|
|
4
|
+
type: "t",
|
|
5
|
+
value: "v",
|
|
6
|
+
tags: "tg",
|
|
7
|
+
description: "d",
|
|
8
|
+
labels: "l",
|
|
9
|
+
lastUpdated: "lu",
|
|
10
|
+
kind: "k",
|
|
11
|
+
increase: "iv",
|
|
12
|
+
p50: "q50",
|
|
13
|
+
p95: "q95",
|
|
14
|
+
p99: "q99",
|
|
15
|
+
observations: "ct",
|
|
16
|
+
total: "sm",
|
|
17
|
+
maximum: "mx",
|
|
18
|
+
};
|
|
19
|
+
const SUMMARY_KEYS = [
|
|
20
|
+
"p50",
|
|
21
|
+
"p95",
|
|
22
|
+
"p99",
|
|
23
|
+
"observations",
|
|
24
|
+
"total",
|
|
25
|
+
"maximum",
|
|
26
|
+
];
|
|
27
|
+
export class CustomMetricsEncoder {
|
|
28
|
+
static encode(metric) {
|
|
29
|
+
const encoded = remapKeys(metric, CUSTOM_METRICS_KEY_MAP);
|
|
30
|
+
if (metric.tags) {
|
|
31
|
+
encoded.tg = metric.tags;
|
|
32
|
+
}
|
|
33
|
+
if (metric.lastUpdated !== undefined) {
|
|
34
|
+
encoded.lu = metric.lastUpdated;
|
|
35
|
+
}
|
|
36
|
+
if (metric.type === "counter" && metric.increase) {
|
|
37
|
+
encoded.iv = metric.increase;
|
|
38
|
+
}
|
|
39
|
+
if (metric.type === "summary") {
|
|
40
|
+
for (const key of SUMMARY_KEYS) {
|
|
41
|
+
const value = metric[key];
|
|
42
|
+
if (value && Object.keys(value).length > 0) {
|
|
43
|
+
encoded[CUSTOM_METRICS_KEY_MAP[key]] =
|
|
44
|
+
value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return encoded;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { JobSnapshot } from "../interfaces/index.js";
|
|
2
|
+
import { RecursiveEncodedTrace } from "./trace.encoder.js";
|
|
3
|
+
declare const SNAPSHOT_KEY_MAP: {
|
|
4
|
+
readonly id: "i";
|
|
5
|
+
readonly traceId: "ti";
|
|
6
|
+
readonly name: "n";
|
|
7
|
+
readonly queueName: "q";
|
|
8
|
+
readonly status: "s";
|
|
9
|
+
readonly calledAt: "c";
|
|
10
|
+
readonly startTimestamp: "st";
|
|
11
|
+
readonly duration: "d";
|
|
12
|
+
readonly enqueuedAt: "ea";
|
|
13
|
+
readonly waitDuration: "wd";
|
|
14
|
+
readonly attemptsMade: "am";
|
|
15
|
+
readonly maxAttempts: "ma";
|
|
16
|
+
readonly tags: "tg";
|
|
17
|
+
readonly traces: "t";
|
|
18
|
+
readonly error: "e";
|
|
19
|
+
};
|
|
20
|
+
export type EncodedJobSnapshot = Omit<{
|
|
21
|
+
[K in keyof JobSnapshot as K extends keyof typeof SNAPSHOT_KEY_MAP ? (typeof SNAPSHOT_KEY_MAP)[K] : never]: JobSnapshot[K];
|
|
22
|
+
}, "t"> & {
|
|
23
|
+
t?: Array<RecursiveEncodedTrace>;
|
|
24
|
+
};
|
|
25
|
+
export declare class JobSnapshotEncoder {
|
|
26
|
+
static encode(snapshot: JobSnapshot): EncodedJobSnapshot;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { remapKeys } from "./remap-keys.util.js";
|
|
2
|
+
import { encodeTrace } from "./trace.encoder.js";
|
|
3
|
+
const SNAPSHOT_KEY_MAP = {
|
|
4
|
+
id: "i",
|
|
5
|
+
traceId: "ti",
|
|
6
|
+
name: "n",
|
|
7
|
+
queueName: "q",
|
|
8
|
+
status: "s",
|
|
9
|
+
calledAt: "c",
|
|
10
|
+
startTimestamp: "st",
|
|
11
|
+
duration: "d",
|
|
12
|
+
enqueuedAt: "ea",
|
|
13
|
+
waitDuration: "wd",
|
|
14
|
+
attemptsMade: "am",
|
|
15
|
+
maxAttempts: "ma",
|
|
16
|
+
tags: "tg",
|
|
17
|
+
traces: "t",
|
|
18
|
+
error: "e",
|
|
19
|
+
};
|
|
20
|
+
export class JobSnapshotEncoder {
|
|
21
|
+
static encode(snapshot) {
|
|
22
|
+
const encoded = remapKeys(snapshot, SNAPSHOT_KEY_MAP);
|
|
23
|
+
if (snapshot.traces) {
|
|
24
|
+
encoded.t = snapshot.traces.map((trace) => encodeTrace(trace));
|
|
25
|
+
}
|
|
26
|
+
return encoded;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function remapKeys<TSource extends object, TEncoded>(source: TSource, keyMap: Partial<Record<keyof TSource, string>>): TEncoded;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RequestSnapshot } from "../interfaces/request-snapshot.interface.js";
|
|
2
|
+
import { RecursiveEncodedTrace } from "./trace.encoder.js";
|
|
3
|
+
declare const ATTRIBUTES_KEY_MAP: {
|
|
4
|
+
readonly method: "m";
|
|
5
|
+
readonly statusCode: "sc";
|
|
6
|
+
readonly originalUrl: "ou";
|
|
7
|
+
};
|
|
8
|
+
declare const SNAPSHOT_KEY_MAP: {
|
|
9
|
+
readonly calledAt: "ct";
|
|
10
|
+
readonly traceId: "ti";
|
|
11
|
+
readonly startTimestamp: "st";
|
|
12
|
+
readonly duration: "d";
|
|
13
|
+
readonly protocol: "p";
|
|
14
|
+
readonly operationId: "op";
|
|
15
|
+
readonly traces: "t";
|
|
16
|
+
readonly attributes: "a";
|
|
17
|
+
readonly tags: "tg";
|
|
18
|
+
readonly error: "e";
|
|
19
|
+
readonly userId: "u";
|
|
20
|
+
};
|
|
21
|
+
export type EncodedAttributes = {
|
|
22
|
+
[K in keyof RequestSnapshot["attributes"] as K extends keyof typeof ATTRIBUTES_KEY_MAP ? (typeof ATTRIBUTES_KEY_MAP)[K] : never]: RequestSnapshot["attributes"][K];
|
|
23
|
+
};
|
|
24
|
+
export type EncodedRequestSnapshot = Omit<{
|
|
25
|
+
[K in keyof RequestSnapshot as K extends keyof typeof SNAPSHOT_KEY_MAP ? (typeof SNAPSHOT_KEY_MAP)[K] : never]: RequestSnapshot[K];
|
|
26
|
+
}, "t" | "a"> & {
|
|
27
|
+
t?: Array<RecursiveEncodedTrace>;
|
|
28
|
+
a?: EncodedAttributes;
|
|
29
|
+
};
|
|
30
|
+
export declare class RequestSnapshotEncoder {
|
|
31
|
+
static encode(snapshot: RequestSnapshot): EncodedRequestSnapshot;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { remapKeys } from "./remap-keys.util.js";
|
|
2
|
+
import { encodeTrace } from "./trace.encoder.js";
|
|
3
|
+
const ATTRIBUTES_KEY_MAP = {
|
|
4
|
+
method: "m",
|
|
5
|
+
statusCode: "sc",
|
|
6
|
+
originalUrl: "ou",
|
|
7
|
+
};
|
|
8
|
+
const SNAPSHOT_KEY_MAP = {
|
|
9
|
+
calledAt: "ct",
|
|
10
|
+
traceId: "ti",
|
|
11
|
+
startTimestamp: "st",
|
|
12
|
+
duration: "d",
|
|
13
|
+
protocol: "p",
|
|
14
|
+
operationId: "op",
|
|
15
|
+
traces: "t",
|
|
16
|
+
attributes: "a",
|
|
17
|
+
tags: "tg",
|
|
18
|
+
error: "e",
|
|
19
|
+
userId: "u",
|
|
20
|
+
};
|
|
21
|
+
export class RequestSnapshotEncoder {
|
|
22
|
+
static encode(snapshot) {
|
|
23
|
+
const encoded = remapKeys(snapshot, SNAPSHOT_KEY_MAP);
|
|
24
|
+
if (snapshot.traces) {
|
|
25
|
+
encoded.t = snapshot.traces.map((trace) => encodeTrace(trace));
|
|
26
|
+
}
|
|
27
|
+
if (snapshot.attributes) {
|
|
28
|
+
encoded.a = remapKeys(snapshot.attributes, ATTRIBUTES_KEY_MAP);
|
|
29
|
+
}
|
|
30
|
+
return encoded;
|
|
31
|
+
}
|
|
32
|
+
}
|