File tree 2 files changed +40
-10
lines changed
2 files changed +40
-10
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,40 @@ impl TransactionContext {
91
91
}
92
92
}
93
93
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
+
94
128
/// Set the sampling decision for this Transaction.
95
129
///
96
130
/// This can be either an explicit boolean flag, or [`None`], which will fall
Original file line number Diff line number Diff line change @@ -26,17 +26,13 @@ fn main_span1() {
26
26
wrap_in_span ( "span1" , "" , || {
27
27
thread:: sleep ( Duration :: from_millis ( 50 ) ) ;
28
28
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
+ ) ;
33
34
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) ;
40
36
sentry:: configure_scope ( |scope| scope. set_span ( Some ( transaction. clone ( ) . into ( ) ) ) ) ;
41
37
42
38
thread:: sleep ( Duration :: from_millis ( 50 ) ) ;
You can’t perform that action at this time.
0 commit comments