Skip to content

feat(metrics): add EphemeralMetrics as a non-singleton option #1676

New issue

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

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

Already on GitHub? # to your account

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Oct 31, 2022

Issue number: #1668

Summary

By default, Metrics shares data across instances. This allows customers to instantiate Metrics in multiple places throughout their code - be a separate file, a middleware, or an abstraction.

However, there are three cases where this is a disadvantage:

  • You want to create the same metrics for different applications
  • You use a metric dimension to distinguish a metric per tenant (EMF mandates the same dimension for all metrics in a given JSON blob).

Changes

Please provide a summary of what's being changed

This PR introduces a new class: EphemeralMetrics. This makes an explicit contract that instances of EphemeralMetrics will not share data sets (metrics, dimensions, metadata).

Quick summary on differences:

Feature Metrics EphemeralMetrics
Share data across instances (metrics, dimensions, metadata, etc.) Yes -
Set default dimensions to persist across Lambda invocations (metric flush) Yes -

User experience

Please share what the user experience looks like before and after this change

from aws_lambda_powertools.metrics import EphemeralMetrics
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.typing import LambdaContext

metrics = EphemeralMetrics()

@metrics.log_metrics
def lambda_handler(event: dict, context: LambdaContext):
    metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.

@heitorlessa heitorlessa requested a review from a team as a code owner October 31, 2022 17:50
@heitorlessa heitorlessa requested review from mploski and removed request for a team October 31, 2022 17:50
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 31, 2022
@heitorlessa heitorlessa marked this pull request as draft October 31, 2022 17:50
@github-actions github-actions bot added the feature New feature or functionality label Oct 31, 2022
@heitorlessa
Copy link
Contributor Author

heitorlessa commented Oct 31, 2022

Still looking for a class name to avoid this implementation.

  • EphemeralMetrics
  • TransientMetrics
  • NonDurableMetrics
  • MetricManager

@pull-request-size pull-request-size bot removed the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 1, 2022
@boring-cyborg boring-cyborg bot added the commons label Nov 1, 2022
@pull-request-size pull-request-size bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Nov 1, 2022
@heitorlessa heitorlessa changed the title feat(metrics): add support to not share data among Metrics instance feat(metrics): add EphemeralMetrics as alternative to singleton Metrics class Nov 1, 2022
@heitorlessa heitorlessa marked this pull request as ready for review November 1, 2022 10:21
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Nov 1, 2022
@heitorlessa
Copy link
Contributor Author

Docs with a new section named Metrics isolation and explanation about the constraints that led us to our Metrics design (and why a new class name).

image

@heitorlessa
Copy link
Contributor Author

both Leandro and Ruben are PTO, therefore I'm merging after my best due diligence in review, docs, and tests.

@heitorlessa heitorlessa self-assigned this Nov 1, 2022
@heitorlessa heitorlessa changed the title feat(metrics): add EphemeralMetrics as alternative to singleton Metrics class feat(metrics): add EphemeralMetrics as a non-singleton option Nov 1, 2022
@heitorlessa heitorlessa merged commit 635bc80 into aws-powertools:develop Nov 1, 2022
@heitorlessa heitorlessa deleted the feat/disable-metrics-borg-pattern branch November 1, 2022 11:47
@heitorlessa heitorlessa linked an issue Nov 1, 2022 that may be closed by this pull request
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
commons documentation Improvements or additions to documentation feature New feature or functionality metrics size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nesting log_metrics only results in one set of metrics being logged
1 participant