Skip to content

Commit 24c247c

Browse files
authoredJun 23, 2023
fix(metrics): flush metrics when data points array reaches max size (#1548)
* test: added failing test * fix(metrics): flush metrics when values array reaches max size
1 parent fe66031 commit 24c247c

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
 

‎packages/metrics/src/Metrics.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
MAX_METRICS_SIZE,
88
DEFAULT_NAMESPACE,
99
COLD_START_METRIC,
10+
MAX_METRIC_VALUES_SIZE,
1011
} from './constants';
1112
import {
1213
MetricsOptions,
@@ -662,7 +663,10 @@ class Metrics extends Utility implements MetricsInterface {
662663
}
663664

664665
/**
665-
* Stores a metric in the buffer
666+
* Stores a metric in the buffer.
667+
*
668+
* If the buffer is full, or the metric reaches the maximum number of values,
669+
* the buffer is published to stdout.
666670
*
667671
* @param name The name of the metric to store
668672
* @param unit The unit of the metric to store
@@ -692,6 +696,9 @@ class Metrics extends Utility implements MetricsInterface {
692696
storedMetric.value = [storedMetric.value];
693697
}
694698
storedMetric.value.push(value);
699+
if (storedMetric.value.length === MAX_METRIC_VALUES_SIZE) {
700+
this.publishStoredMetrics();
701+
}
695702
}
696703
}
697704
}

‎packages/metrics/src/constants.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export const COLD_START_METRIC = 'ColdStart';
2-
export const DEFAULT_NAMESPACE = 'default_namespace';
3-
export const MAX_METRICS_SIZE = 100;
4-
export const MAX_DIMENSION_COUNT = 29;
1+
const COLD_START_METRIC = 'ColdStart';
2+
const DEFAULT_NAMESPACE = 'default_namespace';
3+
const MAX_METRICS_SIZE = 100;
4+
const MAX_METRIC_VALUES_SIZE = 100;
5+
const MAX_DIMENSION_COUNT = 29;
6+
7+
export {
8+
COLD_START_METRIC,
9+
DEFAULT_NAMESPACE,
10+
MAX_METRICS_SIZE,
11+
MAX_METRIC_VALUES_SIZE,
12+
MAX_DIMENSION_COUNT,
13+
};

‎packages/metrics/tests/unit/Metrics.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
DEFAULT_NAMESPACE,
1717
MAX_DIMENSION_COUNT,
1818
MAX_METRICS_SIZE,
19+
MAX_METRIC_VALUES_SIZE,
1920
} from '../../src/constants';
2021
import { setupDecoratorLambdaHandler } from '../helpers/metricsUtils';
2122
import {
@@ -701,6 +702,34 @@ describe('Class: Metrics', () => {
701702
);
702703
});
703704

705+
test('it should publish metrics when the array of values reaches the maximum size', () => {
706+
// Prepare
707+
const metrics: Metrics = new Metrics({ namespace: TEST_NAMESPACE });
708+
const consoleSpy = jest.spyOn(console, 'log');
709+
const metricName = 'test-metric';
710+
711+
// Act
712+
for (let i = 0; i <= MAX_METRIC_VALUES_SIZE; i++) {
713+
metrics.addMetric(`${metricName}`, MetricUnits.Count, i);
714+
}
715+
metrics.publishStoredMetrics();
716+
717+
// Assess
718+
// 2 calls to console.log: 1 for the first batch of metrics, 1 for the second batch (explicit call)
719+
expect(consoleSpy).toHaveBeenCalledTimes(2);
720+
const firstMetricsJson = JSON.parse(
721+
consoleSpy.mock.calls[0][0]
722+
) as EmfOutput;
723+
const secondMetricsJson = JSON.parse(
724+
consoleSpy.mock.calls[1][0]
725+
) as EmfOutput;
726+
727+
// The first batch of values should be an array of size MAX_METRIC_VALUES_SIZE
728+
expect(firstMetricsJson[metricName]).toHaveLength(MAX_METRIC_VALUES_SIZE);
729+
// The second should be a single value (the last value added, which is 100 given we start from 0)
730+
expect(secondMetricsJson[metricName]).toEqual(100);
731+
});
732+
704733
test('it should not publish metrics if stored metrics count has not reached max metric size threshold', () => {
705734
// Prepare
706735
const metrics: Metrics = new Metrics({ namespace: TEST_NAMESPACE });

0 commit comments

Comments
 (0)
Please sign in to comment.