@promster/metrics 8.0.0 → 9.1.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.
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 +17 -0
  18. package/dist/promster-metrics.cjs.dev.js +333 -148
  19. package/dist/promster-metrics.cjs.prod.js +333 -148
  20. package/package.json +8 -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
 
@@ -72,218 +67,275 @@ const shouldObserveHttpRequestsAsCounter = options => options.metricTypes.includ
72
67
 
73
68
  const shouldObserveHttpContentLengthAsHistogram = options => options.metricTypes.includes('httpContentLengthHistogram');
74
69
 
75
- const defaultOptions$1 = {
70
+ const defaultOptions$4 = {
76
71
  getLabelValues: () => ({}),
77
72
  labels: [],
78
- accuracies: ['s'],
79
73
  metricPrefix: '',
80
74
  metricTypes: ['httpRequestsTotal', 'httpRequestsHistogram'],
81
75
  metricNames: {
82
- up: ['up'],
83
- countOfGcs: ['nodejs_gc_runs_total'],
84
- durationOfGc: ['nodejs_gc_pause_seconds_total'],
85
- reclaimedInGc: ['nodejs_gc_reclaimed_bytes_total'],
86
76
  httpRequestsTotal: ['http_requests_total'],
87
- httpRequestDurationPerPercentileInMilliseconds: ['http_request_duration_per_percentile_milliseconds'],
88
77
  httpRequestDurationPerPercentileInSeconds: ['http_request_duration_per_percentile_seconds'],
89
78
  httpRequestDurationInSeconds: ['http_request_duration_seconds'],
90
- httpRequestDurationInMilliseconds: ['http_request_duration_milliseconds'],
91
79
  httpRequestContentLengthInBytes: ['http_request_content_length_bytes'],
92
80
  httpResponseContentLengthInBytes: ['http_response_content_length_bytes']
93
81
  }
94
82
  };
95
83
 
96
- const getDefaultMetrics = options => ({
97
- up: asArray(options.metricNames.up).map(nameOfUpMetric => new Prometheus__namespace.Gauge({
98
- name: `${options.metricPrefix}${nameOfUpMetric}`,
99
- help: '1 = up, 0 = not up'
100
- })),
101
- countOfGcs: asArray(options.metricNames.countOfGcs).map(nameOfCountOfGcsMetric => new Prometheus__namespace.Counter({
102
- name: `${options.metricPrefix}${nameOfCountOfGcsMetric}`,
103
- help: 'Count of total garbage collections.',
104
- labelNames: defaultGcLabels
105
- })),
106
- durationOfGc: asArray(options.metricNames.durationOfGc).map(nameOfDurationOfGcMetric => new Prometheus__namespace.Counter({
107
- name: `${options.metricPrefix}${nameOfDurationOfGcMetric}`,
108
- help: 'Time spent in GC Pause in seconds.',
109
- labelNames: defaultGcLabels
110
- })),
111
- reclaimedInGc: asArray(options.metricNames.reclaimedInGc).map(nameOfReclaimedInGcMetric => new Prometheus__namespace.Counter({
112
- name: `${options.metricPrefix}${nameOfReclaimedInGcMetric}`,
113
- help: 'Total number of bytes reclaimed by GC.',
114
- labelNames: defaultGcLabels
115
- })),
116
- httpRequestContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) && 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({
117
86
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
118
87
  help: 'The HTTP request content length in bytes.',
119
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
88
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
120
89
  buckets: options.buckets || defaultHttpContentLengthInBytes
121
- })),
122
- httpResponseContentLengthInBytes: shouldObserveHttpContentLengthAsHistogram(options) && 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({
123
92
  name: `${options.metricPrefix}${nameOfHttpContentLengthMetric}`,
124
93
  help: 'The HTTP response content length in bytes.',
125
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
94
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
126
95
  buckets: options.buckets || defaultHttpContentLengthInBytes
127
- }))
128
- });
129
-
130
- const getHttpRequestLatencyMetricsInMilliseconds = options => ({
131
- httpRequestDurationPerPercentileInMilliseconds: shouldObserveHttpRequestsAsSummary(options) && asArray(options.metricNames.httpRequestDurationPerPercentileInMilliseconds).map(nameOfHttpRequestDurationPerPercentileInMillisecondsMetric => new Prometheus__namespace.Summary({
132
- name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInMillisecondsMetric}`,
133
- help: 'The HTTP request latencies in milliseconds.',
134
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
135
- percentiles: options.percentiles || defaultHttpRequestDurationPercentilesInMillieconds
136
- })),
137
- httpRequestDurationInMilliseconds: shouldObserveHttpRequestsAsHistogram(options) && asArray(options.metricNames.httpRequestDurationInMilliseconds).map(nameOfHttpRequestDurationInMillisecondsMetric => new Prometheus__namespace.Histogram({
138
- name: `${options.metricPrefix}${nameOfHttpRequestDurationInMillisecondsMetric}`,
139
- help: 'The HTTP request latencies in milliseconds.',
140
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
141
- buckets: options.buckets || defaultHttpRequestDurationInMilliseconds
142
- }))
96
+ })) : undefined
143
97
  });
144
98
 
145
99
  const getHttpRequestLatencyMetricsInSeconds = options => ({
146
- 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({
147
101
  name: `${options.metricPrefix}${nameOfHttpRequestDurationPerPercentileInSeconds}`,
148
102
  help: 'The HTTP request latencies in seconds.',
149
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
103
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
150
104
  percentiles: options.percentiles || defaultHttpRequestDurationPercentileInSeconds
151
- })),
152
- 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({
153
107
  name: `${options.metricPrefix}${nameOfHttpRequestDurationInSecondsMetric}`,
154
108
  help: 'The HTTP request latencies in seconds.',
155
- labelNames: defaultRequestLabels.concat(options.labels).sort(),
109
+ labelNames: defaultLabels$2.concat(options.labels).sort(),
156
110
  buckets: options.buckets || defaultHttpRequestDurationInSeconds
157
- }))
111
+ })) : undefined
158
112
  });
159
113
 
160
114
  const getHttpRequestCounterMetric = options => ({
161
- 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({
162
116
  name: `${options.metricPrefix}${nameOfHttpRequestsTotalMetric}`,
163
117
  help: 'The total HTTP requests.',
164
- labelNames: defaultRequestLabels.concat(options.labels).sort()
165
- }))
118
+ labelNames: defaultLabels$2.concat(options.labels).sort()
119
+ })) : undefined
166
120
  });
167
121
 
168
- const createMetricTypes = options => {
169
- const defaultedOptions = merge__default["default"](defaultOptions$1, options);
122
+ const createHttpMetrics = options => {
123
+ const defaultedOptions = merge__default["default"](defaultOptions$4, options);
170
124
  configure({
171
125
  prefix: defaultedOptions.metricPrefix
172
126
  });
173
- const defaultMetrics = getDefaultMetrics(defaultedOptions);
174
- const httpRequestLatencyMetricsInMilliseconds = shouldObserveMetricsInMilliseconds(defaultedOptions) && getHttpRequestLatencyMetricsInMilliseconds(defaultedOptions);
175
- const httpRequestLatencyMetricsInSeconds = shouldObserveMetricsInSeconds(defaultedOptions) && getHttpRequestLatencyMetricsInSeconds(defaultedOptions);
127
+ const metrics = getMetrics$2(defaultedOptions);
128
+ const httpRequestLatencyMetricsInSeconds = getHttpRequestLatencyMetricsInSeconds(defaultedOptions);
176
129
  const httpRequestCounterMetric = getHttpRequestCounterMetric(defaultedOptions);
177
- return Object.assign({}, defaultMetrics, httpRequestLatencyMetricsInMilliseconds, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
130
+ return Object.assign({}, metrics, httpRequestLatencyMetricsInSeconds, httpRequestCounterMetric);
178
131
  };
179
132
 
180
- createMetricTypes.defaultOptions = defaultOptions$1;
133
+ createHttpMetrics.defaultOptions = defaultOptions$4;
181
134
 
182
- const getSummary = async () => defaultRegister.metrics();
135
+ const defaultGraphQlPercentiles = [0.5, 0.9, 0.95, 0.98, 0.99];
136
+ const defaultLabels$1 = ['operation_name'];
183
137
 
184
- const getContentType = () => defaultRegister.contentType;
138
+ const asArray$1 = maybeArray => Array.isArray(maybeArray) ? maybeArray : [maybeArray];
185
139
 
186
- const NS_PER_SEC = 1e9;
187
- const NS_PER_MS = 1e6;
140
+ const shouldObserveGraphQlParseDurationAsHistogram = options => options.metricTypes.includes('graphQlParseDurationHistogram');
188
141
 
189
- const sortLabels = unsortedLabels => Object.keys(unsortedLabels).sort((a, b) => {
190
- if (a < b) {
191
- return -1;
192
- }
142
+ const shouldObserveGraphQlValidationDurationAsHistogram = options => options.metricTypes.includes('graphQlValidationDurationHistogram');
143
+
144
+ const shouldObserveGraphQlResolveFieldDurationAsHistogram = options => options.metricTypes.includes('graphQlResolveFieldDurationHistogram');
193
145
 
194
- if (a > b) {
195
- 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']
196
161
  }
162
+ };
197
163
 
198
- return 0;
199
- }).reduce((sortedLabels, labelName) => {
200
- sortedLabels[labelName] = unsortedLabels[labelName];
201
- return sortedLabels;
202
- }, {});
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
+ });
203
195
 
204
- const endMeasurementFrom = start => {
205
- const [seconds, nanoseconds] = process.hrtime(start);
206
- return {
207
- durationMs: Math.round((seconds * NS_PER_SEC + nanoseconds) / NS_PER_MS),
208
- durationS: (seconds * NS_PER_SEC + nanoseconds) / NS_PER_SEC
209
- };
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;
210
203
  };
211
204
 
212
- const shouldObserveMetricType = metricType => options => {
213
- var _options$metricTypes;
205
+ createGraphQlMetrics.defaultOptions = defaultOptions$3;
206
+
207
+ const defaultLabels = ['gc_type'];
214
208
 
215
- 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
+ }
216
221
  };
217
222
 
218
- const shouldObserveMetricAccuracy = accuracy => options => {
219
- 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
+ });
220
244
 
221
- 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;
222
252
  };
223
253
 
224
- const defaultOptions = {
225
- accuracies: ['s'],
226
- 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 = {
227
287
  skip: () => false,
228
288
  detectKubernetes: false
229
289
  };
230
290
 
231
- const createRequestRecorder = (metricTypes, options = defaultOptions) => {
232
- const defaultedRecorderOptions = merge__default["default"](defaultOptions, options);
233
- const shouldSkipMetricsByEnvironment = defaultedRecorderOptions.detectKubernetes && !isRunningInKubernetes();
234
- const shouldObserveInSeconds = shouldObserveMetricAccuracy('s')(defaultedRecorderOptions);
235
- const shouldObserveInMilliseconds = shouldObserveMetricAccuracy('ms')(defaultedRecorderOptions);
236
- const shouldObserveInSummary = shouldObserveMetricType('httpRequestsSummary')(defaultedRecorderOptions);
237
- const shouldObserveInHistogram = shouldObserveMetricType('httpRequestsHistogram')(defaultedRecorderOptions);
238
- const shouldObserveInCounter = shouldObserveMetricType('httpRequestsTotal')(defaultedRecorderOptions);
239
- const shouldObserveContentLengthInHistogram = shouldObserveMetricType('httpContentLengthHistogram')(defaultedRecorderOptions); // eslint-disable-next-line complexity
240
-
241
- return (start, recordingOptions) => {
242
- const {
243
- durationMs,
244
- durationS
245
- } = endMeasurementFrom(start);
246
- const labels = sortLabels(recordingOptions.labels);
291
+ function isTiming(timing) {
292
+ return !Array.isArray(timing);
293
+ }
247
294
 
248
- if (shouldObserveInMilliseconds && shouldObserveInHistogram && !shouldSkipMetricsByEnvironment) {
249
- metricTypes.httpRequestDurationInMilliseconds.forEach(httpRequestDurationInMillisecondsMetricType => {
250
- httpRequestDurationInMillisecondsMetricType.observe(labels, durationMs);
251
- });
252
- }
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);
253
301
 
254
- if (shouldObserveInMilliseconds && shouldObserveInSummary && !shouldSkipMetricsByEnvironment) {
255
- metricTypes.httpRequestDurationPerPercentileInMilliseconds.forEach(httpRequestDurationPerPercentileInMillisecondsMetricType => {
256
- httpRequestDurationPerPercentileInMillisecondsMetricType.observe(labels, durationMs);
257
- });
258
- }
302
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
303
+ var _metrics$httpRequestD;
259
304
 
260
- if (shouldObserveInSeconds && shouldObserveInHistogram && !shouldSkipMetricsByEnvironment) {
261
- metricTypes.httpRequestDurationInSeconds.forEach(httpRequestDurationInSecondsMetricType => {
305
+ (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
262
306
  httpRequestDurationInSecondsMetricType.observe(labels, durationS);
263
307
  });
264
308
  }
265
309
 
266
- if (shouldObserveInSeconds && shouldObserveInSummary && !shouldSkipMetricsByEnvironment) {
267
- 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 => {
268
314
  httpRequestDurationPerPercentileInSecondsMetricType.observe(labels, durationS);
269
315
  });
270
316
  }
271
317
 
272
- if (shouldObserveInCounter && !shouldSkipMetricsByEnvironment) {
273
- 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 => {
274
322
  httpRequestsTotalMetricType.inc(labels);
275
323
  });
276
324
  }
277
325
 
278
- if (!shouldSkipMetricsByEnvironment && shouldObserveContentLengthInHistogram && recordingOptions.requestContentLength) {
279
- 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 => {
280
330
  httpRequestContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
281
331
  recordingOptions.requestContentLength);
282
332
  });
283
333
  }
284
334
 
285
- if (!shouldSkipMetricsByEnvironment && shouldObserveContentLengthInHistogram && recordingOptions.responseContentLength) {
286
- 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 => {
287
339
  httpResponseContentLengthInBytesMetricType.observe(labels, // @ts-expect-error
288
340
  recordingOptions.responseContentLength);
289
341
  });
@@ -291,7 +343,7 @@ const createRequestRecorder = (metricTypes, options = defaultOptions) => {
291
343
  };
292
344
  };
293
345
 
294
- createRequestRecorder.defaultOptions = defaultOptions;
346
+ createRequestRecorder.defaultOptions = defaultOptions$1;
295
347
 
296
348
  const gc = requireOptional__default["default"]('@sematext/gc-stats');
297
349
  const gcTypes = {
@@ -303,27 +355,32 @@ const gcTypes = {
303
355
  8: 'weak_phantom',
304
356
  15: 'all'
305
357
  };
306
- const createGcObserver = once__default["default"](metricTypes => () => {
358
+ const defaultOptions = {
359
+ disableGcMetrics: false
360
+ };
361
+ const createGcObserver = once__default["default"]((metrics, options) => () => {
307
362
  if (typeof gc !== 'function') {
308
363
  return;
309
364
  }
310
365
 
366
+ if (options.disableGcMetrics) return;
311
367
  gc().on('stats', stats => {
312
368
  const gcType = gcTypes[stats.gctype];
313
- metricTypes.countOfGcs.forEach(countOfGcMetricType => {
369
+ metrics.countOfGcs.forEach(countOfGcMetricType => {
314
370
  countOfGcMetricType.labels(gcType).inc();
315
371
  });
316
- metricTypes.durationOfGc.forEach(durationOfGcMetricType => {
372
+ metrics.durationOfGc.forEach(durationOfGcMetricType => {
317
373
  durationOfGcMetricType.labels(gcType).inc(stats.pause / 1e9);
318
374
  });
319
375
 
320
376
  if (stats.diff.usedHeapSize < 0) {
321
- metricTypes.reclaimedInGc.forEach(reclaimedInGcMetricType => {
377
+ metrics.reclaimedInGc.forEach(reclaimedInGcMetricType => {
322
378
  reclaimedInGcMetricType.labels(gcType).inc(stats.diff.usedHeapSize * -1);
323
379
  });
324
380
  }
325
381
  });
326
382
  });
383
+ createGcObserver.defaultOptions = defaultOptions;
327
384
 
328
385
  const normalizeStatusCode = statusCode => statusCode;
329
386
 
@@ -339,15 +396,143 @@ const defaultNormalizers = {
339
396
  normalizeMethod
340
397
  };
341
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
+ if (!endTime || !startTime) return {
489
+ seconds: undefined
490
+ };
491
+ return {
492
+ seconds: Number((endTime - startTime) / Timing.NS_PER_SEC)
493
+ };
494
+ }
495
+
496
+ reset() {
497
+ _classPrivateFieldSet(this, _startTime, process.hrtime.bigint());
498
+
499
+ _classPrivateFieldSet(this, _endTime, undefined);
500
+
501
+ return this;
502
+ }
503
+
504
+ end() {
505
+ _classPrivateFieldSet(this, _endTime, process.hrtime.bigint());
506
+
507
+ return this;
508
+ }
509
+
510
+ }
511
+
512
+ _defineProperty(Timing, "NS_PER_SEC", BigInt(1e9));
513
+
514
+ const timing = {
515
+ start() {
516
+ return new Timing();
517
+ }
518
+
519
+ };
520
+
342
521
  exports.Prometheus = Prometheus__namespace;
522
+ exports.createGcMetrics = createGcMetrics;
343
523
  exports.createGcObserver = createGcObserver;
344
- exports.createMetricTypes = createMetricTypes;
524
+ exports.createGraphQlMetrics = createGraphQlMetrics;
525
+ exports.createHttpMetrics = createHttpMetrics;
345
526
  exports.createRequestRecorder = createRequestRecorder;
346
527
  exports.defaultNormalizers = defaultNormalizers;
347
528
  exports.defaultRegister = defaultRegister;
529
+ exports.endMeasurementFrom = endMeasurementFrom;
348
530
  exports.getContentType = getContentType;
349
531
  exports.getSummary = getSummary;
350
532
  exports.isRunningInKubernetes = isRunningInKubernetes;
351
533
  exports.normalizeMethod = normalizeMethod;
352
534
  exports.normalizePath = normalizePath;
353
535
  exports.normalizeStatusCode = normalizeStatusCode;
536
+ exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
537
+ exports.sortLabels = sortLabels;
538
+ exports.timing = timing;