Skip to content

Commit f43c850

Browse files
committed
Changes from background composer bc-4ef8bab4-ef8a-43f8-807e-a78d651cf0a1
1 parent 98f836c commit f43c850

File tree

5 files changed

+110
-204
lines changed

5 files changed

+110
-204
lines changed

sentry_sdk/integrations/pymongo.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
]
4242

4343

44-
def _strip_pii(command):
45-
# type: (Dict[str, Any]) -> Dict[str, Any]
44+
def _strip_pii(command: "Dict[str, Any]") -> "Dict[str, Any]":
4645
for key in command:
4746
is_safe_field = key in SAFE_COMMAND_ATTRIBUTES
4847
if is_safe_field:
@@ -84,8 +83,7 @@ def _strip_pii(command):
8483
return command
8584

8685

87-
def _get_db_data(event):
88-
# type: (Any) -> Dict[str, Any]
86+
def _get_db_data(event: Any) -> "Dict[str, Any]":
8987
data = {}
9088

9189
data[SPANDATA.DB_SYSTEM] = "mongodb"
@@ -106,16 +104,13 @@ def _get_db_data(event):
106104

107105

108106
class CommandTracer(monitoring.CommandListener):
109-
def __init__(self):
110-
# type: () -> None
111-
self._ongoing_operations = {} # type: Dict[int, Span]
107+
def __init__(self) -> None:
108+
self._ongoing_operations: "Dict[int, Span]" = {}
112109

113-
def _operation_key(self, event):
114-
# type: (Union[CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent]) -> int
110+
def _operation_key(self, event: "Union[CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent]") -> int:
115111
return event.request_id
116112

117-
def started(self, event):
118-
# type: (CommandStartedEvent) -> None
113+
def started(self, event: "CommandStartedEvent") -> None:
119114
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
120115
return
121116

@@ -172,8 +167,7 @@ def started(self, event):
172167

173168
self._ongoing_operations[self._operation_key(event)] = span.__enter__()
174169

175-
def failed(self, event):
176-
# type: (CommandFailedEvent) -> None
170+
def failed(self, event: "CommandFailedEvent") -> None:
177171
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
178172
return
179173

@@ -184,8 +178,7 @@ def failed(self, event):
184178
except KeyError:
185179
return
186180

187-
def succeeded(self, event):
188-
# type: (CommandSucceededEvent) -> None
181+
def succeeded(self, event: "CommandSucceededEvent") -> None:
189182
if sentry_sdk.get_client().get_integration(PyMongoIntegration) is None:
190183
return
191184

@@ -202,6 +195,5 @@ class PyMongoIntegration(Integration):
202195
origin = f"auto.db.{identifier}"
203196

204197
@staticmethod
205-
def setup_once():
206-
# type: () -> None
198+
def setup_once() -> None:
207199
monitoring.register(CommandTracer())

sentry_sdk/integrations/sanic.py

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ class SanicIntegration(Integration):
5959
origin = f"auto.http.{identifier}"
6060
version = None
6161

62-
def __init__(self, unsampled_statuses=frozenset({404})):
63-
# type: (Optional[Container[int]]) -> None
62+
def __init__(self, unsampled_statuses: "Optional[Container[int]]" = frozenset({404})) -> None:
6463
"""
6564
The unsampled_statuses parameter can be used to specify for which HTTP statuses the
6665
transactions should not be sent to Sentry. By default, transactions are sent for all
@@ -70,8 +69,7 @@ def __init__(self, unsampled_statuses=frozenset({404})):
7069
self._unsampled_statuses = unsampled_statuses or set()
7170

7271
@staticmethod
73-
def setup_once():
74-
# type: () -> None
72+
def setup_once() -> None:
7573
SanicIntegration.version = parse_version(SANIC_VERSION)
7674
_check_minimum_version(SanicIntegration, SanicIntegration.version)
7775

@@ -103,56 +101,45 @@ def setup_once():
103101

104102

105103
class SanicRequestExtractor(RequestExtractor):
106-
def content_length(self):
107-
# type: () -> int
104+
def content_length(self) -> int:
108105
if self.request.body is None:
109106
return 0
110107
return len(self.request.body)
111108

112-
def cookies(self):
113-
# type: () -> Dict[str, str]
109+
def cookies(self) -> "Dict[str, str]":
114110
return dict(self.request.cookies)
115111

116-
def raw_data(self):
117-
# type: () -> bytes
112+
def raw_data(self) -> bytes:
118113
return self.request.body
119114

120-
def form(self):
121-
# type: () -> RequestParameters
115+
def form(self) -> "RequestParameters":
122116
return self.request.form
123117

124-
def is_json(self):
125-
# type: () -> bool
118+
def is_json(self) -> bool:
126119
raise NotImplementedError()
127120

128-
def json(self):
129-
# type: () -> Optional[Any]
121+
def json(self) -> "Optional[Any]":
130122
return self.request.json
131123

132-
def files(self):
133-
# type: () -> RequestParameters
124+
def files(self) -> "RequestParameters":
134125
return self.request.files
135126

136-
def size_of_file(self, file):
137-
# type: (Any) -> int
127+
def size_of_file(self, file: Any) -> int:
138128
return len(file.body or ())
139129

140130

141-
def _setup_sanic():
142-
# type: () -> None
131+
def _setup_sanic() -> None:
143132
Sanic._startup = _startup
144133
ErrorHandler.lookup = _sentry_error_handler_lookup
145134

146135

147-
def _setup_legacy_sanic():
148-
# type: () -> None
136+
def _setup_legacy_sanic() -> None:
149137
Sanic.handle_request = _legacy_handle_request
150138
Router.get = _legacy_router_get
151139
ErrorHandler.lookup = _sentry_error_handler_lookup
152140

153141

154-
async def _startup(self):
155-
# type: (Sanic) -> None
142+
async def _startup(self: Sanic) -> None:
156143
# This happens about as early in the lifecycle as possible, just after the
157144
# Request object is created. The body has not yet been consumed.
158145
self.signal("http.lifecycle.request")(_context_enter)
@@ -171,8 +158,7 @@ async def _startup(self):
171158
await old_startup(self)
172159

173160

174-
async def _context_enter(request):
175-
# type: (Request) -> None
161+
async def _context_enter(request: "Request") -> None:
176162
request.ctx._sentry_do_integration = (
177163
sentry_sdk.get_client().get_integration(SanicIntegration) is not None
178164
)
@@ -203,8 +189,7 @@ async def _context_enter(request):
203189
).__enter__()
204190

205191

206-
async def _context_exit(request, response=None):
207-
# type: (Request, Optional[BaseHTTPResponse]) -> None
192+
async def _context_exit(request: "Request", response: "Optional[BaseHTTPResponse]" = None) -> None:
208193
with capture_internal_exceptions():
209194
if not request.ctx._sentry_do_integration:
210195
return
@@ -233,17 +218,15 @@ async def _context_exit(request, response=None):
233218
request.ctx._sentry_scope_manager.__exit__(None, None, None)
234219

235220

236-
async def _set_transaction(request, route, **_):
237-
# type: (Request, Route, **Any) -> None
221+
async def _set_transaction(request: "Request", route: "Route", **_: Any) -> None:
238222
if request.ctx._sentry_do_integration:
239223
with capture_internal_exceptions():
240224
scope = sentry_sdk.get_current_scope()
241225
route_name = route.name.replace(request.app.name, "").strip(".")
242226
scope.set_transaction_name(route_name, source=TransactionSource.COMPONENT)
243227

244228

245-
def _sentry_error_handler_lookup(self, exception, *args, **kwargs):
246-
# type: (Any, Exception, *Any, **Any) -> Optional[object]
229+
def _sentry_error_handler_lookup(self: Any, exception: Exception, *args: Any, **kwargs: Any) -> "Optional[object]":
247230
_capture_exception(exception)
248231
old_error_handler = old_error_handler_lookup(self, exception, *args, **kwargs)
249232

@@ -253,8 +236,7 @@ def _sentry_error_handler_lookup(self, exception, *args, **kwargs):
253236
if sentry_sdk.get_client().get_integration(SanicIntegration) is None:
254237
return old_error_handler
255238

256-
async def sentry_wrapped_error_handler(request, exception):
257-
# type: (Request, Exception) -> Any
239+
async def sentry_wrapped_error_handler(request: "Request", exception: Exception) -> Any:
258240
try:
259241
response = old_error_handler(request, exception)
260242
if isawaitable(response):
@@ -276,8 +258,7 @@ async def sentry_wrapped_error_handler(request, exception):
276258
return sentry_wrapped_error_handler
277259

278260

279-
async def _legacy_handle_request(self, request, *args, **kwargs):
280-
# type: (Any, Request, *Any, **Any) -> Any
261+
async def _legacy_handle_request(self: Any, request: "Request", *args: Any, **kwargs: Any) -> Any:
281262
if sentry_sdk.get_client().get_integration(SanicIntegration) is None:
282263
return await old_handle_request(self, request, *args, **kwargs)
283264

@@ -294,8 +275,7 @@ async def _legacy_handle_request(self, request, *args, **kwargs):
294275
return response
295276

296277

297-
def _legacy_router_get(self, *args):
298-
# type: (Any, Union[Any, Request]) -> Any
278+
def _legacy_router_get(self: Any, *args: "Union[Any, Request]") -> Any:
299279
rv = old_router_get(self, *args)
300280
if sentry_sdk.get_client().get_integration(SanicIntegration) is not None:
301281
with capture_internal_exceptions():
@@ -325,8 +305,7 @@ def _legacy_router_get(self, *args):
325305

326306

327307
@ensure_integration_enabled(SanicIntegration)
328-
def _capture_exception(exception):
329-
# type: (Union[ExcInfo, BaseException]) -> None
308+
def _capture_exception(exception: "Union[ExcInfo, BaseException]") -> None:
330309
with capture_internal_exceptions():
331310
event, hint = event_from_exception(
332311
exception,
@@ -340,10 +319,8 @@ def _capture_exception(exception):
340319
sentry_sdk.capture_event(event, hint=hint)
341320

342321

343-
def _make_request_processor(weak_request):
344-
# type: (Callable[[], Request]) -> EventProcessor
345-
def sanic_processor(event, hint):
346-
# type: (Event, Optional[Hint]) -> Optional[Event]
322+
def _make_request_processor(weak_request: "Callable[[], Request]") -> "EventProcessor":
323+
def sanic_processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]":
347324

348325
try:
349326
if hint and issubclass(hint["exc_info"][0], SanicException):

sentry_sdk/integrations/sqlalchemy.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class SqlalchemyIntegration(Integration):
2929
origin = f"auto.db.{identifier}"
3030

3131
@staticmethod
32-
def setup_once():
33-
# type: () -> None
32+
def setup_once() -> None:
3433
version = parse_version(SQLALCHEMY_VERSION)
3534
_check_minimum_version(SqlalchemyIntegration, version)
3635

@@ -41,9 +40,8 @@ def setup_once():
4140

4241
@ensure_integration_enabled(SqlalchemyIntegration)
4342
def _before_cursor_execute(
44-
conn, cursor, statement, parameters, context, executemany, *args
45-
):
46-
# type: (Any, Any, Any, Any, Any, bool, *Any) -> None
43+
conn: Any, cursor: Any, statement: Any, parameters: Any, context: Any, executemany: bool, *args: Any
44+
) -> None:
4745
ctx_mgr = record_sql_queries(
4846
cursor,
4947
statement,
@@ -62,13 +60,12 @@ def _before_cursor_execute(
6260

6361

6462
@ensure_integration_enabled(SqlalchemyIntegration)
65-
def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
66-
# type: (Any, Any, Any, Any, Any, *Any) -> None
67-
ctx_mgr = getattr(
63+
def _after_cursor_execute(conn: Any, cursor: Any, statement: Any, parameters: Any, context: Any, *args: Any) -> None:
64+
ctx_mgr: "Optional[ContextManager[Any]]" = getattr(
6865
context, "_sentry_sql_span_manager", None
69-
) # type: Optional[ContextManager[Any]]
66+
)
7067

71-
span = getattr(context, "_sentry_sql_span", None) # type: Optional[Span]
68+
span: "Optional[Span]" = getattr(context, "_sentry_sql_span", None)
7269
if span is not None:
7370
with capture_internal_exceptions():
7471
add_query_source(span)
@@ -78,32 +75,30 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
7875
ctx_mgr.__exit__(None, None, None)
7976

8077

81-
def _handle_error(context, *args):
82-
# type: (Any, *Any) -> None
78+
def _handle_error(context: Any, *args: Any) -> None:
8379
execution_context = context.execution_context
8480
if execution_context is None:
8581
return
8682

87-
span = getattr(execution_context, "_sentry_sql_span", None) # type: Optional[Span]
83+
span: "Optional[Span]" = getattr(execution_context, "_sentry_sql_span", None)
8884

8985
if span is not None:
9086
span.set_status(SPANSTATUS.INTERNAL_ERROR)
9187

9288
# _after_cursor_execute does not get called for crashing SQL stmts. Judging
9389
# from SQLAlchemy codebase it does seem like any error coming into this
9490
# handler is going to be fatal.
95-
ctx_mgr = getattr(
91+
ctx_mgr: "Optional[ContextManager[Any]]" = getattr(
9692
execution_context, "_sentry_sql_span_manager", None
97-
) # type: Optional[ContextManager[Any]]
93+
)
9894

9995
if ctx_mgr is not None:
10096
execution_context._sentry_sql_span_manager = None
10197
ctx_mgr.__exit__(None, None, None)
10298

10399

104100
# See: https://docs.sqlalchemy.org/en/20/dialects/index.html
105-
def _get_db_system(name):
106-
# type: (str) -> Optional[str]
101+
def _get_db_system(name: str) -> "Optional[str]":
107102
name = str(name)
108103

109104
if "sqlite" in name:
@@ -124,8 +119,7 @@ def _get_db_system(name):
124119
return None
125120

126121

127-
def _set_db_data(span, conn):
128-
# type: (Span, Any) -> None
122+
def _set_db_data(span: "Span", conn: Any) -> None:
129123
db_system = _get_db_system(conn.engine.name)
130124
if db_system is not None:
131125
span.set_attribute(SPANDATA.DB_SYSTEM, db_system)

0 commit comments

Comments
 (0)