Skip to content

Commit b93576e

Browse files
committed
Merge remote-tracking branch 'origin/master' into wip/sessiontracking
2 parents 45956bd + a7b6623 commit b93576e

22 files changed

Lines changed: 347 additions & 129 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.1
2+
3+
* Fix infinite recursion bug in Celery integration.
4+
15
## 0.8.0
26

37
* Add the always_run option in excepthook integration.

mypy.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
[mypy]
22
allow_redefinition = True
3+
disallow_any_generics = True
4+
warn_redundant_casts = True
5+
6+
[mypy-sentry_sdk.integrations.*]
7+
disallow_any_generics = False
8+
9+
[mypy-sentry_sdk.utils]
10+
disallow_any_generics = False

sentry_sdk/client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
from sentry_sdk.utils import ContextVar
1919

2020
if False:
21-
from sentry_sdk.consts import ClientOptions
22-
from sentry_sdk.scope import Scope
2321
from typing import Any
22+
from typing import Callable
2423
from typing import Dict
2524
from typing import Optional
2625

26+
from sentry_sdk.consts import ClientOptions
27+
from sentry_sdk.scope import Scope
28+
from sentry_sdk.utils import Event, Hint
29+
2730

2831
_client_init_debug = ContextVar("client_init_debug")
2932

@@ -94,14 +97,16 @@ def dsn(self):
9497

9598
def _prepare_event(
9699
self,
97-
event, # type: Dict[str, Any]
98-
hint, # type: Optional[Dict[str, Any]]
100+
event, # type: Event
101+
hint, # type: Optional[Hint]
99102
scope, # type: Optional[Scope]
100103
):
101-
# type: (...) -> Optional[Dict[str, Any]]
104+
# type: (...) -> Optional[Event]
102105
if event.get("timestamp") is None:
103106
event["timestamp"] = datetime.utcnow()
104107

108+
hint = dict(hint or ()) # type: Hint
109+
105110
if scope is not None:
106111
event = scope.apply_to_event(event, hint)
107112
if event is None:
@@ -150,15 +155,15 @@ def _prepare_event(
150155
if before_send is not None:
151156
new_event = None
152157
with capture_internal_exceptions():
153-
new_event = before_send(event, hint)
158+
new_event = before_send(event, hint or {})
154159
if new_event is None:
155160
logger.info("before send dropped event (%s)", event)
156161
event = new_event # type: ignore
157162

158163
return event
159164

160165
def _is_ignored_error(self, event, hint):
161-
# type: (Dict[str, Any], Dict[str, Any]) -> bool
166+
# type: (Event, Hint) -> bool
162167
exc_info = hint.get("exc_info")
163168
if exc_info is None:
164169
return False
@@ -180,8 +185,8 @@ def _is_ignored_error(self, event, hint):
180185

181186
def _should_capture(
182187
self,
183-
event, # type: Dict[str, Any]
184-
hint, # type: Dict[str, Any]
188+
event, # type: Event
189+
hint, # type: Hint
185190
scope=None, # type: Scope
186191
):
187192
# type: (...) -> bool
@@ -227,6 +232,7 @@ def capture_event(self, event, hint=None, scope=None):
227232
return rv
228233

229234
def close(self, timeout=None, callback=None):
235+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
230236
"""
231237
Close the client and shut down the transport. Arguments have the same
232238
semantics as `self.flush()`.
@@ -237,6 +243,7 @@ def close(self, timeout=None, callback=None):
237243
self.transport = None
238244

239245
def flush(self, timeout=None, callback=None):
246+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
240247
"""
241248
Wait `timeout` seconds for the current events to be sent. If no
242249
`timeout` is provided, the `shutdown_timeout` option value is used.

sentry_sdk/consts.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
from typing import Callable
77
from typing import Union
88
from typing import List
9+
from typing import Type
910

1011
from sentry_sdk.transport import Transport
1112
from sentry_sdk.integrations import Integration
1213

14+
from sentry_sdk.utils import Event, EventProcessor, BreadcrumbProcessor
15+
1316
ClientOptions = TypedDict(
1417
"ClientOptions",
1518
{
@@ -25,15 +28,17 @@
2528
"in_app_exclude": List[str],
2629
"default_integrations": bool,
2730
"dist": Optional[str],
28-
"transport": Optional[Union[Transport, type, Callable]],
31+
"transport": Optional[
32+
Union[Transport, Type[Transport], Callable[[Event], None]]
33+
],
2934
"sample_rate": int,
3035
"send_default_pii": bool,
3136
"http_proxy": Optional[str],
3237
"https_proxy": Optional[str],
3338
"ignore_errors": List[type],
3439
"request_bodies": str,
35-
"before_send": Optional[Callable],
36-
"before_breadcrumb": Optional[Callable],
40+
"before_send": Optional[EventProcessor],
41+
"before_breadcrumb": Optional[BreadcrumbProcessor],
3742
"debug": bool,
3843
"attach_stacktrace": bool,
3944
"ca_certs": Optional[str],
@@ -44,7 +49,7 @@
4449
)
4550

4651

47-
VERSION = "0.8.0"
52+
VERSION = "0.8.1"
4853
DEFAULT_SERVER_NAME = socket.gethostname() if hasattr(socket, "gethostname") else None
4954
DEFAULT_OPTIONS = {
5055
"dsn": None,

sentry_sdk/hub.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121

2222
if False:
23+
from contextlib import ContextManager
24+
2325
from typing import Union
2426
from typing import Any
2527
from typing import Optional
26-
from typing import Dict
2728
from typing import Tuple
2829
from typing import List
2930
from typing import Callable
31+
from typing import Generator
3032
from typing import overload
31-
from contextlib import ContextManager
33+
3234
from sentry_sdk.integrations import Integration
35+
from sentry_sdk.utils import Event, Hint, Breadcrumb, BreadcrumbHint
3336
else:
3437

3538
def overload(x):
@@ -254,7 +257,7 @@ def bind_client(self, new):
254257
self._stack[-1] = (new, top[1])
255258

256259
def capture_event(self, event, hint=None):
257-
# type: (Dict[str, Any], Dict[str, Any]) -> Optional[str]
260+
# type: (Event, Hint) -> Optional[str]
258261
"""Captures an event. The return value is the ID of the event.
259262
260263
The event is a dictionary following the Sentry v7/v8 protocol
@@ -271,7 +274,7 @@ def capture_event(self, event, hint=None):
271274
return None
272275

273276
def capture_message(self, message, level=None):
274-
# type: (str, Optional[Any]) -> Optional[str]
277+
# type: (str, Optional[str]) -> Optional[str]
275278
"""Captures a message. The message is just a string. If no level
276279
is provided the default level is `info`.
277280
"""
@@ -311,7 +314,7 @@ def _capture_internal_exception(self, exc_info):
311314
logger.error("Internal error in sentry_sdk", exc_info=exc_info)
312315

313316
def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
314-
# type: (Dict[str, Any], Dict[str, Any], **Any) -> None
317+
# type: (Optional[Breadcrumb], Optional[BreadcrumbHint], **Any) -> None
315318
"""Adds a breadcrumb. The breadcrumbs are a dictionary with the
316319
data as the sentry v7/v8 protocol expects. `hint` is an optional
317320
value that can be used by `before_breadcrumb` to customize the
@@ -322,26 +325,27 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
322325
logger.info("Dropped breadcrumb because no client bound")
323326
return
324327

325-
crumb = dict(crumb or ()) # type: Dict[str, Any]
328+
crumb = dict(crumb or ()) # type: Breadcrumb
326329
crumb.update(kwargs)
327330
if not crumb:
328331
return
329332

330-
hint = dict(hint or ())
333+
hint = dict(hint or ()) # type: Hint
331334

332335
if crumb.get("timestamp") is None:
333336
crumb["timestamp"] = datetime.utcnow()
334337
if crumb.get("type") is None:
335338
crumb["type"] = "default"
336339

337-
original_crumb = crumb
338340
if client.options["before_breadcrumb"] is not None:
339-
crumb = client.options["before_breadcrumb"](crumb, hint)
341+
new_crumb = client.options["before_breadcrumb"](crumb, hint)
342+
else:
343+
new_crumb = crumb
340344

341-
if crumb is not None:
342-
scope._breadcrumbs.append(crumb)
345+
if new_crumb is not None:
346+
scope._breadcrumbs.append(new_crumb)
343347
else:
344-
logger.info("before breadcrumb dropped breadcrumb (%s)", original_crumb)
348+
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)
345349

346350
max_breadcrumbs = client.options["max_breadcrumbs"] # type: int
347351
while len(scope._breadcrumbs) > max_breadcrumbs:
@@ -472,12 +476,14 @@ def inner():
472476
return inner()
473477

474478
def flush(self, timeout=None, callback=None):
479+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
475480
"""Alias for self.client.flush"""
476481
client, scope = self._stack[-1]
477482
if client is not None:
478483
return client.flush(timeout=timeout, callback=callback)
479484

480485
def iter_trace_propagation_headers(self):
486+
# type: () -> Generator[Tuple[str, str], None, None]
481487
client, scope = self._stack[-1]
482488
if scope._span is None:
483489
return

sentry_sdk/integrations/argv.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from sentry_sdk.scope import add_global_event_processor
88

99
if False:
10-
from typing import Any
11-
from typing import Dict
10+
from typing import Optional
11+
12+
from sentry_sdk.utils import Event, Hint
1213

1314

1415
class ArgvIntegration(Integration):
@@ -19,7 +20,7 @@ def setup_once():
1920
# type: () -> None
2021
@add_global_event_processor
2122
def processor(event, hint):
22-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
23+
# type: (Event, Optional[Hint]) -> Optional[Event]
2324
if Hub.current.get_integration(ArgvIntegration) is not None:
2425
extra = event.setdefault("extra", {})
2526
# If some event processor decided to set extra to e.g. an

sentry_sdk/integrations/dedupe.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from sentry_sdk.scope import add_global_event_processor
55

66
if False:
7-
from typing import Any
8-
from typing import Dict
97
from typing import Optional
108

9+
from sentry_sdk.utils import Event, Hint
10+
1111

1212
class DedupeIntegration(Integration):
1313
identifier = "dedupe"
@@ -21,13 +21,21 @@ def setup_once():
2121
# type: () -> None
2222
@add_global_event_processor
2323
def processor(event, hint):
24-
# type: (Dict[str, Any], Dict[str, Any]) -> Optional[Dict[str, Any]]
24+
# type: (Event, Optional[Hint]) -> Optional[Event]
25+
if hint is None:
26+
return event
27+
2528
integration = Hub.current.get_integration(DedupeIntegration)
26-
if integration is not None:
27-
exc_info = hint.get("exc_info", None)
28-
if exc_info is not None:
29-
exc = exc_info[1]
30-
if integration._last_seen.get(None) is exc:
31-
return None
32-
integration._last_seen.set(exc)
29+
30+
if integration is None:
31+
return event
32+
33+
exc_info = hint.get("exc_info", None)
34+
if exc_info is None:
35+
return event
36+
37+
exc = exc_info[1]
38+
if integration._last_seen.get(None) is exc:
39+
return None
40+
integration._last_seen.set(exc)
3341
return event

sentry_sdk/integrations/django/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111

1212
if False:
1313
from typing import Any
14+
from typing import Callable
1415
from typing import Dict
16+
from typing import List
17+
from typing import Optional
1518
from typing import Tuple
1619
from typing import Union
17-
from sentry_sdk.integrations.wsgi import _ScopedResponse
18-
from typing import Callable
20+
1921
from django.core.handlers.wsgi import WSGIRequest # type: ignore
2022
from django.http.response import HttpResponse # type: ignore
2123
from django.http.request import QueryDict # type: ignore
2224
from django.utils.datastructures import MultiValueDict # type: ignore
23-
from typing import List
25+
26+
from sentry_sdk.integrations.wsgi import _ScopedResponse
27+
from sentry_sdk.utils import Event, Hint
2428

2529

2630
try:
@@ -137,7 +141,10 @@ def sentry_patched_get_response(self, request):
137141

138142
@add_global_event_processor
139143
def process_django_templates(event, hint):
140-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
144+
# type: (Event, Optional[Hint]) -> Optional[Event]
145+
if hint is None:
146+
return event
147+
141148
exc_info = hint.get("exc_info", None)
142149

143150
if exc_info is None:

sentry_sdk/integrations/modules.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from typing import Tuple
1111
from typing import Iterator
1212

13+
from sentry_sdk.utils import Event
14+
1315
_installed_modules = None
1416

1517

@@ -40,7 +42,7 @@ def setup_once():
4042
# type: () -> None
4143
@add_global_event_processor
4244
def processor(event, hint):
43-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
45+
# type: (Event, Any) -> Dict[str, Any]
4446
if Hub.current.get_integration(ModulesIntegration) is not None:
4547
event["modules"] = dict(_get_installed_modules())
4648
return event

0 commit comments

Comments
 (0)