Skip to content

Commit 70a9e5a

Browse files
committed
Migrated sentry reporter to use sentry-sdk
The Raven client is abandonware.
1 parent 21c7b53 commit 70a9e5a

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

docs/versionhistory.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.
55

66
**UNRELEASED**
77

8+
- **BACKWARDS INCOMPATIBLE** Switched Sentry reporter to use sentry-sdk instead of raven
89
- Added support for Python 3.10
910
- Dropped support for Python 3.5 and 3.6
1011

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ doc = [
4545
"sphinx-autodoc-typehints >= 1.2.0",
4646
"sphinxcontrib-asyncio >= 0.2.0",
4747
]
48-
sentry = [
49-
"raven >= 6.1",
50-
"raven-aiohttp >= 0.5.0"
51-
]
48+
sentry = ["sentry-sdk >= 1.5"]
5249
raygun = ["raygun4py >= 4.3"]
5350

5451
[project.entry-points."asphalt.components"]
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

33
import logging
4-
from typing import Any
4+
from typing import Any, Dict, Sequence, Union
55

6-
from asphalt.core import Context
7-
from raven import Client
8-
from raven_aiohttp import AioHttpTransport
6+
import sentry_sdk
7+
from asphalt.core import Context, resolve_reference
8+
from sentry_sdk.integrations import Integration
9+
from typeguard import check_argument_types
910

1011
from asphalt.exceptions.api import ExceptionReporter
1112

@@ -18,31 +19,37 @@ class SentryExceptionReporter(ExceptionReporter):
1819
1920
To use this backend, install asphalt-exceptions with the ``sentry`` extra.
2021
21-
All keyword arguments are directly passed to :class:`raven.Client`.
22+
All keyword arguments are directly passed to :func:`sentry_sdk.init`.
2223
The following defaults are set for the client arguments:
2324
2425
* environment: "development" or "production", depending on the ``__debug__`` flag
25-
* transport: :class:`raven_aiohttp.AioHttpTransport`
26-
* enable_breadcrumbs: ``False``
27-
* install_logging_hook: ``False``
2826
29-
The extras passed to this backend are passed to :meth:`raven.Client.capture` as keyword
27+
The extras passed to this backend are passed to :func:`sentry_sdk.capture_exception` as keyword
3028
arguments.
3129
32-
For more information, see the `Raven client documentation`_.
30+
For more information, see the `Sentry SDK documentation`_.
3331
3432
.. _Sentry: https://sentry.io/
35-
.. _Raven client documentation: https://docs.sentry.io/clients/python/#configuring-the-client
33+
.. _Sentry SDK documentation: https://docs.sentry.io/platforms/python/
3634
"""
3735

38-
def __init__(self, **client_args) -> None:
39-
client_args.setdefault(
40-
"environment", "development" if __debug__ else "production"
41-
)
42-
client_args.setdefault("transport", AioHttpTransport)
43-
client_args.setdefault("enable_breadcrumbs", False)
44-
client_args.setdefault("install_logging_hook", False)
45-
self.client = Client(**client_args)
36+
def __init__(
37+
self, integrations: Sequence[Union[Integration, Dict[str, Any]]] = (), **options
38+
) -> None:
39+
check_argument_types()
40+
options.setdefault("environment", "development" if __debug__ else "production")
41+
42+
integrations_: list[Integration] = []
43+
for integration in integrations:
44+
if isinstance(integration, dict):
45+
integration_class = resolve_reference(integration["type"])
46+
integration = integration_class(
47+
*integration.get("args", ()), **integration.get("kwargs", {})
48+
)
49+
50+
integrations_.append(integration)
51+
52+
sentry_sdk.init(integrations=integrations_, **options)
4653

4754
def report_exception(
4855
self,
@@ -51,5 +58,4 @@ def report_exception(
5158
message: str,
5259
extra: dict[str, Any],
5360
) -> None:
54-
exc_info = type(exception), exception, exception.__traceback__
55-
self.client.captureException(exc_info, message=message, **extra)
61+
sentry_sdk.capture_exception(exception, **extra)

tests/reporters/test_sentry.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
import asyncio
2-
from unittest.mock import patch
1+
from unittest.mock import MagicMock
32

43
import pytest
54
from asphalt.core import Context
5+
from sentry_sdk import Transport
6+
from sentry_sdk.integrations import Integration
67

78
from asphalt.exceptions.reporters.sentry import SentryExceptionReporter
89

910

11+
class DummyIntegration(Integration):
12+
def __init__(self, arg_a, arg_b):
13+
self.arg_a = arg_a
14+
self.arg_b = arg_b
15+
16+
@staticmethod
17+
def setup_once():
18+
pass
19+
20+
1021
@pytest.mark.asyncio
1122
async def test_sentry():
1223
exception = None
@@ -15,9 +26,20 @@ async def test_sentry():
1526
except ZeroDivisionError as exc:
1627
exception = exc
1728

18-
reporter = SentryExceptionReporter(dsn="http://username:[email protected]/000000")
19-
with patch.object(reporter.client, "send") as send:
20-
reporter.report_exception(Context(), exception, "test exception", {})
29+
transport = MagicMock(Transport)
30+
reporter = SentryExceptionReporter(
31+
dsn="http://username:[email protected]/000000", transport=transport
32+
)
33+
reporter.report_exception(Context(), exception, "test exception", {})
34+
35+
transport.capture_event.assert_called_once()
36+
2137

22-
await asyncio.sleep(0.1)
23-
assert send.call_count == 1
38+
def test_integrations():
39+
integrations = [
40+
DummyIntegration(1, 2),
41+
{"type": f"{__name__}:DummyIntegration", "args": [3], "kwargs": {"arg_b": 4}},
42+
]
43+
SentryExceptionReporter(
44+
dsn="http://username:[email protected]/000000", integrations=integrations
45+
)

0 commit comments

Comments
 (0)