Skip to content

Commit c3d389e

Browse files
authored
Simplify continue_trace to reuse propagation_context values (#5158)
### Description We are already parsing both `sentry-trace` and `baggage` headers and filling in `sample_rand` while constructing the `propagation_context`. We don't need the redundant logic in `Transaction.continue_from_headers` anymore.
1 parent 972c2d7 commit c3d389e

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

sentry_sdk/scope.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,28 +1186,26 @@ def continue_trace(
11861186
"""
11871187
self.generate_propagation_context(environ_or_headers)
11881188

1189-
# When we generate the propagation context, the sample_rand value is set
1190-
# if missing or invalid (we use the original value if it's valid).
1191-
# We want the transaction to use the same sample_rand value. Due to duplicated
1192-
# propagation logic in the transaction, we pass it in to avoid recomputing it
1193-
# in the transaction.
1194-
# TYPE SAFETY: self.generate_propagation_context() ensures that self._propagation_context
1195-
# is not None.
1196-
sample_rand = typing.cast(
1197-
PropagationContext, self._propagation_context
1198-
)._sample_rand()
1199-
1200-
transaction = Transaction.continue_from_headers(
1201-
normalize_incoming_data(environ_or_headers),
1202-
_sample_rand=sample_rand,
1189+
# generate_propagation_context ensures that the propagation_context is not None.
1190+
propagation_context = typing.cast(PropagationContext, self._propagation_context)
1191+
1192+
optional_kwargs = {}
1193+
if name:
1194+
optional_kwargs["name"] = name
1195+
if source:
1196+
optional_kwargs["source"] = source
1197+
1198+
return Transaction(
12031199
op=op,
12041200
origin=origin,
1205-
name=name,
1206-
source=source,
1201+
baggage=propagation_context.baggage,
1202+
parent_sampled=propagation_context.parent_sampled,
1203+
trace_id=propagation_context.trace_id,
1204+
parent_span_id=propagation_context.parent_span_id,
1205+
same_process_as_parent=False,
1206+
**optional_kwargs,
12071207
)
12081208

1209-
return transaction
1210-
12111209
def capture_event(self, event, hint=None, scope=None, **scope_kwargs):
12121210
# type: (Event, Optional[Hint], Optional[Scope], Any) -> Optional[str]
12131211
"""

0 commit comments

Comments
 (0)