Skip to content

Commit 81eec49

Browse files
committed
add resource and excutedversion tags and tests
1 parent 37d7d7c commit 81eec49

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/usr/local/bin/python3"
3+
}

datadog_lambda/tags.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,27 @@ def parse_lambda_tags_from_arn(arn):
3535
split_arn = arn.split(":")
3636

3737
# If ARN includes version / alias at the end, drop it
38-
if len(split_arn) > 7:
39-
split_arn = split_arn[:7]
38+
if len(split_arn) <= 7:
4039

41-
_, _, _, region, account_id, _, function_name = split_arn
40+
_, _, _, region, account_id, _, function_name = split_arn
4241

43-
return [
44-
"region:{}".format(region),
45-
"account_id:{}".format(account_id),
46-
"functionname:{}".format(function_name),
47-
]
42+
return [
43+
"region:{}".format(region),
44+
"account_id:{}".format(account_id),
45+
"functionname:{}".format(function_name),
46+
]
47+
48+
else:
49+
_, _, _, region, account_id, _, function_name, alias = split_arn
50+
51+
resource = function_name + ":" + alias
52+
53+
return [
54+
"region:{}".format(region),
55+
"account_id:{}".format(account_id),
56+
"functionname:{}".format(function_name),
57+
"resource:{}".format(resource),
58+
]
4859

4960

5061
def get_runtime_tag():
@@ -63,6 +74,7 @@ def get_enhanced_metrics_tags(lambda_context):
6374
return parse_lambda_tags_from_arn(lambda_context.invoked_function_arn) + [
6475
get_cold_start_tag(),
6576
"memorysize:{}".format(lambda_context.memory_limit_in_mb),
77+
"executedversion:{}".format(lambda_context.function_version),
6678
get_runtime_tag(),
6779
_format_dd_lambda_layer_tag(),
6880
]

tests/test_tags.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_parse_lambda_tags_from_arn(self):
3434
"region:us-west-1",
3535
"account_id:1234597598159",
3636
"functionname:other-function",
37+
"resource:other-function:function-alias",
3738
],
3839
)
3940

tests/test_wrapper.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ def get_mock_context(
1414
aws_request_id="request-id-1",
1515
memory_limit_in_mb="256",
1616
invoked_function_arn="arn:aws:lambda:us-west-1:123457598159:function:python-layer-test",
17+
function_version="1",
1718
):
1819
lambda_context = MagicMock()
1920
lambda_context.aws_request_id = aws_request_id
2021
lambda_context.memory_limit_in_mb = memory_limit_in_mb
2122
lambda_context.invoked_function_arn = invoked_function_arn
23+
lambda_context.function_version = function_version
2224
return lambda_context
2325

2426

@@ -146,6 +148,7 @@ def lambda_handler(event, context):
146148
"functionname:python-layer-test",
147149
"cold_start:true",
148150
"memorysize:256",
151+
"executedversion:1",
149152
"runtime:python2.7",
150153
"dd_lambda_layer:datadog-python27_0.1.0",
151154
],
@@ -174,6 +177,7 @@ def lambda_handler(event, context):
174177
"functionname:python-layer-test",
175178
"cold_start:true",
176179
"memorysize:256",
180+
"executedversion:1",
177181
"runtime:python2.7",
178182
"dd_lambda_layer:datadog-python27_0.1.0",
179183
],
@@ -187,13 +191,49 @@ def lambda_handler(event, context):
187191
"functionname:python-layer-test",
188192
"cold_start:true",
189193
"memorysize:256",
194+
"executedversion:1",
190195
"runtime:python2.7",
191196
"dd_lambda_layer:datadog-python27_0.1.0",
192197
],
193198
),
194199
]
195200
)
196201

202+
def test_metric_with_function_alias(self):
203+
@datadog_lambda_wrapper
204+
def lambda_handler(event, context):
205+
lambda_metric("test.meetric", 100)
206+
207+
invoked_function_with_alias = (
208+
"arn:aws:lambda:us-west-1:123457598159:function:python-layer-test:foobar"
209+
)
210+
211+
lambda_event = {}
212+
lambda_context = get_mock_context()
213+
lambda_context.invoked_function_arn = invoked_function_with_alias
214+
215+
lambda_handler(lambda_event, lambda_context)
216+
217+
self.mock_write_metric_point_to_stdout.assert_has_calls(
218+
[
219+
call(
220+
"aws.lambda.enhanced.invocations",
221+
1,
222+
tags=[
223+
"region:us-west-1",
224+
"account_id:123457598159",
225+
"functionname:python-layer-test",
226+
"resource:python-layer-test:foobar",
227+
"cold_start:true",
228+
"memorysize:256",
229+
"executedversion:1",
230+
"runtime:python2.7",
231+
"dd_lambda_layer:datadog-python27_0.1.0",
232+
],
233+
)
234+
]
235+
)
236+
197237
def test_enhanced_metrics_cold_start_tag(self):
198238
@datadog_lambda_wrapper
199239
def lambda_handler(event, context):
@@ -220,6 +260,7 @@ def lambda_handler(event, context):
220260
"functionname:python-layer-test",
221261
"cold_start:true",
222262
"memorysize:256",
263+
"executedversion:1",
223264
"runtime:python2.7",
224265
"dd_lambda_layer:datadog-python27_0.1.0",
225266
],
@@ -233,6 +274,7 @@ def lambda_handler(event, context):
233274
"functionname:python-layer-test",
234275
"cold_start:false",
235276
"memorysize:256",
277+
"executedversion:1",
236278
"runtime:python2.7",
237279
"dd_lambda_layer:datadog-python27_0.1.0",
238280
],

0 commit comments

Comments
 (0)