forked from DataDog/datadog-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtags.py
More file actions
68 lines (52 loc) · 1.93 KB
/
tags.py
File metadata and controls
68 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
from platform import python_version_tuple
from datadog_lambda import __version__
from datadog_lambda.cold_start import get_cold_start_tag
def _format_dd_lambda_layer_tag():
"""
Formats the dd_lambda_layer tag, e.g., 'dd_lambda_layer:datadog-python27_1'
"""
runtime = "python{}{}".format(sys.version_info[0], sys.version_info[1])
return "dd_lambda_layer:datadog-{}_{}".format(runtime, __version__)
def tag_dd_lambda_layer(tags):
"""
Used by lambda_metric to insert the dd_lambda_layer tag
"""
dd_lambda_layer_tag = _format_dd_lambda_layer_tag()
if tags:
return tags + [dd_lambda_layer_tag]
else:
return [dd_lambda_layer_tag]
def parse_lambda_tags_from_arn(arn):
"""Generate the list of lambda tags based on the data in the arn
Args:
arn (str): Lambda ARN.
ex: arn:aws:lambda:us-east-1:123597598159:function:my-lambda[:optional-version]
"""
# Cap the number of times to split
split_arn = arn.split(":")
# If ARN includes version / alias at the end, drop it
if len(split_arn) > 7:
split_arn = split_arn[:7]
_, _, _, region, account_id, _, function_name = split_arn
return [
"region:{}".format(region),
"account_id:{}".format(account_id),
"functionname:{}".format(function_name),
]
def get_runtime_tag():
"""Get the runtime tag from the current Python version
"""
major_version, minor_version, _ = python_version_tuple()
return "runtime:python{major}.{minor}".format(
major=major_version, minor=minor_version
)
def get_enhanced_metrics_tags(lambda_context):
"""Get the list of tags to apply to enhanced metrics
"""
return parse_lambda_tags_from_arn(lambda_context.invoked_function_arn) + [
get_cold_start_tag(),
"memorysize:{}".format(lambda_context.memory_limit_in_mb),
get_runtime_tag(),
_format_dd_lambda_layer_tag(),
]