@@ -68,7 +68,7 @@ pub struct Client {
6868 #[ cfg( feature = "logs" ) ]
6969 default_log_attributes : Option < BTreeMap < String , LogAttribute > > ,
7070 #[ cfg( feature = "metrics" ) ]
71- default_metric_attributes : Option < BTreeMap < Cow < ' static , str > , LogAttribute > > ,
71+ default_metric_attributes : BTreeMap < Cow < ' static , str > , LogAttribute > ,
7272 integrations : Vec < ( TypeId , Arc < dyn Integration > ) > ,
7373 pub ( crate ) sdk_info : ClientSdkInfo ,
7474}
@@ -215,7 +215,7 @@ impl Client {
215215 #[ cfg( feature = "logs" ) ]
216216 default_log_attributes : None ,
217217 #[ cfg( feature = "metrics" ) ]
218- default_metric_attributes : None ,
218+ default_metric_attributes : Default :: default ( ) ,
219219 integrations,
220220 sdk_info,
221221 } ;
@@ -282,31 +282,24 @@ impl Client {
282282
283283 #[ cfg( feature = "metrics" ) ]
284284 fn cache_default_metric_attributes ( & mut self ) {
285- let mut attributes = BTreeMap :: new ( ) ;
286-
287- if let Some ( environment) = self . options . environment . as_ref ( ) {
288- attributes. insert ( "sentry.environment" . into ( ) , environment. clone ( ) . into ( ) ) ;
289- }
290-
291- if let Some ( release) = self . options . release . as_ref ( ) {
292- attributes. insert ( "sentry.release" . into ( ) , release. clone ( ) . into ( ) ) ;
293- }
294-
295- attributes. insert (
296- "sentry.sdk.name" . into ( ) ,
297- self . sdk_info . name . to_owned ( ) . into ( ) ,
298- ) ;
299-
300- attributes. insert (
301- "sentry.sdk.version" . into ( ) ,
302- self . sdk_info . version . to_owned ( ) . into ( ) ,
303- ) ;
304-
305- if let Some ( server) = & self . options . server_name {
306- attributes. insert ( "server.address" . into ( ) , server. clone ( ) . into ( ) ) ;
307- }
308-
309- self . default_metric_attributes = Some ( attributes) ;
285+ let always_present_attributes = [
286+ ( "sentry.sdk.name" , & self . sdk_info . name ) ,
287+ ( "sentry.sdk.version" , & self . sdk_info . version ) ,
288+ ]
289+ . into_iter ( )
290+ . map ( |( name, value) | ( name. into ( ) , value. as_str ( ) . into ( ) ) ) ;
291+
292+ let maybe_present_attributes = [
293+ ( "sentry.environment" , & self . options . environment ) ,
294+ ( "sentry.release" , & self . options . release ) ,
295+ ( "server.address" , & self . options . server_name ) ,
296+ ]
297+ . into_iter ( )
298+ . filter_map ( |( name, value) | value. clone ( ) . map ( |value| ( name. into ( ) , value. into ( ) ) ) ) ;
299+
300+ self . default_metric_attributes = maybe_present_attributes
301+ . chain ( always_present_attributes)
302+ . collect ( ) ;
310303 }
311304
312305 pub ( crate ) fn get_integration < I > ( & self ) -> Option < & I >
@@ -565,11 +558,13 @@ impl Client {
565558 /// Captures a metric and sends it to Sentry.
566559 #[ cfg( feature = "metrics" ) ]
567560 pub fn capture_metric ( & self , metric : Metric , scope : & Scope ) {
568- if !self . options . enable_metrics {
569- return ;
570- }
571561 if let Some ( metric) = self . prepare_metric ( metric, scope) {
572- if let Some ( ref batcher) = * self . metrics_batcher . read ( ) . unwrap ( ) {
562+ if let Some ( batcher) = self
563+ . metrics_batcher
564+ . read ( )
565+ . expect ( "metrics batcher lock could not be acquired" )
566+ . as_ref ( )
567+ {
573568 batcher. enqueue ( metric) ;
574569 }
575570 }
@@ -581,10 +576,8 @@ impl Client {
581576 fn prepare_metric ( & self , mut metric : Metric , scope : & Scope ) -> Option < Metric > {
582577 scope. apply_to_metric ( & mut metric, self . options . send_default_pii ) ;
583578
584- if let Some ( default_attributes) = self . default_metric_attributes . as_ref ( ) {
585- for ( key, val) in default_attributes. iter ( ) {
586- metric. attributes . entry ( key. clone ( ) ) . or_insert ( val. clone ( ) ) ;
587- }
579+ for ( key, val) in & self . default_metric_attributes {
580+ metric. attributes . entry ( key. clone ( ) ) . or_insert ( val. clone ( ) ) ;
588581 }
589582
590583 if let Some ( ref func) = self . options . before_send_metric {
0 commit comments