1
+ use std:: borrow:: Cow ;
1
2
use std:: cell:: RefCell ;
2
3
use std:: collections:: BTreeMap ;
3
4
@@ -9,6 +10,7 @@ use tracing_subscriber::layer::{Context, Layer};
9
10
use tracing_subscriber:: registry:: LookupSpan ;
10
11
11
12
use crate :: converters:: * ;
13
+ use crate :: TAGS_PREFIX ;
12
14
13
15
/// The action that Sentry should perform for a [`Metadata`]
14
16
#[ derive( Debug , Clone , Copy ) ]
@@ -142,6 +144,53 @@ where
142
144
}
143
145
}
144
146
147
+ #[ inline( always) ]
148
+ fn record_fields < ' a , K : AsRef < str > + Into < Cow < ' a , str > > > (
149
+ span : & TransactionOrSpan ,
150
+ data : BTreeMap < K , Value > ,
151
+ ) {
152
+ match span {
153
+ TransactionOrSpan :: Span ( span) => {
154
+ let mut span = span. data ( ) ;
155
+ for ( key, value) in data {
156
+ if let Some ( stripped_key) = key. as_ref ( ) . strip_prefix ( TAGS_PREFIX ) {
157
+ match value {
158
+ Value :: Bool ( value) => {
159
+ span. set_tag ( stripped_key. to_owned ( ) , value. to_string ( ) )
160
+ }
161
+ Value :: Number ( value) => {
162
+ span. set_tag ( stripped_key. to_owned ( ) , value. to_string ( ) )
163
+ }
164
+ Value :: String ( value) => span. set_tag ( stripped_key. to_owned ( ) , value) ,
165
+ _ => span. set_data ( key. into ( ) . into_owned ( ) , value) ,
166
+ }
167
+ } else {
168
+ span. set_data ( key. into ( ) . into_owned ( ) , value) ;
169
+ }
170
+ }
171
+ }
172
+ TransactionOrSpan :: Transaction ( transaction) => {
173
+ let mut transaction = transaction. data ( ) ;
174
+ for ( key, value) in data {
175
+ if let Some ( stripped_key) = key. as_ref ( ) . strip_prefix ( TAGS_PREFIX ) {
176
+ match value {
177
+ Value :: Bool ( value) => {
178
+ transaction. set_tag ( stripped_key. into ( ) , value. to_string ( ) )
179
+ }
180
+ Value :: Number ( value) => {
181
+ transaction. set_tag ( stripped_key. into ( ) , value. to_string ( ) )
182
+ }
183
+ Value :: String ( value) => transaction. set_tag ( stripped_key. into ( ) , value) ,
184
+ _ => transaction. set_data ( key. into ( ) , value) ,
185
+ }
186
+ } else {
187
+ transaction. set_data ( key. into ( ) , value) ;
188
+ }
189
+ }
190
+ }
191
+ }
192
+ }
193
+
145
194
/// Data that is attached to the tracing Spans `extensions`, in order to
146
195
/// `finish` the corresponding sentry span `on_close`, and re-set its parent as
147
196
/// the *current* span.
@@ -217,9 +266,7 @@ where
217
266
} ;
218
267
// Add the data from the original span to the sentry span.
219
268
// This comes from typically the `fields` in `tracing::instrument`.
220
- for ( key, value) in data {
221
- sentry_span. set_data ( key, value) ;
222
- }
269
+ record_fields ( & sentry_span, data) ;
223
270
224
271
sentry_core:: configure_scope ( |scope| scope. set_span ( Some ( sentry_span. clone ( ) ) ) ) ;
225
272
@@ -267,9 +314,7 @@ where
267
314
let mut data = FieldVisitor :: default ( ) ;
268
315
values. record ( & mut data) ;
269
316
270
- for ( key, value) in data. json_values {
271
- span. set_data ( & key, value) ;
272
- }
317
+ record_fields ( span, data. json_values ) ;
273
318
}
274
319
}
275
320
0 commit comments