Skip to content

Commit 2ea6c19

Browse files
committed
fix is disable metrics
1 parent d566972 commit 2ea6c19

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

Diff for: aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ def is_metrics_disabled() -> bool:
9393
"""Checks if metrics have been disabled via POWERTOOLS_METRICS_DISABLED"""
9494
if constants.METRICS_DISABLED_ENV not in os.environ:
9595
return False
96-
96+
9797
is_disabled = resolve_truthy_env_var_choice(env=os.getenv(constants.METRICS_DISABLED_ENV))
9898
if is_disabled:
9999
logger.debug("Metrics have been disabled via env var POWERTOOLS_METRICS_DISABLED")
100100
return bool(is_disabled)
101101

102-
103102
def add_metric(
104103
self,
105104
name: str,
@@ -141,8 +140,7 @@ def add_metric(
141140
MetricResolutionError
142141
When metric resolution is not supported by CloudWatch
143142
"""
144-
if self.metrics_disabled:
145-
return
143+
146144
if not isinstance(value, numbers.Number):
147145
raise MetricValueError(f"{value} is not a valid number")
148146

@@ -284,8 +282,7 @@ def add_dimension(self, name: str, value: str) -> None:
284282
value : str
285283
Dimension value
286284
"""
287-
if self.metrics_disabled:
288-
return
285+
289286
logger.debug(f"Adding dimension: {name}:{value}")
290287
if len(self.dimension_set) == MAX_DIMENSIONS:
291288
raise SchemaValidationError(
@@ -334,8 +331,6 @@ def add_metadata(self, key: str, value: Any) -> None:
334331
value : any
335332
Metadata value
336333
"""
337-
if self.metrics_disabled:
338-
return
339334
logger.debug(f"Adding metadata: {key}:{value}")
340335

341336
# Cast key to str according to EMF spec
@@ -388,8 +383,6 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
388383
raise_on_empty_metrics : bool, optional
389384
raise exception if no metrics are emitted, by default False
390385
"""
391-
if self.metrics_disabled:
392-
return
393386
if not raise_on_empty_metrics and not self.metric_set:
394387
warnings.warn(
395388
"No application metrics to publish. The cold-start metric may be published if enabled. "

Diff for: aws_lambda_powertools/metrics/provider/datadog/datadog.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def is_metrics_disabled() -> bool:
7272
"""Checks if metrics have been disabled via POWERTOOLS_METRICS_DISABLED"""
7373
if constants.METRICS_DISABLED_ENV not in os.environ:
7474
return False
75-
75+
7676
is_disabled = resolve_truthy_env_var_choice(env=os.getenv(constants.METRICS_DISABLED_ENV))
7777
if is_disabled:
7878
logger.debug("Metrics have been disabled via env var POWERTOOLS_METRICS_DISABLED")
@@ -111,8 +111,6 @@ def add_metric(
111111
>>> sales='sam'
112112
>>> )
113113
"""
114-
if self.metrics_disabled:
115-
return
116114
# validating metric name
117115
if not self._validate_datadog_metric_name(name):
118116
docs = "https://docs.datadoghq.com/metrics/custom_metrics/#naming-custom-metrics"
@@ -193,8 +191,7 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
193191
raise_on_empty_metrics : bool, optional
194192
raise exception if no metrics are emitted, by default False
195193
"""
196-
if self.metrics_disabled:
197-
return
194+
198195
if not raise_on_empty_metrics and len(self.metric_set) == 0:
199196
warnings.warn(
200197
"No application metrics to publish. The cold-start metric may be published if enabled. "

Diff for: tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1331,12 +1331,12 @@ def lambda_handler(evt, ctx):
13311331
)
13321332

13331333

1334-
def test_metrics_disabled_with_env_var(monkeypatch):
1334+
def test_metrics_disabled_with_env_var(monkeypatch, namespace):
13351335
# GIVEN environment variable is set to disable metrics
13361336
monkeypatch.setenv("POWERTOOLS_METRICS_DISABLED", "true")
13371337

13381338
# WHEN metrics is initialized and adding metrics
1339-
metrics = Metrics()
1339+
metrics = Metrics(namespace=namespace)
13401340
metrics.add_metric(name="test_metric", unit="Count", value=1)
13411341

13421342
# WHEN flushing metrics
@@ -1346,10 +1346,10 @@ def test_metrics_disabled_with_env_var(monkeypatch):
13461346
assert metrics_output is None
13471347

13481348

1349-
def test_metrics_disabled_persists_after_flush(monkeypatch):
1349+
def test_metrics_disabled_persists_after_flush(monkeypatch, namespace):
13501350
# GIVEN environment variable is set to disable metrics
13511351
monkeypatch.setenv("POWERTOOLS_METRICS_DISABLED", "true")
1352-
metrics = Metrics()
1352+
metrics = Metrics(namespace=namespace)
13531353

13541354
# WHEN multiple operations are performed with flush in between
13551355
metrics.add_metric(name="metric1", unit="Count", value=1)

0 commit comments

Comments
 (0)