Skip to content

Commit 2fa1db3

Browse files
committed
introduce a continue_from_span method
1 parent 9ab46da commit 2fa1db3

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

sentry-core/src/performance.rs

+34
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,40 @@ impl TransactionContext {
9191
}
9292
}
9393

94+
/// Creates a new Transaction Context based on an existing Span.
95+
///
96+
/// This should be used when an independent computation is spawned on another
97+
/// thread and should be connected to the calling thread via a distributed
98+
/// tracing transaction.
99+
pub fn continue_from_span(name: &str, op: &str, span: Option<TransactionOrSpan>) -> Self {
100+
let span = match span {
101+
Some(span) => span,
102+
None => return Self::new(name, op),
103+
};
104+
105+
let (trace_id, parent_span_id, sampled) = match span {
106+
TransactionOrSpan::Transaction(transaction) => {
107+
let inner = transaction.inner.lock().unwrap();
108+
(
109+
inner.context.trace_id,
110+
inner.context.span_id,
111+
Some(inner.sampled),
112+
)
113+
}
114+
TransactionOrSpan::Span(span) => {
115+
(span.span.trace_id, span.span.span_id, Some(span.sampled))
116+
}
117+
};
118+
119+
Self {
120+
name: name.into(),
121+
op: op.into(),
122+
trace_id,
123+
parent_span_id: Some(parent_span_id),
124+
sampled,
125+
}
126+
}
127+
94128
/// Set the sampling decision for this Transaction.
95129
///
96130
/// This can be either an explicit boolean flag, or [`None`], which will fall

sentry/examples/performance-demo.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ fn main_span1() {
2626
wrap_in_span("span1", "", || {
2727
thread::sleep(Duration::from_millis(50));
2828

29-
let headers = match sentry::configure_scope(|scope| scope.get_span()) {
30-
Some(span) => vec![span.iter_headers().next().unwrap()],
31-
None => vec![],
32-
};
29+
let transaction_ctx = sentry::TransactionContext::continue_from_span(
30+
"background transaction",
31+
"root span",
32+
sentry::configure_scope(|scope| scope.get_span()),
33+
);
3334
thread::spawn(move || {
34-
let transaction =
35-
sentry::start_transaction(sentry::TransactionContext::continue_from_headers(
36-
"background transaction",
37-
"root span",
38-
headers.iter().map(|(k, v)| (*k, v.as_str())),
39-
));
35+
let transaction = sentry::start_transaction(transaction_ctx);
4036
sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into())));
4137

4238
thread::sleep(Duration::from_millis(50));

0 commit comments

Comments
 (0)