@promster/metrics 9.1.4 → 9.1.6

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.
@@ -1,14 +1,14 @@
1
1
  import type { TPromsterOptions, TLabelValues, THttpMetrics } from '@promster/types';
2
+ import type { Timing } from '../timing';
2
3
  import { sortLabels } from '../sort-labels';
3
4
  import { endMeasurementFrom } from '../end-measurement-from';
4
- import { Timing } from '../timing';
5
- declare type TRecordingOptions = {
5
+ type TRecordingOptions = {
6
6
  labels: TLabelValues;
7
7
  requestContentLength?: number;
8
8
  responseContentLength?: number;
9
9
  };
10
- declare type TLegacyTiming = [number, number];
11
- export declare type TRequestRecorder = (timing: Timing | TLegacyTiming, recordingOptions: TRecordingOptions) => void;
10
+ type TLegacyTiming = [number, number];
11
+ export type TRequestRecorder = (timing: Timing | TLegacyTiming, recordingOptions: TRecordingOptions) => void;
12
12
  declare const createRequestRecorder: {
13
13
  (metrics: THttpMetrics, options?: TPromsterOptions): TRequestRecorder;
14
14
  defaultOptions: TPromsterOptions;
@@ -1,5 +1,5 @@
1
1
  import type { TPromsterOptions } from '@promster/types';
2
- declare type TSkipMetricsInEnvironmentOptions = {
2
+ type TSkipMetricsInEnvironmentOptions = {
3
3
  detectKubernetes?: TPromsterOptions['detectKubernetes'];
4
4
  };
5
5
  declare const skipMetricsInEnvironment: (options: TSkipMetricsInEnvironmentOptions) => boolean;
@@ -40,13 +40,15 @@ const isRunningInKubernetes = () => Boolean(process.env.KUBERNETES_SERVICE_HOST)
40
40
 
41
41
  const skipMetricsInEnvironment = options => options.detectKubernetes === true && !isRunningInKubernetes();
42
42
 
43
+ // NOTE:
43
44
  // This is the `globalRegistry` provided by the `prom-client`
44
45
  // We could create multiple registries with `new Prometheus.registry()`.
45
-
46
46
  const defaultRegister = Prometheus__namespace.register;
47
+
48
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
49
+
47
50
  const configure = once__default["default"](options => {
48
51
  const shouldSkipMetricsInEnvironment = skipMetricsInEnvironment(options);
49
-
50
52
  if (!shouldSkipMetricsInEnvironment) {
51
53
  Prometheus__namespace.collectDefaultMetrics(options);
52
54
  }
@@ -56,17 +58,11 @@ const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.9
56
58
  const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
57
59
  const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
58
60
  const defaultLabels$2 = ['path', 'status_code', 'method'];
59
-
60
61
  const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
61
-
62
62
  const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
63
-
64
63
  const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.includes('httpRequestsHistogram');
65
-
66
64
  const shouldObserveHttpRequestsAsCounter = options => options.metricTypes.includes('httpRequestsTotal');
67
-
68
65
  const shouldObserveHttpContentLengthAsHistogram = options => options.metricTypes.includes('httpContentLengthHistogram');
69
-
70
66
  const defaultOptions$4 = {
71
67
  getLabelValues: () => ({}),
72
68
  labels: [],
@@ -80,7 +76,6 @@ const defaultOptions$4 = {
80
76
  httpResponseContentLengthInBytes: ['http_response_content_length_bytes']
81
77
  }
82
78
  };
83
-
84
79
  const getMetrics$2 = options => ({
85
80
  httpRequestContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray$2(options.metricNames.httpRequestContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
86
81
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
@@ -95,7 +90,6 @@ const getMetrics$2 = options => ({
95
90
  buckets: options.buckets || defaultHttpContentLengthInBytes
96
91
  })) : undefined
97
92
  });
98
-
99
93
  const getHttpRequestLatencyMetricsInSeconds = options => ({
100
94
  httpRequestDurationPerPercentileInSeconds: shouldObserveHttpRequestsAsSummary(options) ? asArray$2(options.metricNames.httpRequestDurationPerPercentileInSeconds).map(nameOfHttpRequestDurationPerPercentileInSeconds => new Prometheus__namespace.Summary({
101
95
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
@@ -110,7 +104,6 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
110
104
  buckets: options.buckets || defaultHttpRequestDurationInSeconds
111
105
  })) : undefined
112
106
  });
113
-
114
107
  const getHttpRequestCounterMetric = options => ({
115
108
  httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray$2(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
116
109
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
@@ -118,7 +111,6 @@ const getHttpRequestCounterMetric = options => ({
118
111
  labelNames: defaultLabels$2.concat(options.labels).sort()
119
112
  })) : undefined
120
113
  });
121
-
122
114
  const createHttpMetrics = options => {
123
115
  const defaultedOptions = merge__default["default"](defaultOptions$4, options);
124
116
  configure({
@@ -129,24 +121,16 @@ const createHttpMetrics = options => {
129
121
  const httpRequestCounterMetric = getHttpRequestCounterMetric(defaultedOptions);
130
122
  return Object.assign({}, metrics, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
131
123
  };
132
-
133
124
  createHttpMetrics.defaultOptions = defaultOptions$4;
134
125
 
135
126
  const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
136
127
  const defaultLabels$1 = ['operation_name'];
137
-
138
128
  const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
139
-
140
129
  const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
141
-
142
130
  const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
143
-
144
131
  const shouldObserveGraphQlResolveFieldDurationAsHistogram = options => options.metricTypes.includes('graphQlResolveFieldDurationHistogram');
145
-
146
132
  const shouldObserveGraphQlRequestDurationAsHistogram = options => options.metricTypes.includes('graphQlRequestDurationHistogram');
147
-
148
133
  const shouldObserveGraphQlErrorsTotalAsCounter = options => options.metricTypes.includes('graphQlErrorsTotal');
149
-
150
134
  const defaultOptions$3 = {
151
135
  getLabelValues: () => ({}),
152
136
  labels: [],
@@ -160,7 +144,6 @@ const defaultOptions$3 = {
160
144
  graphQlErrorsTotal: ['graphql_errors_total']
161
145
  }
162
146
  };
163
-
164
147
  const getMetrics$1 = options => ({
165
148
  graphQlParseDuration: shouldObserveGraphQlParseDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlParseDuration).map(nameOfGraphQlParseDuration => new Prometheus__namespace.Histogram({
166
149
  name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
@@ -192,7 +175,6 @@ const getMetrics$1 = options => ({
192
175
  labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).sort()
193
176
  })) : undefined
194
177
  });
195
-
196
178
  const createGraphQlMetrics = options => {
197
179
  const defaultedOptions = merge__default["default"](defaultOptions$3, options);
198
180
  configure({
@@ -201,13 +183,10 @@ const createGraphQlMetrics = options => {
201
183
  const metrics = getMetrics$1(defaultedOptions);
202
184
  return metrics;
203
185
  };
204
-
205
186
  createGraphQlMetrics.defaultOptions = defaultOptions$3;
206
187
 
207
188
  const defaultLabels = ['gc_type'];
208
-
209
189
  const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
210
-
211
190
  const defaultOptions$2 = {
212
191
  getLabelValues: () => ({}),
213
192
  labels: [],
@@ -219,7 +198,6 @@ const defaultOptions$2 = {
219
198
  reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total']
220
199
  }
221
200
  };
222
-
223
201
  const getMetrics = options => ({
224
202
  up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
225
203
  name: `${options.metricPrefix}${nameOfUpMetric}`,
@@ -241,7 +219,6 @@ const getMetrics = options => ({
241
219
  labelNames: defaultLabels
242
220
  }))
243
221
  });
244
-
245
222
  const createGcMetrics = options => {
246
223
  const defaultedOptions = merge__default["default"](defaultOptions$2, options);
247
224
  configure({
@@ -250,11 +227,9 @@ const createGcMetrics = options => {
250
227
  const gcMetrics = getMetrics(defaultedOptions);
251
228
  return gcMetrics;
252
229
  };
253
-
254
230
  createGcMetrics.defaultOptions = defaultOptions$2;
255
231
 
256
232
  const getSummary = async () => defaultRegister.metrics();
257
-
258
233
  const getContentType = () => defaultRegister.contentType;
259
234
 
260
235
  function sortLabels(unsortedLabels) {
@@ -262,11 +237,9 @@ function sortLabels(unsortedLabels) {
262
237
  if (a < b) {
263
238
  return -1;
264
239
  }
265
-
266
240
  if (a > b) {
267
241
  return 1;
268
242
  }
269
-
270
243
  return 0;
271
244
  }).reduce((sortedLabels, labelName) => {
272
245
  sortedLabels[labelName] = unsortedLabels[labelName];
@@ -275,7 +248,6 @@ function sortLabels(unsortedLabels) {
275
248
  }
276
249
 
277
250
  const NS_PER_SEC = 1e9;
278
-
279
251
  function endMeasurementFrom(start) {
280
252
  const [seconds, nanoseconds] = process.hrtime(start);
281
253
  return {
@@ -287,62 +259,51 @@ const defaultOptions$1 = {
287
259
  skip: () => false,
288
260
  detectKubernetes: false
289
261
  };
290
-
291
262
  function isTiming(timing) {
292
263
  return !Array.isArray(timing);
293
264
  }
294
-
295
265
  const createRequestRecorder = (metrics, options = defaultOptions$1) => {
296
266
  const defaultedRecorderOptions = merge__default["default"](defaultOptions$1, options);
297
267
  const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
298
268
  return (timing, recordingOptions) => {
299
269
  const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
300
270
  const labels = sortLabels(recordingOptions.labels);
301
-
302
271
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
303
272
  var _metrics$httpRequestD;
304
-
305
273
  (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
306
274
  httpRequestDurationInSecondsMetricType.observe(labels, durationS);
307
275
  });
308
276
  }
309
-
310
277
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
311
278
  var _metrics$httpRequestD2;
312
-
313
279
  (_metrics$httpRequestD2 = metrics.httpRequestDurationPerPercentileInSeconds) === null || _metrics$httpRequestD2 === void 0 ? void 0 : _metrics$httpRequestD2.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
314
280
  httpRequestDurationPerPercentileInSecondsMetricType.observe(labels, durationS);
315
281
  });
316
282
  }
317
-
318
283
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
319
284
  var _metrics$httpRequests;
320
-
321
285
  (_metrics$httpRequests = metrics.httpRequestsTotal) === null || _metrics$httpRequests === void 0 ? void 0 : _metrics$httpRequests.forEach(httpRequestsTotalMetricType => {
322
286
  httpRequestsTotalMetricType.inc(labels);
323
287
  });
324
288
  }
325
-
326
289
  if (recordingOptions.requestContentLength) {
327
290
  var _metrics$httpRequestC;
328
-
329
291
  (_metrics$httpRequestC = metrics.httpRequestContentLengthInBytes) === null || _metrics$httpRequestC === void 0 ? void 0 : _metrics$httpRequestC.forEach(httpRequestContentLengthInBytesMetricType => {
330
- httpRequestContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
292
+ httpRequestContentLengthInBytesMetricType.observe(labels,
293
+ // @ts-expect-error
331
294
  recordingOptions.requestContentLength);
332
295
  });
333
296
  }
334
-
335
297
  if (recordingOptions.responseContentLength) {
336
298
  var _metrics$httpResponse;
337
-
338
299
  (_metrics$httpResponse = metrics.httpResponseContentLengthInBytes) === null || _metrics$httpResponse === void 0 ? void 0 : _metrics$httpResponse.forEach(httpResponseContentLengthInBytesMetricType => {
339
- httpResponseContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
300
+ httpResponseContentLengthInBytesMetricType.observe(labels,
301
+ // @ts-expect-error
340
302
  recordingOptions.responseContentLength);
341
303
  });
342
304
  }
343
305
  };
344
306
  };
345
-
346
307
  createRequestRecorder.defaultOptions = defaultOptions$1;
347
308
 
348
309
  const gc = requireOptional__default["default"]('@sematext/gc-stats');
@@ -362,7 +323,6 @@ const createGcObserver = once__default["default"]((metrics, options) => () => {
362
323
  if (typeof gc !== 'function') {
363
324
  return;
364
325
  }
365
-
366
326
  if (options.disableGcMetrics) return;
367
327
  gc().on('stats', stats => {
368
328
  const gcType = gcTypes[stats.gctype];
@@ -372,7 +332,6 @@ const createGcObserver = once__default["default"]((metrics, options) => () => {
372
332
  metrics.durationOfGc.forEach(durationOfGcMetricType => {
373
333
  durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
374
334
  });
375
-
376
335
  if (stats.diff.usedHeapSize < 0) {
377
336
  metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
378
337
  reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
@@ -385,7 +344,6 @@ createGcObserver.defaultOptions = defaultOptions;
385
344
  const normalizeStatusCode = statusCode => statusCode;
386
345
 
387
346
  const urlValueParser = new UrlValueParser__default["default"]();
388
-
389
347
  const normalizePath = path => urlValueParser.replacePathValues(url__default["default"].parse(path).pathname, '#val');
390
348
 
391
349
  const normalizeMethod = method => method.toLowerCase();
@@ -400,11 +358,9 @@ class Timing {
400
358
  static NS_PER_SEC = BigInt(1e9);
401
359
  #startTime;
402
360
  #endTime;
403
-
404
361
  constructor() {
405
362
  this.reset();
406
363
  }
407
-
408
364
  value() {
409
365
  const startTime = this.#startTime;
410
366
  const endTime = this.#endTime;
@@ -415,25 +371,20 @@ class Timing {
415
371
  seconds: Number(endTime - startTime) / Number(Timing.NS_PER_SEC)
416
372
  };
417
373
  }
418
-
419
374
  reset() {
420
375
  this.#startTime = process.hrtime.bigint();
421
376
  this.#endTime = undefined;
422
377
  return this;
423
378
  }
424
-
425
379
  end() {
426
380
  this.#endTime = process.hrtime.bigint();
427
381
  return this;
428
382
  }
429
-
430
383
  }
431
-
432
384
  const timing = {
433
385
  start() {
434
386
  return new Timing();
435
387
  }
436
-
437
388
  };
438
389
 
439
390
  exports.Prometheus = Prometheus__namespace;
@@ -40,13 +40,15 @@ const isRunningInKubernetes = () => Boolean(process.env.KUBERNETES_SERVICE_HOST)
40
40
 
41
41
  const skipMetricsInEnvironment = options => options.detectKubernetes === true && !isRunningInKubernetes();
42
42
 
43
+ // NOTE:
43
44
  // This is the `globalRegistry` provided by the `prom-client`
44
45
  // We could create multiple registries with `new Prometheus.registry()`.
45
-
46
46
  const defaultRegister = Prometheus__namespace.register;
47
+
48
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
49
+
47
50
  const configure = once__default["default"](options => {
48
51
  const shouldSkipMetricsInEnvironment = skipMetricsInEnvironment(options);
49
-
50
52
  if (!shouldSkipMetricsInEnvironment) {
51
53
  Prometheus__namespace.collectDefaultMetrics(options);
52
54
  }
@@ -56,17 +58,11 @@ const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.9
56
58
  const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
57
59
  const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
58
60
  const defaultLabels$2 = ['path', 'status_code', 'method'];
59
-
60
61
  const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
61
-
62
62
  const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
63
-
64
63
  const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.includes('httpRequestsHistogram');
65
-
66
64
  const shouldObserveHttpRequestsAsCounter = options => options.metricTypes.includes('httpRequestsTotal');
67
-
68
65
  const shouldObserveHttpContentLengthAsHistogram = options => options.metricTypes.includes('httpContentLengthHistogram');
69
-
70
66
  const defaultOptions$4 = {
71
67
  getLabelValues: () => ({}),
72
68
  labels: [],
@@ -80,7 +76,6 @@ const defaultOptions$4 = {
80
76
  httpResponseContentLengthInBytes: ['http_response_content_length_bytes']
81
77
  }
82
78
  };
83
-
84
79
  const getMetrics$2 = options => ({
85
80
  httpRequestContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray$2(options.metricNames.httpRequestContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
86
81
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
@@ -95,7 +90,6 @@ const getMetrics$2 = options => ({
95
90
  buckets: options.buckets || defaultHttpContentLengthInBytes
96
91
  })) : undefined
97
92
  });
98
-
99
93
  const getHttpRequestLatencyMetricsInSeconds = options => ({
100
94
  httpRequestDurationPerPercentileInSeconds: shouldObserveHttpRequestsAsSummary(options) ? asArray$2(options.metricNames.httpRequestDurationPerPercentileInSeconds).map(nameOfHttpRequestDurationPerPercentileInSeconds => new Prometheus__namespace.Summary({
101
95
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
@@ -110,7 +104,6 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
110
104
  buckets: options.buckets || defaultHttpRequestDurationInSeconds
111
105
  })) : undefined
112
106
  });
113
-
114
107
  const getHttpRequestCounterMetric = options => ({
115
108
  httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray$2(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
116
109
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
@@ -118,7 +111,6 @@ const getHttpRequestCounterMetric = options => ({
118
111
  labelNames: defaultLabels$2.concat(options.labels).sort()
119
112
  })) : undefined
120
113
  });
121
-
122
114
  const createHttpMetrics = options => {
123
115
  const defaultedOptions = merge__default["default"](defaultOptions$4, options);
124
116
  configure({
@@ -129,24 +121,16 @@ const createHttpMetrics = options => {
129
121
  const httpRequestCounterMetric = getHttpRequestCounterMetric(defaultedOptions);
130
122
  return Object.assign({}, metrics, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
131
123
  };
132
-
133
124
  createHttpMetrics.defaultOptions = defaultOptions$4;
134
125
 
135
126
  const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
136
127
  const defaultLabels$1 = ['operation_name'];
137
-
138
128
  const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
139
-
140
129
  const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
141
-
142
130
  const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
143
-
144
131
  const shouldObserveGraphQlResolveFieldDurationAsHistogram = options => options.metricTypes.includes('graphQlResolveFieldDurationHistogram');
145
-
146
132
  const shouldObserveGraphQlRequestDurationAsHistogram = options => options.metricTypes.includes('graphQlRequestDurationHistogram');
147
-
148
133
  const shouldObserveGraphQlErrorsTotalAsCounter = options => options.metricTypes.includes('graphQlErrorsTotal');
149
-
150
134
  const defaultOptions$3 = {
151
135
  getLabelValues: () => ({}),
152
136
  labels: [],
@@ -160,7 +144,6 @@ const defaultOptions$3 = {
160
144
  graphQlErrorsTotal: ['graphql_errors_total']
161
145
  }
162
146
  };
163
-
164
147
  const getMetrics$1 = options => ({
165
148
  graphQlParseDuration: shouldObserveGraphQlParseDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlParseDuration).map(nameOfGraphQlParseDuration => new Prometheus__namespace.Histogram({
166
149
  name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
@@ -192,7 +175,6 @@ const getMetrics$1 = options => ({
192
175
  labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).sort()
193
176
  })) : undefined
194
177
  });
195
-
196
178
  const createGraphQlMetrics = options => {
197
179
  const defaultedOptions = merge__default["default"](defaultOptions$3, options);
198
180
  configure({
@@ -201,13 +183,10 @@ const createGraphQlMetrics = options => {
201
183
  const metrics = getMetrics$1(defaultedOptions);
202
184
  return metrics;
203
185
  };
204
-
205
186
  createGraphQlMetrics.defaultOptions = defaultOptions$3;
206
187
 
207
188
  const defaultLabels = ['gc_type'];
208
-
209
189
  const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
210
-
211
190
  const defaultOptions$2 = {
212
191
  getLabelValues: () => ({}),
213
192
  labels: [],
@@ -219,7 +198,6 @@ const defaultOptions$2 = {
219
198
  reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total']
220
199
  }
221
200
  };
222
-
223
201
  const getMetrics = options => ({
224
202
  up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
225
203
  name: `${options.metricPrefix}${nameOfUpMetric}`,
@@ -241,7 +219,6 @@ const getMetrics = options => ({
241
219
  labelNames: defaultLabels
242
220
  }))
243
221
  });
244
-
245
222
  const createGcMetrics = options => {
246
223
  const defaultedOptions = merge__default["default"](defaultOptions$2, options);
247
224
  configure({
@@ -250,11 +227,9 @@ const createGcMetrics = options => {
250
227
  const gcMetrics = getMetrics(defaultedOptions);
251
228
  return gcMetrics;
252
229
  };
253
-
254
230
  createGcMetrics.defaultOptions = defaultOptions$2;
255
231
 
256
232
  const getSummary = async () => defaultRegister.metrics();
257
-
258
233
  const getContentType = () => defaultRegister.contentType;
259
234
 
260
235
  function sortLabels(unsortedLabels) {
@@ -262,11 +237,9 @@ function sortLabels(unsortedLabels) {
262
237
  if (a < b) {
263
238
  return -1;
264
239
  }
265
-
266
240
  if (a > b) {
267
241
  return 1;
268
242
  }
269
-
270
243
  return 0;
271
244
  }).reduce((sortedLabels, labelName) => {
272
245
  sortedLabels[labelName] = unsortedLabels[labelName];
@@ -275,7 +248,6 @@ function sortLabels(unsortedLabels) {
275
248
  }
276
249
 
277
250
  const NS_PER_SEC = 1e9;
278
-
279
251
  function endMeasurementFrom(start) {
280
252
  const [seconds, nanoseconds] = process.hrtime(start);
281
253
  return {
@@ -287,62 +259,51 @@ const defaultOptions$1 = {
287
259
  skip: () => false,
288
260
  detectKubernetes: false
289
261
  };
290
-
291
262
  function isTiming(timing) {
292
263
  return !Array.isArray(timing);
293
264
  }
294
-
295
265
  const createRequestRecorder = (metrics, options = defaultOptions$1) => {
296
266
  const defaultedRecorderOptions = merge__default["default"](defaultOptions$1, options);
297
267
  const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
298
268
  return (timing, recordingOptions) => {
299
269
  const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
300
270
  const labels = sortLabels(recordingOptions.labels);
301
-
302
271
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
303
272
  var _metrics$httpRequestD;
304
-
305
273
  (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
306
274
  httpRequestDurationInSecondsMetricType.observe(labels, durationS);
307
275
  });
308
276
  }
309
-
310
277
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
311
278
  var _metrics$httpRequestD2;
312
-
313
279
  (_metrics$httpRequestD2 = metrics.httpRequestDurationPerPercentileInSeconds) === null || _metrics$httpRequestD2 === void 0 ? void 0 : _metrics$httpRequestD2.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
314
280
  httpRequestDurationPerPercentileInSecondsMetricType.observe(labels, durationS);
315
281
  });
316
282
  }
317
-
318
283
  if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
319
284
  var _metrics$httpRequests;
320
-
321
285
  (_metrics$httpRequests = metrics.httpRequestsTotal) === null || _metrics$httpRequests === void 0 ? void 0 : _metrics$httpRequests.forEach(httpRequestsTotalMetricType => {
322
286
  httpRequestsTotalMetricType.inc(labels);
323
287
  });
324
288
  }
325
-
326
289
  if (recordingOptions.requestContentLength) {
327
290
  var _metrics$httpRequestC;
328
-
329
291
  (_metrics$httpRequestC = metrics.httpRequestContentLengthInBytes) === null || _metrics$httpRequestC === void 0 ? void 0 : _metrics$httpRequestC.forEach(httpRequestContentLengthInBytesMetricType => {
330
- httpRequestContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
292
+ httpRequestContentLengthInBytesMetricType.observe(labels,
293
+ // @ts-expect-error
331
294
  recordingOptions.requestContentLength);
332
295
  });
333
296
  }
334
-
335
297
  if (recordingOptions.responseContentLength) {
336
298
  var _metrics$httpResponse;
337
-
338
299
  (_metrics$httpResponse = metrics.httpResponseContentLengthInBytes) === null || _metrics$httpResponse === void 0 ? void 0 : _metrics$httpResponse.forEach(httpResponseContentLengthInBytesMetricType => {
339
- httpResponseContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
300
+ httpResponseContentLengthInBytesMetricType.observe(labels,
301
+ // @ts-expect-error
340
302
  recordingOptions.responseContentLength);
341
303
  });
342
304
  }
343
305
  };
344
306
  };
345
-
346
307
  createRequestRecorder.defaultOptions = defaultOptions$1;
347
308
 
348
309
  const gc = requireOptional__default["default"]('@sematext/gc-stats');
@@ -362,7 +323,6 @@ const createGcObserver = once__default["default"]((metrics, options) => () => {
362
323
  if (typeof gc !== 'function') {
363
324
  return;
364
325
  }
365
-
366
326
  if (options.disableGcMetrics) return;
367
327
  gc().on('stats', stats => {
368
328
  const gcType = gcTypes[stats.gctype];
@@ -372,7 +332,6 @@ const createGcObserver = once__default["default"]((metrics, options) => () => {
372
332
  metrics.durationOfGc.forEach(durationOfGcMetricType => {
373
333
  durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
374
334
  });
375
-
376
335
  if (stats.diff.usedHeapSize < 0) {
377
336
  metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
378
337
  reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
@@ -385,7 +344,6 @@ createGcObserver.defaultOptions = defaultOptions;
385
344
  const normalizeStatusCode = statusCode => statusCode;
386
345
 
387
346
  const urlValueParser = new UrlValueParser__default["default"]();
388
-
389
347
  const normalizePath = path => urlValueParser.replacePathValues(url__default["default"].parse(path).pathname, '#val');
390
348
 
391
349
  const normalizeMethod = method => method.toLowerCase();
@@ -400,11 +358,9 @@ class Timing {
400
358
  static NS_PER_SEC = BigInt(1e9);
401
359
  #startTime;
402
360
  #endTime;
403
-
404
361
  constructor() {
405
362
  this.reset();
406
363
  }
407
-
408
364
  value() {
409
365
  const startTime = this.#startTime;
410
366
  const endTime = this.#endTime;
@@ -415,25 +371,20 @@ class Timing {
415
371
  seconds: Number(endTime - startTime) / Number(Timing.NS_PER_SEC)
416
372
  };
417
373
  }
418
-
419
374
  reset() {
420
375
  this.#startTime = process.hrtime.bigint();
421
376
  this.#endTime = undefined;
422
377
  return this;
423
378
  }
424
-
425
379
  end() {
426
380
  this.#endTime = process.hrtime.bigint();
427
381
  return this;
428
382
  }
429
-
430
383
  }
431
-
432
384
  const timing = {
433
385
  start() {
434
386
  return new Timing();
435
387
  }
436
-
437
388
  };
438
389
 
439
390
  exports.Prometheus = Prometheus__namespace;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promster/metrics",
3
- "version": "9.1.4",
3
+ "version": "9.1.6",
4
4
  "description": "Metrics utilities used by all other server integrations",
5
5
  "main": "dist/promster-metrics.cjs.js",
6
6
  "typings": "dist/promster-metrics.cjs.d.ts",
@@ -40,18 +40,18 @@
40
40
  "lodash.once": "4.1.1",
41
41
  "merge-options": "3.0.4",
42
42
  "optional": "0.1.4",
43
- "ts-essentials": "9.1.2",
44
- "tslib": "2.3.1",
45
- "url-value-parser": "2.1.0"
43
+ "ts-essentials": "9.3.0",
44
+ "tslib": "2.4.1",
45
+ "url-value-parser": "2.2.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@promster/types": "^3.2.3",
49
- "@types/lodash": "4.14.178",
50
- "prom-client": "14.0.1",
51
- "typescript": "4.5.5"
48
+ "@promster/types": "^3.2.5",
49
+ "@types/lodash": "4.14.191",
50
+ "prom-client": "14.1.0",
51
+ "typescript": "4.9.4"
52
52
  },
53
53
  "optionalDependencies": {
54
- "@sematext/gc-stats": "1.5.7"
54
+ "@sematext/gc-stats": "1.5.8"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "prom-client": "13.x.x || 14.x"