Skip to content
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

ref(beam): Use new scopes API #2879

Merged
merged 7 commits into from
Mar 25, 2024
Merged
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
49 changes: 21 additions & 28 deletions sentry_sdk/integrations/beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
import types
from functools import wraps

from sentry_sdk.hub import Hub
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception, reraise
import sentry_sdk
from sentry_sdk.integrations import Integration
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
reraise,
)
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any
from typing import Iterator
from typing import TypeVar
from typing import Optional
from typing import Callable

from sentry_sdk.client import BaseClient
from sentry_sdk._types import ExcInfo

T = TypeVar("T")
Expand Down Expand Up @@ -113,63 +116,53 @@ def _wrap_task_call(func):
# type: (F) -> F
"""
Wrap task call with a try catch to get exceptions.
Pass the client on to raise_exception so it can get rebinded.
"""
client = Hub.current.client

@wraps(func)
def _inner(*args, **kwargs):
# type: (*Any, **Any) -> Any
try:
gen = func(*args, **kwargs)
except Exception:
raise_exception(client)
raise_exception()

if not isinstance(gen, types.GeneratorType):
return gen
return _wrap_generator_call(gen, client)
return _wrap_generator_call(gen)

setattr(_inner, USED_FUNC, True)
return _inner # type: ignore


def _capture_exception(exc_info, hub):
# type: (ExcInfo, Hub) -> None
@ensure_integration_enabled(BeamIntegration)
def _capture_exception(exc_info):
# type: (ExcInfo) -> None
"""
Send Beam exception to Sentry.
"""
integration = hub.get_integration(BeamIntegration)
if integration is None:
return

client = hub.client
if client is None:
return
client = sentry_sdk.get_client()

event, hint = event_from_exception(
exc_info,
client_options=client.options,
mechanism={"type": "beam", "handled": False},
)
hub.capture_event(event, hint=hint)
sentry_sdk.capture_event(event, hint=hint)


def raise_exception(client):
# type: (Optional[BaseClient]) -> None
def raise_exception():
# type: () -> None
"""
Raise an exception. If the client is not in the hub, rebind it.
Raise an exception.
"""
hub = Hub.current
if hub.client is None:
hub.bind_client(client)
exc_info = sys.exc_info()
with capture_internal_exceptions():
_capture_exception(exc_info, hub)
_capture_exception(exc_info)
reraise(*exc_info)


def _wrap_generator_call(gen, client):
# type: (Iterator[T], Optional[BaseClient]) -> Iterator[T]
def _wrap_generator_call(gen):
# type: (Iterator[T]) -> Iterator[T]
"""
Wrap the generator to handle any failures.
"""
Expand All @@ -179,4 +172,4 @@ def _wrap_generator_call(gen, client):
except StopIteration:
break
except Exception:
raise_exception(client)
raise_exception()
Loading