Skip to content

Commit a388918

Browse files
authored
Do not recalculate enhanced metric setting each time. (#462)
1 parent ad81d15 commit a388918

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

datadog_lambda/extension.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from os import path
2+
import os
33

44
AGENT_URL = "http://127.0.0.1:8124"
55
FLUSH_PATH = "/lambda/flush"
@@ -9,9 +9,7 @@
99

1010

1111
def is_extension_present():
12-
if path.exists(EXTENSION_PATH):
13-
return True
14-
return False
12+
return os.path.exists(EXTENSION_PATH)
1513

1614

1715
def flush_extension():

datadog_lambda/metric.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true"
3333
lambda_stats = ThreadStatsWriter(flush_in_thread)
3434

35+
enhanced_metrics_enabled = (
36+
os.environ.get("DD_ENHANCED_METRICS", "true").lower() == "true"
37+
)
38+
3539

3640
def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=False):
3741
"""
@@ -90,24 +94,14 @@ def flush_stats():
9094
lambda_stats.flush()
9195

9296

93-
def are_enhanced_metrics_enabled():
94-
"""Check env var to find if enhanced metrics should be submitted
95-
96-
Returns:
97-
boolean for whether enhanced metrics are enabled
98-
"""
99-
# DD_ENHANCED_METRICS defaults to true
100-
return os.environ.get("DD_ENHANCED_METRICS", "true").lower() == "true"
101-
102-
10397
def submit_enhanced_metric(metric_name, lambda_context):
10498
"""Submits the enhanced metric with the given name
10599
106100
Args:
107101
metric_name (str): metric name w/o enhanced prefix i.e. "invocations" or "errors"
108102
lambda_context (dict): Lambda context dict passed to the function by AWS
109103
"""
110-
if not are_enhanced_metrics_enabled():
104+
if not enhanced_metrics_enabled:
111105
logger.debug(
112106
"Not submitting enhanced metric %s because enhanced metrics are disabled",
113107
metric_name,

tests/test_wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ def lambda_handler(event, context):
467467
)
468468

469469
def test_no_enhanced_metrics_without_env_var(self):
470-
os.environ["DD_ENHANCED_METRICS"] = "false"
470+
patcher = patch("datadog_lambda.metric.enhanced_metrics_enabled", False)
471+
patcher.start()
472+
self.addCleanup(patcher.stop)
471473

472474
@wrapper.datadog_lambda_wrapper
473475
def lambda_handler(event, context):
@@ -480,8 +482,6 @@ def lambda_handler(event, context):
480482

481483
self.mock_write_metric_point_to_stdout.assert_not_called()
482484

483-
del os.environ["DD_ENHANCED_METRICS"]
484-
485485
def test_only_one_wrapper_in_use(self):
486486
patcher = patch("datadog_lambda.wrapper.submit_invocations_metric")
487487
self.mock_submit_invocations_metric = patcher.start()

0 commit comments

Comments
 (0)