Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions aws/logs_monitoring/enhanced_lambda_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def sanitize_aws_tag_string(tag, remove_colons=False):
"""
global Sanitize, Dedupe, FixInit

# 1. Replaces colons with _
# 1. Replace colons with _
# 2. Convert to all lowercase unicode string
# 3. Convert bad characters to underscores
# 4. Dedupe contiguous underscores
Expand All @@ -222,25 +222,24 @@ def sanitize_aws_tag_string(tag, remove_colons=False):
# FIXME: tag normalization incorrectly supports tags starting
# with a ':', but this behavior should be phased out in future
# as it results in unqueryable data. See dogweb/#11193
# 6. Truncate to 200 characters
# 7. Strip trailing underscores
# 6. Strip trailing underscores

if len(tag) == 0:
# if tag is empty, nothing to do
return tag

if remove_colons:
tag = tag.replace(":", "_")
tag = Dedupe(u"_", Sanitize(u"_", tag.lower()))
tag = Dedupe("_", Sanitize("_", tag.lower()))
first_char = tag[0]
if first_char == u"_" or u"0" <= first_char <= "9":
tag = FixInit(u"", tag)
tag = tag[0:200].rstrip("_")
if first_char == "_" or "0" <= first_char <= "9":
tag = FixInit("", tag)
tag = tag.rstrip("_")
return tag


def get_dd_tag_string_from_aws_dict(aws_key_value_tag_dict):
"""Converts the AWS dict tag format to the dd key:value string format
"""Converts the AWS dict tag format to the dd key:value string format and truncates to 200 characters

Args:
aws_key_value_tag_dict (dict): the dict the GetResources endpoint returns for a tag
Expand All @@ -255,7 +254,7 @@ def get_dd_tag_string_from_aws_dict(aws_key_value_tag_dict):
# Value is optional in DD and AWS
if not value:
return key
return "{}:{}".format(key, value)
return f"{key}:{value}"[0:200]


def parse_get_resources_response_for_tags_by_arn(get_resources_page):
Expand Down
22 changes: 22 additions & 0 deletions aws/logs_monitoring/tests/test_enhanced_lambda_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
LambdaTagsCache,
parse_get_resources_response_for_tags_by_arn,
create_timeout_enhanced_metric,
get_dd_tag_string_from_aws_dict,
)


Expand Down Expand Up @@ -55,6 +56,27 @@ def test_sanitize_tag_string(self):
self.assertEqual(sanitize_aws_tag_string("serVerLess"), "serverless")
self.assertEqual(sanitize_aws_tag_string(""), "")

def test_get_dd_tag_string_from_aws_dict(self):
# Sanitize the key and value, combine them into a string
test_dict = {
"Key": "region",
"Value": "us-east-1",
}

self.assertEqual(get_dd_tag_string_from_aws_dict(test_dict), "region:us-east-1")

# Truncate to 200 characters
long_string = "a" * 300

test_dict = {
"Key": "too-long",
"Value": long_string,
}

self.assertEqual(
get_dd_tag_string_from_aws_dict(test_dict), f"too-long:{long_string[0:191]}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice test!

)

def test_parse_lambda_tags_from_arn(self):
self.assertListEqual(
parse_lambda_tags_from_arn(
Expand Down