Skip to content

Commit edd395c

Browse files
authored
Merge pull request DataDog#259 from DataDog/stephenf/fix-tags-lookup-casing
Store lowercase ARN keys in tags cache
2 parents 8257922 + 3b015f2 commit edd395c

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

aws/logs_monitoring/enhanced_lambda_metrics.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ def get(self, resource_arn):
118118
Will refetch the tags if they are out of date, or a lambda arn is encountered
119119
which isn't in the tag list
120120
121+
Note: the ARNs in the cache have been lowercased, so resource_arn must be lowercased
122+
123+
Args:
124+
resource_arn (str): the arn we're getting tags from the cache for
125+
121126
Returns:
122127
lambda_tags (str[]): the list of "key:value" Datadog tag strings
123128
"""
@@ -263,10 +268,12 @@ def parse_get_resources_response_for_tags_by_arn(get_resources_page):
263268
aws_resouce_tag_mappings = get_resources_page["ResourceTagMappingList"]
264269
for aws_resource_tag_mapping in aws_resouce_tag_mappings:
265270
function_arn = aws_resource_tag_mapping["ResourceARN"]
271+
lowercase_function_arn = function_arn.lower()
272+
266273
raw_aws_tags = aws_resource_tag_mapping["Tags"]
267274
tags = map(get_dd_tag_string_from_aws_dict, raw_aws_tags)
268275

269-
tags_by_arn[function_arn] += tags
276+
tags_by_arn[lowercase_function_arn] += tags
270277

271278
return tags_by_arn
272279

@@ -363,6 +370,7 @@ def generate_enhanced_lambda_metrics(log, tags_cache):
363370
Returns:
364371
DatadogMetricPoint[], where each metric has all of its tags
365372
"""
373+
# Note: this arn attribute is always lowercased when it's created
366374
log_function_arn = log.get("lambda", {}).get("arn")
367375
log_message = log.get("message")
368376
timestamp = log.get("timestamp")
@@ -472,17 +480,20 @@ def calculate_estimated_cost(billed_duration_ms, memory_allocated):
472480
return BASE_LAMBDA_INVOCATION_PRICE + gb_seconds * LAMBDA_PRICE_PER_GB_SECOND
473481

474482

475-
def get_enriched_lambda_log_tags(log):
483+
def get_enriched_lambda_log_tags(log_event):
476484
""" Retrieves extra tags from lambda, either read from the function arn, or by fetching lambda tags from the function itself.
477485
478486
Args:
479487
log (dict<str, str | dict | int>): a log parsed from the event in the split method
480488
"""
481-
log_function_arn = log.get("lambda", {}).get("arn")
489+
# Note that this arn attribute has been lowercased already
490+
log_function_arn = log_event.get("lambda", {}).get("arn")
491+
482492
if not log_function_arn:
483493
return []
484494
tags_from_arn = parse_lambda_tags_from_arn(log_function_arn)
485495
lambda_custom_tags = account_lambda_tags_cache.get(log_function_arn)
496+
486497
# Combine and dedup tags
487498
tags = list(set(tags_from_arn + lambda_custom_tags))
488499
return tags

aws/logs_monitoring/lambda_function.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,17 @@ def add_metadata_to_lambda_log(event):
677677

678678
# Add any enhanced tags from metadata
679679
if IS_ENHANCED_METRICS_FILE_PRESENT:
680-
tags += get_enriched_lambda_log_tags(event)
680+
custom_lambda_tags = get_enriched_lambda_log_tags(event)
681+
682+
# Check if one of the Lambda's custom tags is env
683+
# If an env tag exists, remove the env:none placeholder
684+
custom_env_tag = next(
685+
(tag for tag in custom_lambda_tags if tag.startswith("env:")), None
686+
)
687+
if custom_env_tag is not None:
688+
event[DD_CUSTOM_TAGS] = event[DD_CUSTOM_TAGS].replace("env:none", "")
689+
690+
tags += custom_lambda_tags
681691

682692
# Dedup tags, so we don't end up with functionname twice
683693
tags = list(set(tags))
@@ -964,15 +974,15 @@ def awslogs_handler(event, context, metadata):
964974
if metadata[DD_SOURCE] == "lambda":
965975
log_group_parts = logs["logGroup"].split("/lambda/")
966976
if len(log_group_parts) > 1:
967-
function_name = log_group_parts[1].lower()
977+
lowercase_function_name = log_group_parts[1].lower()
968978
# Split the arn of the forwarder to extract the prefix
969979
arn_parts = context.invoked_function_arn.split("function:")
970980
if len(arn_parts) > 0:
971981
arn_prefix = arn_parts[0]
972-
# Rebuild the arn by replacing the function name
973-
arn = arn_prefix + "function:" + function_name
974-
# Add the arn as a log attribute
975-
arn_attributes = {"lambda": {"arn": arn}}
982+
# Rebuild the arn with the lowercased function name
983+
lowercase_arn = arn_prefix + "function:" + lowercase_function_name
984+
# Add the lowercased arn as a log attribute
985+
arn_attributes = {"lambda": {"arn": lowercase_arn}}
976986
aws_attributes = merge_dicts(aws_attributes, arn_attributes)
977987

978988
env_tag_exists = (

0 commit comments

Comments
 (0)