Skip to content

Commit bf3f3d1

Browse files
committed
go via client
1 parent 6c6fd25 commit bf3f3d1

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

sentry_sdk/_span_batcher.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020

2121
class SpanBatcher:
22-
# TODO[span-first]: Adjust limits. However, there's still a restriction of
23-
# at most 1000 spans per envelope.
22+
# TODO[span-first]: Adjust limits. Protocol dictates at most 1000 spans
23+
# in an envelope.
2424
MAX_SPANS_BEFORE_FLUSH = 1_000
2525
MAX_SPANS_BEFORE_DROP = 2_000
2626
FLUSH_WAIT_TIME = 5.0
@@ -107,9 +107,7 @@ def add(self, span):
107107
return None
108108

109109
self._span_buffer[span.trace_id].append(span)
110-
if (
111-
self.get_size() >= self.MAX_SPANS_BEFORE_FLUSH
112-
): # TODO[span-first] should this be per bucket?
110+
if self.get_size() >= self.MAX_SPANS_BEFORE_FLUSH:
113111
self._flush_event.set()
114112

115113
def kill(self):
@@ -130,10 +128,12 @@ def _span_to_transport_format(span):
130128
# type: (Span) -> SpanV2
131129
from sentry_sdk.utils import attribute_value_to_transport_format, safe_repr
132130

131+
is_segment = isinstance(span, Transaction)
132+
133133
res = {
134134
"trace_id": span.trace_id,
135135
"span_id": span.span_id,
136-
"name": span.description, # TODO[span-first]
136+
"name": span.name if is_segment else span.description,
137137
"status": SPANSTATUS.OK
138138
if span.status in (SPANSTATUS.OK, SPANSTATUS.UNSET)
139139
else SPANSTATUS.ERROR,
@@ -151,6 +151,11 @@ def _span_to_transport_format(span):
151151
for (k, v) in span._attributes.items()
152152
}
153153

154+
# We set these as late as possible to increase the odds of the span
155+
# getting a good segment name
156+
res["attributes"]["sentry.segment.id"] = span.containing_transaction.span_id
157+
res["attributes"]["sentry.segment.name"] = span.containing_transaction.name
158+
154159
return res
155160

156161
def _flush(self):

sentry_sdk/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,14 @@ def capture_event(
937937

938938
return return_value
939939

940+
def _capture_span(self, span):
941+
# type: (Span) -> None
942+
# Used for span streaming (trace_lifecycle == "stream").
943+
if not has_span_streaming_enabled(self.options):
944+
return
945+
946+
self._span_batcher.add(span)
947+
940948
def _capture_log(self, log):
941949
# type: (Optional[Log]) -> None
942950
# TODO[ivana]: Use get_default_attributes here

sentry_sdk/tracing.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,6 @@ def _set_initial_attributes(self):
361361
attributes = get_default_attributes()
362362
self._attributes = attributes | self._attributes
363363

364-
self._attributes["sentry.segment.id"] = self.containing_transaction.span_id
365-
# TODO[span-first]: This might need to be updated if the segment name is updated
366-
self._attributes["sentry.segment.name"] = self.containing_transaction.name
367-
368364
# TODO this should really live on the Transaction class rather than the Span
369365
# class
370366
def init_span_recorder(self, maxlen):
@@ -753,7 +749,7 @@ def finish(self, scope=None, end_timestamp=None):
753749
and self.containing_transaction.sampled
754750
):
755751
logger.debug(f"[Tracing] Adding span {self.span_id} to buffer")
756-
client._span_batcher.add(self)
752+
client._capture_span(self)
757753

758754
return None
759755

@@ -1121,9 +1117,10 @@ def finish(
11211117

11221118
return None
11231119

1124-
if self._mode == "stream" and self.containing_transaction.sampled:
1125-
logger.debug(f"[Tracing] Adding span {self.span_id} to buffer")
1126-
client._span_batcher.add(self)
1120+
if self._mode == "stream":
1121+
if self.containing_transaction.sampled:
1122+
logger.debug(f"[Tracing] Adding span {self.span_id} to batcher")
1123+
client._capture_span(self)
11271124
return
11281125

11291126
finished_spans = [
@@ -1167,7 +1164,6 @@ def finish(
11671164
self._profile = None
11681165

11691166
event["measurements"] = self._measurements
1170-
11711167
return scope.capture_event(event)
11721168

11731169
def set_measurement(self, name, value, unit=""):

0 commit comments

Comments
 (0)