@promster/metrics 13.0.0 → 14.0.0

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,7 +1,9 @@
1
+ import type { PrometheusContentType } from 'prom-client';
1
2
  import * as Prometheus from 'prom-client';
2
3
  declare const defaultRegister: Prometheus.Registry<"text/plain; version=0.0.4; charset=utf-8">;
3
- interface TClientOptions extends Prometheus.DefaultMetricsCollectorConfiguration {
4
+ interface TClientOptions extends Prometheus.DefaultMetricsCollectorConfiguration<PrometheusContentType> {
4
5
  detectKubernetes?: boolean;
6
+ prefix?: string;
5
7
  }
6
8
  declare const configure: (options: TClientOptions) => void;
7
9
  export { Prometheus, defaultRegister, configure };
@@ -7,9 +7,6 @@ declare const createGcMetrics: {
7
7
  metricPrefix: string;
8
8
  metricNames: {
9
9
  up: string[];
10
- countOfGcs: string[];
11
- durationOfGc: string[];
12
- reclaimedInGc: string[];
13
10
  };
14
11
  };
15
12
  };
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var once = require('lodash.once');
6
6
  var Prometheus = require('prom-client');
7
7
  var merge = require('merge-options');
8
- var requireOptional = require('optional');
8
+ var gcStats = require('prometheus-gc-stats');
9
9
  var url = require('url');
10
10
  var UrlValueParser = require('url-value-parser');
11
11
  var types = require('@promster/types');
@@ -33,7 +33,7 @@ function _interopNamespace(e) {
33
33
  var once__default = /*#__PURE__*/_interopDefault(once);
34
34
  var Prometheus__namespace = /*#__PURE__*/_interopNamespace(Prometheus);
35
35
  var merge__default = /*#__PURE__*/_interopDefault(merge);
36
- var requireOptional__default = /*#__PURE__*/_interopDefault(requireOptional);
36
+ var gcStats__default = /*#__PURE__*/_interopDefault(gcStats);
37
37
  var url__default = /*#__PURE__*/_interopDefault(url);
38
38
  var UrlValueParser__default = /*#__PURE__*/_interopDefault(UrlValueParser);
39
39
 
@@ -58,7 +58,7 @@ const configure = once__default["default"](options => {
58
58
  const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.99];
59
59
  const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
60
60
  const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
61
- const defaultLabels$2 = ['path', 'status_code', 'method'];
61
+ const defaultLabels$1 = ['path', 'status_code', 'method'];
62
62
  const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
63
63
  const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
64
64
  const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.includes('httpRequestsHistogram');
@@ -91,7 +91,7 @@ const getMetrics$2 = options => ({
91
91
  return new Prometheus__namespace.Histogram({
92
92
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
93
93
  help: 'The HTTP request content length in bytes.',
94
- labelNames: defaultLabels$2.concat(options.labels).sort(),
94
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
95
95
  buckets: ((_options$metricBucket = options.metricBuckets) === null || _options$metricBucket === void 0 ? void 0 : _options$metricBucket.httpRequestContentLengthInBytes) || defaultHttpContentLengthInBytes
96
96
  });
97
97
  }) : undefined,
@@ -100,7 +100,7 @@ const getMetrics$2 = options => ({
100
100
  return new Prometheus__namespace.Histogram({
101
101
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
102
102
  help: 'The HTTP response content length in bytes.',
103
- labelNames: defaultLabels$2.concat(options.labels).sort(),
103
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
104
104
  buckets: ((_options$metricBucket2 = options.metricBuckets) === null || _options$metricBucket2 === void 0 ? void 0 : _options$metricBucket2.httpResponseContentLengthInBytes) || defaultHttpContentLengthInBytes
105
105
  });
106
106
  }) : undefined
@@ -111,7 +111,7 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
111
111
  return new Prometheus__namespace.Summary({
112
112
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
113
113
  help: 'The HTTP request latencies in seconds.',
114
- labelNames: defaultLabels$2.concat(options.labels).sort(),
114
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
115
115
  percentiles: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.httpRequestDurationPerPercentileInSeconds) || defaultHttpRequestDurationPercentileInSeconds
116
116
  });
117
117
  }) : undefined,
@@ -120,7 +120,7 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
120
120
  return new Prometheus__namespace.Histogram({
121
121
  name: `${options.metricPrefix}${nameOfHttpRequestDurationInSecondsMetric}`,
122
122
  help: 'The HTTP request latencies in seconds.',
123
- labelNames: defaultLabels$2.concat(options.labels).sort(),
123
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
124
124
  buckets: ((_options$metricBucket3 = options.metricBuckets) === null || _options$metricBucket3 === void 0 ? void 0 : _options$metricBucket3.httpRequestDurationInSeconds) || defaultHttpRequestDurationInSeconds
125
125
  });
126
126
  }) : undefined
@@ -129,7 +129,7 @@ const getHttpRequestCounterMetric = options => ({
129
129
  httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray$2(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
130
130
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
131
131
  help: 'The total HTTP requests.',
132
- labelNames: defaultLabels$2.concat(options.labels).sort()
132
+ labelNames: defaultLabels$1.concat(options.labels).sort()
133
133
  })) : undefined
134
134
  });
135
135
  const createHttpMetrics = options => {
@@ -145,7 +145,7 @@ const createHttpMetrics = options => {
145
145
  createHttpMetrics.defaultOptions = defaultOptions$4;
146
146
 
147
147
  const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
148
- const defaultLabels$1 = ['operation_name'];
148
+ const defaultLabels = ['operation_name'];
149
149
  const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
150
150
  const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
151
151
  const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
@@ -178,7 +178,7 @@ const getMetrics$1 = options => ({
178
178
  return new Prometheus__namespace.Histogram({
179
179
  name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
180
180
  help: 'The GraphQL request parse time in seconds.',
181
- labelNames: defaultLabels$1.concat(options.labels).sort(),
181
+ labelNames: defaultLabels.concat(options.labels).sort(),
182
182
  buckets: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.graphQlParseDuration) || defaultGraphQlPercentiles
183
183
  });
184
184
  }) : undefined,
@@ -187,7 +187,7 @@ const getMetrics$1 = options => ({
187
187
  return new Prometheus__namespace.Histogram({
188
188
  name: `${options.metricPrefix}${nameOfGraphQlValidationDuration}`,
189
189
  help: 'The GraphQL request validation time in seconds.',
190
- labelNames: defaultLabels$1.concat(options.labels).sort(),
190
+ labelNames: defaultLabels.concat(options.labels).sort(),
191
191
  buckets: ((_options$metricPercen2 = options.metricPercentiles) === null || _options$metricPercen2 === void 0 ? void 0 : _options$metricPercen2.graphQlValidationDuration) || defaultGraphQlPercentiles
192
192
  });
193
193
  }) : undefined,
@@ -196,7 +196,7 @@ const getMetrics$1 = options => ({
196
196
  return new Prometheus__namespace.Histogram({
197
197
  name: `${options.metricPrefix}${nameOfGraphQlResolveFieldDuration}`,
198
198
  help: 'The GraphQL field resolving time in seconds.',
199
- labelNames: defaultLabels$1.concat(['field_name']).concat(options.labels).sort(),
199
+ labelNames: defaultLabels.concat(['field_name']).concat(options.labels).sort(),
200
200
  buckets: ((_options$metricPercen3 = options.metricPercentiles) === null || _options$metricPercen3 === void 0 ? void 0 : _options$metricPercen3.graphQlResolveFieldDuration) || defaultGraphQlPercentiles
201
201
  });
202
202
  }) : undefined,
@@ -205,14 +205,14 @@ const getMetrics$1 = options => ({
205
205
  return new Prometheus__namespace.Histogram({
206
206
  name: `${options.metricPrefix}${nameOfGraphQlRequestDuration}`,
207
207
  help: 'The GraphQL request duration time in seconds.',
208
- labelNames: defaultLabels$1.concat(options.labels).sort(),
208
+ labelNames: defaultLabels.concat(options.labels).sort(),
209
209
  buckets: ((_options$metricPercen4 = options.metricPercentiles) === null || _options$metricPercen4 === void 0 ? void 0 : _options$metricPercen4.graphQlRequestDuration) || defaultGraphQlPercentiles
210
210
  });
211
211
  }) : undefined,
212
212
  graphQlErrorsTotal: shouldObserveGraphQlErrorsTotalAsCounter(options) ? asArray$1(options.metricNames.graphQlErrorsTotal).map(nameOfGraphQlErrorsCount => new Prometheus__namespace.Counter({
213
213
  name: `${options.metricPrefix}${nameOfGraphQlErrorsCount}`,
214
214
  help: 'Count of errors while parsing, validating, or executing a GraphQL operation.',
215
- labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).sort()
215
+ labelNames: defaultLabels.concat(['phase']).concat(options.labels).sort()
216
216
  })) : undefined
217
217
  });
218
218
  const createGraphQlMetrics = options => {
@@ -225,38 +225,19 @@ const createGraphQlMetrics = options => {
225
225
  };
226
226
  createGraphQlMetrics.defaultOptions = defaultOptions$3;
227
227
 
228
- const defaultLabels = ['gc_type'];
229
228
  const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
230
229
  const defaultOptions$2 = {
231
230
  getLabelValues: () => ({}),
232
231
  labels: [],
233
232
  metricPrefix: '',
234
233
  metricNames: {
235
- up: ['nodejs_up'],
236
- countOfGcs: ['nodejs_gc_runs_total'],
237
- durationOfGc: ['nodejs_gc_pause_seconds_total'],
238
- reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total']
234
+ up: ['nodejs_up']
239
235
  }
240
236
  };
241
237
  const getMetrics = options => ({
242
238
  up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
243
239
  name: `${options.metricPrefix}${nameOfUpMetric}`,
244
240
  help: '1 = nodejs server is up, 0 = nodejs server is not up'
245
- })),
246
- countOfGcs: asArray(options.metricNames.countOfGcs).map(nameOfCountOfGcsMetric => new Prometheus__namespace.Counter({
247
- name: `${options.metricPrefix}${nameOfCountOfGcsMetric}`,
248
- help: 'Count of total garbage collections.',
249
- labelNames: defaultLabels
250
- })),
251
- durationOfGc: asArray(options.metricNames.durationOfGc).map(nameOfDurationOfGcMetric => new Prometheus__namespace.Counter({
252
- name: `${options.metricPrefix}${nameOfDurationOfGcMetric}`,
253
- help: 'Time spent in GC Pause in seconds.',
254
- labelNames: defaultLabels
255
- })),
256
- reclaimedInGc: asArray(options.metricNames.reclaimedInGc).map(nameOfReclaimedInGcMetric => new Prometheus__namespace.Counter({
257
- name: `${options.metricPrefix}${nameOfReclaimedInGcMetric}`,
258
- help: 'Total number of bytes reclaimed by GC.',
259
- labelNames: defaultLabels
260
241
  }))
261
242
  });
262
243
  const createGcMetrics = options => {
@@ -345,39 +326,14 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
345
326
  };
346
327
  createRequestRecorder.defaultOptions = defaultOptions$1;
347
328
 
348
- const gc = requireOptional__default["default"]('@sematext/gc-stats');
349
- const gcTypes = {
350
- 0: 'unknown',
351
- 1: 'scavenge',
352
- 2: 'mark_sweep_compact',
353
- 3: 'scavenge_and_mark_sweep_compact',
354
- 4: 'incremental_marking',
355
- 8: 'weak_phantom',
356
- 15: 'all'
357
- };
358
329
  const defaultOptions = {
359
330
  disableGcMetrics: false
360
331
  };
361
332
  const createGcObserver = once__default["default"]((metrics, options) => () => {
362
- if (typeof gc !== 'function') {
363
- return;
364
- }
365
- if (options.disableGcMetrics) return;
366
- gc().on('stats', stats => {
367
- // @ts-expect-error
368
- const gcType = gcTypes[stats.gctype];
369
- metrics.countOfGcs.forEach(countOfGcMetricType => {
370
- countOfGcMetricType.labels(gcType).inc();
371
- });
372
- metrics.durationOfGc.forEach(durationOfGcMetricType => {
373
- durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
374
- });
375
- if (stats.diff.usedHeapSize < 0) {
376
- metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
377
- reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
378
- });
379
- }
333
+ const startGcStats = gcStats__default["default"](defaultRegister, {
334
+ prefix: options.metricPrefix
380
335
  });
336
+ startGcStats();
381
337
  });
382
338
 
383
339
  // @ts-expect-error
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var once = require('lodash.once');
6
6
  var Prometheus = require('prom-client');
7
7
  var merge = require('merge-options');
8
- var requireOptional = require('optional');
8
+ var gcStats = require('prometheus-gc-stats');
9
9
  var url = require('url');
10
10
  var UrlValueParser = require('url-value-parser');
11
11
  var types = require('@promster/types');
@@ -33,7 +33,7 @@ function _interopNamespace(e) {
33
33
  var once__default = /*#__PURE__*/_interopDefault(once);
34
34
  var Prometheus__namespace = /*#__PURE__*/_interopNamespace(Prometheus);
35
35
  var merge__default = /*#__PURE__*/_interopDefault(merge);
36
- var requireOptional__default = /*#__PURE__*/_interopDefault(requireOptional);
36
+ var gcStats__default = /*#__PURE__*/_interopDefault(gcStats);
37
37
  var url__default = /*#__PURE__*/_interopDefault(url);
38
38
  var UrlValueParser__default = /*#__PURE__*/_interopDefault(UrlValueParser);
39
39
 
@@ -58,7 +58,7 @@ const configure = once__default["default"](options => {
58
58
  const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.99];
59
59
  const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
60
60
  const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
61
- const defaultLabels$2 = ['path', 'status_code', 'method'];
61
+ const defaultLabels$1 = ['path', 'status_code', 'method'];
62
62
  const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
63
63
  const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
64
64
  const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.includes('httpRequestsHistogram');
@@ -91,7 +91,7 @@ const getMetrics$2 = options => ({
91
91
  return new Prometheus__namespace.Histogram({
92
92
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
93
93
  help: 'The HTTP request content length in bytes.',
94
- labelNames: defaultLabels$2.concat(options.labels).sort(),
94
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
95
95
  buckets: ((_options$metricBucket = options.metricBuckets) === null || _options$metricBucket === void 0 ? void 0 : _options$metricBucket.httpRequestContentLengthInBytes) || defaultHttpContentLengthInBytes
96
96
  });
97
97
  }) : undefined,
@@ -100,7 +100,7 @@ const getMetrics$2 = options => ({
100
100
  return new Prometheus__namespace.Histogram({
101
101
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
102
102
  help: 'The HTTP response content length in bytes.',
103
- labelNames: defaultLabels$2.concat(options.labels).sort(),
103
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
104
104
  buckets: ((_options$metricBucket2 = options.metricBuckets) === null || _options$metricBucket2 === void 0 ? void 0 : _options$metricBucket2.httpResponseContentLengthInBytes) || defaultHttpContentLengthInBytes
105
105
  });
106
106
  }) : undefined
@@ -111,7 +111,7 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
111
111
  return new Prometheus__namespace.Summary({
112
112
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
113
113
  help: 'The HTTP request latencies in seconds.',
114
- labelNames: defaultLabels$2.concat(options.labels).sort(),
114
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
115
115
  percentiles: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.httpRequestDurationPerPercentileInSeconds) || defaultHttpRequestDurationPercentileInSeconds
116
116
  });
117
117
  }) : undefined,
@@ -120,7 +120,7 @@ const getHttpRequestLatencyMetricsInSeconds = options => ({
120
120
  return new Prometheus__namespace.Histogram({
121
121
  name: `${options.metricPrefix}${nameOfHttpRequestDurationInSecondsMetric}`,
122
122
  help: 'The HTTP request latencies in seconds.',
123
- labelNames: defaultLabels$2.concat(options.labels).sort(),
123
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
124
124
  buckets: ((_options$metricBucket3 = options.metricBuckets) === null || _options$metricBucket3 === void 0 ? void 0 : _options$metricBucket3.httpRequestDurationInSeconds) || defaultHttpRequestDurationInSeconds
125
125
  });
126
126
  }) : undefined
@@ -129,7 +129,7 @@ const getHttpRequestCounterMetric = options => ({
129
129
  httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray$2(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
130
130
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
131
131
  help: 'The total HTTP requests.',
132
- labelNames: defaultLabels$2.concat(options.labels).sort()
132
+ labelNames: defaultLabels$1.concat(options.labels).sort()
133
133
  })) : undefined
134
134
  });
135
135
  const createHttpMetrics = options => {
@@ -145,7 +145,7 @@ const createHttpMetrics = options => {
145
145
  createHttpMetrics.defaultOptions = defaultOptions$4;
146
146
 
147
147
  const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
148
- const defaultLabels$1 = ['operation_name'];
148
+ const defaultLabels = ['operation_name'];
149
149
  const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
150
150
  const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
151
151
  const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
@@ -178,7 +178,7 @@ const getMetrics$1 = options => ({
178
178
  return new Prometheus__namespace.Histogram({
179
179
  name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
180
180
  help: 'The GraphQL request parse time in seconds.',
181
- labelNames: defaultLabels$1.concat(options.labels).sort(),
181
+ labelNames: defaultLabels.concat(options.labels).sort(),
182
182
  buckets: ((_options$metricPercen = options.metricPercentiles) === null || _options$metricPercen === void 0 ? void 0 : _options$metricPercen.graphQlParseDuration) || defaultGraphQlPercentiles
183
183
  });
184
184
  }) : undefined,
@@ -187,7 +187,7 @@ const getMetrics$1 = options => ({
187
187
  return new Prometheus__namespace.Histogram({
188
188
  name: `${options.metricPrefix}${nameOfGraphQlValidationDuration}`,
189
189
  help: 'The GraphQL request validation time in seconds.',
190
- labelNames: defaultLabels$1.concat(options.labels).sort(),
190
+ labelNames: defaultLabels.concat(options.labels).sort(),
191
191
  buckets: ((_options$metricPercen2 = options.metricPercentiles) === null || _options$metricPercen2 === void 0 ? void 0 : _options$metricPercen2.graphQlValidationDuration) || defaultGraphQlPercentiles
192
192
  });
193
193
  }) : undefined,
@@ -196,7 +196,7 @@ const getMetrics$1 = options => ({
196
196
  return new Prometheus__namespace.Histogram({
197
197
  name: `${options.metricPrefix}${nameOfGraphQlResolveFieldDuration}`,
198
198
  help: 'The GraphQL field resolving time in seconds.',
199
- labelNames: defaultLabels$1.concat(['field_name']).concat(options.labels).sort(),
199
+ labelNames: defaultLabels.concat(['field_name']).concat(options.labels).sort(),
200
200
  buckets: ((_options$metricPercen3 = options.metricPercentiles) === null || _options$metricPercen3 === void 0 ? void 0 : _options$metricPercen3.graphQlResolveFieldDuration) || defaultGraphQlPercentiles
201
201
  });
202
202
  }) : undefined,
@@ -205,14 +205,14 @@ const getMetrics$1 = options => ({
205
205
  return new Prometheus__namespace.Histogram({
206
206
  name: `${options.metricPrefix}${nameOfGraphQlRequestDuration}`,
207
207
  help: 'The GraphQL request duration time in seconds.',
208
- labelNames: defaultLabels$1.concat(options.labels).sort(),
208
+ labelNames: defaultLabels.concat(options.labels).sort(),
209
209
  buckets: ((_options$metricPercen4 = options.metricPercentiles) === null || _options$metricPercen4 === void 0 ? void 0 : _options$metricPercen4.graphQlRequestDuration) || defaultGraphQlPercentiles
210
210
  });
211
211
  }) : undefined,
212
212
  graphQlErrorsTotal: shouldObserveGraphQlErrorsTotalAsCounter(options) ? asArray$1(options.metricNames.graphQlErrorsTotal).map(nameOfGraphQlErrorsCount => new Prometheus__namespace.Counter({
213
213
  name: `${options.metricPrefix}${nameOfGraphQlErrorsCount}`,
214
214
  help: 'Count of errors while parsing, validating, or executing a GraphQL operation.',
215
- labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).sort()
215
+ labelNames: defaultLabels.concat(['phase']).concat(options.labels).sort()
216
216
  })) : undefined
217
217
  });
218
218
  const createGraphQlMetrics = options => {
@@ -225,38 +225,19 @@ const createGraphQlMetrics = options => {
225
225
  };
226
226
  createGraphQlMetrics.defaultOptions = defaultOptions$3;
227
227
 
228
- const defaultLabels = ['gc_type'];
229
228
  const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
230
229
  const defaultOptions$2 = {
231
230
  getLabelValues: () => ({}),
232
231
  labels: [],
233
232
  metricPrefix: '',
234
233
  metricNames: {
235
- up: ['nodejs_up'],
236
- countOfGcs: ['nodejs_gc_runs_total'],
237
- durationOfGc: ['nodejs_gc_pause_seconds_total'],
238
- reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total']
234
+ up: ['nodejs_up']
239
235
  }
240
236
  };
241
237
  const getMetrics = options => ({
242
238
  up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
243
239
  name: `${options.metricPrefix}${nameOfUpMetric}`,
244
240
  help: '1 = nodejs server is up, 0 = nodejs server is not up'
245
- })),
246
- countOfGcs: asArray(options.metricNames.countOfGcs).map(nameOfCountOfGcsMetric => new Prometheus__namespace.Counter({
247
- name: `${options.metricPrefix}${nameOfCountOfGcsMetric}`,
248
- help: 'Count of total garbage collections.',
249
- labelNames: defaultLabels
250
- })),
251
- durationOfGc: asArray(options.metricNames.durationOfGc).map(nameOfDurationOfGcMetric => new Prometheus__namespace.Counter({
252
- name: `${options.metricPrefix}${nameOfDurationOfGcMetric}`,
253
- help: 'Time spent in GC Pause in seconds.',
254
- labelNames: defaultLabels
255
- })),
256
- reclaimedInGc: asArray(options.metricNames.reclaimedInGc).map(nameOfReclaimedInGcMetric => new Prometheus__namespace.Counter({
257
- name: `${options.metricPrefix}${nameOfReclaimedInGcMetric}`,
258
- help: 'Total number of bytes reclaimed by GC.',
259
- labelNames: defaultLabels
260
241
  }))
261
242
  });
262
243
  const createGcMetrics = options => {
@@ -345,39 +326,14 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
345
326
  };
346
327
  createRequestRecorder.defaultOptions = defaultOptions$1;
347
328
 
348
- const gc = requireOptional__default["default"]('@sematext/gc-stats');
349
- const gcTypes = {
350
- 0: 'unknown',
351
- 1: 'scavenge',
352
- 2: 'mark_sweep_compact',
353
- 3: 'scavenge_and_mark_sweep_compact',
354
- 4: 'incremental_marking',
355
- 8: 'weak_phantom',
356
- 15: 'all'
357
- };
358
329
  const defaultOptions = {
359
330
  disableGcMetrics: false
360
331
  };
361
332
  const createGcObserver = once__default["default"]((metrics, options) => () => {
362
- if (typeof gc !== 'function') {
363
- return;
364
- }
365
- if (options.disableGcMetrics) return;
366
- gc().on('stats', stats => {
367
- // @ts-expect-error
368
- const gcType = gcTypes[stats.gctype];
369
- metrics.countOfGcs.forEach(countOfGcMetricType => {
370
- countOfGcMetricType.labels(gcType).inc();
371
- });
372
- metrics.durationOfGc.forEach(durationOfGcMetricType => {
373
- durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
374
- });
375
- if (stats.diff.usedHeapSize < 0) {
376
- metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
377
- reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
378
- });
379
- }
333
+ const startGcStats = gcStats__default["default"](defaultRegister, {
334
+ prefix: options.metricPrefix
380
335
  });
336
+ startGcStats();
381
337
  });
382
338
 
383
339
  // @ts-expect-error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promster/metrics",
3
- "version": "13.0.0",
3
+ "version": "14.0.0",
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",
@@ -36,23 +36,20 @@
36
36
  "prometheus"
37
37
  ],
38
38
  "dependencies": {
39
- "@promster/types": "^13.0.0",
39
+ "@promster/types": "^14.0.0",
40
40
  "lodash.memoize": "4.1.2",
41
+ "prometheus-gc-stats": "1.1.0",
41
42
  "lodash.once": "4.1.1",
42
43
  "merge-options": "3.0.4",
43
- "optional": "0.1.4",
44
44
  "tslib": "2.6.2",
45
45
  "url-value-parser": "2.2.0"
46
46
  },
47
47
  "devDependencies": {
48
- "prom-client": "15.1.0",
48
+ "prom-client": "15.1.1",
49
49
  "typescript": "5.3.3",
50
- "@types/node": "20.11.16",
50
+ "@types/node": "20.11.30",
51
51
  "@types/lodash.once": "4.1.9"
52
52
  },
53
- "optionalDependencies": {
54
- "@sematext/gc-stats": "1.5.9"
55
- },
56
53
  "peerDependencies": {
57
54
  "prom-client": "13.x.x || 14.x || 15.x"
58
55
  }