@promster/metrics 9.0.0 → 9.1.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,12 +1,14 @@
1
- import type { TPromsterOptions, TLabelValues, THttpMetrics, TRequestTiming } from '@promster/types';
1
+ import type { TPromsterOptions, TLabelValues, THttpMetrics } from '@promster/types';
2
2
  import { sortLabels } from '../sort-labels';
3
3
  import { endMeasurementFrom } from '../end-measurement-from';
4
+ import { Timing } from '../timing';
4
5
  declare type TRecordingOptions = {
5
6
  labels: TLabelValues;
6
7
  requestContentLength?: number;
7
8
  responseContentLength?: number;
8
9
  };
9
- export declare type TRequestRecorder = (start: TRequestTiming, recordingOptions: TRecordingOptions) => void;
10
+ declare type TLegacyTiming = [number, number];
11
+ export declare type TRequestRecorder = (timing: Timing | TLegacyTiming, recordingOptions: TRecordingOptions) => void;
10
12
  declare const createRequestRecorder: {
11
13
  (metrics: THttpMetrics, options?: TPromsterOptions): TRequestRecorder;
12
14
  defaultOptions: TPromsterOptions;
@@ -1,5 +1,4 @@
1
- import type { TRequestTiming } from '@promster/types';
2
- declare function endMeasurementFrom(start: TRequestTiming): {
1
+ declare function endMeasurementFrom(start: [number, number]): {
3
2
  durationS: number;
4
3
  };
5
4
  export { endMeasurementFrom };
@@ -9,5 +9,7 @@ import { defaultNormalizers, normalizeStatusCode, normalizePath, normalizeMethod
9
9
  import { isRunningInKubernetes, skipMetricsInEnvironment } from './environment';
10
10
  import { endMeasurementFrom } from './end-measurement-from';
11
11
  import { sortLabels } from './sort-labels';
12
+ import { timing } from './timing';
12
13
  export type { TRequestRecorder } from './create-request-recorder';
13
- export { Prometheus, defaultRegister, createHttpMetrics, createGraphQlMetrics, createGcMetrics, getSummary, getContentType, createRequestRecorder, createGcObserver, defaultNormalizers, normalizeStatusCode, normalizePath, normalizeMethod, isRunningInKubernetes, skipMetricsInEnvironment, endMeasurementFrom, sortLabels, };
14
+ export type { Timing as TPromsterTiming } from './timing';
15
+ export { Prometheus, defaultRegister, createHttpMetrics, createGraphQlMetrics, createGcMetrics, getSummary, getContentType, createRequestRecorder, createGcObserver, defaultNormalizers, normalizeStatusCode, normalizePath, normalizeMethod, isRunningInKubernetes, skipMetricsInEnvironment, endMeasurementFrom, sortLabels, timing, };
@@ -0,0 +1,2 @@
1
+ import timing, { Timing } from './timing';
2
+ export { timing, Timing };
@@ -0,0 +1,15 @@
1
+ declare class Timing {
2
+ #private;
3
+ static NS_PER_SEC: bigint;
4
+ constructor();
5
+ value(): {
6
+ seconds: number | 0n | undefined;
7
+ };
8
+ reset(): this;
9
+ end(): this;
10
+ }
11
+ declare const timing: {
12
+ start(): Timing;
13
+ };
14
+ export default timing;
15
+ export { Timing };
@@ -288,16 +288,18 @@ const defaultOptions$1 = {
288
288
  detectKubernetes: false
289
289
  };
290
290
 
291
+ function isTiming(timing) {
292
+ return !Array.isArray(timing);
293
+ }
294
+
291
295
  const createRequestRecorder = (metrics, options = defaultOptions$1) => {
292
296
  const defaultedRecorderOptions = merge__default["default"](defaultOptions$1, options);
293
297
  const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
294
- return (start, recordingOptions) => {
295
- const {
296
- durationS
297
- } = endMeasurementFrom(start);
298
+ return (timing, recordingOptions) => {
299
+ const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
298
300
  const labels = sortLabels(recordingOptions.labels);
299
301
 
300
- if (!shouldSkipMetricsByEnvironment) {
302
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
301
303
  var _metrics$httpRequestD;
302
304
 
303
305
  (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
@@ -305,7 +307,7 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
305
307
  });
306
308
  }
307
309
 
308
- if (!shouldSkipMetricsByEnvironment) {
310
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
309
311
  var _metrics$httpRequestD2;
310
312
 
311
313
  (_metrics$httpRequestD2 = metrics.httpRequestDurationPerPercentileInSeconds) === null || _metrics$httpRequestD2 === void 0 ? void 0 : _metrics$httpRequestD2.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
@@ -313,7 +315,7 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
313
315
  });
314
316
  }
315
317
 
316
- if (!shouldSkipMetricsByEnvironment) {
318
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
317
319
  var _metrics$httpRequests;
318
320
 
319
321
  (_metrics$httpRequests = metrics.httpRequestsTotal) === null || _metrics$httpRequests === void 0 ? void 0 : _metrics$httpRequests.forEach(httpRequestsTotalMetricType => {
@@ -394,6 +396,125 @@ const defaultNormalizers = {
394
396
  normalizeMethod
395
397
  };
396
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
+
397
518
  exports.Prometheus = Prometheus__namespace;
398
519
  exports.createGcMetrics = createGcMetrics;
399
520
  exports.createGcObserver = createGcObserver;
@@ -411,3 +532,4 @@ exports.normalizePath = normalizePath;
411
532
  exports.normalizeStatusCode = normalizeStatusCode;
412
533
  exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
413
534
  exports.sortLabels = sortLabels;
535
+ exports.timing = timing;
@@ -288,16 +288,18 @@ const defaultOptions$1 = {
288
288
  detectKubernetes: false
289
289
  };
290
290
 
291
+ function isTiming(timing) {
292
+ return !Array.isArray(timing);
293
+ }
294
+
291
295
  const createRequestRecorder = (metrics, options = defaultOptions$1) => {
292
296
  const defaultedRecorderOptions = merge__default["default"](defaultOptions$1, options);
293
297
  const shouldSkipMetricsByEnvironment = skipMetricsInEnvironment(defaultedRecorderOptions);
294
- return (start, recordingOptions) => {
295
- const {
296
- durationS
297
- } = endMeasurementFrom(start);
298
+ return (timing, recordingOptions) => {
299
+ const durationS = isTiming(timing) ? timing.end().value().seconds : endMeasurementFrom(timing).durationS;
298
300
  const labels = sortLabels(recordingOptions.labels);
299
301
 
300
- if (!shouldSkipMetricsByEnvironment) {
302
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
301
303
  var _metrics$httpRequestD;
302
304
 
303
305
  (_metrics$httpRequestD = metrics.httpRequestDurationInSeconds) === null || _metrics$httpRequestD === void 0 ? void 0 : _metrics$httpRequestD.forEach(httpRequestDurationInSecondsMetricType => {
@@ -305,7 +307,7 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
305
307
  });
306
308
  }
307
309
 
308
- if (!shouldSkipMetricsByEnvironment) {
310
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
309
311
  var _metrics$httpRequestD2;
310
312
 
311
313
  (_metrics$httpRequestD2 = metrics.httpRequestDurationPerPercentileInSeconds) === null || _metrics$httpRequestD2 === void 0 ? void 0 : _metrics$httpRequestD2.forEach(httpRequestDurationPerPercentileInSecondsMetricType => {
@@ -313,7 +315,7 @@ const createRequestRecorder = (metrics, options = defaultOptions$1) => {
313
315
  });
314
316
  }
315
317
 
316
- if (!shouldSkipMetricsByEnvironment) {
318
+ if (!shouldSkipMetricsByEnvironment && durationS !== undefined) {
317
319
  var _metrics$httpRequests;
318
320
 
319
321
  (_metrics$httpRequests = metrics.httpRequestsTotal) === null || _metrics$httpRequests === void 0 ? void 0 : _metrics$httpRequests.forEach(httpRequestsTotalMetricType => {
@@ -394,6 +396,125 @@ const defaultNormalizers = {
394
396
  normalizeMethod
395
397
  };
396
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
+
397
518
  exports.Prometheus = Prometheus__namespace;
398
519
  exports.createGcMetrics = createGcMetrics;
399
520
  exports.createGcObserver = createGcObserver;
@@ -411,3 +532,4 @@ exports.normalizePath = normalizePath;
411
532
  exports.normalizeStatusCode = normalizeStatusCode;
412
533
  exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
413
534
  exports.sortLabels = sortLabels;
535
+ exports.timing = timing;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promster/metrics",
3
- "version": "9.0.0",
3
+ "version": "9.1.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",
@@ -46,7 +46,7 @@
46
46
  "url-value-parser": "2.0.3"
47
47
  },
48
48
  "devDependencies": {
49
- "@promster/types": "^3.2.0",
49
+ "@promster/types": "^3.2.1",
50
50
  "@types/lodash": "4.14.176",
51
51
  "prom-client": "14.0.1",
52
52
  "typescript": "4.4.4"