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