Skip to content

Commit b350ac4

Browse files
committed
Make enhanced metrics opt-in via an env var
1 parent 6ab22dd commit b350ac4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

datadog_lambda/metric.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,18 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None):
7676
lambda_stats.distribution(metric_name, value, timestamp=timestamp, tags=tags)
7777

7878

79+
def are_enhanced_metrics_enabled():
80+
"""Check env var to find if enhanced metrics should be submitted
81+
"""
82+
return os.environ.get("DD_ENHANCED_METRICS", "false").lower() == "true"
83+
84+
7985
def submit_invocations_metric(lambda_arn):
8086
"""Increment aws.lambda.enhanced.invocations by 1
8187
"""
88+
if not are_enhanced_metrics_enabled():
89+
return
90+
8291
lambda_metric(
8392
"{}.invocations".format(ENHANCED_METRICS_NAMESPACE_PREFIX),
8493
1,
@@ -89,6 +98,9 @@ def submit_invocations_metric(lambda_arn):
8998
def submit_errors_metric(lambda_arn):
9099
"""Increment aws.lambda.enhanced.errors by 1
91100
"""
101+
if not are_enhanced_metrics_enabled():
102+
return
103+
92104
lambda_metric(
93105
"{}.errors".format(ENHANCED_METRICS_NAMESPACE_PREFIX),
94106
1,

tests/test_wrapper.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def lambda_handler(event, context):
106106
del os.environ["DD_LOGS_INJECTION"]
107107

108108
def test_invocations_metric(self):
109+
os.environ["DD_ENHANCED_METRICS"] = "True"
110+
109111
@datadog_lambda_wrapper
110112
def lambda_handler(event, context):
111113
lambda_metric("test.metric", 100)
@@ -129,7 +131,11 @@ def lambda_handler(event, context):
129131
]
130132
)
131133

134+
del os.environ["DD_ENHANCED_METRICS"]
135+
132136
def test_errors_metric(self):
137+
os.environ["DD_ENHANCED_METRICS"] = "True"
138+
133139
@datadog_lambda_wrapper
134140
def lambda_handler(event, context):
135141
raise RuntimeError()
@@ -164,7 +170,11 @@ def lambda_handler(event, context):
164170
]
165171
)
166172

173+
del os.environ["DD_ENHANCED_METRICS"]
174+
167175
def test_enhanced_metrics_cold_start_tag(self):
176+
os.environ["DD_ENHANCED_METRICS"] = "True"
177+
168178
@datadog_lambda_wrapper
169179
def lambda_handler(event, context):
170180
lambda_metric("test.metric", 100)
@@ -203,3 +213,18 @@ def lambda_handler(event, context):
203213
),
204214
]
205215
)
216+
217+
del os.environ["DD_ENHANCED_METRICS"]
218+
219+
def test_no_enhanced_metrics_without_env_var(self):
220+
@datadog_lambda_wrapper
221+
def lambda_handler(event, context):
222+
raise RuntimeError()
223+
224+
lambda_event = {}
225+
226+
with self.assertRaises(RuntimeError):
227+
lambda_handler(lambda_event, get_mock_context())
228+
229+
self.mock_wrapper_lambda_metric.assert_not_called()
230+

0 commit comments

Comments
 (0)