@@ -6,15 +6,15 @@ use std::sync::Mutex;
66use std:: sync:: { Arc , PoisonError , RwLock } ;
77
88use crate :: performance:: TransactionOrSpan ;
9- use crate :: protocol:: {
10- Attachment , Breadcrumb , Context , Event , Level , TraceContext , Transaction , User , Value ,
11- } ;
129#[ cfg( feature = "logs" ) ]
1310use crate :: protocol:: Log ;
1411#[ cfg( any( feature = "logs" , feature = "metrics" ) ) ]
1512use crate :: protocol:: LogAttribute ;
1613#[ cfg( feature = "metrics" ) ]
1714use crate :: protocol:: Metric ;
15+ use crate :: protocol:: {
16+ Attachment , Breadcrumb , Context , Event , Level , TraceContext , Transaction , User , Value ,
17+ } ;
1818#[ cfg( feature = "release-health" ) ]
1919use crate :: session:: Session ;
2020use crate :: { Client , SentryTrace , TraceHeader , TraceHeadersIter } ;
@@ -408,52 +408,26 @@ impl Scope {
408408 /// `send_default_pii` is `true`.
409409 #[ cfg( feature = "metrics" ) ]
410410 pub fn apply_to_metric ( & self , metric : & mut Metric , send_default_pii : bool ) {
411- if let Some ( span) = self . span . as_ref ( ) {
412- metric. trace_id = span. get_trace_context ( ) . trace_id ;
413- } else {
414- metric. trace_id = self . propagation_context . trace_id ;
415- }
411+ metric. trace_id = match self . span . as_ref ( ) . as_ref ( ) {
412+ Some ( span) => span. get_trace_context ( ) . trace_id ,
413+ None => self . propagation_context . trace_id ,
414+ } ;
416415
417- if metric. span_id . is_none ( ) {
418- if let Some ( span) = self . get_span ( ) {
419- let span_id = match span {
420- crate :: TransactionOrSpan :: Transaction ( transaction) => {
421- transaction. get_trace_context ( ) . span_id
422- }
423- crate :: TransactionOrSpan :: Span ( span) => span. get_span_id ( ) ,
424- } ;
425- metric. span_id = Some ( span_id) ;
426- }
427- }
416+ metric. span_id = metric
417+ . span_id
418+ . or_else ( || self . get_span ( ) . map ( |ts| ts. span_id ( ) ) ) ;
428419
429- if send_default_pii {
430- if let Some ( user) = self . user . as_ref ( ) {
431- if !metric. attributes . contains_key ( "user.id" ) {
432- if let Some ( id) = user. id . as_ref ( ) {
433- metric
434- . attributes
435- . insert ( "user.id" . to_owned ( ) , LogAttribute ( id. to_owned ( ) . into ( ) ) ) ;
436- }
437- }
420+ if !send_default_pii {
421+ return ;
422+ }
438423
439- if !metric. attributes . contains_key ( "user.name" ) {
440- if let Some ( name) = user. username . as_ref ( ) {
441- metric
442- . attributes
443- . insert ( "user.name" . to_owned ( ) , LogAttribute ( name. to_owned ( ) . into ( ) ) ) ;
444- }
445- }
424+ let Some ( user) = self . user . as_ref ( ) else {
425+ return ;
426+ } ;
446427
447- if !metric. attributes . contains_key ( "user.email" ) {
448- if let Some ( email) = user. email . as_ref ( ) {
449- metric. attributes . insert (
450- "user.email" . to_owned ( ) ,
451- LogAttribute ( email. to_owned ( ) . into ( ) ) ,
452- ) ;
453- }
454- }
455- }
456- }
428+ metric. insert_attribute ( "user.id" , user. id . as_deref ( ) ) ;
429+ metric. insert_attribute ( "user.name" , user. username . as_deref ( ) ) ;
430+ metric. insert_attribute ( "user.email" , user. email . as_deref ( ) ) ;
457431 }
458432
459433 /// Set the given [`TransactionOrSpan`] as the active span for this scope.
@@ -501,3 +475,22 @@ impl Scope {
501475 }
502476 }
503477}
478+
479+ #[ cfg( feature = "metrics" ) ]
480+ trait MetricExt {
481+ fn insert_attribute < K , V > ( & mut self , key : K , value : V )
482+ where
483+ K : Into < String > ,
484+ V : Into < Value > ;
485+ }
486+
487+ #[ cfg( feature = "metrics" ) ]
488+ impl MetricExt for Metric {
489+ fn insert_attribute < K , V > ( & mut self , key : K , value : V )
490+ where
491+ K : Into < String > ,
492+ V : Into < Value > ,
493+ {
494+ self . attributes . insert ( key, LogAttribute ( value. into ( ) ) ) ;
495+ }
496+ }
0 commit comments