@@ -2,6 +2,8 @@ use std::sync::Arc;
22
33use cadence:: MetricSink ;
44
5+ use crate :: metrics:: { Metric , MetricType , MetricValue } ;
6+ use crate :: units:: MetricUnit ;
57use crate :: { Client , Hub } ;
68
79/// A [`cadence`] compatible [`MetricSink`].
@@ -28,9 +30,12 @@ impl<S> MetricSink for SentryMetricSink<S>
2830where
2931 S : MetricSink ,
3032{
31- fn emit ( & self , metric : & str ) -> std:: io:: Result < usize > {
32- self . client . add_metric ( metric) ;
33- self . sink . emit ( metric)
33+ fn emit ( & self , string : & str ) -> std:: io:: Result < usize > {
34+ if let Some ( metric) = parse_metric ( string) {
35+ self . client . add_metric ( metric) ;
36+ }
37+
38+ self . sink . emit ( string)
3439 }
3540
3641 fn flush ( & self ) -> std:: io:: Result < ( ) > {
4550 }
4651}
4752
53+ fn parse_metric ( string : & str ) -> Option < Metric > {
54+ let mut components = string. split ( '|' ) ;
55+
56+ let ( mri_str, value_str) = components. next ( ) ?. split_once ( ':' ) ?;
57+ let ( name, unit) = match mri_str. split_once ( '@' ) {
58+ Some ( ( name, unit_str) ) => ( name, unit_str. parse ( ) . ok ( ) ?) ,
59+ None => ( mri_str, MetricUnit :: None ) ,
60+ } ;
61+
62+ let ty = components. next ( ) . and_then ( |s| s. parse ( ) . ok ( ) ) ?;
63+ let value = match ty {
64+ MetricType :: Counter => MetricValue :: Counter ( value_str. parse ( ) . ok ( ) ?) ,
65+ MetricType :: Distribution => MetricValue :: Distribution ( value_str. parse ( ) . ok ( ) ?) ,
66+ MetricType :: Set => MetricValue :: Set ( value_str. parse ( ) . ok ( ) ?) ,
67+ MetricType :: Gauge => MetricValue :: Gauge ( value_str. parse ( ) . ok ( ) ?) ,
68+ } ;
69+
70+ let mut builder = Metric :: build ( name. to_owned ( ) , value) . with_unit ( unit) ;
71+
72+ for component in components {
73+ if let Some ( '#' ) = component. chars ( ) . next ( ) {
74+ for pair in string. get ( 1 ..) ?. split ( ',' ) {
75+ let mut key_value = pair. splitn ( 2 , ':' ) ;
76+
77+ let key = key_value. next ( ) ?. to_owned ( ) ;
78+ let value = key_value. next ( ) . unwrap_or_default ( ) . to_owned ( ) ;
79+
80+ builder = builder. with_tag ( key, value) ;
81+ }
82+ }
83+ }
84+
85+ Some ( builder. finish ( ) )
86+ }
87+
4888#[ cfg( test) ]
4989mod tests {
5090 use cadence:: { Counted , Distributed } ;
0 commit comments