@promster/metrics 7.0.4 → 9.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/dist/declarations/src/create-gc-metrics/create-gc-metrics.d.ts +16 -0
  2. package/dist/declarations/src/create-gc-metrics/index.d.ts +2 -0
  3. package/dist/declarations/src/create-graphql-metrics/create-graphql-metrics.d.ts +18 -0
  4. package/dist/declarations/src/create-graphql-metrics/index.d.ts +2 -0
  5. package/dist/declarations/src/create-http-metrics/create-http-metrics.d.ts +18 -0
  6. package/dist/declarations/src/create-http-metrics/index.d.ts +2 -0
  7. package/dist/declarations/src/create-request-recorder/create-request-recorder.d.ts +7 -9
  8. package/dist/declarations/src/end-measurement-from/end-measurement-from.d.ts +4 -0
  9. package/dist/declarations/src/end-measurement-from/index.d.ts +2 -0
  10. package/dist/declarations/src/environment/index.d.ts +3 -0
  11. package/dist/declarations/src/{kubernetes → environment}/kubernetes.d.ts +0 -0
  12. package/dist/declarations/src/environment/skip-metrics-in-environment.d.ts +6 -0
  13. package/dist/declarations/src/index.d.ts +9 -3
  14. package/dist/declarations/src/sort-labels/index.d.ts +2 -0
  15. package/dist/declarations/src/sort-labels/sort-labels.d.ts +3 -0
  16. package/dist/declarations/src/timing/index.d.ts +2 -0
  17. package/dist/declarations/src/timing/timing.d.ts +15 -0
  18. package/dist/promster-metrics.cjs.dev.js +332 -146
  19. package/dist/promster-metrics.cjs.prod.js +332 -146
  20. package/package.json +9 -8
  21. package/readme.md +1 -2
  22. package/dist/declarations/src/create-metric-types/create-metric-types.d.ts +0 -25
  23. package/dist/declarations/src/create-metric-types/index.d.ts +0 -2
  24. package/dist/declarations/src/kubernetes/index.d.ts +0 -2
@@ -38,31 +38,26 @@ var UrlValueParser__default = /*#__PURE__*/_interopDefault(UrlValueParser);
38
38
 
39
39
  const isRunningInKubernetes = () => Boolean(process.env.KUBERNETES_SERVICE_HOST);
40
40
 
41
+ const skipMetricsInEnvironment = options => options.detectKubernetes === true && !isRunningInKubernetes();
42
+
41
43
  // This is the `globalRegistry` provided by the `prom-client`
42
44
  // We could create multiple registries with `new Prometheus.registry()`.
43
45
 
44
46
  const defaultRegister = Prometheus__namespace.register;
45
47
  const configure = once__default["default"](options => {
46
- const shouldSkipMetricsByEnvironment = options.detectKubernetes === true && !isRunningInKubernetes();
48
+ const shouldSkipMetricsInEnvironment = skipMetricsInEnvironment(options);
47
49
 
48
- if (!shouldSkipMetricsByEnvironment) {
50
+ if (!shouldSkipMetricsInEnvironment) {
49
51
  Prometheus__namespace.collectDefaultMetrics(options);
50
52
  }
51
53
  });
52
54
 
53
- const defaultHttpRequestDurationPercentilesInMillieconds = [0.5, 0.9, 0.95, 0.98, 0.99];
54
- const defaultHttpRequestDurationInMilliseconds = [50, 100, 300, 500, 800, 1000, 1500, 2000, 3000, 5000, 10000];
55
55
  const defaultHttpRequestDurationPercentileInSeconds = [0.5, 0.9, 0.95, 0.98, 0.99];
56
56
  const defaultHttpRequestDurationInSeconds = [0.05, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 10];
57
57
  const defaultHttpContentLengthInBytes = [100000, 200000, 500000, 1000000, 1500000, 2000000, 3000000, 5000000, 10000000];
58
- const defaultRequestLabels = ['path', 'status_code', 'method'];
59
- const defaultGcLabels = ['gc_type'];
60
-
61
- const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
58
+ const defaultLabels$2 = ['path', 'status_code', 'method'];
62
59
 
63
- const shouldObserveMetricsInSeconds = options => options.accuracies.includes('s');
64
-
65
- const shouldObserveMetricsInMilliseconds = options => options.accuracies.includes('ms');
60
+ const asArray$2 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
66
61
 
67
62
  const shouldObserveHttpRequestsAsSummary = options => options.metricTypes.includes('httpRequestsSummary');
68
63
 
@@ -70,216 +65,277 @@ const shouldObserveHttpRequestsAsHistogram = options => options.metricTypes.incl
70
65
 
71
66
  const shouldObserveHttpRequestsAsCounter = options => options.metricTypes.includes('httpRequestsTotal');
72
67
 
73
- const defaultOptions$1 = {
68
+ const shouldObserveHttpContentLengthAsHistogram = options => options.metricTypes.includes('httpContentLengthHistogram');
69
+
70
+ const defaultOptions$4 = {
74
71
  getLabelValues: () => ({}),
75
72
  labels: [],
76
- accuracies: ['s'],
77
73
  metricPrefix: '',
78
74
  metricTypes: ['httpRequestsTotal', 'httpRequestsHistogram'],
79
75
  metricNames: {
80
- up: ['up'],
81
- countOfGcs: ['nodejs_gc_runs_total'],
82
- durationOfGc: ['nodejs_gc_pause_seconds_total'],
83
- reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total'],
84
76
  httpRequestsTotal: ['http_requests_total'],
85
- httpRequestDurationPerPercentileInMilliseconds: ['http_request_duration_per_percentile_milliseconds'],
86
77
  httpRequestDurationPerPercentileInSeconds: ['http_request_duration_per_percentile_seconds'],
87
78
  httpRequestDurationInSeconds: ['http_request_duration_seconds'],
88
- httpRequestDurationInMilliseconds: ['http_request_duration_milliseconds'],
89
79
  httpRequestContentLengthInBytes: ['http_request_content_length_bytes'],
90
80
  httpResponseContentLengthInBytes: ['http_response_content_length_bytes']
91
81
  }
92
82
  };
93
83
 
94
- const getDefaultMetrics = options => ({
95
- up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
96
- name: `${options.metricPrefix}${nameOfUpMetric}`,
97
- help: '1 = up, 0 = not up'
98
- })),
99
- countOfGcs: asArray(options.metricNames.countOfGcs).map(nameOfCountOfGcsMetric => new Prometheus__namespace.Counter({
100
- name: `${options.metricPrefix}${nameOfCountOfGcsMetric}`,
101
- help: 'Count of total garbage collections.',
102
- labelNames: defaultGcLabels
103
- })),
104
- durationOfGc: asArray(options.metricNames.durationOfGc).map(nameOfDurationOfGcMetric => new Prometheus__namespace.Counter({
105
- name: `${options.metricPrefix}${nameOfDurationOfGcMetric}`,
106
- help: 'Time spent in GC Pause in seconds.',
107
- labelNames: defaultGcLabels
108
- })),
109
- reclaimedInGc: asArray(options.metricNames.reclaimedInGc).map(nameOfReclaimedInGcMetric => new Prometheus__namespace.Counter({
110
- name: `${options.metricPrefix}${nameOfReclaimedInGcMetric}`,
111
- help: 'Total number of bytes reclaimed by GC.',
112
- labelNames: defaultGcLabels
113
- })),
114
- httpRequestContentLengthInBytes: asArray(options.metricNames.httpRequestContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
84
+ const getMetrics$2 = options => ({
85
+ httpRequestContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray$2(options.metricNames.httpRequestContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
115
86
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
116
87
  help: 'The HTTP request content length in bytes.',
117
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
88
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
118
89
  buckets: options.buckets || defaultHttpContentLengthInBytes
119
- })),
120
- httpResponseContentLengthInBytes: asArray(options.metricNames.httpResponseContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
90
+ })) : undefined,
91
+ httpResponseContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) ? asArray$2(options.metricNames.httpResponseContentLengthInBytes).map(nameOfHttpContentLengthMetric => new Prometheus__namespace.Histogram({
121
92
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
122
93
  help: 'The HTTP response content length in bytes.',
123
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
94
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
124
95
  buckets: options.buckets || defaultHttpContentLengthInBytes
125
- }))
126
- });
127
-
128
- const getHttpRequestLatencyMetricsInMilliseconds = options => ({
129
- httpRequestDurationPerPercentileInMilliseconds: shouldObserveHttpRequestsAsSummary(options) && asArray(options.metricNames.httpRequestDurationPerPercentileInMilliseconds).map(nameOfHttpRequestDurationPerPercentileInMillisecondsMetric => new Prometheus__namespace.Summary({
130
- name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInMillisecondsMetric}`,
131
- help: 'The HTTP request latencies in milliseconds.',
132
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
133
- percentiles: options.percentiles || defaultHttpRequestDurationPercentilesInMillieconds
134
- })),
135
- httpRequestDurationInMilliseconds: shouldObserveHttpRequestsAsHistogram(options) && asArray(options.metricNames.httpRequestDurationInMilliseconds).map(nameOfHttpRequestDurationInMillisecondsMetric => new Prometheus__namespace.Histogram({
136
- name: `${options.metricPrefix}${nameOfHttpRequestDurationInMillisecondsMetric}`,
137
- help: 'The HTTP request latencies in milliseconds.',
138
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
139
- buckets: options.buckets || defaultHttpRequestDurationInMilliseconds
140
- }))
96
+ })) : undefined
141
97
  });
142
98
 
143
99
  const getHttpRequestLatencyMetricsInSeconds = options => ({
144
- httpRequestDurationPerPercentileInSeconds: shouldObserveHttpRequestsAsSummary(options) && asArray(options.metricNames.httpRequestDurationPerPercentileInSeconds).map(nameOfHttpRequestDurationPerPercentileInSeconds => new Prometheus__namespace.Summary({
100
+ httpRequestDurationPerPercentileInSeconds: shouldObserveHttpRequestsAsSummary(options) ? asArray$2(options.metricNames.httpRequestDurationPerPercentileInSeconds).map(nameOfHttpRequestDurationPerPercentileInSeconds => new Prometheus__namespace.Summary({
145
101
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
146
102
  help: 'The HTTP request latencies in seconds.',
147
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
103
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
148
104
  percentiles: options.percentiles || defaultHttpRequestDurationPercentileInSeconds
149
- })),
150
- httpRequestDurationInSeconds: shouldObserveHttpRequestsAsHistogram(options) && asArray(options.metricNames.httpRequestDurationInSeconds).map(nameOfHttpRequestDurationInSecondsMetric => new Prometheus__namespace.Histogram({
105
+ })) : undefined,
106
+ httpRequestDurationInSeconds: shouldObserveHttpRequestsAsHistogram(options) ? asArray$2(options.metricNames.httpRequestDurationInSeconds).map(nameOfHttpRequestDurationInSecondsMetric => new Prometheus__namespace.Histogram({
151
107
  name: `${options.metricPrefix}${nameOfHttpRequestDurationInSecondsMetric}`,
152
108
  help: 'The HTTP request latencies in seconds.',
153
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
109
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
154
110
  buckets: options.buckets || defaultHttpRequestDurationInSeconds
155
- }))
111
+ })) : undefined
156
112
  });
157
113
 
158
114
  const getHttpRequestCounterMetric = options => ({
159
- httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) && asArray(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
115
+ httpRequestsTotal: shouldObserveHttpRequestsAsCounter(options) ? asArray$2(options.metricNames.httpRequestsTotal).map(nameOfHttpRequestsTotalMetric => new Prometheus__namespace.Counter({
160
116
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
161
117
  help: 'The total HTTP requests.',
162
- labelNames: defaultRequestLabels.concat(options.labels).sort()
163
- }))
118
+ labelNames: defaultLabels$2.concat(options.labels).sort()
119
+ })) : undefined
164
120
  });
165
121
 
166
- const createMetricTypes = options => {
167
- const defaultedOptions = merge__default["default"](defaultOptions$1, options);
122
+ const createHttpMetrics = options => {
123
+ const defaultedOptions = merge__default["default"](defaultOptions$4, options);
168
124
  configure({
169
125
  prefix: defaultedOptions.metricPrefix
170
126
  });
171
- const defaultMetrics = getDefaultMetrics(defaultedOptions);
172
- const httpRequestLatencyMetricsInMilliseconds = shouldObserveMetricsInMilliseconds(defaultedOptions) && getHttpRequestLatencyMetricsInMilliseconds(defaultedOptions);
173
- const httpRequestLatencyMetricsInSeconds = shouldObserveMetricsInSeconds(defaultedOptions) && getHttpRequestLatencyMetricsInSeconds(defaultedOptions);
127
+ const metrics = getMetrics$2(defaultedOptions);
128
+ const httpRequestLatencyMetricsInSeconds = getHttpRequestLatencyMetricsInSeconds(defaultedOptions);
174
129
  const httpRequestCounterMetric = getHttpRequestCounterMetric(defaultedOptions);
175
- return Object.assign({}, defaultMetrics, httpRequestLatencyMetricsInMilliseconds, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
130
+ return Object.assign({}, metrics, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
176
131
  };
177
132
 
178
- createMetricTypes.defaultOptions = defaultOptions$1;
133
+ createHttpMetrics.defaultOptions = defaultOptions$4;
179
134
 
180
- const getSummary = async () => defaultRegister.metrics();
135
+ const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
136
+ const defaultLabels$1 = ['operation_name'];
181
137
 
182
- const getContentType = () => defaultRegister.contentType;
138
+ const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
183
139
 
184
- const NS_PER_SEC = 1e9;
185
- const NS_PER_MS = 1e6;
140
+ const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
186
141
 
187
- const sortLabels = unsortedLabels => Object.keys(unsortedLabels).sort((a, b) => {
188
- if (a < b) {
189
- return -1;
190
- }
142
+ const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
143
+
144
+ const shouldObserveGraphQlResolveFieldDurationAsHistogram = options => options.metricTypes.includes('graphQlResolveFieldDurationHistogram');
191
145
 
192
- if (a > b) {
193
- return 1;
146
+ const shouldObserveGraphQlRequestDurationAsHistogram = options => options.metricTypes.includes('graphQlRequestDurationHistogram');
147
+
148
+ const shouldObserveGraphQlErrorsTotalAsCounter = options => options.metricTypes.includes('graphQlErrorsTotal');
149
+
150
+ const defaultOptions$3 = {
151
+ getLabelValues: () => ({}),
152
+ labels: [],
153
+ metricPrefix: '',
154
+ metricTypes: ['graphQlParseDurationHistogram', 'graphQlValidationDurationHistogram', 'graphQlResolveFieldDurationHistogram', 'graphQlRequestDurationHistogram', 'graphQlErrorsTotal'],
155
+ metricNames: {
156
+ graphQlParseDuration: ['graphql_parse_duration_seconds'],
157
+ graphQlValidationDuration: ['graphql_validation_duration_seconds'],
158
+ graphQlResolveFieldDuration: ['graphql_resolve_field_duration_seconds'],
159
+ graphQlRequestDuration: ['graphql_request_duration_seconds'],
160
+ graphQlErrorsTotal: ['graphql_errors_total']
194
161
  }
162
+ };
195
163
 
196
- return 0;
197
- }).reduce((sortedLabels, labelName) => {
198
- sortedLabels[labelName] = unsortedLabels[labelName];
199
- return sortedLabels;
200
- }, {});
164
+ const getMetrics$1 = options => ({
165
+ graphQlParseDuration: shouldObserveGraphQlParseDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlParseDuration).map(nameOfGraphQlParseDuration => new Prometheus__namespace.Histogram({
166
+ name: `${options.metricPrefix}${nameOfGraphQlParseDuration}`,
167
+ help: 'The GraphQL request parse time in seconds.',
168
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
169
+ buckets: options.buckets || defaultGraphQlPercentiles
170
+ })) : undefined,
171
+ graphQlValidationDuration: shouldObserveGraphQlValidationDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlValidationDuration).map(nameOfGraphQlValidationDuration => new Prometheus__namespace.Histogram({
172
+ name: `${options.metricPrefix}${nameOfGraphQlValidationDuration}`,
173
+ help: 'The GraphQL request validation time in seconds.',
174
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
175
+ buckets: options.buckets || defaultGraphQlPercentiles
176
+ })) : undefined,
177
+ graphQlResolveFieldDuration: shouldObserveGraphQlResolveFieldDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlResolveFieldDuration).map(nameOfGraphQlResolveFieldDuration => new Prometheus__namespace.Histogram({
178
+ name: `${options.metricPrefix}${nameOfGraphQlResolveFieldDuration}`,
179
+ help: 'The GraphQL field resolving time in seconds.',
180
+ labelNames: defaultLabels$1.concat(['field_name']).concat(options.labels).sort(),
181
+ buckets: options.buckets || defaultGraphQlPercentiles
182
+ })) : undefined,
183
+ graphQlRequestDuration: shouldObserveGraphQlRequestDurationAsHistogram(options) ? asArray$1(options.metricNames.graphQlRequestDuration).map(nameOfGraphQlRequestDuration => new Prometheus__namespace.Histogram({
184
+ name: `${options.metricPrefix}${nameOfGraphQlRequestDuration}`,
185
+ help: 'The GraphQL request duration time in seconds.',
186
+ labelNames: defaultLabels$1.concat(options.labels).sort(),
187
+ buckets: options.buckets || defaultGraphQlPercentiles
188
+ })) : undefined,
189
+ graphQlErrorsTotal: shouldObserveGraphQlErrorsTotalAsCounter(options) ? asArray$1(options.metricNames.graphQlErrorsTotal).map(nameOfGraphQlErrorsCount => new Prometheus__namespace.Counter({
190
+ name: `${options.metricPrefix}${nameOfGraphQlErrorsCount}`,
191
+ help: 'Count of errors while parsing, validating, or executing a GraphQL operation.',
192
+ labelNames: defaultLabels$1.concat(['phase']).concat(options.labels).sort()
193
+ })) : undefined
194
+ });
201
195
 
202
- const endMeasurementFrom = start => {
203
- const [seconds, nanoseconds] = process.hrtime(start);
204
- return {
205
- durationMs: Math.round((seconds * NS_PER_SEC + nanoseconds) / NS_PER_MS),
206
- durationS: (seconds * NS_PER_SEC + nanoseconds) / NS_PER_SEC
207
- };
196
+ const createGraphQlMetrics = options => {
197
+ const defaultedOptions = merge__default["default"](defaultOptions$3, options);
198
+ configure({
199
+ prefix: defaultedOptions.metricPrefix
200
+ });
201
+ const metrics = getMetrics$1(defaultedOptions);
202
+ return metrics;
208
203
  };
209
204
 
210
- const shouldObserveMetricType = metricType => options => {
211
- var _options$metricTypes;
205
+ createGraphQlMetrics.defaultOptions = defaultOptions$3;
206
+
207
+ const defaultLabels = ['gc_type'];
212
208
 
213
- return (_options$metricTypes = options.metricTypes) === null || _options$metricTypes === void 0 ? void 0 : _options$metricTypes.includes(metricType);
209
+ const asArray = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
210
+
211
+ const defaultOptions$2 = {
212
+ getLabelValues: () => ({}),
213
+ labels: [],
214
+ metricPrefix: '',
215
+ metricNames: {
216
+ up: ['up'],
217
+ countOfGcs: ['nodejs_gc_runs_total'],
218
+ durationOfGc: ['nodejs_gc_pause_seconds_total'],
219
+ reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total']
220
+ }
214
221
  };
215
222
 
216
- const shouldObserveMetricAccuracy = accuracy => options => {
217
- var _options$accuracies;
223
+ const getMetrics = options => ({
224
+ up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
225
+ name: `${options.metricPrefix}${nameOfUpMetric}`,
226
+ help: '1 = up, 0 = not up'
227
+ })),
228
+ countOfGcs: asArray(options.metricNames.countOfGcs).map(nameOfCountOfGcsMetric => new Prometheus__namespace.Counter({
229
+ name: `${options.metricPrefix}${nameOfCountOfGcsMetric}`,
230
+ help: 'Count of total garbage collections.',
231
+ labelNames: defaultLabels
232
+ })),
233
+ durationOfGc: asArray(options.metricNames.durationOfGc).map(nameOfDurationOfGcMetric => new Prometheus__namespace.Counter({
234
+ name: `${options.metricPrefix}${nameOfDurationOfGcMetric}`,
235
+ help: 'Time spent in GC Pause in seconds.',
236
+ labelNames: defaultLabels
237
+ })),
238
+ reclaimedInGc: asArray(options.metricNames.reclaimedInGc).map(nameOfReclaimedInGcMetric => new Prometheus__namespace.Counter({
239
+ name: `${options.metricPrefix}${nameOfReclaimedInGcMetric}`,
240
+ help: 'Total number of bytes reclaimed by GC.',
241
+ labelNames: defaultLabels
242
+ }))
243
+ });
218
244
 
219
- return (_options$accuracies = options.accuracies) === null || _options$accuracies === void 0 ? void 0 : _options$accuracies.includes(accuracy);
245
+ const createGcMetrics = options => {
246
+ const defaultedOptions = merge__default["default"](defaultOptions$2, options);
247
+ configure({
248
+ prefix: defaultedOptions.metricPrefix
249
+ });
250
+ const gcMetrics = getMetrics(defaultedOptions);
251
+ return gcMetrics;
220
252
  };
221
253
 
222
- const defaultOptions = {
223
- accuracies: ['s'],
224
- metricTypes: ['httpRequestsTotal', 'httpRequestsHistogram'],
254
+ createGcMetrics.defaultOptions = defaultOptions$2;
255
+
256
+ const getSummary = async () => defaultRegister.metrics();
257
+
258
+ const getContentType = () => defaultRegister.contentType;
259
+
260
+ function sortLabels(unsortedLabels) {
261
+ return Object.keys(unsortedLabels).sort((a, b) => {
262
+ if (a < b) {
263
+ return -1;
264
+ }
265
+
266
+ if (a > b) {
267
+ return 1;
268
+ }
269
+
270
+ return 0;
271
+ }).reduce((sortedLabels, labelName) => {
272
+ sortedLabels[labelName] = unsortedLabels[labelName];
273
+ return sortedLabels;
274
+ }, {});
275
+ }
276
+
277
+ const NS_PER_SEC = 1e9;
278
+
279
+ function endMeasurementFrom(start) {
280
+ const [seconds, nanoseconds] = process.hrtime(start);
281
+ return {
282
+ durationS: (seconds * NS_PER_SEC + nanoseconds) / NS_PER_SEC
283
+ };
284
+ }
285
+
286
+ const defaultOptions$1 = {
225
287
  skip: () => false,
226
288
  detectKubernetes: false
227
289
  };
228
290
 
229
- const createRequestRecorder = (metricTypes, options = defaultOptions) => {
230
- const defaultedRecorderOptions = merge__default["default"](defaultOptions, options);
231
- const shouldSkipMetricsByEnvironment = defaultedRecorderOptions.detectKubernetes && !isRunningInKubernetes();
232
- const shouldObserveInSeconds = shouldObserveMetricAccuracy('s')(defaultedRecorderOptions);
233
- const shouldObserveInMilliseconds = shouldObserveMetricAccuracy('ms')(defaultedRecorderOptions);
234
- const shouldObserveInSummary = shouldObserveMetricType('httpRequestsSummary')(defaultedRecorderOptions);
235
- const shouldObserveInHistogram = shouldObserveMetricType('httpRequestsHistogram')(defaultedRecorderOptions);
236
- const shouldObserveInCounter = shouldObserveMetricType('httpRequestsTotal')(defaultedRecorderOptions);
237
- return (start, recordingOptions) => {
238
- const {
239
- durationMs,
240
- durationS
241
- } = endMeasurementFrom(start);
242
- const labels = sortLabels(recordingOptions.labels);
291
+ function isTiming(timing) {
292
+ return !Array.isArray(timing);
293
+ }
243
294
 
244
- if (shouldObserveInMilliseconds && shouldObserveInHistogram && !shouldSkipMetricsByEnvironment) {
245
- metricTypes.httpRequestDurationInMilliseconds.forEach(httpRequestDurationInMillisecondsMetricType => {
246
- httpRequestDurationInMillisecondsMetricType.observe(labels, durationMs);
247
- });
248
- }
295
+ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
296
+ const defaultedRecorderOptions = merge__default["default"](defaultOptions$1, options);
297
+ const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
298
+ return (timing, recordingOptions) => {
299
+ const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
300
+ const labels = sortLabels(recordingOptions.labels);
249
301
 
250
- if (shouldObserveInMilliseconds && shouldObserveInSummary && !shouldSkipMetricsByEnvironment) {
251
- metricTypes.httpRequestDurationPerPercentileInMilliseconds.forEach(httpRequestDurationPerPercentileInMillisecondsMetricType => {
252
- httpRequestDurationPerPercentileInMillisecondsMetricType.observe(labels, durationMs);
253
- });
254
- }
302
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
303
+ var _metrics$httpRequestD;
255
304
 
256
- if (shouldObserveInSeconds && shouldObserveInHistogram && !shouldSkipMetricsByEnvironment) {
257
- metricTypes.httpRequestDurationInSeconds.forEach(httpRequestDurationInSecondsMetricType => {
305
+ (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
258
306
  httpRequestDurationInSecondsMetricType.observe(labels, durationS);
259
307
  });
260
308
  }
261
309
 
262
- if (shouldObserveInSeconds && shouldObserveInSummary && !shouldSkipMetricsByEnvironment) {
263
- metricTypes.httpRequestDurationPerPercentileInSeconds.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
310
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
311
+ var _metrics$httpRequestD2;
312
+
313
+ (_metrics$httpRequestD2 = metrics.httpRequestDurationPerPercentileInSeconds) === null || _metrics$httpRequestD2 === void 0 ? void 0 : _metrics$httpRequestD2.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
264
314
  httpRequestDurationPerPercentileInSecondsMetricType.observe(labels, durationS);
265
315
  });
266
316
  }
267
317
 
268
- if (shouldObserveInCounter && !shouldSkipMetricsByEnvironment) {
269
- metricTypes.httpRequestsTotal.forEach(httpRequestsTotalMetricType => {
318
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
319
+ var _metrics$httpRequests;
320
+
321
+ (_metrics$httpRequests = metrics.httpRequestsTotal) === null || _metrics$httpRequests === void 0 ? void 0 : _metrics$httpRequests.forEach(httpRequestsTotalMetricType => {
270
322
  httpRequestsTotalMetricType.inc(labels);
271
323
  });
272
324
  }
273
325
 
274
- if (metricTypes.httpRequestContentLengthInBytes && recordingOptions.requestContentLength) {
275
- metricTypes.httpRequestContentLengthInBytes.forEach(httpRequestContentLengthInBytesMetricType => {
326
+ if (recordingOptions.requestContentLength) {
327
+ var _metrics$httpRequestC;
328
+
329
+ (_metrics$httpRequestC = metrics.httpRequestContentLengthInBytes) === null || _metrics$httpRequestC === void 0 ? void 0 : _metrics$httpRequestC.forEach(httpRequestContentLengthInBytesMetricType => {
276
330
  httpRequestContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
277
331
  recordingOptions.requestContentLength);
278
332
  });
279
333
  }
280
334
 
281
- if (metricTypes.httpResponseContentLengthInBytes && recordingOptions.responseContentLength) {
282
- metricTypes.httpResponseContentLengthInBytes.forEach(httpResponseContentLengthInBytesMetricType => {
335
+ if (recordingOptions.responseContentLength) {
336
+ var _metrics$httpResponse;
337
+
338
+ (_metrics$httpResponse = metrics.httpResponseContentLengthInBytes) === null || _metrics$httpResponse === void 0 ? void 0 : _metrics$httpResponse.forEach(httpResponseContentLengthInBytesMetricType => {
283
339
  httpResponseContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
284
340
  recordingOptions.responseContentLength);
285
341
  });
@@ -287,7 +343,7 @@ const createRequestRecorder = (metricTypes, options = defaultOptions) => {
287
343
  };
288
344
  };
289
345
 
290
- createRequestRecorder.defaultOptions = defaultOptions;
346
+ createRequestRecorder.defaultOptions = defaultOptions$1;
291
347
 
292
348
  const gc = requireOptional__default["default"]('@sematext/gc-stats');
293
349
  const gcTypes = {
@@ -299,27 +355,32 @@ const gcTypes = {
299
355
  8: 'weak_phantom',
300
356
  15: 'all'
301
357
  };
302
- const createGcObserver = once__default["default"](metricTypes => () => {
358
+ const defaultOptions = {
359
+ disableGcMetrics: false
360
+ };
361
+ const createGcObserver = once__default["default"]((metrics, options) => () => {
303
362
  if (typeof gc !== 'function') {
304
363
  return;
305
364
  }
306
365
 
366
+ if (options.disableGcMetrics) return;
307
367
  gc().on('stats', stats => {
308
368
  const gcType = gcTypes[stats.gctype];
309
- metricTypes.countOfGcs.forEach(countOfGcMetricType => {
369
+ metrics.countOfGcs.forEach(countOfGcMetricType => {
310
370
  countOfGcMetricType.labels(gcType).inc();
311
371
  });
312
- metricTypes.durationOfGc.forEach(durationOfGcMetricType => {
372
+ metrics.durationOfGc.forEach(durationOfGcMetricType => {
313
373
  durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
314
374
  });
315
375
 
316
376
  if (stats.diff.usedHeapSize < 0) {
317
- metricTypes.reclaimedInGc.forEach(reclaimedInGcMetricType => {
377
+ metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
318
378
  reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
319
379
  });
320
380
  }
321
381
  });
322
382
  });
383
+ createGcObserver.defaultOptions = defaultOptions;
323
384
 
324
385
  const normalizeStatusCode = statusCode => statusCode;
325
386
 
@@ -335,15 +396,140 @@ const defaultNormalizers = {
335
396
  normalizeMethod
336
397
  };
337
398
 
399
+ function _checkPrivateRedeclaration(obj, privateCollection) {
400
+ if (privateCollection.has(obj)) {
401
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
402
+ }
403
+ }
404
+
405
+ function _classPrivateFieldInitSpec(obj, privateMap, value) {
406
+ _checkPrivateRedeclaration(obj, privateMap);
407
+ privateMap.set(obj, value);
408
+ }
409
+
410
+ function _defineProperty(obj, key, value) {
411
+ if (key in obj) {
412
+ Object.defineProperty(obj, key, {
413
+ value: value,
414
+ enumerable: true,
415
+ configurable: true,
416
+ writable: true
417
+ });
418
+ } else {
419
+ obj[key] = value;
420
+ }
421
+
422
+ return obj;
423
+ }
424
+
425
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
426
+ if (descriptor.set) {
427
+ descriptor.set.call(receiver, value);
428
+ } else {
429
+ if (!descriptor.writable) {
430
+ throw new TypeError("attempted to set read only private field");
431
+ }
432
+
433
+ descriptor.value = value;
434
+ }
435
+ }
436
+
437
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
438
+ if (!privateMap.has(receiver)) {
439
+ throw new TypeError("attempted to " + action + " private field on non-instance");
440
+ }
441
+
442
+ return privateMap.get(receiver);
443
+ }
444
+
445
+ function _classPrivateFieldSet(receiver, privateMap, value) {
446
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
447
+ _classApplyDescriptorSet(receiver, descriptor, value);
448
+ return value;
449
+ }
450
+
451
+ function _classApplyDescriptorGet(receiver, descriptor) {
452
+ if (descriptor.get) {
453
+ return descriptor.get.call(receiver);
454
+ }
455
+
456
+ return descriptor.value;
457
+ }
458
+
459
+ function _classPrivateFieldGet(receiver, privateMap) {
460
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
461
+ return _classApplyDescriptorGet(receiver, descriptor);
462
+ }
463
+
464
+ var _startTime = /*#__PURE__*/new WeakMap();
465
+
466
+ var _endTime = /*#__PURE__*/new WeakMap();
467
+
468
+ class Timing {
469
+ constructor() {
470
+ _classPrivateFieldInitSpec(this, _startTime, {
471
+ writable: true,
472
+ value: void 0
473
+ });
474
+
475
+ _classPrivateFieldInitSpec(this, _endTime, {
476
+ writable: true,
477
+ value: void 0
478
+ });
479
+
480
+ this.reset();
481
+ }
482
+
483
+ value() {
484
+ const startTime = _classPrivateFieldGet(this, _startTime);
485
+
486
+ const endTime = _classPrivateFieldGet(this, _endTime);
487
+
488
+ return {
489
+ seconds: endTime && startTime && Number((endTime - startTime) / Timing.NS_PER_SEC)
490
+ };
491
+ }
492
+
493
+ reset() {
494
+ _classPrivateFieldSet(this, _startTime, process.hrtime.bigint());
495
+
496
+ _classPrivateFieldSet(this, _endTime, undefined);
497
+
498
+ return this;
499
+ }
500
+
501
+ end() {
502
+ _classPrivateFieldSet(this, _endTime, process.hrtime.bigint());
503
+
504
+ return this;
505
+ }
506
+
507
+ }
508
+
509
+ _defineProperty(Timing, "NS_PER_SEC", BigInt(1e9));
510
+
511
+ const timing = {
512
+ start() {
513
+ return new Timing();
514
+ }
515
+
516
+ };
517
+
338
518
  exports.Prometheus = Prometheus__namespace;
519
+ exports.createGcMetrics = createGcMetrics;
339
520
  exports.createGcObserver = createGcObserver;
340
- exports.createMetricTypes = createMetricTypes;
521
+ exports.createGraphQlMetrics = createGraphQlMetrics;
522
+ exports.createHttpMetrics = createHttpMetrics;
341
523
  exports.createRequestRecorder = createRequestRecorder;
342
524
  exports.defaultNormalizers = defaultNormalizers;
343
525
  exports.defaultRegister = defaultRegister;
526
+ exports.endMeasurementFrom = endMeasurementFrom;
344
527
  exports.getContentType = getContentType;
345
528
  exports.getSummary = getSummary;
346
529
  exports.isRunningInKubernetes = isRunningInKubernetes;
347
530
  exports.normalizeMethod = normalizeMethod;
348
531
  exports.normalizePath = normalizePath;
349
532
  exports.normalizeStatusCode = normalizeStatusCode;
533
+ exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
534
+ exports.sortLabels = sortLabels;
535
+ exports.timing = timing;