Skip to content

Commit 445bf76

Browse files
committed
debug etc
1 parent d0cbcc4 commit 445bf76

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

sentry_sdk/_span_batcher.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def add(self, span):
112112
self.get_size() >= self.MAX_SPANS_BEFORE_FLUSH
113113
): # TODO[span-first] should this be per bucket?
114114
self._flush_event.set()
115+
print(self._span_buffer)
115116

116117
def kill(self):
117118
# type: (...) -> None
@@ -132,19 +133,19 @@ def _span_to_transport_format(span):
132133
res = {
133134
"trace_id": span.trace_id,
134135
"span_id": span.span_id,
135-
"name": span.name,
136+
"name": span.description, # TODO[span-first]
136137
"status": SPANSTATUS.OK
137138
if span.status in (SPANSTATUS.OK, SPANSTATUS.UNSET)
138139
else SPANSTATUS.ERROR,
139140
"is_segment": span.containing_transaction == span,
140-
"start_timestamp": span.start_timestamp,
141-
"end_timestamp": span.timestamp,
141+
"start_timestamp": span.start_timestamp.timestamp(), # TODO[span-first]
142+
"end_timestamp": span.timestamp.timestamp(),
142143
}
143144

144145
if span.parent_span_id:
145146
res["parent_span_id"] = span.parent_span_id
146147

147-
if span["attributes"]:
148+
if span.attributes:
148149
res["attributes"] = {
149150
k: format_attribute_value(v) for (k, v) in span["attributes"].items()
150151
}
@@ -157,7 +158,7 @@ def _flush(self):
157158
if len(self._span_buffer) == 0:
158159
return None
159160

160-
for trace_id, spans in self._span_buffer:
161+
for trace_id, spans in self._span_buffer.items():
161162
envelope = Envelope(
162163
headers={
163164
"sent_at": format_timestamp(datetime.now(timezone.utc)),

sentry_sdk/tracing.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class Span:
282282
"_flags",
283283
"_flags_capacity",
284284
"_mode",
285+
"attributes",
285286
)
286287

287288
def __init__(
@@ -300,6 +301,7 @@ def __init__(
300301
scope=None, # type: Optional[sentry_sdk.Scope]
301302
origin="manual", # type: str
302303
name=None, # type: Optional[str]
304+
attributes=None, # type: Optional[dict]
303305
):
304306
# type: (...) -> None
305307
self._trace_id = trace_id
@@ -319,6 +321,8 @@ def __init__(
319321
self._containing_transaction = containing_transaction
320322
self._flags = {} # type: Dict[str, bool]
321323
self._flags_capacity = 10
324+
self.attributes = attributes or {}
325+
# TODO[span-first]: fill attributes
322326

323327
if hub is not None:
324328
warnings.warn(
@@ -676,22 +680,7 @@ def is_success(self):
676680
# type: () -> bool
677681
return self.status == "ok"
678682

679-
def finish(self, scope=None, end_timestamp=None):
680-
# type: (Optional[sentry_sdk.Scope], Optional[Union[float, datetime]]) -> Optional[str]
681-
"""
682-
Sets the end timestamp of the span.
683-
684-
Additionally it also creates a breadcrumb from the span,
685-
if the span represents a database or HTTP request.
686-
687-
:param scope: The scope to use for this transaction.
688-
If not provided, the current scope will be used.
689-
:param end_timestamp: Optional timestamp that should
690-
be used as timestamp instead of the current time.
691-
692-
:return: Always ``None``. The type is ``Optional[str]`` to match
693-
the return value of :py:meth:`sentry_sdk.tracing.Transaction.finish`.
694-
"""
683+
def _finish(self, scope=None, end_timestamp=None):
695684
if self.timestamp is not None:
696685
# This span is already finished, ignore.
697686
return None
@@ -712,12 +701,31 @@ def finish(self, scope=None, end_timestamp=None):
712701
scope = scope or sentry_sdk.get_current_scope()
713702
maybe_create_breadcrumbs_from_span(scope, self)
714703

704+
def finish(self, scope=None, end_timestamp=None):
705+
# type: (Optional[sentry_sdk.Scope], Optional[Union[float, datetime]]) -> Optional[str]
706+
"""
707+
Sets the end timestamp of the span.
708+
709+
Additionally it also creates a breadcrumb from the span,
710+
if the span represents a database or HTTP request.
711+
712+
:param scope: The scope to use for this transaction.
713+
If not provided, the current scope will be used.
714+
:param end_timestamp: Optional timestamp that should
715+
be used as timestamp instead of the current time.
716+
717+
:return: Always ``None``. The type is ``Optional[str]`` to match
718+
the return value of :py:meth:`sentry_sdk.tracing.Transaction.finish`.
719+
"""
720+
self._finish(scope, end_timestamp)
721+
715722
client = sentry_sdk.get_client()
716723
if client.is_active():
717724
if (
718725
has_span_streaming_enabled(client.options)
719726
and self.containing_transaction.sampled
720727
):
728+
logger.debug(f"[Tracing] Adding span {self.span_id} to buffer")
721729
client._span_batcher.add(self)
722730

723731
return None
@@ -1048,11 +1056,12 @@ def finish(
10481056
)
10491057
self.name = "<unlabeled transaction>"
10501058

1051-
super().finish(scope, end_timestamp)
1059+
super()._finish(scope, end_timestamp)
10521060

10531061
status_code = self._data.get(SPANDATA.HTTP_STATUS_CODE)
10541062
if (
1055-
status_code is not None
1063+
self._mode == "static"
1064+
and status_code is not None
10561065
and status_code in client.options["trace_ignore_status_codes"]
10571066
):
10581067
logger.debug(
@@ -1084,6 +1093,11 @@ def finish(
10841093

10851094
return None
10861095

1096+
if self._mode == "stream" and self.containing_transaction.sampled:
1097+
logger.debug(f"[Tracing] Adding span {self.span_id} to buffer")
1098+
client._span_batcher.add(self)
1099+
return
1100+
10871101
finished_spans = [
10881102
span.to_json()
10891103
for span in self._span_recorder.spans

sentry_sdk/transport.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,23 @@ def _send_envelope(self, envelope):
471471
if content_encoding:
472472
headers["Content-Encoding"] = content_encoding
473473

474-
self._send_request(
475-
body.getvalue(),
476-
headers=headers,
477-
endpoint_type=EndpointType.ENVELOPE,
478-
envelope=envelope,
479-
)
474+
print("ENVELOPE")
475+
print(envelope.headers)
476+
for i, item in enumerate(envelope.items):
477+
print("Item", i, item.type)
478+
print(item.headers)
479+
print(item.payload.json)
480+
print()
481+
482+
print("-----------------------------------")
483+
print()
484+
485+
# self._send_request(
486+
# body.getvalue(),
487+
# headers=headers,
488+
# endpoint_type=EndpointType.ENVELOPE,
489+
# envelope=envelope,
490+
# )
480491
return None
481492

482493
def _serialize_envelope(self, envelope):

0 commit comments

Comments
 (0)