Skip to content

a try-catch-all solution #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 20 additions & 10 deletions datadog_lambda/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,29 @@ def extract_trigger_tags(event: dict, context: Any) -> dict:
"""
Parses the trigger event object to get tags to be added to the span metadata
"""
trigger_tags = {}
event_source = parse_event_source(event)
if event_source.to_string() is not None and event_source.to_string() != "unknown":
trigger_tags["function_trigger.event_source"] = event_source.to_string()
try:
trigger_tags = {}
event_source = parse_event_source(event)
if (
event_source.to_string() is not None
and event_source.to_string() != "unknown"
):
trigger_tags["function_trigger.event_source"] = event_source.to_string()

event_source_arn = get_event_source_arn(event_source, event, context)
if event_source_arn:
trigger_tags["function_trigger.event_source_arn"] = event_source_arn

event_source_arn = get_event_source_arn(event_source, event, context)
if event_source_arn:
trigger_tags["function_trigger.event_source_arn"] = event_source_arn
if event_source.event_type in _http_event_types:
trigger_tags.update(extract_http_tags(event))

if event_source.event_type in _http_event_types:
trigger_tags.update(extract_http_tags(event))
return trigger_tags
except Exception as e:
import logging

return trigger_tags
logger = logging.getLogger(__name__)
logger.info(f"Datadog failed to extract trigger tags: {e}")
return {}


_str_http_triggers = [et.value for et in _http_event_types]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ def test_extract_trigger_tags_list_type_event(self):
tags = extract_trigger_tags(event, ctx)
self.assertEqual(tags, {})

def test_extract_trigger_tags_error_handling(self):
event = {
"requestContext": "not_a_dict"
} # This would cause an error as requestContext is not a dict
ctx = get_mock_context()

tags = extract_trigger_tags(event, ctx)
self.assertEqual(tags, {})


class ExtractHTTPStatusCodeTag(unittest.TestCase):
def test_extract_http_status_code_tag_from_response_dict(self):
Expand Down
Loading