Skip to content

Commit dda2c03

Browse files
authored
chore(tracing): Pre-lazy-tracestate cleanup (getsentry#1168)
Mostly docstrings and comments. Also a stray, no-longer-necessary variable initialization and a few log statements. Pulled from an upcoming PR in order to make it less noisy.
1 parent 7876df8 commit dda2c03

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

sentry_sdk/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def capture_event(
332332

333333
# this is outside of the `if` immediately below because even if we don't
334334
# use the value, we want to make sure we remove it before the event is
335-
# sent (which the `.pop()` does)
335+
# sent
336336
raw_tracestate = (
337337
event_opt.get("contexts", {}).get("trace", {}).pop("tracestate", "")
338338
)

sentry_sdk/integrations/httpx.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sentry_sdk import Hub
22
from sentry_sdk.integrations import Integration, DidNotEnable
3+
from sentry_sdk.utils import logger
34

45
from sentry_sdk._types import MYPY
56

@@ -45,6 +46,11 @@ def send(self, request, **kwargs):
4546
span.set_data("method", request.method)
4647
span.set_data("url", str(request.url))
4748
for key, value in hub.iter_trace_propagation_headers():
49+
logger.debug(
50+
"[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format(
51+
key=key, value=value, url=request.url
52+
)
53+
)
4854
request.headers[key] = value
4955
rv = real_send(self, request, **kwargs)
5056

@@ -72,6 +78,11 @@ async def send(self, request, **kwargs):
7278
span.set_data("method", request.method)
7379
span.set_data("url", str(request.url))
7480
for key, value in hub.iter_trace_propagation_headers():
81+
logger.debug(
82+
"[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format(
83+
key=key, value=value, url=request.url
84+
)
85+
)
7586
request.headers[key] = value
7687
rv = await real_send(self, request, **kwargs)
7788

sentry_sdk/integrations/stdlib.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sentry_sdk.integrations import Integration
88
from sentry_sdk.scope import add_global_event_processor
99
from sentry_sdk.tracing_utils import EnvironHeaders
10-
from sentry_sdk.utils import capture_internal_exceptions, safe_repr
10+
from sentry_sdk.utils import capture_internal_exceptions, logger, safe_repr
1111

1212
from sentry_sdk._types import MYPY
1313

@@ -86,6 +86,11 @@ def putrequest(self, method, url, *args, **kwargs):
8686
rv = real_putrequest(self, method, url, *args, **kwargs)
8787

8888
for key, value in hub.iter_trace_propagation_headers(span):
89+
logger.debug(
90+
"[Tracing] Adding `{key}` header {value} to outgoing request to {real_url}.".format(
91+
key=key, value=value, real_url=real_url
92+
)
93+
)
8994
self.putheader(key, value)
9095

9196
self._sentrysdk_span = span

sentry_sdk/scope.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def transaction(self, value):
174174
# anything set in the scope.
175175
# XXX: note that with the introduction of the Scope.transaction getter,
176176
# there is a semantic and type mismatch between getter and setter. The
177-
# getter returns a transaction, the setter sets a transaction name.
177+
# getter returns a Transaction, the setter sets a transaction name.
178178
# Without breaking version compatibility, we could make the setter set a
179179
# transaction name or transaction (self._span) depending on the type of
180180
# the value argument.

sentry_sdk/tracing.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
if MYPY:
2323
import typing
2424

25-
from typing import Generator
2625
from typing import Optional
2726
from typing import Any
2827
from typing import Dict
2928
from typing import List
3029
from typing import Tuple
30+
from typing import Iterator
3131

3232
from sentry_sdk._types import SamplingContext
3333

@@ -183,19 +183,19 @@ def start_child(self, **kwargs):
183183
"""
184184
kwargs.setdefault("sampled", self.sampled)
185185

186-
rv = Span(
186+
child = Span(
187187
trace_id=self.trace_id, span_id=None, parent_span_id=self.span_id, **kwargs
188188
)
189189

190190
if isinstance(self, Transaction):
191-
rv._containing_transaction = self
191+
child._containing_transaction = self
192192
else:
193-
rv._containing_transaction = self._containing_transaction
193+
child._containing_transaction = self._containing_transaction
194194

195-
rv._span_recorder = recorder = self._span_recorder
195+
child._span_recorder = recorder = self._span_recorder
196196
if recorder:
197-
recorder.add(rv)
198-
return rv
197+
recorder.add(child)
198+
return child
199199

200200
def new_span(self, **kwargs):
201201
# type: (**Any) -> Span
@@ -253,9 +253,15 @@ def continue_from_headers(
253253
return transaction
254254

255255
def iter_headers(self):
256-
# type: () -> Generator[Tuple[str, str], None, None]
256+
# type: () -> Iterator[Tuple[str, str]]
257+
"""
258+
Creates a generator which returns the span's `sentry-trace` and
259+
`tracestate` headers.
260+
"""
257261
yield "sentry-trace", self.to_traceparent()
262+
258263
tracestate = self.to_tracestate()
264+
# `tracestate` will only be `None` if there's no client or no DSN
259265
if tracestate:
260266
yield "tracestate", tracestate
261267

@@ -295,9 +301,11 @@ def to_traceparent(self):
295301
def to_tracestate(self):
296302
# type: () -> Optional[str]
297303
"""
298-
Generates the `tracestate` header value to attach to outgoing requests.
304+
Computes the `tracestate` header value using data from the containing
305+
transaction.
306+
307+
Returns None if there's no client and/or no DSN.
299308
"""
300-
header_value = None
301309

302310
if isinstance(self, Transaction):
303311
transaction = self # type: Optional[Transaction]

sentry_sdk/tracing_utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ def compute_tracestate_value(data):
285285
# type: (typing.Mapping[str, str]) -> str
286286
"""
287287
Computes a new tracestate value using the given data.
288+
289+
Note: Returns just the base64-encoded data, NOT the full `sentry=...`
290+
tracestate entry.
288291
"""
289292

290293
tracestate_json = json.dumps(data)
@@ -300,7 +303,9 @@ def compute_tracestate_value(data):
300303
def compute_tracestate_entry(span):
301304
# type: (Span) -> str
302305
"""
303-
Computes a new tracestate value for the span.
306+
Computes a new sentry tracestate for the span. Includes the `sentry=`.
307+
308+
Will return `None` if there's no client and/or no DSN.
304309
"""
305310
data = {}
306311

0 commit comments

Comments
 (0)