@emartech/aggregate-metric-logger 3.2.0 → 3.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emartech/aggregate-metric-logger",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "aggregate metrics in memory for logging",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -16,6 +16,16 @@ exports.count = (metricName, labels = {}, value = 1) =>
16
16
  exports.measure = (metricName, labels = {}, value = 1) =>
17
17
  addToMetric(metricName, labels, 'distribution', value);
18
18
 
19
+ exports.start = (metricName, labels = {}) => ({ metricName, labels, start: Date.now() });
20
+
21
+ exports.stop = measurement =>
22
+ addToMetric(
23
+ measurement.metricName,
24
+ measurement.labels,
25
+ 'distribution',
26
+ Date.now() - measurement.start
27
+ );
28
+
19
29
  const addToMetric = (metricName, labels = {}, type, value) => {
20
30
  if (process.env.METRIC_LOGGER_ENABLED !== 'true') return;
21
31
 
@@ -239,4 +239,45 @@ describe('gcpMetricCollector', () => {
239
239
  expect(getPublishedMetrics()).toEqual([]);
240
240
  });
241
241
  });
242
+
243
+ describe('start', () => {
244
+ it('should return a measurement object with the given tag, params and start time', () => {
245
+ const fakeCurrentTimestamp = 2323434543;
246
+ jest.useFakeTimers().setSystemTime(new Date(fakeCurrentTimestamp));
247
+
248
+ const measurement = gcpMetricCollector.start('pulyka', { foo: 'bar' });
249
+
250
+ expect(measurement).toEqual({
251
+ metricName: 'pulyka',
252
+ labels: { foo: 'bar' },
253
+ start: fakeCurrentTimestamp
254
+ });
255
+ });
256
+ });
257
+
258
+ describe('stop', () => {
259
+ it('should send in time since start as a distribution measurement', () => {
260
+ jest.useFakeTimers().setSystemTime(new Date(20000));
261
+ stubCreateTimeSeries();
262
+
263
+ const measurement = gcpMetricCollector.start('elephant', { size: 23 });
264
+ jest.advanceTimersByTime(45);
265
+
266
+ gcpMetricCollector.stop(measurement);
267
+
268
+ jest.advanceTimersByTime(60 * 1000 + 1);
269
+ expect(getPublishedMetrics()).toMatchObject([
270
+ [
271
+ {
272
+ value: {
273
+ distributionValue: {
274
+ count: 1,
275
+ mean: 45
276
+ }
277
+ }
278
+ }
279
+ ]
280
+ ]);
281
+ });
282
+ });
242
283
  });
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
- const { createLogger } = require('@emartech/json-logger');
3
+ const logger = require('@emartech/json-logger').createLogger('aggregate-metric-logger');
4
4
  const monitoring = require('@google-cloud/monitoring');
5
5
  const { chunk } = require('lodash');
6
6
  const ms = require('ms');
7
+ const { setTimeout } = require('timers/promises');
7
8
 
8
- const logger = createLogger('aggregate-metric-logger');
9
9
  const MAX_TIME_SERIES_COUNT = 200;
10
10
 
11
11
  exports.publishTimeSeries = async timeSeries => {
@@ -24,13 +24,11 @@ const createTimeSeries = async (client, timeSeries) => {
24
24
  try {
25
25
  await client.createTimeSeries({ name, timeSeries });
26
26
  } catch (error) {
27
- await sleep(ms('10 sec'));
27
+ await setTimeout(ms('10 sec'));
28
28
  await client.createTimeSeries({ name, timeSeries });
29
29
  }
30
30
  };
31
31
 
32
- const sleep = async sleepTime => new Promise(resolve => setTimeout(resolve, sleepTime));
33
-
34
32
  let monitoringClientInstance = null;
35
33
  const getMonitoringClient = () => {
36
34
  if (!monitoringClientInstance) {
package/src/index.js CHANGED
@@ -21,9 +21,10 @@ exposedFactoryInstanceHybrid.error = exposedInstance.error;
21
21
  exposedFactoryInstanceHybrid.fatal = exposedInstance.fatal;
22
22
  exposedFactoryInstanceHybrid.start = exposedInstance.start;
23
23
  exposedFactoryInstanceHybrid.stop = exposedInstance.stop;
24
- exposedFactoryInstanceHybrid.cancel = exposedInstance.cancel;
25
24
 
26
25
  exposedFactoryInstanceHybrid.count = gcpMetricCollector.count;
27
26
  exposedFactoryInstanceHybrid.measureGcp = gcpMetricCollector.measure;
27
+ exposedFactoryInstanceHybrid.startGcp = gcpMetricCollector.start;
28
+ exposedFactoryInstanceHybrid.stopGcp = gcpMetricCollector.stop;
28
29
 
29
30
  module.exports = exposedFactoryInstanceHybrid;