@emartech/aggregate-metric-logger 3.2.0 → 3.3.1

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.
@@ -0,0 +1,73 @@
1
+ name: Aggregate Metric Logger Build
2
+
3
+ on: push
4
+
5
+ env:
6
+ NPM_TOKEN: ${{ secrets.NPM_PUBLISHER_TOKEN }}
7
+ GH_TOKEN: ${{ secrets.GH_TOKEN }}
8
+ SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
9
+
10
+ jobs:
11
+ test:
12
+ name: Tests 🚦
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - name: Check out repository
16
+ uses: actions/checkout@v3
17
+ - name: Use correct Node version
18
+ uses: actions/setup-node@v3
19
+ with:
20
+ node-version-file: '.nvmrc'
21
+ - name: Cache node_modules
22
+ id: cache_node_modules
23
+ uses: actions/cache@v3
24
+ with:
25
+ path: node_modules
26
+ key: node_modules-${{ hashFiles('**/package-lock.json') }}
27
+ - name: Install dependencies
28
+ if: ${{ steps.cache_node_modules.outputs.cache-hit != 'true' }}
29
+ continue-on-error: true
30
+ run: |
31
+ npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
32
+ npm i
33
+ - name: Run tests
34
+ run: npm test
35
+ - name: Slack notification
36
+ uses: 8398a7/action-slack@a189acbf0b7ea434558662ae25a0de71df69a435
37
+ if: ${{ failure() }}
38
+ with:
39
+ status: ${{ job.status }}
40
+ fields: repo,message,author,job,ref
41
+ job_name: Test 🚦
42
+ env:
43
+ SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
44
+
45
+ deploy:
46
+ name: Deploy 🚢
47
+ needs: [test]
48
+ runs-on: ubuntu-latest
49
+ steps:
50
+ - name: Check out repository
51
+ uses: actions/checkout@v3
52
+ - name: Use correct Node version
53
+ uses: actions/setup-node@v3
54
+ with:
55
+ node-version-file: '.nvmrc'
56
+ - name: Cache node_modules
57
+ id: cache_node_modules
58
+ uses: actions/cache@v3
59
+ with:
60
+ path: node_modules
61
+ key: node_modules-${{ hashFiles('**/package-lock.json') }}
62
+ - name: Release package
63
+ if: github.ref_name == 'master'
64
+ run: CI=true npm run semantic-release
65
+ - name: Slack notification
66
+ uses: 8398a7/action-slack@a189acbf0b7ea434558662ae25a0de71df69a435
67
+ with:
68
+ status: ${{ job.status }}
69
+ fields: repo,message,author,job,ref
70
+ job_name: Deploy 🚢
71
+ env:
72
+ SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
73
+ if: always()
package/README.md CHANGED
@@ -34,7 +34,7 @@ const metricLogger = require('@emartech/aggregate-metric-logger')
34
34
  metricLogger.warn('etwas-went-wrong', { customer_id: 11 });
35
35
  metricLogger.warn('etwas-went-wrong', { customer_id: 11 });
36
36
 
37
- // will aggrete these to a single warning log
37
+ // will aggregate these to a single warning log
38
38
  ```
39
39
 
40
40
  ### Measure values
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.1",
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;