Skip to content

Commit e8d67d8

Browse files
feat(rust-tracing): Support span streaming (#6433)
1 parent 0749d3e commit e8d67d8

2 files changed

Lines changed: 602 additions & 215 deletions

File tree

sentry_sdk/integrations/rust_tracing.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232

3333
import json
3434
from enum import Enum, auto
35-
from typing import Any, Callable, Dict, Optional
35+
from typing import Any, Callable, Dict, Optional, Union
3636

3737
import sentry_sdk
3838
from sentry_sdk.integrations import Integration
3939
from sentry_sdk.scope import should_send_default_pii
40+
from sentry_sdk.traces import StreamedSpan
4041
from sentry_sdk.tracing import Span as SentrySpan
42+
from sentry_sdk.tracing_utils import has_span_streaming_enabled
4143
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
4244

4345

@@ -182,7 +184,9 @@ def on_event(self, event: str, sentry_span: "SentrySpan") -> None:
182184
elif event_type == EventTypeMapping.Event:
183185
process_event(deserialized_event)
184186

185-
def on_new_span(self, attrs: str, span_id: str) -> "Optional[SentrySpan]":
187+
def on_new_span(
188+
self, attrs: str, span_id: str
189+
) -> "Optional[Union[SentrySpan, StreamedSpan]]":
186190
attrs = json.loads(attrs)
187191
metadata = attrs.get("metadata", {})
188192

@@ -202,13 +206,29 @@ def on_new_span(self, attrs: str, span_id: str) -> "Optional[SentrySpan]":
202206
else:
203207
sentry_span_name = "<unknown>"
204208

205-
kwargs = {
206-
"op": "function",
207-
"name": sentry_span_name,
208-
"origin": self.origin,
209-
}
210-
211-
sentry_span = sentry_sdk.start_span(**kwargs)
209+
span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options)
210+
if span_streaming:
211+
sentry_span = sentry_sdk.traces.start_span(
212+
name=sentry_span_name,
213+
attributes={
214+
"sentry.op": "function",
215+
"sentry.origin": self.origin,
216+
},
217+
)
218+
fields = metadata.get("fields", [])
219+
for field in fields:
220+
if self._include_tracing_fields():
221+
sentry_span.set_attribute(field, attrs.get(field))
222+
else:
223+
sentry_span.set_attribute(field, SENSITIVE_DATA_SUBSTITUTE)
224+
225+
return sentry_span
226+
227+
sentry_span = sentry_sdk.start_span(
228+
op="function",
229+
name=sentry_span_name,
230+
origin=self.origin,
231+
)
212232
fields = metadata.get("fields", [])
213233
for field in fields:
214234
if self._include_tracing_fields():
@@ -225,16 +245,24 @@ def on_close(self, span_id: str, sentry_span: "SentrySpan") -> None:
225245

226246
sentry_span.__exit__(None, None, None)
227247

228-
def on_record(self, span_id: str, values: str, sentry_span: "SentrySpan") -> None:
248+
def on_record(
249+
self, span_id: str, values: str, sentry_span: "Union[SentrySpan, StreamedSpan]"
250+
) -> None:
229251
if sentry_span is None:
230252
return
231253

254+
set_on_span = (
255+
sentry_span.set_attribute
256+
if isinstance(sentry_span, StreamedSpan)
257+
else sentry_span.set_data
258+
)
259+
232260
deserialized_values = json.loads(values)
233261
for key, value in deserialized_values.items():
234262
if self._include_tracing_fields():
235-
sentry_span.set_data(key, value)
263+
set_on_span(key, value)
236264
else:
237-
sentry_span.set_data(key, SENSITIVE_DATA_SUBSTITUTE)
265+
set_on_span(key, SENSITIVE_DATA_SUBSTITUTE)
238266

239267

240268
class RustTracingIntegration(Integration):

0 commit comments

Comments
 (0)