Skip to content

fix(metrics): flush metrics when data points array reaches max size #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MAX_METRICS_SIZE,
DEFAULT_NAMESPACE,
COLD_START_METRIC,
MAX_METRIC_VALUES_SIZE,
} from './constants';
import {
MetricsOptions,
Expand Down Expand Up @@ -662,7 +663,10 @@ class Metrics extends Utility implements MetricsInterface {
}

/**
* Stores a metric in the buffer
* Stores a metric in the buffer.
*
* If the buffer is full, or the metric reaches the maximum number of values,
* the buffer is published to stdout.
*
* @param name The name of the metric to store
* @param unit The unit of the metric to store
Expand Down Expand Up @@ -692,6 +696,9 @@ class Metrics extends Utility implements MetricsInterface {
storedMetric.value = [storedMetric.value];
}
storedMetric.value.push(value);
if (storedMetric.value.length === MAX_METRIC_VALUES_SIZE) {
this.publishStoredMetrics();
}
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions packages/metrics/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export const COLD_START_METRIC = 'ColdStart';
export const DEFAULT_NAMESPACE = 'default_namespace';
export const MAX_METRICS_SIZE = 100;
export const MAX_DIMENSION_COUNT = 29;
const COLD_START_METRIC = 'ColdStart';
const DEFAULT_NAMESPACE = 'default_namespace';
const MAX_METRICS_SIZE = 100;
const MAX_METRIC_VALUES_SIZE = 100;
const MAX_DIMENSION_COUNT = 29;

export {
COLD_START_METRIC,
DEFAULT_NAMESPACE,
MAX_METRICS_SIZE,
MAX_METRIC_VALUES_SIZE,
MAX_DIMENSION_COUNT,
};
29 changes: 29 additions & 0 deletions packages/metrics/tests/unit/Metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DEFAULT_NAMESPACE,
MAX_DIMENSION_COUNT,
MAX_METRICS_SIZE,
MAX_METRIC_VALUES_SIZE,
} from '../../src/constants';
import { setupDecoratorLambdaHandler } from '../helpers/metricsUtils';
import {
Expand Down Expand Up @@ -701,6 +702,34 @@ describe('Class: Metrics', () => {
);
});

test('it should publish metrics when the array of values reaches the maximum size', () => {
// Prepare
const metrics: Metrics = new Metrics({ namespace: TEST_NAMESPACE });
const consoleSpy = jest.spyOn(console, 'log');
const metricName = 'test-metric';

// Act
for (let i = 0; i <= MAX_METRIC_VALUES_SIZE; i++) {
metrics.addMetric(`${metricName}`, MetricUnits.Count, i);
}
metrics.publishStoredMetrics();

// Assess
// 2 calls to console.log: 1 for the first batch of metrics, 1 for the second batch (explicit call)
expect(consoleSpy).toHaveBeenCalledTimes(2);
const firstMetricsJson = JSON.parse(
consoleSpy.mock.calls[0][0]
) as EmfOutput;
const secondMetricsJson = JSON.parse(
consoleSpy.mock.calls[1][0]
) as EmfOutput;

// The first batch of values should be an array of size MAX_METRIC_VALUES_SIZE
expect(firstMetricsJson[metricName]).toHaveLength(MAX_METRIC_VALUES_SIZE);
// The second should be a single value (the last value added, which is 100 given we start from 0)
expect(secondMetricsJson[metricName]).toEqual(100);
});

test('it should not publish metrics if stored metrics count has not reached max metric size threshold', () => {
// Prepare
const metrics: Metrics = new Metrics({ namespace: TEST_NAMESPACE });
Expand Down