Skip to content

Commit

Permalink
Fix Unit tests, Integration tests, python formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0Aja committed Nov 29, 2023
1 parent 62545f3 commit 1ba9782
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
25 changes: 18 additions & 7 deletions aws/logs_monitoring/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ def add_metadata_to_lambda_log(event):
custom_lambda_tags = get_enriched_lambda_log_tags(event)

# If not set during parsing or set with a default value
# Set the `service` tag and metadata field. If the Lambda function is
# Try to set the `service` tag and metadata field. If the Lambda function is
# tagged with a `service` tag, use it, otherwise use the function name.
# Otherwise, remove the `service` tag from the Lambda function if it exists
if not event[DD_SERVICE] or event[DD_SERVICE] == event[DD_SOURCE]:
service_tag = next(
(tag for tag in custom_lambda_tags if tag.startswith("service:")),
f"service:{function_name}",
)
tags.append(service_tag)
event[DD_SERVICE] = service_tag.split(":")[1]
if service_tag:
tags.append(service_tag)
event[DD_SERVICE] = service_tag.split(":")[1]
else:
# remove the service tag from the cusotm lambda tags if it exists
# as we don't want to add it again
Expand Down Expand Up @@ -329,11 +330,17 @@ def extract_ddtags_from_message(event):
logger.debug(f"Failed to extract ddtags from: {event}")
return

event[DD_CUSTOM_TAGS] = _merge_custom_and_application_tags(event[DD_CUSTOM_TAGS], extracted_ddtags)
event[DD_CUSTOM_TAGS] = _merge_custom_and_application_tags(
event[DD_CUSTOM_TAGS], extracted_ddtags
)

# Extract service tag from message.ddtags if exists
## Extract service tag from message.ddtags if exists
if extracted_ddtags.contains("service:"):
event[DD_SERVICE] = next(tag[8:] for tag in extracted_ddtags.split(",") if tag.startswith("service:"))
event[DD_SERVICE] = next(
tag[8:]
for tag in extracted_ddtags.split(",")
if tag.startswith("service:")
)


def _merge_custom_and_application_tags(custom_tags, application_tags):
Expand Down Expand Up @@ -362,7 +369,11 @@ def _merge_custom_and_application_tags(custom_tags, application_tags):

for application_tag_key in application_tags_keys:
custom_tags_set = set(
[tag for tag in custom_tags_set if not tag.startswith(f"{application_tag_key}:")]
[
tag
for tag in custom_tags_set
if not tag.startswith(f"{application_tag_key}:")
]
)

return ",".join(list(custom_tags_set.union(application_tags_set)))
Expand Down
1 change: 0 additions & 1 deletion aws/logs_monitoring/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ def awslogs_handler(event, context, metadata):
# Returns DD_SOURCE by default
metadata[DD_SERVICE] = get_service_from_tags_and_remove_duplicates(metadata)


# Set host as log group where cloudwatch is source
if metadata[DD_SOURCE] == "cloudwatch" or metadata.get(DD_HOST, None) == None:
metadata[DD_HOST] = aws_attributes["aws"]["awslogs"]["logGroup"]
Expand Down
14 changes: 11 additions & 3 deletions aws/logs_monitoring/tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
parse_service_arn,
separate_security_hub_findings,
parse_aws_waf_logs,
get_service_from_tags,
get_service_from_tags_and_remove_duplicates,
get_state_machine_arn,
get_lower_cased_lambda_function_name,
)
Expand Down Expand Up @@ -958,14 +958,22 @@ def test_get_service_from_tags(self):
DD_SOURCE: "ecs",
DD_CUSTOM_TAGS: "env:dev,tag,stack:aws:ecs,service:web,version:v1",
}
self.assertEqual(get_service_from_tags(metadata), "web")
self.assertEqual(get_service_from_tags_and_remove_duplicates(metadata), "web")

def test_get_service_from_tags_default_to_source(self):
metadata = {
DD_SOURCE: "ecs",
DD_CUSTOM_TAGS: "env:dev,tag,stack:aws:ecs,version:v1",
}
self.assertEqual(get_service_from_tags(metadata), "ecs")
self.assertEqual(get_service_from_tags_and_remove_duplicates(metadata), "ecs")

def test_get_service_from_tags_removing_duplicates(self):
metadata = {
DD_SOURCE: "ecs",
DD_CUSTOM_TAGS: "env:dev,tag,stack:aws:ecs,service:web,version:v1,service:other",
}
self.assertEqual(get_service_from_tags_and_remove_duplicates(metadata), "web")
self.assertEqual(metadata[DD_CUSTOM_TAGS], "env:dev,tag,stack:aws:ecs,service:web,version:v1")


class TestParsingStepFunctionLogs(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"ddsource": "cloudwatch",
"ddsourcecategory": "aws",
"ddtags": "forwardername:test,forwarder_memorysize:1536,forwarder_version:<redacted from snapshot>,custom_tag1:value1,custom_tag2:value2",
"ddtags": "forwardername:test,custom_tag2:value2,custom_tag1:value1,forwarder_memorysize:1536,forwarder_version:<redacted from snapshot>",
"host": "testLogGroup",
"id": "eventId1",
"message": "{\"message\": \"hello world\"}",
Expand Down

0 comments on commit 1ba9782

Please sign in to comment.