From 3c3fd4922d4c5a6c4d566242951c4576cb3ea69b Mon Sep 17 00:00:00 2001 From: devonwarren Date: Thu, 4 Jul 2024 08:12:49 -0400 Subject: [PATCH 1/2] add logic for internal exceptions function --- structlog_sentry/__init__.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/structlog_sentry/__init__.py b/structlog_sentry/__init__.py index 02a68a1..4f477f4 100644 --- a/structlog_sentry/__init__.py +++ b/structlog_sentry/__init__.py @@ -8,7 +8,7 @@ from sentry_sdk import Hub from sentry_sdk.integrations.logging import _IGNORED_LOGGERS -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception +from sentry_sdk.utils import capture_internal_exceptions, event_from_exception, current_stacktrace from structlog.types import EventDict, ExcInfo, WrappedLogger @@ -121,10 +121,29 @@ def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]: if has_exc_info: client = self._get_hub().client options: dict[str, Any] = client.options if client else {} - event, hint = event_from_exception( - exc_info, - client_options=options, - ) + + # if the stack_info field is set utilize the internal sentry capture function to send out the exception + # with the stack trace. Ref: https://github.com/kiwicom/structlog-sentry/issues/87 + if event_dict.get("stack_info", False): + event, hint = {}, {} + with capture_internal_exceptions(): + event["threads"] = { + "values": [ + { + "stacktrace": current_stacktrace( + include_local_variables=options["include_local_variables"], + max_value_length=options["max_value_length"], + ), + "crashed": False, + "current": True, + } + ] + } + else: + event, hint = event_from_exception( + exc_info, + client_options=options, + ) else: event, hint = {}, {} From 45d9b298ac28e6fc5e0d7e2287c30297e328608e Mon Sep 17 00:00:00 2001 From: devonwarren Date: Thu, 11 Jul 2024 09:28:42 -0400 Subject: [PATCH 2/2] fix linting errors --- structlog_sentry/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/structlog_sentry/__init__.py b/structlog_sentry/__init__.py index 4f477f4..25ea91c 100644 --- a/structlog_sentry/__init__.py +++ b/structlog_sentry/__init__.py @@ -8,7 +8,11 @@ from sentry_sdk import Hub from sentry_sdk.integrations.logging import _IGNORED_LOGGERS -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception, current_stacktrace +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, + current_stacktrace, +) from structlog.types import EventDict, ExcInfo, WrappedLogger @@ -121,7 +125,7 @@ def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]: if has_exc_info: client = self._get_hub().client options: dict[str, Any] = client.options if client else {} - + # if the stack_info field is set utilize the internal sentry capture function to send out the exception # with the stack trace. Ref: https://github.com/kiwicom/structlog-sentry/issues/87 if event_dict.get("stack_info", False): @@ -131,7 +135,9 @@ def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]: "values": [ { "stacktrace": current_stacktrace( - include_local_variables=options["include_local_variables"], + include_local_variables=options[ + "include_local_variables" + ], max_value_length=options["max_value_length"], ), "crashed": False,