Skip to content

Commit 8022c6f

Browse files
committed
fix: typing
1 parent 4ebac46 commit 8022c6f

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

sentry_sdk/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ def _should_capture(
244244
and random.random() >= self.options["sample_rate"]
245245
):
246246
# record a lost event if we did not sample this.
247-
self.transport.record_lost_event("sample_rate", "error")
247+
if self.transport:
248+
self.transport.record_lost_event("sample_rate", "error")
248249
return False
249250

250251
if self._is_ignored_error(event, hint):

sentry_sdk/tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def finish(self, hub=None):
517517
# This is not entirely accurate because discards here are not
518518
# exclusively based on sample rate but also traces sampler, but
519519
# we handle this the same here.
520-
if client:
520+
if client and client.transport:
521521
client.transport.record_lost_event("sample_rate", "transaction")
522522

523523
return None

sentry_sdk/transport.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88

99
from datetime import datetime, timedelta
10+
from collections import defaultdict
1011

1112
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions, json_dumps
1213
from sentry_sdk.worker import BackgroundWorker
@@ -23,6 +24,7 @@
2324
from typing import Tuple
2425
from typing import Type
2526
from typing import Union
27+
from typing import DefaultDict
2628

2729
from urllib3.poolmanager import PoolManager # type: ignore
2830
from urllib3.poolmanager import ProxyManager
@@ -93,12 +95,16 @@ def kill(self):
9395
"""Forcefully kills the transport."""
9496
pass
9597

96-
def record_lost_event(self, reason, data_category="default"):
98+
def record_lost_event(
99+
self,
100+
reason, # type: str
101+
data_category="default", # type: str
102+
):
103+
# type: (...) -> None
97104
"""This increments a counter for event loss by reason and
98105
data category.
99106
"""
100-
# type: (...) -> None
101-
pass
107+
return None
102108

103109
def __del__(self):
104110
# type: () -> None
@@ -139,8 +145,10 @@ def __init__(
139145
self._auth = self.parsed_dsn.to_auth("sentry.python/%s" % VERSION)
140146
self._disabled_until = {} # type: Dict[DataCategory, datetime]
141147
self._retry = urllib3.util.Retry()
142-
self._discarded_events = {}
143-
self._last_event_loss_sent = None
148+
self._discarded_events = defaultdict(
149+
int
150+
) # type: DefaultDict[Tuple[str, str], int]
151+
self._last_event_loss_sent = None # type: Optional[float]
144152

145153
self._pool = self._make_pool(
146154
self.parsed_dsn,
@@ -153,10 +161,13 @@ def __init__(
153161

154162
self.hub_cls = Hub
155163

156-
def record_lost_event(self, reason, data_category="default"):
157-
# This is not locked because we are okay with small mismeasuring.
158-
key = (data_category, reason)
159-
self._discarded_events[key] = self._discarded_events.get(key, 0) + 1
164+
def record_lost_event(
165+
self,
166+
reason, # type: str
167+
data_category="default", # type: str
168+
):
169+
# type: (...) -> None
170+
self._discarded_events[data_category, reason] += 1
160171

161172
def _update_rate_limits(self, response):
162173
# type: (urllib3.HTTPResponse) -> None
@@ -184,14 +195,16 @@ def _send_request(
184195
endpoint_type="store", # type: EndpointType
185196
envelope=None, # type: Optional[Envelope]
186197
):
198+
# type: (...) -> None
199+
187200
def record_loss(reason):
201+
# type: (str) -> None
188202
if envelope is None:
189203
self.record_lost_event(reason, "error")
190204
else:
191205
for item in envelope.items:
192206
self.record_lost_event(reason, item.data_category)
193207

194-
# type: (...) -> None
195208
headers.update(
196209
{
197210
"User-Agent": str(self._auth.client),
@@ -237,6 +250,7 @@ def on_dropped_event(self, reason):
237250
return None
238251

239252
def _flush_stats(self, force=False):
253+
# type: (bool) -> None
240254
discarded_events = self._discarded_events
241255

242256
if not (
@@ -247,7 +261,7 @@ def _flush_stats(self, force=False):
247261
):
248262
return
249263

250-
self._discarded_events = {}
264+
self._discarded_events = defaultdict(int)
251265
self._last_event_loss_sent = time.time()
252266

253267
client_report = Item(

0 commit comments

Comments
 (0)