@emartech/aggregate-metric-logger 6.7.1 → 6.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emartech/aggregate-metric-logger",
3
- "version": "6.7.1",
3
+ "version": "6.7.3",
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,47 @@ 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
+ expect(MetricServiceClient.prototype.createTimeSeries).toHaveBeenCalledTimes(1);
555
+
556
+ firstCreateTimeSeriesPromise.resolve();
557
+ await flushPromises();
558
+ });
559
+
560
+ it('should publish the second count after the first flush resolves', async () => {
561
+ const firstCreateTimeSeriesPromise = resolvablePromise();
562
+ jest.spyOn(MetricServiceClient.prototype, 'createTimeSeries')
563
+ .mockReturnValueOnce(firstCreateTimeSeriesPromise)
564
+ .mockResolvedValue();
565
+
566
+ gcpMetricCollector.count('jolo', {}, 1);
567
+ jest.advanceTimersByTime(60 * 1000 + 1);
568
+
569
+ gcpMetricCollector.count('jolo', {}, 1);
570
+ jest.advanceTimersByTime(60 * 1000 + 1);
571
+
572
+ firstCreateTimeSeriesPromise.resolve();
573
+ await flushPromises();
574
+
575
+ jest.advanceTimersByTime(60 * 1000 + 1);
576
+ await flushPromises();
577
+
578
+ expect(MetricServiceClient.prototype.createTimeSeries).toHaveBeenCalledTimes(2);
579
+ });
580
+ });
581
+
541
582
  describe('shutdown', () => {
542
583
  it('should flush pending metrics immediately', async () => {
543
584
  stubCreateTimeSeries();