diff --git a/structlog_sentry/__init__.py b/structlog_sentry/__init__.py index 02a68a1..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 +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, + current_stacktrace, +) from structlog.types import EventDict, ExcInfo, WrappedLogger @@ -121,10 +125,31 @@ 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 = {}, {}