@tramvai/module-metrics 7.27.0 → 7.27.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,3 +1,3 @@
1
1
  import type { Metrics } from '@tramvai/tokens-metrics';
2
- export declare const eventLoopMetrics: (metrics: Metrics) => void;
2
+ export declare const eventLoopMetrics: (metrics: Metrics) => () => void;
3
3
  //# sourceMappingURL=eventLoop.d.ts.map
@@ -2,6 +2,7 @@ const NODEJS_EVENTLOOP_LAG = 'nodejs_eventloop_setinterval_lag_seconds';
2
2
  const RESOLUTION = 100;
3
3
  function startEventLoopLagMeasure(histogram) {
4
4
  let start = process.hrtime();
5
+ let currentTimer;
5
6
  const measure = () => {
6
7
  const delta = process.hrtime(start);
7
8
  const nanosec = delta[0] * 1e9 + delta[1];
@@ -9,9 +10,12 @@ function startEventLoopLagMeasure(histogram) {
9
10
  const lag = ms - RESOLUTION;
10
11
  histogram.observe(lag / 1e3);
11
12
  start = process.hrtime();
12
- setTimeout(measure, RESOLUTION).unref();
13
+ currentTimer = setTimeout(measure, RESOLUTION).unref();
14
+ };
15
+ currentTimer = setTimeout(measure, RESOLUTION).unref();
16
+ return () => {
17
+ clearTimeout(currentTimer);
13
18
  };
14
- setTimeout(measure, RESOLUTION).unref();
15
19
  }
16
20
  const eventLoopMetrics = (metrics) => {
17
21
  const histogram = metrics.histogram({
@@ -19,7 +23,7 @@ const eventLoopMetrics = (metrics) => {
19
23
  help: 'Lag of event loop in seconds (setTimeout based).',
20
24
  buckets: [0.01, 0.02, 0.1, 0.2, 0.5, 1, 3, 5, 7.5, 10],
21
25
  });
22
- startEventLoopLagMeasure(histogram);
26
+ return startEventLoopLagMeasure(histogram);
23
27
  };
24
28
 
25
29
  export { eventLoopMetrics };
@@ -6,6 +6,7 @@ const NODEJS_EVENTLOOP_LAG = 'nodejs_eventloop_setinterval_lag_seconds';
6
6
  const RESOLUTION = 100;
7
7
  function startEventLoopLagMeasure(histogram) {
8
8
  let start = process.hrtime();
9
+ let currentTimer;
9
10
  const measure = () => {
10
11
  const delta = process.hrtime(start);
11
12
  const nanosec = delta[0] * 1e9 + delta[1];
@@ -13,9 +14,12 @@ function startEventLoopLagMeasure(histogram) {
13
14
  const lag = ms - RESOLUTION;
14
15
  histogram.observe(lag / 1e3);
15
16
  start = process.hrtime();
16
- setTimeout(measure, RESOLUTION).unref();
17
+ currentTimer = setTimeout(measure, RESOLUTION).unref();
18
+ };
19
+ currentTimer = setTimeout(measure, RESOLUTION).unref();
20
+ return () => {
21
+ clearTimeout(currentTimer);
17
22
  };
18
- setTimeout(measure, RESOLUTION).unref();
19
23
  }
20
24
  const eventLoopMetrics = (metrics) => {
21
25
  const histogram = metrics.histogram({
@@ -23,7 +27,7 @@ const eventLoopMetrics = (metrics) => {
23
27
  help: 'Lag of event loop in seconds (setTimeout based).',
24
28
  buckets: [0.01, 0.02, 0.1, 0.2, 0.5, 1, 3, 5, 7.5, 10],
25
29
  });
26
- startEventLoopLagMeasure(histogram);
30
+ return startEventLoopLagMeasure(histogram);
27
31
  };
28
32
 
29
33
  exports.eventLoopMetrics = eventLoopMetrics;
@@ -1,8 +1,12 @@
1
1
  import monkeypatch from '@tinkoff/monkeypatch';
2
+ import { TRAMVAI_INITED_SYMBOL } from '@tramvai/core';
2
3
  import { DEFAULT_BUCKETS } from '../constants.es.js';
3
4
  import { addMetricsForFetch, initConnectionResolveMetrics } from './createRequestWithMetrics.es.js';
4
5
 
5
6
  const initRequestsMetrics = ({ metrics, getServiceName, http, https, createRequestWithMetrics, config, }) => {
7
+ if (globalThis[TRAMVAI_INITED_SYMBOL]) {
8
+ return;
9
+ }
6
10
  const metricsInstances = {
7
11
  requestsTotal: metrics.counter({
8
12
  name: 'http_sent_requests_total',
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var monkeypatch = require('@tinkoff/monkeypatch');
6
+ var core = require('@tramvai/core');
6
7
  var constants = require('../constants.js');
7
8
  var createRequestWithMetrics = require('./createRequestWithMetrics.js');
8
9
 
@@ -11,6 +12,9 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
11
12
  var monkeypatch__default = /*#__PURE__*/_interopDefaultLegacy(monkeypatch);
12
13
 
13
14
  const initRequestsMetrics = ({ metrics, getServiceName, http, https, createRequestWithMetrics: createRequestWithMetrics$1, config, }) => {
15
+ if (globalThis[core.TRAMVAI_INITED_SYMBOL]) {
16
+ return;
17
+ }
14
18
  const metricsInstances = {
15
19
  requestsTotal: metrics.counter({
16
20
  name: 'http_sent_requests_total',
package/lib/server.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { __decorate } from 'tslib';
2
- import { Module, provide, Scope, commandLineListTokens } from '@tramvai/core';
2
+ import { Module, provide, Scope, TRAMVAI_INITED_SYMBOL, commandLineListTokens, DI_TOKEN } from '@tramvai/core';
3
3
  import { COMMAND_LINE_EXECUTION_END_TOKEN } from '@tramvai/tokens-core-private';
4
4
  import { UTILITY_SERVER_PATHS } from '@tramvai/tokens-server';
5
5
  import { WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, UTILITY_WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_METRICS_TOKEN, WEB_FASTIFY_APP_TOKEN, PAPI_METRICS_TOKEN } from '@tramvai/tokens-server-private';
@@ -38,7 +38,9 @@ MetricsModule = __decorate([
38
38
  provide({
39
39
  provide: METRICS_MODULE_TOKEN,
40
40
  useFactory: ({ registry }) => {
41
- collectDefaultMetrics({ register: registry });
41
+ if (!globalThis[TRAMVAI_INITED_SYMBOL]) {
42
+ collectDefaultMetrics({ register: registry });
43
+ }
42
44
  return {
43
45
  counter: (opt) => new Counter({ registers: [registry], ...opt }),
44
46
  gauge: (opt) => new Gauge({ registers: [registry], ...opt }),
@@ -115,13 +117,18 @@ MetricsModule = __decorate([
115
117
  }),
116
118
  provide({
117
119
  provide: commandLineListTokens.listen,
118
- useFactory: ({ metrics }) => {
120
+ useFactory: ({ metrics, di }) => {
119
121
  return () => {
120
- eventLoopMetrics(metrics);
122
+ const clearEventLoopMetrics = eventLoopMetrics(metrics);
123
+ di.register({
124
+ provide: commandLineListTokens.close,
125
+ useValue: clearEventLoopMetrics,
126
+ });
121
127
  };
122
128
  },
123
129
  deps: {
124
130
  metrics: METRICS_MODULE_TOKEN,
131
+ di: DI_TOKEN,
125
132
  },
126
133
  multi: true,
127
134
  }),
package/lib/server.js CHANGED
@@ -44,7 +44,9 @@ exports.MetricsModule = tslib.__decorate([
44
44
  core.provide({
45
45
  provide: tokensMetrics.METRICS_MODULE_TOKEN,
46
46
  useFactory: ({ registry }) => {
47
- promClient.collectDefaultMetrics({ register: registry });
47
+ if (!globalThis[core.TRAMVAI_INITED_SYMBOL]) {
48
+ promClient.collectDefaultMetrics({ register: registry });
49
+ }
48
50
  return {
49
51
  counter: (opt) => new promClient.Counter({ registers: [registry], ...opt }),
50
52
  gauge: (opt) => new promClient.Gauge({ registers: [registry], ...opt }),
@@ -121,13 +123,18 @@ exports.MetricsModule = tslib.__decorate([
121
123
  }),
122
124
  core.provide({
123
125
  provide: core.commandLineListTokens.listen,
124
- useFactory: ({ metrics }) => {
126
+ useFactory: ({ metrics, di }) => {
125
127
  return () => {
126
- eventLoop.eventLoopMetrics(metrics);
128
+ const clearEventLoopMetrics = eventLoop.eventLoopMetrics(metrics);
129
+ di.register({
130
+ provide: core.commandLineListTokens.close,
131
+ useValue: clearEventLoopMetrics,
132
+ });
127
133
  };
128
134
  },
129
135
  deps: {
130
136
  metrics: tokensMetrics.METRICS_MODULE_TOKEN,
137
+ di: core.DI_TOKEN,
131
138
  },
132
139
  multi: true,
133
140
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-metrics",
3
- "version": "7.27.0",
3
+ "version": "7.27.3",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -25,16 +25,16 @@
25
25
  "@tinkoff/monkeypatch": "7.0.1",
26
26
  "@tinkoff/url": "0.13.1",
27
27
  "@tinkoff/utils": "^2.1.2",
28
- "@tramvai/core": "7.27.0",
29
- "@tramvai/papi": "7.27.0",
30
- "@tramvai/state": "7.27.0",
31
- "@tramvai/tokens-common": "7.27.0",
32
- "@tramvai/tokens-core-private": "7.27.0",
33
- "@tramvai/tokens-http-client": "7.27.0",
34
- "@tramvai/tokens-metrics": "7.27.0",
35
- "@tramvai/tokens-router": "7.27.0",
36
- "@tramvai/tokens-server": "7.27.0",
37
- "@tramvai/tokens-server-private": "7.27.0",
28
+ "@tramvai/core": "7.27.3",
29
+ "@tramvai/papi": "7.27.3",
30
+ "@tramvai/state": "7.27.3",
31
+ "@tramvai/tokens-common": "7.27.3",
32
+ "@tramvai/tokens-core-private": "7.27.3",
33
+ "@tramvai/tokens-http-client": "7.27.3",
34
+ "@tramvai/tokens-metrics": "7.27.3",
35
+ "@tramvai/tokens-router": "7.27.3",
36
+ "@tramvai/tokens-server": "7.27.3",
37
+ "@tramvai/tokens-server-private": "7.27.3",
38
38
  "prom-client": "^14.2.0",
39
39
  "undici": "^7.24.4"
40
40
  },