Skip to content

Conversation

@phipag
Copy link
Contributor

@phipag phipag commented Dec 10, 2025

Summary

This PR clears custom dimensions when .flush() is called on the metrics instance. Note: This does not affect calls to .flushMetrics() since they create a temporary second metrics instance that supports flushing on its own with its own dimension set (copy).

This was the default behavior in v1 of Powertools Java but is also the same behavior in the other runtimes such as TypeScript (see comment).

On flush, we clear:

  • Custom dimensions and preserve default dimensions
  • Metadata (also consistent with TS runtime)

Changes

Issue number: #2327


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@sonarqubecloud
Copy link

@phipag phipag moved this to Pending review in Powertools for AWS Lambda (Java) Dec 10, 2025
Copy link

@surajkumar surajkumar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this code with the original snippet from the bug and now no metrics are being emitted. In the log group I am seeing:

{
    "level": "WARN",
    "message": "No metrics were emitted",
    "function_arn": null,
    "function_name": null,
    "function_request_id": null,
    "function_version": null,
    "service": null,
    "timestamp": "2025-12-10T13:24:42.385Z"
}

To add context on how I am pulling the changes. I have grabbed a copy of the EmfMetricsLogger and create the instance like so:

private static final Metrics METRICS = new EmfMetricsLogger(new EnvironmentProvider(), new MetricsContext());

Don't think this makes a difference though for this small test. Deploying to mavenLocal might be a bit more tricky for me.

@phipag
Copy link
Contributor Author

phipag commented Dec 10, 2025

@surajkumar Thanks for testing! can you try to call as last statement in your testing code METRICS.flush()?

The reason why you see this warning is because @FlushMetrics uses a request-scoped proxy to a thread-local metrics instance (https://github.com/aws-powertools/powertools-lambda-java/blob/main/powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java#L33). Hence, @FlushMetrics emitted this warning because it did not collect any metrics. It created an instance which was not used because you manually created a separate EmfMetricsLogger.

@surajkumar
Copy link

surajkumar commented Dec 10, 2025

@surajkumar Thanks for testing! can you try to call as last statement in your testing code METRICS.flush()?

The reason why you see this warning is because @FlushMetrics uses a request-scoped proxy to a thread-local metrics instance (https://github.com/aws-powertools/powertools-lambda-java/blob/main/powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java#L33). Hence, @FlushMetrics emitted this warning because it did not collect any metrics. It created an instance which was not used because you manually created a separate EmfMetricsLogger.

I tried with the flush at the end of the method and yes that works.

I then re-tried with MetricsFactory.getMetricsInstance() (again, copying it over and updating where needed) and it gives me the same result of "No metrics were emitted" - Going to pull the repo, build and test - I'll get back to you.

Copy link

@surajkumar surajkumar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled repo and performed my manual integration test. All looks good to me. Thanks @phipag !

@phipag
Copy link
Contributor Author

phipag commented Dec 10, 2025

Awesome, thanks for confirming @surajkumar!

Merged this PR.

@phipag phipag merged commit bb70c1b into main Dec 10, 2025
16 checks passed
@phipag phipag deleted the 2327-bug-metric-dimensions-are-persisting-across-lambda-invocations branch December 10, 2025 15:21
@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (Java) Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working metrics size/L

Projects

Status: Coming soon

Development

Successfully merging this pull request may close these issues.

Bug: Metric dimensions are persisting across Lambda invocations

3 participants