@@ -9,7 +9,7 @@ use tracing_subscriber::layer::Context;
9
9
use tracing_subscriber:: registry:: LookupSpan ;
10
10
11
11
use super :: layer:: SentrySpanData ;
12
- use crate :: TAGS_PREFIX ;
12
+ use crate :: { SpanPropagation , TAGS_PREFIX } ;
13
13
14
14
/// Converts a [`tracing_core::Level`] to a Sentry [`Level`]
15
15
fn convert_tracing_level ( level : & tracing_core:: Level ) -> Level {
@@ -54,40 +54,34 @@ fn extract_event_data(event: &tracing_core::Event) -> (Option<String>, FieldVisi
54
54
55
55
fn extract_event_data_with_context < S > (
56
56
event : & tracing_core:: Event ,
57
- ctx : Option < Context < S > > ,
57
+ ctx : Option < ( SpanPropagation , Context < S > ) > ,
58
58
) -> ( Option < String > , FieldVisitor )
59
59
where
60
60
S : Subscriber + for < ' a > LookupSpan < ' a > ,
61
61
{
62
62
let ( message, mut visitor) = extract_event_data ( event) ;
63
63
64
64
// Add the context fields of every parent span.
65
- let current_span = ctx. as_ref ( ) . and_then ( |ctx| {
65
+ let current_span = ctx. as_ref ( ) . and_then ( |( propagation , ctx) | {
66
66
event
67
67
. parent ( )
68
- . and_then ( |id| ctx. span ( id) )
69
- . or_else ( || ctx. lookup_current ( ) )
68
+ . and_then ( |id| ctx. span ( id) . map ( |span| ( * propagation , span ) ) )
69
+ . or_else ( || ctx. lookup_current ( ) . map ( |span| ( * propagation , span ) ) )
70
70
} ) ;
71
- if let Some ( span) = current_span {
71
+ if let Some ( ( propagation , span) ) = current_span {
72
72
for span in span. scope ( ) {
73
73
let name = span. name ( ) ;
74
74
let ext = span. extensions ( ) ;
75
75
if let Some ( span_data) = ext. get :: < SentrySpanData > ( ) {
76
76
match & span_data. sentry_span {
77
77
TransactionOrSpan :: Span ( span) => {
78
78
for ( key, value) in span. data ( ) . iter ( ) {
79
- if key != "message" {
80
- let key = format ! ( "{}:{}" , name, key) ;
81
- visitor. json_values . insert ( key, value. clone ( ) ) ;
82
- }
79
+ visitor. propagate_span_attr ( key, value, propagation, name) ;
83
80
}
84
81
}
85
82
TransactionOrSpan :: Transaction ( transaction) => {
86
83
for ( key, value) in transaction. data ( ) . iter ( ) {
87
- if key != "message" {
88
- let key = format ! ( "{}:{}" , name, key) ;
89
- visitor. json_values . insert ( key, value. clone ( ) ) ;
90
- }
84
+ visitor. propagate_span_attr ( key, value, propagation, name) ;
91
85
}
92
86
}
93
87
}
@@ -106,6 +100,26 @@ pub(crate) struct FieldVisitor {
106
100
}
107
101
108
102
impl FieldVisitor {
103
+ fn propagate_span_attr (
104
+ & mut self ,
105
+ key : & str ,
106
+ value : & Value ,
107
+ span_propagation : SpanPropagation ,
108
+ span_name : & str ,
109
+ ) {
110
+ if key != "message" {
111
+ if span_propagation. is_tags_enabled ( ) && key. starts_with ( TAGS_PREFIX ) {
112
+ //Propagate tags as it is, it will be extracted later on
113
+ if !self . json_values . contains_key ( key) {
114
+ self . json_values . insert ( key. to_owned ( ) , value. clone ( ) ) ;
115
+ }
116
+ } else if span_propagation. is_attrs_enabled ( ) {
117
+ let key = format ! ( "{}:{}" , span_name, key) ;
118
+ self . json_values . insert ( key, value. clone ( ) ) ;
119
+ }
120
+ }
121
+ }
122
+
109
123
fn record < T : Into < Value > > ( & mut self , field : & Field , value : T ) {
110
124
self . json_values
111
125
. insert ( field. name ( ) . to_owned ( ) , value. into ( ) ) ;
@@ -144,12 +158,19 @@ impl Visit for FieldVisitor {
144
158
/// Creates a [`Breadcrumb`] from a given [`tracing_core::Event`]
145
159
pub fn breadcrumb_from_event < ' context , S > (
146
160
event : & tracing_core:: Event ,
147
- ctx : impl Into < Option < Context < ' context , S > > > ,
161
+ ctx : impl Into < Option < ( SpanPropagation , Context < ' context , S > ) > > ,
148
162
) -> Breadcrumb
149
163
where
150
164
S : Subscriber + for < ' a > LookupSpan < ' a > ,
151
165
{
152
- let ( message, visitor) = extract_event_data_with_context ( event, ctx. into ( ) ) ;
166
+ let ctx = match ctx. into ( ) {
167
+ Some ( ( propagation, ctx) ) if propagation. is_attrs_enabled ( ) => {
168
+ Some ( ( SpanPropagation :: Attributes , ctx) )
169
+ }
170
+ //Breadcrumb has no tags, so propagate only attributes
171
+ _ => None ,
172
+ } ;
173
+ let ( message, visitor) = extract_event_data_with_context ( event, ctx) ;
153
174
Breadcrumb {
154
175
category : Some ( event. metadata ( ) . target ( ) . to_owned ( ) ) ,
155
176
ty : "log" . into ( ) ,
@@ -220,7 +241,7 @@ fn contexts_from_event(
220
241
/// Creates an [`Event`] from a given [`tracing_core::Event`]
221
242
pub fn event_from_event < ' context , S > (
222
243
event : & tracing_core:: Event ,
223
- ctx : impl Into < Option < Context < ' context , S > > > ,
244
+ ctx : impl Into < Option < ( SpanPropagation , Context < ' context , S > ) > > ,
224
245
) -> Event < ' static >
225
246
where
226
247
S : Subscriber + for < ' a > LookupSpan < ' a > ,
@@ -240,7 +261,7 @@ where
240
261
/// Creates an exception [`Event`] from a given [`tracing_core::Event`]
241
262
pub fn exception_from_event < ' context , S > (
242
263
event : & tracing_core:: Event ,
243
- ctx : impl Into < Option < Context < ' context , S > > > ,
264
+ ctx : impl Into < Option < ( SpanPropagation , Context < ' context , S > ) > > ,
244
265
) -> Event < ' static >
245
266
where
246
267
S : Subscriber + for < ' a > LookupSpan < ' a > ,
0 commit comments