Skip to content

Commit 2e3d78b

Browse files
committed
format
1 parent 18b763d commit 2e3d78b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+796
-232
lines changed

sentry_sdk/ai/monitoring.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ async def async_wrapped(*args: "Any", **kwargs: "Any") -> "Any":
9494

9595

9696
def record_token_usage(
97-
span: Span, prompt_tokens: "Optional[int]" = None, completion_tokens: "Optional[int]" = None, total_tokens: "Optional[int]" = None
97+
span: Span,
98+
prompt_tokens: "Optional[int]" = None,
99+
completion_tokens: "Optional[int]" = None,
100+
total_tokens: "Optional[int]" = None,
98101
) -> None:
99102
ai_pipeline_name = get_ai_pipeline_name()
100103
if ai_pipeline_name:

sentry_sdk/client.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ def capture_session(self, *args: Any, **kwargs: Any) -> None:
192192
if TYPE_CHECKING:
193193

194194
@overload
195-
def get_integration(self, name_or_class: str) -> Optional["Integration"]:
196-
...
195+
def get_integration(self, name_or_class: str) -> Optional["Integration"]: ...
197196

198197
@overload
199-
def get_integration(self, name_or_class: "type[I]") -> Optional["I"]:
200-
...
198+
def get_integration(self, name_or_class: "type[I]") -> Optional["I"]: ...
201199

202-
def get_integration(self, name_or_class: Union[str, "type[Integration]"]) -> Optional["Integration"]:
200+
def get_integration(
201+
self, name_or_class: Union[str, "type[Integration]"]
202+
) -> Optional["Integration"]:
203203
return None
204204

205205
def close(self, *args: Any, **kwargs: Any) -> None:
@@ -246,7 +246,9 @@ def __setstate__(self, state: Any) -> None:
246246
self.options = state["options"]
247247
self._init_impl()
248248

249-
def _setup_instrumentation(self, functions_to_trace: "Sequence[Dict[str, str]]") -> None:
249+
def _setup_instrumentation(
250+
self, functions_to_trace: "Sequence[Dict[str, str]]"
251+
) -> None:
250252
"""
251253
Instruments the functions given in the list `functions_to_trace` with the `@sentry_sdk.tracing.trace` decorator.
252254
"""
@@ -890,14 +892,14 @@ def capture_session(self, session: "Session") -> None:
890892
if TYPE_CHECKING:
891893

892894
@overload
893-
def get_integration(self, name_or_class: str) -> Optional["Integration"]:
894-
...
895+
def get_integration(self, name_or_class: str) -> Optional["Integration"]: ...
895896

896897
@overload
897-
def get_integration(self, name_or_class: "type[I]") -> Optional["I"]:
898-
...
898+
def get_integration(self, name_or_class: "type[I]") -> Optional["I"]: ...
899899

900-
def get_integration(self, name_or_class: Union[str, "Type[Integration]"]) -> Optional["Integration"]:
900+
def get_integration(
901+
self, name_or_class: Union[str, "Type[Integration]"]
902+
) -> Optional["Integration"]:
901903
"""Returns the integration for this client by name or class.
902904
If the client does not have that integration then `None` is returned.
903905
"""

sentry_sdk/integrations/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def _generate_default_integrations_iterator(
3434
auto_enabling_integrations: "List[str]",
3535
) -> "Callable[[bool], Iterator[Type[Integration]]]":
3636

37-
def iter_default_integrations(with_auto_enabling_integrations: bool) -> "Iterator[Type[Integration]]":
37+
def iter_default_integrations(
38+
with_auto_enabling_integrations: bool,
39+
) -> "Iterator[Type[Integration]]":
3840
"""Returns an iterator of the default integration classes:"""
3941
from importlib import import_module
4042

@@ -236,7 +238,11 @@ def setup_integrations(
236238
return integrations
237239

238240

239-
def _check_minimum_version(integration: "type[Integration]", version: "Optional[tuple[int, ...]]", package: "Optional[str]" = None) -> None:
241+
def _check_minimum_version(
242+
integration: "type[Integration]",
243+
version: "Optional[tuple[int, ...]]",
244+
package: "Optional[str]" = None,
245+
) -> None:
240246
package = package or integration.identifier
241247

242248
if version is None:

sentry_sdk/integrations/_asgi_common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def _get_headers(asgi_scope: "Any") -> "Dict[str, str]":
3131
return headers
3232

3333

34-
def _get_url(asgi_scope: "Dict[str, Any]", default_scheme: "Optional[Literal['ws', 'http']]" = None, host: "Optional[Union[AnnotatedValue, str]]" = None) -> str:
34+
def _get_url(
35+
asgi_scope: "Dict[str, Any]",
36+
default_scheme: "Optional[Literal['ws', 'http']]" = None,
37+
host: "Optional[Union[AnnotatedValue, str]]" = None,
38+
) -> str:
3539
"""
3640
Extract URL from the ASGI scope, without also including the querystring.
3741
"""

sentry_sdk/integrations/_wsgi_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ def _is_json_content_type(ct: "Optional[str]") -> bool:
199199
)
200200

201201

202-
def _filter_headers(headers: "Mapping[str, str]") -> "Mapping[str, Union[AnnotatedValue, str]]":
202+
def _filter_headers(
203+
headers: "Mapping[str, str]",
204+
) -> "Mapping[str, Union[AnnotatedValue, str]]":
203205
if should_send_default_pii():
204206
return headers
205207

sentry_sdk/integrations/aiohttp.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ def setup_once() -> None:
115115

116116
old_handle = Application._handle
117117

118-
async def sentry_app_handle(self: "Any", request: "Request", *args: "Any", **kwargs: "Any") -> "Any":
118+
async def sentry_app_handle(
119+
self: "Any", request: "Request", *args: "Any", **kwargs: "Any"
120+
) -> "Any":
119121
integration = sentry_sdk.get_client().get_integration(AioHttpIntegration)
120122
if integration is None:
121123
return await old_handle(self, request, *args, **kwargs)
@@ -169,7 +171,9 @@ async def sentry_app_handle(self: "Any", request: "Request", *args: "Any", **kwa
169171
old_urldispatcher_resolve = UrlDispatcher.resolve
170172

171173
@wraps(old_urldispatcher_resolve)
172-
async def sentry_urldispatcher_resolve(self: "UrlDispatcher", request: "Request") -> "UrlMappingMatchInfo":
174+
async def sentry_urldispatcher_resolve(
175+
self: "UrlDispatcher", request: "Request"
176+
) -> "UrlMappingMatchInfo":
173177
rv = await old_urldispatcher_resolve(self, request)
174178

175179
integration = sentry_sdk.get_client().get_integration(AioHttpIntegration)
@@ -214,7 +218,9 @@ def init(*args: "Any", **kwargs: "Any") -> None:
214218

215219
def create_trace_config() -> "Any":
216220

217-
async def on_request_start(session: "Any", trace_config_ctx: "SimpleNamespace", params: "Any") -> None:
221+
async def on_request_start(
222+
session: "Any", trace_config_ctx: "SimpleNamespace", params: "Any"
223+
) -> None:
218224
if sentry_sdk.get_client().get_integration(AioHttpIntegration) is None:
219225
return
220226

@@ -270,7 +276,9 @@ async def on_request_start(session: "Any", trace_config_ctx: "SimpleNamespace",
270276
trace_config_ctx.span = span
271277
trace_config_ctx.span_data = data
272278

273-
async def on_request_end(session: "Any", trace_config_ctx: "SimpleNamespace", params: "Any") -> None:
279+
async def on_request_end(
280+
session: "Any", trace_config_ctx: "SimpleNamespace", params: "Any"
281+
) -> None:
274282
if trace_config_ctx.span is None:
275283
return
276284

@@ -299,7 +307,9 @@ async def on_request_end(session: "Any", trace_config_ctx: "SimpleNamespace", pa
299307
return trace_config
300308

301309

302-
def _make_request_processor(weak_request: "weakref.ReferenceType[Request]") -> "EventProcessor":
310+
def _make_request_processor(
311+
weak_request: "weakref.ReferenceType[Request]",
312+
) -> "EventProcessor":
303313
def aiohttp_processor(
304314
event: "Event",
305315
hint: "dict[str, Tuple[type, BaseException, Any]]",
@@ -346,7 +356,9 @@ def _capture_exception() -> "ExcInfo":
346356
BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"
347357

348358

349-
def get_aiohttp_request_data(request: "Request") -> "Union[Optional[str], AnnotatedValue]":
359+
def get_aiohttp_request_data(
360+
request: "Request",
361+
) -> "Union[Optional[str], AnnotatedValue]":
350362
bytes_body = request._read_bytes
351363

352364
if bytes_body is not None:

sentry_sdk/integrations/anthropic.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ def _get_responses(content: "list[Any]") -> "list[dict[str, Any]]":
8080
return responses
8181

8282

83-
def _collect_ai_data(event: "MessageStreamEvent", input_tokens: int, output_tokens: int, content_blocks: "list[str]") -> "tuple[int, int, list[str]]":
83+
def _collect_ai_data(
84+
event: "MessageStreamEvent",
85+
input_tokens: int,
86+
output_tokens: int,
87+
content_blocks: "list[str]",
88+
) -> "tuple[int, int, list[str]]":
8489
"""
8590
Count token usage and collect content blocks from the AI streaming response.
8691
"""
@@ -106,7 +111,11 @@ def _collect_ai_data(event: "MessageStreamEvent", input_tokens: int, output_toke
106111

107112

108113
def _add_ai_data_to_span(
109-
span: "Span", integration: "AnthropicIntegration", input_tokens: int, output_tokens: int, content_blocks: "list[str]"
114+
span: "Span",
115+
integration: "AnthropicIntegration",
116+
input_tokens: int,
117+
output_tokens: int,
118+
content_blocks: "list[str]",
110119
) -> None:
111120
"""
112121
Add token usage and content blocks from the AI streaming response to the span.

sentry_sdk/integrations/ariadne.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@ def _patch_graphql() -> None:
4848
old_handle_query_result = ariadne_graphql.handle_query_result
4949

5050
@ensure_integration_enabled(AriadneIntegration, old_parse_query)
51-
def _sentry_patched_parse_query(context_value: "Optional[Any]", query_parser: "Optional[QueryParser]", data: "Any") -> "DocumentNode":
51+
def _sentry_patched_parse_query(
52+
context_value: "Optional[Any]",
53+
query_parser: "Optional[QueryParser]",
54+
data: "Any",
55+
) -> "DocumentNode":
5256
event_processor = _make_request_event_processor(data)
5357
sentry_sdk.get_isolation_scope().add_event_processor(event_processor)
5458

5559
result = old_parse_query(context_value, query_parser, data)
5660
return result
5761

5862
@ensure_integration_enabled(AriadneIntegration, old_handle_errors)
59-
def _sentry_patched_handle_graphql_errors(errors: "List[GraphQLError]", *args: "Any", **kwargs: "Any") -> "GraphQLResult":
63+
def _sentry_patched_handle_graphql_errors(
64+
errors: "List[GraphQLError]", *args: "Any", **kwargs: "Any"
65+
) -> "GraphQLResult":
6066
result = old_handle_errors(errors, *args, **kwargs)
6167

6268
event_processor = _make_response_event_processor(result[1])
@@ -79,7 +85,9 @@ def _sentry_patched_handle_graphql_errors(errors: "List[GraphQLError]", *args: "
7985
return result
8086

8187
@ensure_integration_enabled(AriadneIntegration, old_handle_query_result)
82-
def _sentry_patched_handle_query_result(result: "Any", *args: "Any", **kwargs: "Any") -> "GraphQLResult":
88+
def _sentry_patched_handle_query_result(
89+
result: "Any", *args: "Any", **kwargs: "Any"
90+
) -> "GraphQLResult":
8391
query_result = old_handle_query_result(result, *args, **kwargs)
8492

8593
event_processor = _make_response_event_processor(query_result[1])

sentry_sdk/integrations/arq.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def patch_enqueue_job() -> None:
6868
old_enqueue_job = ArqRedis.enqueue_job
6969
original_kwdefaults = old_enqueue_job.__kwdefaults__
7070

71-
async def _sentry_enqueue_job(self: "ArqRedis", function: str, *args: "Any", **kwargs: "Any") -> "Optional[Job]":
71+
async def _sentry_enqueue_job(
72+
self: "ArqRedis", function: str, *args: "Any", **kwargs: "Any"
73+
) -> "Optional[Job]":
7274
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
7375
if integration is None:
7476
return await old_enqueue_job(self, function, *args, **kwargs)
@@ -135,7 +137,9 @@ def _capture_exception(exc_info: "ExcInfo") -> None:
135137
sentry_sdk.capture_event(event, hint=hint)
136138

137139

138-
def _make_event_processor(ctx: "Dict[Any, Any]", *args: "Any", **kwargs: "Any") -> "EventProcessor":
140+
def _make_event_processor(
141+
ctx: "Dict[Any, Any]", *args: "Any", **kwargs: "Any"
142+
) -> "EventProcessor":
139143
def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
140144
with capture_internal_exceptions():
141145
scope = sentry_sdk.get_current_scope()
@@ -164,7 +168,9 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
164168

165169

166170
def _wrap_coroutine(name: str, coroutine: "WorkerCoroutine") -> "WorkerCoroutine":
167-
async def _sentry_coroutine(ctx: "Dict[Any, Any]", *args: "Any", **kwargs: "Any") -> "Any":
171+
async def _sentry_coroutine(
172+
ctx: "Dict[Any, Any]", *args: "Any", **kwargs: "Any"
173+
) -> "Any":
168174
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
169175
if integration is None:
170176
return await coroutine(ctx, *args, **kwargs)

sentry_sdk/integrations/asgi.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ async def inner(receive: Any, send: Any) -> Any:
155155
async def _run_asgi3(self, scope: Any, receive: Any, send: Any) -> Any:
156156
return await self._run_app(scope, receive, send, asgi_version=3)
157157

158-
async def _run_original_app(self, scope: Any, receive: Any, send: Any, asgi_version: int) -> Any:
158+
async def _run_original_app(
159+
self, scope: Any, receive: Any, send: Any, asgi_version: int
160+
) -> Any:
159161
try:
160162
if asgi_version == 2:
161163
return await self.app(scope)(receive, send)
@@ -166,7 +168,9 @@ async def _run_original_app(self, scope: Any, receive: Any, send: Any, asgi_vers
166168
_capture_exception(exc, mechanism_type=self.mechanism_type)
167169
raise exc from None
168170

169-
async def _run_app(self, scope: Any, receive: Any, send: Any, asgi_version: int) -> Any:
171+
async def _run_app(
172+
self, scope: Any, receive: Any, send: Any, asgi_version: int
173+
) -> Any:
170174
is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
171175
is_lifespan = scope["type"] == "lifespan"
172176
if is_recursive_asgi_middleware or is_lifespan:
@@ -220,7 +224,9 @@ async def _run_app(self, scope: Any, receive: Any, send: Any, asgi_version: int)
220224
logger.debug("[ASGI] Started transaction: %s", span)
221225
span.set_tag("asgi.type", ty)
222226

223-
async def _sentry_wrapped_send(event: "Dict[str, Any]") -> Any:
227+
async def _sentry_wrapped_send(
228+
event: "Dict[str, Any]",
229+
) -> Any:
224230
is_http_response = (
225231
event.get("type") == "http.response.start"
226232
and span is not None
@@ -237,7 +243,9 @@ async def _sentry_wrapped_send(event: "Dict[str, Any]") -> Any:
237243
finally:
238244
_asgi_middleware_applied.set(False)
239245

240-
def event_processor(self, event: "Event", hint: "Hint", asgi_scope: Any) -> "Optional[Event]":
246+
def event_processor(
247+
self, event: "Event", hint: "Hint", asgi_scope: Any
248+
) -> "Optional[Event]":
241249
request_data = event.get("request", {})
242250
request_data.update(_get_request_data(asgi_scope))
243251
event["request"] = deepcopy(request_data)
@@ -276,7 +284,9 @@ def event_processor(self, event: "Event", hint: "Hint", asgi_scope: Any) -> "Opt
276284
# data to your liking it's recommended to use the `before_send` callback
277285
# for that.
278286

279-
def _get_transaction_name_and_source(self, transaction_style: str, asgi_scope: Any) -> "Tuple[str, str]":
287+
def _get_transaction_name_and_source(
288+
self, transaction_style: str, asgi_scope: Any
289+
) -> "Tuple[str, str]":
280290
name = None
281291
source = SOURCE_FOR_STYLE[transaction_style]
282292
ty = asgi_scope.get("type")

0 commit comments

Comments
 (0)