Skip to content

Commit 8b8bc8c

Browse files
Changing metrics flush decision
1 parent 7a1d389 commit 8b8bc8c

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

aws_lambda_powertools/metrics/functions.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
1212
from aws_lambda_powertools.shared import constants
13-
from aws_lambda_powertools.shared.functions import resolve_truthy_env_var_choice
13+
from aws_lambda_powertools.shared.functions import strtobool
1414

1515
logger = logging.getLogger(__name__)
1616

@@ -142,24 +142,25 @@ def convert_timestamp_to_emf_format(timestamp: int | datetime) -> int:
142142

143143

144144
def is_metrics_disabled() -> bool:
145-
"""Checks if metrics have been disabled via POWERTOOLS_METRICS_DISABLED or POWERTOOLS_DEV
145+
"""
146+
Determine if metrics should be disabled based on environment variables.
146147
147148
Returns:
148-
bool: True if metrics are disabled, False otherwise
149+
bool: True if metrics are disabled, False otherwise.
150+
151+
Rules:
152+
- If POWERTOOLS_DEV is True and POWERTOOLS_METRICS_DISABLED is True: Disable metrics
153+
- If POWERTOOLS_METRICS_DISABLED is True: Disable metrics
154+
- If POWERTOOLS_DEV is True and POWERTOOLS_METRICS_DISABLED is not set: Disable metrics
149155
"""
150-
# First check POWERTOOLS_METRICS_DISABLED as it should take precedence
151-
if constants.METRICS_DISABLED_ENV in os.environ:
152-
env_value = os.getenv(constants.METRICS_DISABLED_ENV)
153-
is_disabled = resolve_truthy_env_var_choice(env=env_value or "false")
154-
if is_disabled:
155-
logger.debug("Metrics have been disabled via env var POWERTOOLS_METRICS_DISABLED")
156-
return bool(is_disabled)
157-
158-
# Then check if POWERTOOLS_DEV is enabled
159-
dev_mode_value = os.getenv(constants.POWERTOOLS_DEV_ENV)
160-
dev_mode = resolve_truthy_env_var_choice(env=dev_mode_value or "false")
161-
if dev_mode:
162-
logger.debug("Metrics have been disabled via env var POWERTOOLS_DEV")
163-
return True
164-
165-
return False
156+
157+
is_dev_mode = strtobool(os.getenv(constants.POWERTOOLS_DEV_ENV, "false"))
158+
is_metrics_disabled = strtobool(os.getenv(constants.METRICS_DISABLED_ENV, "false"))
159+
160+
disable_conditions = [
161+
is_metrics_disabled,
162+
is_metrics_disabled and is_dev_mode,
163+
is_dev_mode and os.getenv(constants.METRICS_DISABLED_ENV) is None,
164+
]
165+
166+
return any(disable_conditions)

aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def __init__(
7878
self.default_dimensions = default_dimensions or {}
7979
self.namespace = resolve_env_var_choice(choice=namespace, env=os.getenv(constants.METRICS_NAMESPACE_ENV))
8080
self.service = resolve_env_var_choice(choice=service, env=os.getenv(constants.SERVICE_NAME_ENV))
81-
self.metrics_disabled = is_metrics_disabled()
8281

8382
self.metadata_set = metadata_set if metadata_set is not None else {}
8483
self.timestamp: int | None = None
@@ -379,7 +378,7 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
379378
"If application metrics should never be empty, consider using 'raise_on_empty_metrics'",
380379
stacklevel=2,
381380
)
382-
elif not self.metrics_disabled:
381+
elif not is_metrics_disabled():
383382
logger.debug("Flushing existing metrics")
384383
metrics = self.serialize_metric_set()
385384
print(json.dumps(metrics, separators=(",", ":")))

aws_lambda_powertools/metrics/provider/datadog/datadog.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def __init__(
6666
)
6767
self.default_tags = default_tags or {}
6868
self.flush_to_log = resolve_env_var_choice(choice=flush_to_log, env=os.getenv(constants.DATADOG_FLUSH_TO_LOG))
69-
self.metrics_disabled = is_metrics_disabled()
7069

7170
# adding name,value,timestamp,tags
7271
def add_metric(
@@ -202,7 +201,7 @@ def flush_metrics(self, raise_on_empty_metrics: bool = False) -> None:
202201
timestamp=metric_item["e"],
203202
tags=metric_item["t"],
204203
)
205-
elif not self.metrics_disabled:
204+
elif not is_metrics_disabled():
206205
# dd module not found: flush to log, this format can be recognized via datadog log forwarder
207206
# https://github.com/Datadog/datadog-lambda-python/blob/main/datadog_lambda/metric.py#L77
208207
for metric_item in metrics:

docs/core/metrics.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ If you're new to Amazon CloudWatch, there are five terminologies you must be awa
3636

3737
Metric has three global settings that will be used across all metrics emitted:
3838

39-
| Setting | Description | Environment variable | Constructor parameter |
40-
| -------------------- | ------------------------------------------------------------------------------- | ------------------------------ | --------------------- |
41-
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
42-
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
43-
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics. | `POWERTOOLS_METRICS_DISABLED` | N/A |
39+
| Setting | Description | Environment variable | Constructor parameter |
40+
| ------------------------------- | ------------------------------------------------------------------------------- | ------------------------------ | --------------------- |
41+
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
42+
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
43+
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics. | `POWERTOOLS_METRICS_DISABLED` | N/A |
4444

45-
???+ warning
45+
???+ info
4646
`POWERTOOLS_METRICS_DISABLED` will not disable default metrics created by AWS services.
4747

4848
???+ tip

docs/core/metrics/datadog.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ This has the advantage of keeping cold start metric separate from your applicati
174174

175175
You can use any of the following environment variables to configure `DatadogMetrics`:
176176

177-
| Setting | Description | Environment variable | Constructor parameter |
178-
| -------------------- | -------------------------------------------------------------------------------- | ------------------------------ | --------------------- |
179-
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
180-
| **Flush to log** | Use this when you want to flush metrics to be exported through Datadog Forwarder | `DD_FLUSH_TO_LOG` | `flush_to_log` |
181-
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics. | `POWERTOOLS_METRICS_DISABLED` | N/A |
177+
| Setting | Description | Environment variable | Constructor parameter |
178+
| ------------------------------ | -------------------------------------------------------------------------------- | ------------------------------ | --------------------- |
179+
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
180+
| **Flush to log** | Use this when you want to flush metrics to be exported through Datadog Forwarder | `DD_FLUSH_TO_LOG` | `flush_to_log` |
181+
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics. | `POWERTOOLS_METRICS_DISABLED` | N/A |
182182

183-
???+ warning
183+
???+ info
184184
`POWERTOOLS_METRICS_DISABLED` will not disable default metrics created by AWS services.
185185

186186
## Advanced

0 commit comments

Comments
 (0)