@emartech/aggregate-metric-logger 6.7.0 → 6.7.2

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": "6.7.0",
3
+ "version": "6.7.2",
4
4
  "description": "aggregate metrics in memory for logging",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -19,6 +19,7 @@ class GcpMetricCollector {
19
19
  initialized = false;
20
20
  timeoutId = null;
21
21
  projectId = null;
22
+ flushInProgress = false;
22
23
 
23
24
  constructor(projectId = null) {
24
25
  this.projectId = projectId ?? process.env.MONITORING_PROJECT_ID;
@@ -106,11 +107,17 @@ class GcpMetricCollector {
106
107
  }
107
108
 
108
109
  async publishPending() {
109
- const timeSeries = createGcpTimeSeriesBuilder(this.projectId).buildTimeSeries(this.metrics);
110
- for (const [, metric] of Object.entries(this.metrics)) {
111
- metric.aggregator.advanceTimer();
110
+ if (this.flushInProgress) return;
111
+ this.flushInProgress = true;
112
+ try {
113
+ const timeSeries = createGcpTimeSeriesBuilder(this.projectId).buildTimeSeries(this.metrics);
114
+ for (const [, metric] of Object.entries(this.metrics)) {
115
+ metric.aggregator.advanceTimer();
116
+ }
117
+ await createGcpTimeSeriesPublisher(this.projectId).publishTimeSeries(timeSeries);
118
+ } finally {
119
+ this.flushInProgress = false;
112
120
  }
113
- await createGcpTimeSeriesPublisher(this.projectId).publishTimeSeries(timeSeries);
114
121
  }
115
122
  }
116
123
 
@@ -538,6 +538,26 @@ describe('gcpMetricCollector', () => {
538
538
  });
539
539
  });
540
540
 
541
+ describe('concurrent flush', () => {
542
+ it('should not publish concurrently if a flush is already in flight', async () => {
543
+ const firstCreateTimeSeriesPromise = resolvablePromise();
544
+ jest.spyOn(MetricServiceClient.prototype, 'createTimeSeries')
545
+ .mockReturnValueOnce(firstCreateTimeSeriesPromise)
546
+ .mockResolvedValue();
547
+
548
+ gcpMetricCollector.count('jolo', {}, 1);
549
+ jest.advanceTimersByTime(60 * 1000 + 1);
550
+
551
+ gcpMetricCollector.count('jolo', {}, 1);
552
+ jest.advanceTimersByTime(60 * 1000 + 1);
553
+
554
+ firstCreateTimeSeriesPromise.resolve();
555
+ await flushPromises();
556
+
557
+ expect(MetricServiceClient.prototype.createTimeSeries).toHaveBeenCalledTimes(1);
558
+ });
559
+ });
560
+
541
561
  describe('shutdown', () => {
542
562
  it('should flush pending metrics immediately', async () => {
543
563
  stubCreateTimeSeries();
@@ -2,7 +2,7 @@
2
2
 
3
3
  exports.createCounter = () => {
4
4
  let count = 0;
5
- const startTime = Date.now() - 1000;
5
+ let startTime = Date.now() - 1000;
6
6
 
7
7
  return {
8
8
  getState: () => ({
@@ -13,6 +13,8 @@ exports.createCounter = () => {
13
13
  value: { int64Value: `${count}` }
14
14
  }),
15
15
  add: value => (count += value),
16
- advanceTimer: () => {}
16
+ advanceTimer: () => {
17
+ startTime = Date.now();
18
+ }
17
19
  };
18
20
  };
@@ -43,11 +43,23 @@ describe('count', () => {
43
43
 
44
44
  expect(counter.getState()).toMatchObject({
45
45
  value: { int64Value: '3' },
46
- startTime: 7880,
46
+ startTime: 8900,
47
47
  endTime: 8900
48
48
  });
49
49
  });
50
50
 
51
+ it('should advance startTime on advanceTimer so successive GCP writes are in order', () => {
52
+ jest.useFakeTimers().setSystemTime(new Date(8880));
53
+ const counter = createCounter();
54
+
55
+ const firstStartTime = counter.getState().startTime;
56
+ jest.advanceTimersByTime(60000);
57
+ counter.advanceTimer();
58
+ const secondStartTime = counter.getState().startTime;
59
+
60
+ expect(secondStartTime).toBeGreaterThan(firstStartTime);
61
+ });
62
+
51
63
  it('should return current time as endTime', () => {
52
64
  jest.useFakeTimers().setSystemTime(new Date(880));
53
65
  const counter = createCounter();