@promster/metrics 9.0.0 → 9.1.3

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,17 @@
1
+ declare class Timing {
2
+ #private;
3
+ static NS_PER_SEC: bigint;
4
+ constructor();
5
+ value(): {
6
+ seconds: undefined;
7
+ } | {
8
+ seconds: number;
9
+ };
10
+ reset(): this;
11
+ end(): this;
12
+ }
13
+ declare const timing: {
14
+ start(): Timing;
15
+ };
16
+ export default timing;
17
+ 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,46 @@ const defaultNormalizers = {
394
396
  normalizeMethod
395
397
  };
396
398
 
399
+ class Timing {
400
+ static NS_PER_SEC = BigInt(1e9);
401
+ #startTime;
402
+ #endTime;
403
+
404
+ constructor() {
405
+ this.reset();
406
+ }
407
+
408
+ value() {
409
+ const startTime = this.#startTime;
410
+ const endTime = this.#endTime;
411
+ if (!endTime || !startTime) return {
412
+ seconds: undefined
413
+ };
414
+ return {
415
+ seconds: Number((endTime - startTime) / Timing.NS_PER_SEC)
416
+ };
417
+ }
418
+
419
+ reset() {
420
+ this.#startTime = process.hrtime.bigint();
421
+ this.#endTime = undefined;
422
+ return this;
423
+ }
424
+
425
+ end() {
426
+ this.#endTime = process.hrtime.bigint();
427
+ return this;
428
+ }
429
+
430
+ }
431
+
432
+ const timing = {
433
+ start() {
434
+ return new Timing();
435
+ }
436
+
437
+ };
438
+
397
439
  exports.Prometheus = Prometheus__namespace;
398
440
  exports.createGcMetrics = createGcMetrics;
399
441
  exports.createGcObserver = createGcObserver;
@@ -411,3 +453,4 @@ exports.normalizePath = normalizePath;
411
453
  exports.normalizeStatusCode = normalizeStatusCode;
412
454
  exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
413
455
  exports.sortLabels = sortLabels;
456
+ 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,46 @@ const defaultNormalizers = {
394
396
  normalizeMethod
395
397
  };
396
398
 
399
+ class Timing {
400
+ static NS_PER_SEC = BigInt(1e9);
401
+ #startTime;
402
+ #endTime;
403
+
404
+ constructor() {
405
+ this.reset();
406
+ }
407
+
408
+ value() {
409
+ const startTime = this.#startTime;
410
+ const endTime = this.#endTime;
411
+ if (!endTime || !startTime) return {
412
+ seconds: undefined
413
+ };
414
+ return {
415
+ seconds: Number((endTime - startTime) / Timing.NS_PER_SEC)
416
+ };
417
+ }
418
+
419
+ reset() {
420
+ this.#startTime = process.hrtime.bigint();
421
+ this.#endTime = undefined;
422
+ return this;
423
+ }
424
+
425
+ end() {
426
+ this.#endTime = process.hrtime.bigint();
427
+ return this;
428
+ }
429
+
430
+ }
431
+
432
+ const timing = {
433
+ start() {
434
+ return new Timing();
435
+ }
436
+
437
+ };
438
+
397
439
  exports.Prometheus = Prometheus__namespace;
398
440
  exports.createGcMetrics = createGcMetrics;
399
441
  exports.createGcObserver = createGcObserver;
@@ -411,3 +453,4 @@ exports.normalizePath = normalizePath;
411
453
  exports.normalizeStatusCode = normalizeStatusCode;
412
454
  exports.skipMetricsInEnvironment = skipMetricsInEnvironment;
413
455
  exports.sortLabels = sortLabels;
456
+ 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.3",
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",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "repository": {
23
23
  "type": "git",
24
- "url": "https://github.com/tdeekens/flopflip.git",
24
+ "url": "https://github.com/tdeekens/promster.git",
25
25
  "directory": "packages/metrics"
26
26
  },
27
27
  "author": "Tobias Deekens <nerd@tdeekens.name>",
@@ -40,19 +40,18 @@
40
40
  "lodash.once": "4.1.1",
41
41
  "merge-options": "3.0.4",
42
42
  "optional": "0.1.4",
43
- "ts-essentials": "9.0.0",
43
+ "ts-essentials": "9.1.2",
44
44
  "tslib": "2.3.1",
45
- "url": "0.11.0",
46
- "url-value-parser": "2.0.3"
45
+ "url-value-parser": "2.1.0"
47
46
  },
48
47
  "devDependencies": {
49
- "@promster/types": "^3.2.0",
50
- "@types/lodash": "4.14.176",
48
+ "@promster/types": "^3.2.3",
49
+ "@types/lodash": "4.14.178",
51
50
  "prom-client": "14.0.1",
52
- "typescript": "4.4.4"
51
+ "typescript": "4.5.5"
53
52
  },
54
53
  "optionalDependencies": {
55
- "@sematext/gc-stats": "1.5.5"
54
+ "@sematext/gc-stats": "1.5.6"
56
55
  },
57
56
  "peerDependencies": {
58
57
  "prom-client": "13.x.x || 14.x"