8000 Tag enhanced metrics with runtime and memorysize · DataDog/datadog-lambda-python@191710f · GitHub
[go: up one dir, main page]

Skip to content

Commit 191710f

Browse files
committed
Tag enhanced metrics with runtime and memorysize
1 parent 6087ba6 commit 191710f

File tree

6 files changed

+85
-30
lines changed

6 files changed

+85
-30
lines changed

datadog_lambda/metric.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from datadog import api
1515
from datadog.threadstats import ThreadStats
1616
from datadog_lambda import __version__
17-
from datadog_lambda.cold_start import get_cold_start_tag
18-
from datadog_lambda.tags import parse_lambda_tags_from_arn
17+
from datadog_lambda.tags import get_enhanced_metrics_tags
1918

2019

2120
ENHANCED_METRICS_NAMESPACE_PREFIX = "aws.lambda.enhanced"
@@ -82,7 +81,7 @@ def are_enhanced_metrics_enabled():
8281
return os.environ.get("DD_ENHANCED_METRICS", "false").lower() == "true"
8382

8483

85-
def submit_invocations_metric(lambda_arn):
84+
def submit_invocations_metric(lambda_context):
8685
"""Increment aws.lambda.enhanced.invocations by 1
8786
"""
8887
if not are_enhanced_metrics_enabled():
@@ -91,11 +90,11 @@ def submit_invocations_metric(lambda_arn):
9190
lambda_metric(
9291
"{}.invocations".format(ENHANCED_METRICS_NAMESPACE_PREFIX),
9392
1,
94-
tags=parse_lambda_tags_from_arn(lambda_arn) + [get_cold_start_tag()],
93+
tags=get_enhanced_metrics_tags(lambda_context),
9594
)
9695

9796

98-
def submit_errors_metric(lambda_arn):
97+
def submit_errors_metric(lambda_context):
9998
"""Increment aws.lambda.enhanced.errors by 1
10099
"""
101100
if not are_enhanced_metrics_enabled():
@@ -104,7 +103,7 @@ def submit_errors_metric(lambda_arn):
104103
lambda_metric(
105104
"{}.errors".format(ENHANCED_METRICS_NAMESPACE_PREFIX),
106105
1,
107-
tags=parse_lambda_tags_from_arn(lambda_arn) + [get_cold_start_tag()],
106+
tags=get_enhanced_metrics_tags(lambda_context),
108107
)
109108

110109

datadog_lambda/tags.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from platform import python_version_tuple
2+
3+
from datadog_lambda.cold_start import get_cold_start_tag
4+
5+
16
def parse_lambda_tags_from_arn(arn):
27
"""Generate the list of lambda tags based on the data in the arn
38
Args:
@@ -18,3 +23,23 @@ def parse_lambda_tags_from_arn(arn):
1823
"account_id:{}".format(account_id),
1924
"functionname:{}".format(function_name),
2025
]
26+
27+
28+
def get_runtime_tag():
29+
"""Get the runtime tag from the current Python version
30+
"""
31+
major_version, minor_version, _ = python_version_tuple()
32+
33+
return "runtime:python{major}.{minor}".format(
34+
major=major_version, minor=minor_version
35+
)
36+
37+
38+
def get_enhanced_metrics_tags(lambda_context):
39+
"""Get the list of tags to apply to enhanced metrics
40+
"""
41+
return parse_lambda_tags_from_arn(lambda_context.invoked_function_arn) + [
42+
get_cold_start_tag(),
43+
"memorysize:{}".format(lambda_context.memory_limit_in_mb),
44+
get_runtime_tag(),
45+
]

datadog_lambda/wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _before(self, event, context):
6161
set_cold_start()
6262

6363
try:
64-
submit_invocations_metric(context.invoked_function_arn)
64+
submit_invocations_metric(context)
6565
# Extract Datadog trace context from incoming requests
6666
extract_dd_trace_context(event)
6767

@@ -82,7 +82,7 @@ def __call__(self, event, context):
8282
try:
8383
return self.func(event, context)
8484
except Exception:
85-
submit_errors_metric(context.invoked_function_arn)
85+
submit_errors_metric(context)
8686
raise
8787
finally:
8888
self._after(event, context)

tests/test_metric.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
import os
22
import unittest
3+
34
try:
45
from unittest.mock import patch, call
56
except ImportError:
67
from mock import patch, call
78

8-
from datadog_lambda.metric import (
9-
lambda_metric,
10-
_format_dd_lambda_layer_tag,
11-
)
9+
from datadog_lambda.metric import lambda_metric, _format_dd_lambda_layer_tag
1210

1311

1412
class TestLambdaMetric(unittest.TestCase):
15-
1613
def setUp(self):
17-
patcher = patch('datadog_lambda.metric.lambda_stats')
14+
patcher = patch("datadog_lambda.metric.lambda_stats")
1815
self.mock_metric_lambda_stats = patcher.start()
1916
self.addCleanup(patcher.stop)
2017

2118
def test_lambda_metric_tagged_with_dd_lambda_layer(self):
22-
lambda_metric('test', 1)
23-
lambda_metric('test', 1, 123, [])
24-
lambda_metric('test', 1, tags=['tag1:test'])
19+
lambda_metric("test", 1)
20+
lambda_metric("test", 1, 123, [])
21+
lambda_metric("test", 1, tags=["tag1:test"])
2522
expected_tag = _format_dd_lambda_layer_tag()
26-
self.mock_metric_lambda_stats.distribution.assert_has_calls([
27-
call('test', 1, timestamp=None, tags=[expected_tag]),
28-
call('test', 1, timestamp=123, tags=[expected_tag]),
29-
call('test', 1, timestamp=None, tags=['tag1:test', expected_tag]),
30-
])
23+
self.mock_metric_lambda_stats.distribution.assert_has_calls(
24+
[
25+
call("test", 1, timestamp=None, tags=[expected_tag]),
26+
call("test", 1, timestamp=123, tags=[expected_tag]),
27+
call("test", 1, timestamp=None, tags=["tag1:test", expected_tag]),
28+
]
29+
)
3130

3231
def test_lambda_metric_flush_to_log(self):
33-
os.environ["DD_FLUSH_TO_LOG"] = 'True'
32+
os.environ["DD_FLUSH_TO_LOG"] = "True"
3433

35-
lambda_metric('test', 1)
34+
lambda_metric("test", 1)
3635
self.mock_metric_lambda_stats.distribution.assert_not_called()
3736

3837
del os.environ["DD_FLUSH_TO_LOG"]

tests/test_tags.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import unittest
22

3-
from datadog_lambda.tags import parse_lambda_tags_from_arn
3+
try:
4+
from unittest.mock import patch
5+
except ImportError:
6+
from mock import patch
7+
8+
from datadog_lambda.tags import parse_lambda_tags_from_arn, get_runtime_tag
49

510

611
class TestMetricTags(unittest.TestCase):
12+
def setUp(self):
13+
patcher = patch("datadog_lambda.tags.python_version_tuple")
14+
self.mock_python_version_tuple = patcher.start()
15+
self.addCleanup(patcher.stop)
16+
717
def test_parse_lambda_tags_from_arn(self):
818
self.assertListEqual(
919
parse_lambda_tags_from_arn(
@@ -27,3 +37,10 @@ def test_parse_lambda_tags_from_arn(self):
2737
],
2838
)
2939

40+
def test_get_runtime_tag(self):
41+
self.mock_python_version_tuple.return_value = ("2", "7", "10")
42+
self.assertEqual(get_runtime_tag(), "runtime:python2.7")
43+
44+
self.mock_python_version_tuple.return_value = ("3", "7", "2")
45+
self.assertEqual(get_runtime_tag(), "runtime:python3.7")
46+

tests/test_wrapper.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def setUp(self):
3333
self.addCleanup(patcher.stop)
3434

3535
patcher = patch("datadog_lambda.metric.lambda_metric")
36-
self.mock_wrapper_lambda_metric = patcher.start()
36+
self.mock_lambda_metric = patcher.start()
3737
self.addCleanup(patcher.stop)
3838

3939
patcher = patch("datadog_lambda.wrapper.extract_dd_trace_context")
@@ -57,6 +57,11 @@ def setUp(self):
5757
self.mock_is_cold_start.return_value = True
5858
self.addCleanup(patcher.stop)
5959

60+
patcher = patch("datadog_lambda.tags.python_version_tuple")
61+
self.mock_python_version_tuple = patcher.start()
62+
self.mock_python_version_tuple.return_value = ("2", "7", "10")
63+
self.addCleanup(patcher.stop)
64+
6065
def test_datadog_lambda_wrapper(self):
6166
@datadog_lambda_wrapper
6267
def lambda_handler(event, context):
@@ -116,7 +121,7 @@ def lambda_handler(event, context):
116121

117122
lambda_handler(lambda_event, get_mock_context())
118123

119-
self.mock_wrapper_lambda_metric.assert_has_calls(
124+
self.mock_lambda_metric.assert_has_calls(
120125
[
121126
call(
122127
"aws.lambda.enhanced.invocations",
@@ -126,6 +131,8 @@ def lambda_handler(event, context):
126131
"account_id:123457598159",
127132
"functionname:python-layer-test",
128133
"cold_start:true",
134+
"memorysize:256",
135+
"runtime:python2.7",
129136
],
130137
)
131138
]
@@ -145,7 +152,7 @@ def lambda_handler(event, context):
145152
with self.assertRaises(RuntimeError):
146153
lambda_handler(lambda_event, get_mock_context())
147154

148-
self.mock_wrapper_lambda_metric.assert_has_calls(
155+
self.mock_lambda_metric.assert_has_calls(
149156
[
150157
call(
151158
"aws.lambda.enhanced.invocations",
@@ -155,6 +162,8 @@ def lambda_handler(event, context):
155162
"account_id:123457598159",
156163
"functionname:python-layer-test",
157164
"cold_start:true",
165+
"memorysize:256",
166+
"runtime:python2.7",
158167
],
159168
),
160169
call(
@@ -165,6 +174,8 @@ def lambda_handler(event, context):
165174
"account_id:123457598159",
166175
"functionname:python-layer-test",
167176
"cold_start:true",
177+
"memorysize:256",
178+
"runtime:python2.7",
168179
],
169180
),
170181
]
@@ -189,7 +200,7 @@ def lambda_handler(event, context):
189200
lambda_event, get_mock_context(aws_request_id="second-request-id")
190201
)
191202

192-
self.mock_wrapper_lambda_metric.assert_has_calls(
203+
self.mock_lambda_metric.assert_has_calls(
193204
[
194205
call(
195206
"aws.lambda.enhanced.invocations",
@@ -199,6 +210,8 @@ def lambda_handler(event, context):
199210
"account_id:123457598159",
200211
"functionname:python-layer-test",
201212
"cold_start:true",
213+
"memorysize:256",
214+
"runtime:python2.7",
202215
],
203216
),
204217
call(
@@ -209,6 +222,8 @@ def lambda_handler(event, context):
209222
"account_id:123457598159",
210223
"functionname:python-layer-test",
211224
"cold_start:false",
225+
"memorysize:256",
226+
"runtime:python2.7",
212227
],
213228
),
214229
]
@@ -226,5 +241,5 @@ def lambda_handler(event, context):
226241
with self.assertRaises(RuntimeError):
227242
lambda_handler(lambda_event, get_mock_context())
228243

229-
self.mock_wrapper_lambda_metric.assert_not_called()
244+
self.mock_lambda_metric.assert_not_called()
230245

0 commit comments

Comments
 (0)
0