Skip to content

Conversation

@anafalcao
Copy link
Contributor

@anafalcao anafalcao commented Feb 7, 2025

Issue number:
#2996

Summary

Add environment variable POWERTOOLS_METRICS_DISABLED to control metrics emission, allowing developers to disable metrics during testing and local development.
The behavior remains unchanged when POWERTOOLS_METRICS_DISABLED is not set or is set to a falsy value.
If POWERTOOLS_DEV is enabled, it also stops sending metrics.
The POWERTOOLS_METRICS_DISABLED environment variable takes precedence over POWERTOOLS_DEV.

Changes

  • Added support for POWERTOOLS_METRICS_DISABLED environment variable.
  • When set to a truthy value (true, True, TRUE, 1), metrics collection will be disabled.
  • Works for Amazon CloudWatch and Datadog providers.
  • Updated documentation to reflect the new functionality.
    • POWERTOOLS_DEV includes disabling metrics as default, but can be overwritten by POWERTOOLS_METRICS_DISABLED

User experience

os.environ["POWERTOOLS_METRICS_DISABLED"] = "true"
metrics = Metrics(namespace="TestApplication")
metrics.add_metric(name="test", unit="Count", value=1)
result = metrics.flush_metrics()  # Returns empty as it's disabled

OR

os.environ["POWERTOOLS_DEV"] = "true"
metrics = Metrics(namespace="TestApplication")
metrics.add_metric(name="test", unit="Count", value=1)
result = metrics.flush_metrics()  # Returns empty as it's disabled

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.

@boring-cyborg boring-cyborg bot added commons documentation Improvements or additions to documentation metrics tests labels Feb 7, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 7, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Feb 7, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 7, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 7, 2025
@codecov
Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.25%. Comparing base (1ca05a2) to head (762e67b).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6046   +/-   ##
========================================
  Coverage    96.25%   96.25%           
========================================
  Files          234      234           
  Lines        11057    11068   +11     
  Branches       803      805    +2     
========================================
+ Hits         10643    10654   +11     
  Misses         327      327           
  Partials        87       87           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 7, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 7, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 7, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 7, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 7, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 7, 2025
@anafalcao anafalcao self-assigned this Feb 7, 2025
@anafalcao anafalcao marked this pull request as ready for review February 7, 2025 17:59
@anafalcao anafalcao requested a review from a team February 7, 2025 17:59
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 7, 2025
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hello @anafalcao! Super nice PR with a code/design very well written!

I think the only thing missing is the part where you don't flush the metrics when it disabled. 🚀

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@leandrodamascena leandrodamascena changed the title feat(metrics): disable metrics with POWERTOOLS_METRICS_DISABLED feat(metrics): disable metrics flush via environment variables Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 11, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 11, 2025
@sonarqubecloud
Copy link

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Good work!

@leandrodamascena leandrodamascena merged commit 1fa4700 into develop Feb 11, 2025
13 checks passed
@leandrodamascena leandrodamascena deleted the disablemetrics branch February 11, 2025 08:59
sinofseven pushed a commit to sinofseven/powertools-lambda-python-my-extend that referenced this pull request Feb 13, 2025
…owertools#6046)

* disable metrics env var cloudwatch

* add for datadog

* add tests

* acept none env var

* fix is disable metrics

* add documentation

* fix mypy

* add datadog tests

* disable when  powertools dev

* add disable to flush

* remove print

* Changing metrics flush decision

* Changing metrics flush decision

---------

Co-authored-by: Leandro Damascena <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commons feature New feature or functionality metrics size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Create an environment variable to allow customer disable custom metrics

3 participants