11use std:: any:: TypeId ;
22use std:: borrow:: Cow ;
3- #[ cfg( feature = "logs" ) ]
3+ #[ cfg( any ( feature = "logs" , feature = "metrics" ) ) ]
44use std:: collections:: BTreeMap ;
55use std:: fmt;
66use std:: panic:: RefUnwindSafe ;
@@ -24,6 +24,8 @@ use crate::SessionMode;
2424use crate :: { ClientOptions , Envelope , Hub , Integration , Scope , Transport } ;
2525#[ cfg( feature = "logs" ) ]
2626use sentry_types:: protocol:: v7:: Context ;
27+ #[ cfg( all( feature = "metrics" , not( feature = "logs" ) ) ) ]
28+ use sentry_types:: protocol:: v7:: LogAttribute ;
2729#[ cfg( feature = "metrics" ) ]
2830use sentry_types:: protocol:: v7:: TraceMetric ;
2931#[ cfg( feature = "logs" ) ]
@@ -65,6 +67,8 @@ pub struct Client {
6567 metrics_batcher : RwLock < Option < Batcher < TraceMetric > > > ,
6668 #[ cfg( feature = "logs" ) ]
6769 default_log_attributes : Option < BTreeMap < String , LogAttribute > > ,
70+ #[ cfg( feature = "metrics" ) ]
71+ default_metric_attributes : Option < BTreeMap < String , LogAttribute > > ,
6872 integrations : Vec < ( TypeId , Arc < dyn Integration > ) > ,
6973 pub ( crate ) sdk_info : ClientSdkInfo ,
7074}
@@ -113,6 +117,8 @@ impl Clone for Client {
113117 metrics_batcher,
114118 #[ cfg( feature = "logs" ) ]
115119 default_log_attributes : self . default_log_attributes . clone ( ) ,
120+ #[ cfg( feature = "metrics" ) ]
121+ default_metric_attributes : self . default_metric_attributes . clone ( ) ,
116122 integrations : self . integrations . clone ( ) ,
117123 sdk_info : self . sdk_info . clone ( ) ,
118124 }
@@ -208,13 +214,18 @@ impl Client {
208214 metrics_batcher,
209215 #[ cfg( feature = "logs" ) ]
210216 default_log_attributes : None ,
217+ #[ cfg( feature = "metrics" ) ]
218+ default_metric_attributes : None ,
211219 integrations,
212220 sdk_info,
213221 } ;
214222
215223 #[ cfg( feature = "logs" ) ]
216224 client. cache_default_log_attributes ( ) ;
217225
226+ #[ cfg( feature = "metrics" ) ]
227+ client. cache_default_metric_attributes ( ) ;
228+
218229 client
219230 }
220231
@@ -269,6 +280,35 @@ impl Client {
269280 self . default_log_attributes = Some ( attributes) ;
270281 }
271282
283+ #[ cfg( feature = "metrics" ) ]
284+ 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" . to_owned ( ) , environment. clone ( ) . into ( ) ) ;
289+ }
290+
291+ if let Some ( release) = self . options . release . as_ref ( ) {
292+ attributes. insert ( "sentry.release" . to_owned ( ) , release. clone ( ) . into ( ) ) ;
293+ }
294+
295+ attributes. insert (
296+ "sentry.sdk.name" . to_owned ( ) ,
297+ self . sdk_info . name . to_owned ( ) . into ( ) ,
298+ ) ;
299+
300+ attributes. insert (
301+ "sentry.sdk.version" . to_owned ( ) ,
302+ self . sdk_info . version . to_owned ( ) . into ( ) ,
303+ ) ;
304+
305+ if let Some ( server) = & self . options . server_name {
306+ attributes. insert ( "server.address" . to_owned ( ) , server. clone ( ) . into ( ) ) ;
307+ }
308+
309+ self . default_metric_attributes = Some ( attributes) ;
310+ }
311+
272312 pub ( crate ) fn get_integration < I > ( & self ) -> Option < & I >
273313 where
274314 I : Integration ,
@@ -524,16 +564,37 @@ impl Client {
524564
525565 /// Captures a trace metric and sends it to Sentry.
526566 #[ cfg( feature = "metrics" ) ]
527- pub fn capture_metric ( & self , metric : TraceMetric , _: & Scope ) {
528- // TODO: Read scope
529- if let Some ( batcher) = self
530- . metrics_batcher
531- . read ( )
532- . expect ( "metrics batcher lock could not be acquired" )
533- . as_ref ( )
534- {
535- batcher. enqueue ( metric) ;
567+ pub fn capture_metric ( & self , metric : TraceMetric , scope : & Scope ) {
568+ if !self . options . enable_metrics {
569+ return ;
536570 }
571+ if let Some ( metric) = self . prepare_metric ( metric, scope) {
572+ if let Some ( ref batcher) = * self . metrics_batcher . read ( ) . unwrap ( ) {
573+ batcher. enqueue ( metric) ;
574+ }
575+ }
576+ }
577+
578+ /// Prepares a metric to be sent, setting the `trace_id` and other default attributes, and
579+ /// processing it through `before_send_metric`.
580+ #[ cfg( feature = "metrics" ) ]
581+ fn prepare_metric ( & self , mut metric : TraceMetric , scope : & Scope ) -> Option < TraceMetric > {
582+ scope. apply_to_metric ( & mut metric, self . options . send_default_pii ) ;
583+
584+ if let Some ( default_attributes) = self . default_metric_attributes . as_ref ( ) {
585+ for ( key, val) in default_attributes. iter ( ) {
586+ metric
587+ . attributes
588+ . entry ( key. to_owned ( ) )
589+ . or_insert ( val. clone ( ) ) ;
590+ }
591+ }
592+
593+ if let Some ( ref func) = self . options . before_send_metric {
594+ metric = func ( metric) ?;
595+ }
596+
597+ Some ( metric)
537598 }
538599}
539600
0 commit comments