Skip to content

Commit 9f33e34

Browse files
committed
cleanup, pt 1
1 parent a77f45c commit 9f33e34

15 files changed

Lines changed: 311 additions & 297 deletions

sentry_sdk/_span_batcher.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ def _to_transport_format(item: "StreamedSpan") -> "Any":
8888
"name": item._name,
8989
"status": item._status,
9090
"is_segment": item.is_segment(),
91-
"start_timestamp": item.start_timestamp.timestamp(),
91+
"start_timestamp": item._start_timestamp.timestamp(),
9292
}
9393

94-
if item.timestamp:
95-
res["end_timestamp"] = item.timestamp.timestamp()
94+
if item._timestamp:
95+
res["end_timestamp"] = item._timestamp.timestamp()
9696

97-
if item.parent_span_id:
98-
res["parent_span_id"] = item.parent_span_id
97+
if item._parent_span_id:
98+
res["parent_span_id"] = item._parent_span_id
9999

100100
if item._attributes:
101101
res["attributes"] = {

sentry_sdk/integrations/asgi.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from typing import Tuple
5151
from typing import Union
5252

53-
from sentry_sdk._types import Event, Hint
53+
from sentry_sdk._types import Attributes, Event, Hint
5454
from sentry_sdk.tracing import NoOpSpan
5555

5656

@@ -217,34 +217,27 @@ async def _run_app(
217217
span_ctx: "ContextManager[Union[Span, StreamedSpan, None]]"
218218
if span_streaming:
219219
segment: "Optional[StreamedSpan]" = None
220+
attributes: "Attributes" = {}
220221
sentry_scope.set_custom_sampling_context({"asgi_scope": scope})
221222
if ty in ("http", "websocket"):
222223
if (
223224
ty == "websocket"
224225
or method in self.http_methods_to_capture
225226
):
226227
sentry_sdk.traces.continue_trace(_get_headers(scope))
227-
segment = sentry_sdk.traces.start_span(
228-
name=transaction_name
229-
)
230-
segment.set_attribute("sentry.op", f"{ty}.server")
228+
attributes["sentry.op"] = f"{ty}.server"
231229
else:
232230
sentry_sdk.traces.new_trace()
233-
segment = sentry_sdk.traces.start_span(
234-
name=transaction_name,
235-
)
236-
segment.set_attribute("sentry.op", OP.HTTP_SERVER)
237-
238-
if segment is not None:
239-
segment.set_attribute(
240-
"sentry.span.source",
241-
getattr(
242-
transaction_source, "value", transaction_source
243-
),
244-
)
245-
segment.set_attribute("sentry.origin", self.span_origin)
246-
segment.set_attribute("asgi.type", ty)
231+
attributes["sentry.op"] = OP.HTTP_SERVER
247232

233+
attributes["sentry.span.source"] = getattr(
234+
transaction_source, "value", transaction_source
235+
)
236+
attributes["sentry.origin"] = self.span_origin
237+
attributes["asgi.type"] = ty
238+
segment = sentry_sdk.traces.start_span(
239+
name=transaction_name, attributes=attributes
240+
)
248241
span_ctx = segment or nullcontext()
249242

250243
else:
@@ -293,7 +286,14 @@ async def _sentry_wrapped_send(
293286
and "status" in event
294287
)
295288
if is_http_response:
296-
span.set_http_status(event["status"])
289+
if isinstance(span, StreamedSpan):
290+
span.status = (
291+
"error"
292+
if event["status"] >= 400
293+
else "ok"
294+
)
295+
else:
296+
span.set_http_status(event["status"])
297297

298298
return await send(event)
299299

sentry_sdk/integrations/celery/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _set_status(status: str) -> None:
102102
scope = sentry_sdk.get_current_scope()
103103
if scope.span is not None:
104104
if isinstance(scope.span, StreamedSpan):
105-
scope.span.set_status(SpanStatus.ERROR)
105+
scope.span.status = SpanStatus.ERROR
106106
else:
107107
scope.span.set_status(status)
108108

@@ -286,9 +286,14 @@ def apply_async(*args: "Any", **kwargs: "Any") -> "Any":
286286
span_mgr: "Union[StreamedSpan, Span, NoOpMgr]" = NoOpMgr()
287287
if span_streaming:
288288
if not task_started_from_beat:
289-
span_mgr = sentry_sdk.traces.start_span(name=task_name)
290-
span_mgr.set_attribute("sentry.op", OP.QUEUE_SUBMIT_CELERY)
291-
span_mgr.set_attribute("sentry.origin", CeleryIntegration.origin)
289+
span_mgr = sentry_sdk.traces.start_span(
290+
name=task_name,
291+
attributes={
292+
"sentry.op": OP.QUEUE_SUBMIT_CELERY,
293+
"sentry.origin": CeleryIntegration.origin,
294+
},
295+
)
296+
292297
else:
293298
if not task_started_from_beat:
294299
span_mgr = sentry_sdk.start_span(
@@ -335,12 +340,14 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any":
335340
headers = args[3].get("headers") or {}
336341
if span_streaming:
337342
sentry_sdk.traces.continue_trace(headers)
338-
transaction = sentry_sdk.traces.start_span(name=task.name)
339-
transaction.set_attribute("sentry.origin", CeleryIntegration.origin)
340-
transaction.set_attribute(
341-
"sentry.span.source", TransactionSource.TASK.value
343+
transaction = sentry_sdk.traces.start_span(
344+
name=task.name,
345+
attributes={
346+
"sentry.origin": CeleryIntegration.origin,
347+
"sentry.span.source": TransactionSource.TASK.value,
348+
"sentry.op": OP.QUEUE_TASK_CELERY,
349+
},
342350
)
343-
transaction.set_attribute("sentry.op", OP.QUEUE_TASK_CELERY)
344351

345352
span_ctx = transaction
346353

sentry_sdk/integrations/graphene.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,16 @@ def graphql_span(
149149
span_streaming = has_span_streaming_enabled(client.options)
150150
_graphql_span: "Union[Span, StreamedSpan]"
151151
if span_streaming:
152-
_graphql_span = sentry_sdk.traces.start_span(name=operation_name or "operation")
153-
_graphql_span.set_attribute("sentry.op", op)
154-
_graphql_span.set_attribute("graphql.document", source)
152+
attributes = {
153+
"sentry.op": op,
154+
"graphql.document": source,
155+
"graphql.operation.type": operation_type,
156+
}
155157
if operation_name:
156-
_graphql_span.set_attribute("graphql.operation.name", operation_name)
157-
_graphql_span.set_attribute("graphql.operation.type", operation_type)
158-
_graphql_span.start()
158+
attributes["graphql.operation.name"] = operation_name
159+
_graphql_span = sentry_sdk.traces.start_span(
160+
name=operation_name or "operation", attributes=attributes
161+
)
159162
else:
160163
_graphql_span = sentry_sdk.start_span(op=op, name=operation_name)
161164
_graphql_span.set_data("graphql.document", source)

sentry_sdk/integrations/httpx.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,20 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response":
6262

6363
span_ctx: "Optional[Union[Span, StreamedSpan]]" = None
6464
if span_streaming:
65+
attributes = {
66+
"sentry.op": OP.HTTP_CLIENT,
67+
"sentry.origin": HttpxIntegration.origin,
68+
SPANDATA.HTTP_METHOD: request.method,
69+
}
70+
if parsed_url is not None:
71+
attributes["url"] = parsed_url.url
72+
attributes[SPANDATA.HTTP_QUERY] = parsed_url.query
73+
attributes[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment
6574
span_ctx = sentry_sdk.traces.start_span(
66-
name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}"
75+
name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}",
76+
attributes=attributes,
6777
)
78+
6879
else:
6980
span_ctx = start_span(
7081
op=OP.HTTP_CLIENT,
@@ -77,16 +88,7 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response":
7788
)
7889

7990
with span_ctx as span:
80-
if isinstance(span, StreamedSpan):
81-
span.set_attribute("sentry.op", OP.HTTP_CLIENT)
82-
span.set_attribute("sentry.origin", HttpxIntegration.origin)
83-
84-
span.set_attribute(SPANDATA.HTTP_METHOD, request.method)
85-
if parsed_url is not None:
86-
span.set_attribute("url", parsed_url.url)
87-
span.set_attribute(SPANDATA.HTTP_QUERY, parsed_url.query)
88-
span.set_attribute(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
89-
else:
91+
if not isinstance(span, StreamedSpan):
9092
span.set_data(SPANDATA.HTTP_METHOD, request.method)
9193
if parsed_url is not None:
9294
span.set_data("url", parsed_url.url)
@@ -111,10 +113,11 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response":
111113

112114
rv = real_send(self, request, **kwargs)
113115

114-
span.set_http_status(rv.status_code)
115116
if isinstance(span, StreamedSpan):
117+
span.status = "error" if rv.status_code >= 400 else "ok"
116118
span.set_attribute("reason", rv.reason_phrase)
117119
else:
120+
span.set_http_status(rv.status_code)
118121
span.set_data("reason", rv.reason_phrase)
119122

120123
with capture_internal_exceptions():
@@ -143,8 +146,19 @@ async def send(
143146

144147
span_ctx: "Optional[Union[Span, StreamedSpan]]" = None
145148
if span_streaming:
149+
attributes = {
150+
"sentry.op": OP.HTTP_CLIENT,
151+
"sentry.origin": HttpxIntegration.origin,
152+
SPANDATA.HTTP_METHOD: request.method,
153+
}
154+
if parsed_url is not None:
155+
attributes["url"] = parsed_url.url
156+
attributes[SPANDATA.HTTP_QUERY] = parsed_url.query
157+
attributes[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment
158+
146159
span_ctx = sentry_sdk.traces.start_span(
147-
name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}"
160+
name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}",
161+
attributes=attributes,
148162
)
149163
else:
150164
span_ctx = start_span(
@@ -158,15 +172,7 @@ async def send(
158172
)
159173

160174
with span_ctx as span:
161-
if isinstance(span, StreamedSpan):
162-
span.set_attribute("sentry.op", OP.HTTP_CLIENT)
163-
span.set_attribute("sentry.origin", HttpxIntegration.origin)
164-
span.set_attribute(SPANDATA.HTTP_METHOD, request.method)
165-
if parsed_url is not None:
166-
span.set_attribute("url", parsed_url.url)
167-
span.set_attribute(SPANDATA.HTTP_QUERY, parsed_url.query)
168-
span.set_attribute(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
169-
else:
175+
if not isinstance(span, StreamedSpan):
170176
span.set_data(SPANDATA.HTTP_METHOD, request.method)
171177
if parsed_url is not None:
172178
span.set_data("url", parsed_url.url)
@@ -190,10 +196,11 @@ async def send(
190196

191197
rv = await real_send(self, request, **kwargs)
192198

193-
span.set_http_status(rv.status_code)
194199
if isinstance(span, StreamedSpan):
200+
span.status = "error" if rv.status_code >= 400 else "ok"
195201
span.set_attribute("reason", rv.reason_phrase)
196202
else:
203+
span.set_http_status(rv.status_code)
197204
span.set_data("reason", rv.reason_phrase)
198205

199206
with capture_internal_exceptions():

sentry_sdk/integrations/huggingface_hub.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
9393
span_streaming = has_span_streaming_enabled(sentry_client.options)
9494
span: "Union[StreamedSpan, Span]"
9595
if span_streaming:
96-
span = sentry_sdk.traces.start_span(name=f"{operation_name} {model}")
97-
span.set_attribute("sentry.op", op)
98-
span.set_attribute("sentry.origin", HuggingfaceHubIntegration.origin)
96+
span = sentry_sdk.traces.start_span(
97+
name=f"{operation_name} {model}",
98+
attributes={
99+
"sentry.op": op,
100+
"sentry.origin": HuggingfaceHubIntegration.origin,
101+
},
102+
)
99103
else:
100104
span = sentry_sdk.start_span(
101105
op=op,

sentry_sdk/integrations/redis/_async_common.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ async def _sentry_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
4343

4444
span: "Union[Span, StreamedSpan]"
4545
if span_streaming:
46-
span = sentry_sdk.traces.start_span(name="redis.pipeline.execute")
47-
span.set_attribute("sentry.origin", SPAN_ORIGIN)
48-
span.set_attribute("sentry.op", OP.DB_REDIS)
46+
span = sentry_sdk.traces.start_span(
47+
name="redis.pipeline.execute",
48+
attributes={
49+
"sentry.origin": SPAN_ORIGIN,
50+
"sentry.op": OP.DB_REDIS,
51+
},
52+
)
4953
else:
5054
span = sentry_sdk.start_span(
5155
op=OP.DB_REDIS,
@@ -107,10 +111,12 @@ async def _sentry_execute_command(
107111
if cache_properties["is_cache_key"] and cache_properties["op"] is not None:
108112
if span_streaming:
109113
cache_span = sentry_sdk.traces.start_span(
110-
name=cache_properties["description"]
114+
name=cache_properties["description"],
115+
attributes={
116+
"sentry.op": cache_properties["op"],
117+
"sentry.origin": SPAN_ORIGIN,
118+
},
111119
)
112-
cache_span.set_attribute("sentry.op", cache_properties["op"])
113-
cache_span.set_attribute("sentry.origin", SPAN_ORIGIN)
114120
else:
115121
cache_span = sentry_sdk.start_span(
116122
op=cache_properties["op"],
@@ -123,9 +129,13 @@ async def _sentry_execute_command(
123129

124130
db_span: "Union[Span, StreamedSpan]"
125131
if span_streaming:
126-
db_span = sentry_sdk.traces.start_span(name=db_properties["description"])
127-
db_span.set_attribute("sentry.op", db_properties["op"])
128-
db_span.set_attribute("sentry.origin", SPAN_ORIGIN)
132+
db_span = sentry_sdk.traces.start_span(
133+
name=db_properties["description"],
134+
attributes={
135+
"sentry.op": db_properties["op"],
136+
"sentry.origin": SPAN_ORIGIN,
137+
},
138+
)
129139
else:
130140
db_span = sentry_sdk.start_span(
131141
op=db_properties["op"],

sentry_sdk/integrations/redis/_sync_common.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ def sentry_patched_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
4141

4242
span: "Union[Span, StreamedSpan]"
4343
if span_streaming:
44-
span = sentry_sdk.traces.start_span(name="redis.pipeline.execute")
45-
span.set_attribute("sentry.origin", SPAN_ORIGIN)
46-
span.set_attribute("sentry.op", OP.DB_REDIS)
44+
span = sentry_sdk.traces.start_span(
45+
name="redis.pipeline.execute",
46+
attributes={
47+
"sentry.origin": SPAN_ORIGIN,
48+
"sentry.op": OP.DB_REDIS,
49+
},
50+
)
4751
else:
4852
span = sentry_sdk.start_span(
4953
op=OP.DB_REDIS,
@@ -107,10 +111,12 @@ def sentry_patched_execute_command(
107111
if cache_properties["is_cache_key"] and cache_properties["op"] is not None:
108112
if span_streaming:
109113
cache_span = sentry_sdk.traces.start_span(
110-
name=cache_properties["description"]
114+
name=cache_properties["description"],
115+
attributes={
116+
"sentry.op": cache_properties["op"],
117+
"sentry.origin": SPAN_ORIGIN,
118+
},
111119
)
112-
cache_span.set_attribute("sentry.op", cache_properties["op"])
113-
cache_span.set_attribute("sentry.origin", SPAN_ORIGIN)
114120
else:
115121
cache_span = sentry_sdk.start_span(
116122
op=cache_properties["op"],
@@ -123,9 +129,13 @@ def sentry_patched_execute_command(
123129

124130
db_span: "Union[Span, StreamedSpan]"
125131
if span_streaming:
126-
db_span = sentry_sdk.traces.start_span(name=db_properties["description"])
127-
db_span.set_attribute("sentry.op", db_properties["op"])
128-
db_span.set_attribute("sentry.origin", SPAN_ORIGIN)
132+
db_span = sentry_sdk.traces.start_span(
133+
name=db_properties["description"],
134+
attributes={
135+
"sentry.op": db_properties["op"],
136+
"sentry.origin": SPAN_ORIGIN,
137+
},
138+
)
129139
else:
130140
db_span = sentry_sdk.start_span(
131141
op=db_properties["op"],

0 commit comments

Comments
 (0)