@promster/metrics 15.5.1 → 15.5.2
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/dist/index.cjs +385 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +336 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -7
- package/dist/declarations/src/client/client.d.ts +0 -9
- package/dist/declarations/src/client/index.d.ts +0 -2
- package/dist/declarations/src/create-gc-metrics/create-gc-metrics.d.ts +0 -14
- package/dist/declarations/src/create-gc-metrics/index.d.ts +0 -2
- package/dist/declarations/src/create-gc-observer/create-gc-observer.d.ts +0 -3
- package/dist/declarations/src/create-gc-observer/index.d.ts +0 -2
- package/dist/declarations/src/create-graphql-metrics/create-graphql-metrics.d.ts +0 -25
- package/dist/declarations/src/create-graphql-metrics/index.d.ts +0 -2
- package/dist/declarations/src/create-http-metrics/create-http-metrics.d.ts +0 -26
- package/dist/declarations/src/create-http-metrics/index.d.ts +0 -2
- package/dist/declarations/src/create-request-recorder/create-request-recorder.d.ts +0 -16
- package/dist/declarations/src/create-request-recorder/index.d.ts +0 -3
- package/dist/declarations/src/end-measurement-from/end-measurement-from.d.ts +0 -4
- package/dist/declarations/src/end-measurement-from/index.d.ts +0 -2
- package/dist/declarations/src/environment/index.d.ts +0 -3
- package/dist/declarations/src/environment/kubernetes.d.ts +0 -2
- package/dist/declarations/src/environment/skip-metrics-in-environment.d.ts +0 -6
- package/dist/declarations/src/index.d.ts +0 -16
- package/dist/declarations/src/normalizers/index.d.ts +0 -9
- package/dist/declarations/src/normalizers/method/index.d.ts +0 -2
- package/dist/declarations/src/normalizers/method/method.d.ts +0 -2
- package/dist/declarations/src/normalizers/path/index.d.ts +0 -2
- package/dist/declarations/src/normalizers/path/path.d.ts +0 -2
- package/dist/declarations/src/normalizers/status-code/index.d.ts +0 -2
- package/dist/declarations/src/normalizers/status-code/status-code.d.ts +0 -2
- package/dist/declarations/src/sort-labels/index.d.ts +0 -2
- package/dist/declarations/src/sort-labels/sort-labels.d.ts +0 -3
- package/dist/declarations/src/summary/index.d.ts +0 -2
- package/dist/declarations/src/summary/summary.d.ts +0 -3
- package/dist/declarations/src/timing/index.d.ts +0 -2
- package/dist/declarations/src/timing/timing.d.ts +0 -17
- package/dist/promster-metrics.cjs.d.ts +0 -2
- package/dist/promster-metrics.cjs.dev.js +0 -409
- package/dist/promster-metrics.cjs.js +0 -7
- package/dist/promster-metrics.cjs.prod.js +0 -409
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var _function = require('es-toolkit/function');
|
|
6
|
-
var Prometheus = require('prom-client');
|
|
7
|
-
var merge = require('merge-options');
|
|
8
|
-
var prometheusGcStats = require('@chainsafe/prometheus-gc-stats');
|
|
9
|
-
var url = require('node:url');
|
|
10
|
-
var UrlValueParser = require('url-value-parser');
|
|
11
|
-
|
|
12
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
function _interopNamespace(e) {
|
|
15
|
-
if (e && e.__esModule) return e;
|
|
16
|
-
var n = Object.create(null);
|
|
17
|
-
if (e) {
|
|
18
|
-
Object.keys(e).forEach(function (k) {
|
|
19
|
-
if (k !== 'default') {
|
|
20
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () { return e[k]; }
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
n["default"] = e;
|
|
29
|
-
return Object.freeze(n);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var Prometheus__namespace = /*#__PURE__*/_interopNamespace(Prometheus);
|
|
33
|
-
var merge__default = /*#__PURE__*/_interopDefault(merge);
|
|
34
|
-
var url__default = /*#__PURE__*/_interopDefault(url);
|
|
35
|
-
var UrlValueParser__default = /*#__PURE__*/_interopDefault(UrlValueParser);
|
|
36
|
-
|
|
37
|
-
const isRunningInKubernetes = () => Boolean(process.env.KUBERNETES_SERVICE_HOST);
|
|
38
|
-
|
|
39
|
-
const skipMetricsInEnvironment = options => options.detectKubernetes === true && !isRunningInKubernetes();
|
|
40
|
-
|
|
41
|
-
// NOTE:
|
|
42
|
-
// This is the `globalRegistry` provided by the `prom-client`
|
|
43
|
-
// We could create multiple registries with `new Prometheus.registry()`.
|
|
44
|
-
const defaultRegister = Prometheus__namespace.register;
|
|
45
|
-
const configure = _function.once(options => {
|
|
46
|
-
const shouldSkipMetricsInEnvironment = skipMetricsInEnvironment(options);
|
|
47
|
-
if (!shouldSkipMetricsInEnvironment) {
|
|
48
|
-
Prometheus__namespace.collectDefaultMetrics(options);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
|
|
53
|
-
const defaultOptions$4 = {
|
|
54
|
-
getLabelValues: () => ({}),
|
|
55
|
-
labels: [],
|
|
56
|
-
metricPrefix: '',
|
|
57
|
-
metricNames: {
|
|
58
|
-
up: ['nodejs_up']
|
|
59
|
-
},
|
|
60
|
-
gcCollectionInterval: 6000
|
|
61
|
-
};
|
|
62
|
-
const getMetrics$2 = options => ({
|
|
63
|
-
up: asArray$2(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
|
|
64
|
-
name: `${options.metricPrefix}${nameOfUpMetric}`,
|
|
65
|
-
help: '1 = nodejs server is up, 0 = nodejs server is not up'
|
|
66
|
-
}))
|
|
67
|
-
});
|
|
68
|
-
const createGcMetrics = options => {
|
|
69
|
-
const defaultedOptions = merge__default["default"](defaultOptions$4, options);
|
|
70
|
-
configure({
|
|
71
|
-
prefix: defaultedOptions.metricPrefix
|
|
72
|
-
});
|
|
73
|
-
const gcMetrics = getMetrics$2(defaultedOptions);
|
|
74
|
-
return gcMetrics;
|
|
75
|
-
};
|
|
76
|
-
createGcMetrics.defaultOptions = defaultOptions$4;
|
|
77
|
-
|
|
78
|
-
const defaultOptions$3 = {
|
|
79
|
-
disableGcMetrics: false
|
|
80
|
-
};
|
|
81
|
-
const createGcObserver = _function.once((_metrics, options) => () => {
|
|
82
|
-
prometheusGcStats.gcStats(defaultRegister, {
|
|
83
|
-
collectionInterval: options.gcCollectionInterval,
|
|
84
|
-
prefix: options.metricPrefix
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// @ts-expect-error
|
|
89
|
-
createGcObserver.defaultOptions = defaultOptions$3;
|
|
90
|
-
|
|
91
|
-
const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
|
|
92
|
-
const defaultLabels$1 = ['operation_name'];
|
|
93
|
-
const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
|
|
94
|
-
const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
|
|
95
|
-
const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
|
|
96
|
-
const shouldObserveGraphQlResolveFieldDurationAsHistogram = options => options.metricTypes.includes('graphQlResolveFieldDurationHistogram');
|
|
97
|
-
const shouldObserveGraphQlRequestDurationAsHistogram = options => options.metricTypes.includes('graphQlRequestDurationHistogram');
|
|
98
|
-
const shouldObserveGraphQlErrorsTotalAsCounter = options => options.metricTypes.includes('graphQlErrorsTotal');
|
|
99
|
-
const defaultOptions$2 = {
|
|
100
|
-
getLabelValues: () => ({}),
|
|
101
|
-
labels: [],
|
|
102
|
-
metricPrefix: '',
|
|
103
|
-
metricTypes: ['graphQlParseDurationHistogram', 'graphQlValidationDurationHistogram', 'graphQlResolveFieldDurationHistogram', 'graphQlRequestDurationHistogram', 'graphQlErrorsTotal'],
|
|
104
|
-
metricNames: {
|
|
105
|
-
graphQlParseDuration: ['graphql_parse_duration_seconds'],
|
|
106
|
-
graphQlValidationDuration: ['graphql_validation_duration_seconds'],
|
|
107
|
-
graphQlResolveFieldDuration: ['graphql_resolve_field_duration_seconds'],
|
|
108
|
-
graphQlRequestDuration: ['graphql_request_duration_seconds'],
|
|
109
|
-
graphQlErrorsTotal: ['graphql_errors_total']
|
|
110
|
-
},
|
|
111
|
-
metricPercentiles: {
|
|
112
|
-
graphQlParseDuration: defaultGraphQlPercentiles,
|
|
113
|
-
graphQlValidationDuration: defaultGraphQlPercentiles,
|
|
114
|
-
graphQlResolveFieldDuration: defaultGraphQlPercentiles,
|
|
115
|
-
graphQlRequestDuration: defaultGraphQlPercentiles,
|
|
116
|
-
graphQlErrorsTotal: defaultGraphQlPercentiles
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const getMetrics$1 = options => ({
|
|
120
|
-
graphQlParseDuration: shouldObserveGraphQlParseDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlParseDuration).map(nameOfGraphQlParseDuration => {
|
|
121
|
-
var _options$metricPercen;
|
|
122
|
-
return new Prometheus__namespace.Histogram({
|
|
123
|
-
name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
|
|
124
|
-
help: 'The GraphQL request parse time in seconds.',
|
|
125
|
-
labelNames: defaultLabels$1.concat(options.labels).toSorted(),
|
|
126
|
-
buckets: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.graphQlParseDuration) || defaultGraphQlPercentiles
|
|
127
|
-
});
|
|
128
|
-
}) : undefined,
|
|
129
|
-
graphQlValidationDuration: shouldObserveGraphQlValidationDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlValidationDuration).map(nameOfGraphQlValidationDuration => {
|
|
130
|
-
var _options$metricPercen2;
|
|
131
|
-
return new Prometheus__namespace.Histogram({
|
|
132
|
-
name: `${options.metricPrefix}${nameOfGraphQlValidationDuration}`,
|
|
133
|
-
help: 'The GraphQL request validation time in seconds.',
|
|
134
|
-
labelNames: defaultLabels$1.concat(options.labels).toSorted(),
|
|
135
|
-
buckets: ((_options$metricPercen2 = options.metricPercentiles) === null || _options$metricPercen2 === void 0 ? void 0 : _options$metricPercen2.graphQlValidationDuration) || defaultGraphQlPercentiles
|
|
136
|
-
});
|
|
137
|
-
}) : undefined,
|
|
138
|
-
graphQlResolveFieldDuration: shouldObserveGraphQlResolveFieldDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlResolveFieldDuration).map(nameOfGraphQlResolveFieldDuration => {
|
|
139
|
-
var _options$metricPercen3;
|
|
140
|
-
return new Prometheus__namespace.Histogram({
|
|
141
|
-
name: `${options.metricPrefix}${nameOfGraphQlResolveFieldDuration}`,
|
|
142
|
-
help: 'The GraphQL field resolving time in seconds.',
|
|
143
|
-
labelNames: defaultLabels$1.concat(['field_name']).concat(options.labels).toSorted(),
|
|
144
|
-
buckets: ((_options$metricPercen3 = options.metricPercentiles) === null || _options$metricPercen3 === void 0 ? void 0 : _options$metricPercen3.graphQlResolveFieldDuration) || defaultGraphQlPercentiles
|
|
145
|
-
});
|
|
146
|
-
}) : undefined,
|
|
147
|
-
graphQlRequestDuration: shouldObserveGraphQlRequestDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlRequestDuration).map(nameOfGraphQlRequestDuration => {
|
|
148
|
-
var _options$metricPercen4;
|
|
149
|
-
return new Prometheus__namespace.Histogram({
|
|
150
|
-
name: `${options.metricPrefix}${nameOfGraphQlRequestDuration}`,
|
|
151
|
-
help: 'The GraphQL request duration time in seconds.',
|
|
152
|
-
labelNames: defaultLabels$1.concat(options.labels).toSorted(),
|
|
153
|
-
buckets: ((_options$metricPercen4 = options.metricPercentiles) === null || _options$metricPercen4 === void 0 ? void 0 : _options$metricPercen4.graphQlRequestDuration) || defaultGraphQlPercentiles
|
|
154
|
-
});
|
|
155
|
-
}) : undefined,
|
|
156
|
-
graphQlErrorsTotal: shouldObserveGraphQlErrorsTotalAsCounter(options) ? asArray$1(options.metricNames.graphQlErrorsTotal).map(nameOfGraphQlErrorsCount => new Prometheus__namespace.Counter({
|
|
157
|
-
name: `${options.metricPrefix}${nameOfGraphQlErrorsCount}`,
|
|
158
|
-
help: 'Count of errors while parsing, validating, or executing a GraphQL operation.',
|
|
159
|
-
labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).toSorted()
|
|
160
|
-
})) : undefined
|
|
161
|
-
});
|
|
162
|
-
const createGraphQlMetrics = options => {
|
|
163
|
-
const defaultedOptions = merge__default["default"](defaultOptions$2, options);
|
|
164
|
-
configure({
|
|
165
|
-
prefix: defaultedOptions.metricPrefix
|
|
166
|
-
});
|
|
167
|
-
const metrics = getMetrics$1(defaultedOptions);
|
|
168
|
-
return metrics;
|
|
169
|
-
};
|
|
170
|
-
createGraphQlMetrics.defaultOptions = defaultOptions$2;
|
|
171
|
-
|
|
172
|
-
const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.99];
|
|
173
|
-
const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
|
|
174
|
-
const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
|
|
175
|
-
const defaultLabels = ['path', 'status_code', 'method'];
|
|
176
|
-
const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
|
|
177
|
-
const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
|
|
178
|
-
const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.includes('httpRequestsHistogram');
|
|
179
|
-
const shouldObserveHttpRequestsAsCounter = options => options.metricTypes.includes('httpRequestsTotal');
|
|
180
|
-
const shouldObserveHttpContentLengthAsHistogram = options => options.metricTypes.includes('httpContentLengthHistogram');
|
|
181
|
-
const defaultOptions$1 = {
|
|
182
|
-
getLabelValues: () => ({}),
|
|
183
|
-
labels: [],
|
|
184
|
-
metricPrefix: '',
|
|
185
|
-
metricTypes: ['httpRequestsTotal', 'httpRequestsHistogram'],
|
|
186
|
-
metricNames: {
|
|
187
|
-
httpRequestsTotal: ['http_requests_total'],
|
|
188
|
-
httpRequestDurationPerPercentileInSeconds: ['http_request_duration_per_percentile_seconds'],
|
|
189
|
-
httpRequestDurationInSeconds: ['http_request_duration_seconds'],
|
|
190
|
-
httpRequestContentLengthInBytes: ['http_request_content_length_bytes'],
|
|
191
|
-
httpResponseContentLengthInBytes: ['http_response_content_length_bytes']
|
|
192
|
-
},
|
|
193
|
-
metricBuckets: {
|
|
194
|
-
httpRequestContentLengthInBytes: defaultHttpContentLengthInBytes,
|
|
195
|
-
httpRequestDurationInSeconds: defaultHttpRequestDurationInSeconds
|
|
196
|
-
},
|
|
197
|
-
metricPercentiles: {
|
|
198
|
-
httpRequestDurationPerPercentileInSeconds: defaultHttpRequestDurationPercentileInSeconds,
|
|
199
|
-
httpResponseContentLengthInBytes: defaultHttpContentLengthInBytes
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
const getMetrics = options => ({
|
|
203
|
-
httpRequestContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray(options.metricNames.httpRequestContentLengthInBytes).map(nameOfHttpContentLengthMetric => {
|
|
204
|
-
var _options$metricBucket;
|
|
205
|
-
return new Prometheus__namespace.Histogram({
|
|
206
|
-
name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
|
|
207
|
-
help: 'The HTTP request content length in bytes.',
|
|
208
|
-
labelNames: defaultLabels.concat(options.labels).toSorted(),
|
|
209
|
-
buckets: ((_options$metricBucket = options.metricBuckets) === null || _options$metricBucket === void 0 ? void 0 : _options$metricBucket.httpRequestContentLengthInBytes) || defaultHttpContentLengthInBytes
|
|
210
|
-
});
|
|
211
|
-
}) : undefined,
|
|
212
|
-
httpResponseContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray(options.metricNames.httpResponseContentLengthInBytes).map(nameOfHttpContentLengthMetric => {
|
|
213
|
-
var _options$metricBucket2;
|
|
214
|
-
return new Prometheus__namespace.Histogram({
|
|
215
|
-
name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
|
|
216
|
-
help: 'The HTTP response content length in bytes.',
|
|
217
|
-
labelNames: defaultLabels.concat(options.labels).toSorted(),
|
|
218
|
-
buckets: ((_options$metricBucket2 = options.metricBuckets) === null || _options$metricBucket2 === void 0 ? void 0 : _options$metricBucket2.httpResponseContentLengthInBytes) || defaultHttpContentLengthInBytes
|
|
219
|
-
});
|
|
220
|
-
}) : undefined
|
|
221
|
-
});
|
|
222
|
-
const getHttpRequestLatencyMetricsInSeconds = options => ({
|
|
223
|
-
httpRequestDurationPerPercentileInSeconds: shouldObserveHttpRequestsAsSummary(options) ? asArray(options.metricNames.httpRequestDurationPerPercentileInSeconds).map(nameOfHttpRequestDurationPerPercentileInSeconds => {
|
|
224
|
-
var _options$metricPercen;
|
|
225
|
-
return new Prometheus__namespace.Summary({
|
|
226
|
-
name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
|
|
227
|
-
help: 'The HTTP request latencies in seconds.',
|
|
228
|
-
labelNames: defaultLabels.concat(options.labels).toSorted(),
|
|
229
|
-
percentiles: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.httpRequestDurationPerPercentileInSeconds) || defaultHttpRequestDurationPercentileInSeconds
|
|
230
|
-
});
|
|
231
|
-
}) : undefined,
|
|
232
|
-
httpRequestDurationInSeconds: shouldObserveHttpRequestsAsHistogram(options) ? asArray(options.metricNames.httpRequestDurationInSeconds).map(nameOfHttpRequestDurationInSecondsMetric => {
|
|
233
|
-
var _options$metricBucket3;
|
|
234
|
-
return new Prometheus__namespace.Histogram({
|
|
235
|
-
name: `${options.metricPrefix}${nameOfHttpRequestDurationInSecondsMetric}`,
|
|
236
|
-
help: 'The HTTP request latencies in seconds.',
|
|
237
|
-
labelNames: defaultLabels.concat(options.labels).toSorted(),
|
|
238
|
-
buckets: ((_options$metricBucket3 = options.metricBuckets) === null || _options$metricBucket3 === void 0 ? void 0 : _options$metricBucket3.httpRequestDurationInSeconds) || defaultHttpRequestDurationInSeconds
|
|
239
|
-
});
|
|
240
|
-
}) : undefined
|
|
241
|
-
});
|
|
242
|
-
const getHttpRequestCounterMetric = options => ({
|
|
243
|
-
httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
|
|
244
|
-
name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
|
|
245
|
-
help: 'The total HTTP requests.',
|
|
246
|
-
labelNames: defaultLabels.concat(options.labels).toSorted()
|
|
247
|
-
})) : undefined
|
|
248
|
-
});
|
|
249
|
-
const createHttpMetrics = options => {
|
|
250
|
-
const defaultedOptions = merge__default["default"](defaultOptions$1, options);
|
|
251
|
-
configure({
|
|
252
|
-
prefix: defaultedOptions.metricPrefix
|
|
253
|
-
});
|
|
254
|
-
const metrics = getMetrics(defaultedOptions);
|
|
255
|
-
const httpRequestLatencyMetricsInSeconds = getHttpRequestLatencyMetricsInSeconds(defaultedOptions);
|
|
256
|
-
const httpRequestCounterMetric = getHttpRequestCounterMetric(defaultedOptions);
|
|
257
|
-
return Object.assign({}, metrics, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
|
|
258
|
-
};
|
|
259
|
-
createHttpMetrics.defaultOptions = defaultOptions$1;
|
|
260
|
-
|
|
261
|
-
const NS_PER_SEC = 1e9;
|
|
262
|
-
function endMeasurementFrom(start) {
|
|
263
|
-
const [seconds, nanoseconds] = process.hrtime(start);
|
|
264
|
-
return {
|
|
265
|
-
durationS: (seconds * NS_PER_SEC + nanoseconds) / NS_PER_SEC
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function sortLabels(unsortedLabels) {
|
|
270
|
-
return Object.keys(unsortedLabels).toSorted((a, b) => {
|
|
271
|
-
if (a < b) {
|
|
272
|
-
return -1;
|
|
273
|
-
}
|
|
274
|
-
if (a > b) {
|
|
275
|
-
return 1;
|
|
276
|
-
}
|
|
277
|
-
return 0;
|
|
278
|
-
}).reduce((sortedLabels, labelName) => {
|
|
279
|
-
sortedLabels[labelName] = unsortedLabels[labelName];
|
|
280
|
-
return sortedLabels;
|
|
281
|
-
}, {});
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const defaultOptions = {
|
|
285
|
-
detectKubernetes: false
|
|
286
|
-
};
|
|
287
|
-
function isTiming(timing) {
|
|
288
|
-
return !Array.isArray(timing);
|
|
289
|
-
}
|
|
290
|
-
const createRequestRecorder = (metrics, options = defaultOptions) => {
|
|
291
|
-
const defaultedRecorderOptions = merge__default["default"](defaultOptions, options);
|
|
292
|
-
const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
|
|
293
|
-
return (timing, recordingOptions) => {
|
|
294
|
-
const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
|
|
295
|
-
const labels = sortLabels(recordingOptions.labels);
|
|
296
|
-
if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
|
|
297
|
-
if (metrics.httpRequestDurationInSeconds) {
|
|
298
|
-
for (const httpRequestDurationInSecondsMetricType of metrics.httpRequestDurationInSeconds) {
|
|
299
|
-
httpRequestDurationInSecondsMetricType.observe(labels, durationS);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
|
|
304
|
-
if (metrics.httpRequestDurationPerPercentileInSeconds) {
|
|
305
|
-
for (const httpRequestDurationPerPercentileInSecondsMetricType of metrics.httpRequestDurationPerPercentileInSeconds) {
|
|
306
|
-
httpRequestDurationPerPercentileInSecondsMetricType.observe(labels, durationS);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
|
|
311
|
-
if (metrics.httpRequestsTotal) {
|
|
312
|
-
for (const httpRequestsTotalMetricType of metrics.httpRequestsTotal) {
|
|
313
|
-
httpRequestsTotalMetricType.inc(labels);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (recordingOptions.requestContentLength) {
|
|
318
|
-
if (metrics.httpRequestContentLengthInBytes) {
|
|
319
|
-
for (const httpRequestContentLengthInBytesMetricType of metrics.httpRequestContentLengthInBytes) {
|
|
320
|
-
httpRequestContentLengthInBytesMetricType.observe(labels, recordingOptions.requestContentLength);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
if (recordingOptions.responseContentLength) {
|
|
325
|
-
if (metrics.httpResponseContentLengthInBytes) {
|
|
326
|
-
for (const httpResponseContentLengthInBytesMetricType of metrics.httpResponseContentLengthInBytes) {
|
|
327
|
-
httpResponseContentLengthInBytesMetricType.observe(labels, recordingOptions.responseContentLength);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
};
|
|
333
|
-
createRequestRecorder.defaultOptions = defaultOptions;
|
|
334
|
-
|
|
335
|
-
const normalizeMethod = method => method.toLowerCase();
|
|
336
|
-
|
|
337
|
-
const urlValueParser = new UrlValueParser__default["default"]();
|
|
338
|
-
const normalizePath = path => {
|
|
339
|
-
const parsedPathname = url__default["default"].parse(path).pathname;
|
|
340
|
-
if (!parsedPathname) {
|
|
341
|
-
return '';
|
|
342
|
-
}
|
|
343
|
-
return urlValueParser.replacePathValues(parsedPathname, '#val');
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
const normalizeStatusCode = statusCode => statusCode;
|
|
347
|
-
|
|
348
|
-
const defaultNormalizers = {
|
|
349
|
-
normalizeStatusCode,
|
|
350
|
-
normalizePath,
|
|
351
|
-
normalizeMethod
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
const getSummary = async () => defaultRegister.metrics();
|
|
355
|
-
const getContentType = () => defaultRegister.contentType;
|
|
356
|
-
|
|
357
|
-
class Timing {
|
|
358
|
-
static NS_PER_SEC = BigInt(1e9);
|
|
359
|
-
#startTime;
|
|
360
|
-
#endTime;
|
|
361
|
-
constructor() {
|
|
362
|
-
this.reset();
|
|
363
|
-
}
|
|
364
|
-
value() {
|
|
365
|
-
const startTime = this.#startTime;
|
|
366
|
-
const endTime = this.#endTime;
|
|
367
|
-
if (!endTime || !startTime) {
|
|
368
|
-
return {
|
|
369
|
-
seconds: undefined
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
return {
|
|
373
|
-
seconds: Number(endTime - startTime) / Number(Timing.NS_PER_SEC)
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
reset() {
|
|
377
|
-
this.#startTime = process.hrtime.bigint();
|
|
378
|
-
this.#endTime = undefined;
|
|
379
|
-
return this;
|
|
380
|
-
}
|
|
381
|
-
end() {
|
|
382
|
-
this.#endTime = process.hrtime.bigint();
|
|
383
|
-
return this;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
const timing = {
|
|
387
|
-
start() {
|
|
388
|
-
return new Timing();
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
exports.Prometheus = Prometheus__namespace;
|
|
393
|
-
exports.createGcMetrics = createGcMetrics;
|
|
394
|
-
exports.createGcObserver = createGcObserver;
|
|
395
|
-
exports.createGraphQlMetrics = createGraphQlMetrics;
|
|
396
|
-
exports.createHttpMetrics = createHttpMetrics;
|
|
397
|
-
exports.createRequestRecorder = createRequestRecorder;
|
|
398
|
-
exports.defaultNormalizers = defaultNormalizers;
|
|
399
|
-
exports.defaultRegister = defaultRegister;
|
|
400
|
-
exports.endMeasurementFrom = endMeasurementFrom;
|
|
401
|
-
exports.getContentType = getContentType;
|
|
402
|
-
exports.getSummary = getSummary;
|
|
403
|
-
exports.isRunningInKubernetes = isRunningInKubernetes;
|
|
404
|
-
exports.normalizeMethod = normalizeMethod;
|
|
405
|
-
exports.normalizePath = normalizePath;
|
|
406
|
-
exports.normalizeStatusCode = normalizeStatusCode;
|
|
407
|
-
exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
|
|
408
|
-
exports.sortLabels = sortLabels;
|
|
409
|
-
exports.timing = timing;
|