Skip to content

Bug: Prevent a single metric have more than 100 data points #1529

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

Closed
leandrodamascena opened this issue Jun 21, 2023 · 3 comments · Fixed by #1548
Closed

Bug: Prevent a single metric have more than 100 data points #1529

leandrodamascena opened this issue Jun 21, 2023 · 3 comments · Fixed by #1548
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility

Comments

@leandrodamascena
Copy link
Contributor

Expected Behaviour

Flush metric set when data point is reached to prevent potential data loss, where 100+ data points are added thus being ignored silently by CloudWatch EMF when processed in the background.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html#CloudWatch_Embedded_Metric_Format_Specification_structure_target

Current Behaviour

More than 100 metric data points can be added.

Code snippet

import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';

const metrics = new Metrics({
  namespace: 'serverlessAirline',
  serviceName: 'orders',
});

export const handler = async (
  _event: unknown,
  _context: unknown
): Promise<void> => {
  metrics.addMetric('performedActionA', MetricUnits.Count, 1);
  metrics.addMetric('performedActionA', MetricUnits.Count, 2);
 // add other 100 values to the same metric
};

Steps to Reproduce

Run the code added in code snippet

Possible Solution

Serialize and flush metric data set when a single metric reaches 100 data points. Dimensions etc should be kept intact.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

18.x

Packaging format used

Lambda Layers, npm

Execution logs

No response

@leandrodamascena leandrodamascena added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Jun 21, 2023
@dreamorosi
Copy link
Contributor

Hi @leandrodamascena thanks for reporting this.

I have added a (failing) test that looks like this:

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_METRICS_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;

  expect(firstMetricsJson[metricName]).toHaveLength(MAX_METRICS_SIZE);
  expect(secondMetricsJson[metricName]).toHaveLength(1);
});

Based on my understanding of this section of the EMF Specification, the values array should not exceed length 100:

Valid values of target members depend on what is referencing them. A metric target MUST be a numeric value or an array of numeric values. Numeric array metric targets MUST NOT have more than 100 members. A dimension target MUST have a string value.

As a result, Metrics should flush the stored metrics and handle the next ones transparently - which is not the case at the moment.

We'll work on a fix and include it in the next release.

@dreamorosi dreamorosi added metrics This item relates to the Metrics Utility confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Jun 21, 2023
@dreamorosi dreamorosi self-assigned this Jun 21, 2023
@dreamorosi dreamorosi linked a pull request Jun 22, 2023 that will close this issue
9 tasks
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jun 23, 2023
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jun 23, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 23, 2023

This is now released under version 1.10.0!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jun 23, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility
Projects
2 participants