Skip to content

Commit e312a29

Browse files
committed
disable metrics env var cloudwatch
1 parent 568a692 commit e312a29

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.constants import MAX_DIMENSIONS, MAX_METRICS
2222
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
2323
from aws_lambda_powertools.shared import constants
24-
from aws_lambda_powertools.shared.functions import resolve_env_var_choice
24+
from aws_lambda_powertools.shared.functions import resolve_env_var_choice, resolve_truthy_env_var_choice
2525
from aws_lambda_powertools.warnings import PowertoolsUserWarning
2626

2727
if TYPE_CHECKING:
@@ -77,6 +77,8 @@ def __init__(
7777
self.default_dimensions = default_dimensions or {}
7878
self.namespace = resolve_env_var_choice(choice=namespace, env=os.getenv(constants.METRICS_NAMESPACE_ENV))
7979
self.service = resolve_env_var_choice(choice=service, env=os.getenv(constants.SERVICE_NAME_ENV))
80+
self.metrics_disabled = self.is_metrics_disabled()
81+
8082
self.metadata_set = metadata_set if metadata_set is not None else {}
8183
self.timestamp: int | None = None
8284

@@ -86,6 +88,14 @@ def __init__(
8688

8789
self.dimension_set.update(**self.default_dimensions)
8890

91+
@staticmethod
92+
def is_metrics_disabled() -> bool:
93+
"""Checks if metrics have been disabled via POWERTOOLS_METRICS_DISABLE"""
94+
is_disabled = resolve_truthy_env_var_choice(env=os.getenv(constants.METRICS_DISABLED_ENV, "false"))
95+
if is_disabled:
96+
logger.debug("Metrics have been disabled via env var POWERTOOLS_METRICS_DISABLED")
97+
return is_disabled
98+
8999
def add_metric(
90100
self,
91101
name: str,
@@ -127,6 +137,8 @@ def add_metric(
127137
MetricResolutionError
128138
When metric resolution is not supported by CloudWatch
129139
"""
140+
if self.metrics_disabled:
141+
return
130142
if not isinstance(value, numbers.Number):
131143
raise MetricValueError(f"{value} is not a valid number")
132144

@@ -268,6 +280,8 @@ def add_dimension(self, name: str, value: str) -> None:
268280
value : str
269281
Dimension value
270282
"""
283+
if self.metrics_disabled:
284+
return
271285
logger.debug(f"Adding dimension: {name}:{value}")
272286
if len(self.dimension_set) == MAX_DIMENSIONS:
273287
raise SchemaValidationError(
@@ -316,6 +330,8 @@ def add_metadata(self, key: str, value: Any) -> None:
316330
value : any
317331
Metadata value
318332
"""
333+
if self.metrics_disabled:
334+
return
319335
logger.debug(f"Adding metadata: {key}:{value}")
320336

321337
# Cast key to str according to EMF spec
@@ -368,6 +384,8 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
368384
raise_on_empty_metrics : bool, optional
369385
raise exception if no metrics are emitted, by default False
370386
"""
387+
if self.metrics_disabled:
388+
return
371389
if not raise_on_empty_metrics and not self.metric_set:
372390
warnings.warn(
373391
"No application metrics to publish. The cold-start metric may be published if enabled. "

aws_lambda_powertools/shared/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
METRICS_NAMESPACE_ENV: str = "POWERTOOLS_METRICS_NAMESPACE"
4141
DATADOG_FLUSH_TO_LOG: str = "DD_FLUSH_TO_LOG"
4242
SERVICE_NAME_ENV: str = "POWERTOOLS_SERVICE_NAME"
43+
METRICS_DISABLED_ENV: str = "POWERTOOLS_METRICS_DISABLED"
4344
# If the timestamp of log event is more than 2 hours in future, the log event is skipped.
4445
# If the timestamp of log event is more than 14 days in past, the log event is skipped.
4546
# See https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html

0 commit comments

Comments
 (0)