@@ -2,6 +2,8 @@ use std::sync::Arc;
2
2
3
3
use cadence:: MetricSink ;
4
4
5
+ use crate :: metrics:: { MetricType , MetricValue } ;
6
+ use crate :: units:: MetricUnit ;
5
7
use crate :: { Client , Hub } ;
6
8
7
9
/// A [`cadence`] compatible [`MetricSink`].
@@ -28,9 +30,9 @@ impl<S> MetricSink for SentryMetricSink<S>
28
30
where
29
31
S : MetricSink ,
30
32
{
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
+ parse_and_add ( string , & self . client ) ; // Ignore error for now
35
+ self . sink . emit ( string )
34
36
}
35
37
36
38
fn flush ( & self ) -> std:: io:: Result < ( ) > {
45
47
}
46
48
}
47
49
50
+ fn parse_and_add ( string : & str , client : & Client ) -> Option < ( ) > {
51
+ let mut components = string. split ( '|' ) ;
52
+
53
+ let ( mri_str, value_str) = components. next ( ) ?. split_once ( ':' ) ?;
54
+ let ( name, unit) = match mri_str. split_once ( '@' ) {
55
+ Some ( ( name, unit_str) ) => ( name, unit_str. parse ( ) . ok ( ) ?) ,
56
+ None => ( mri_str, MetricUnit :: None ) ,
57
+ } ;
58
+
59
+ let ty = components. next ( ) . and_then ( |s| s. parse ( ) . ok ( ) ) ?;
60
+ let value = match ty {
61
+ MetricType :: Counter => MetricValue :: Counter ( value_str. parse ( ) . ok ( ) ?) ,
62
+ MetricType :: Distribution => MetricValue :: Distribution ( value_str. parse ( ) . ok ( ) ?) ,
63
+ MetricType :: Set => MetricValue :: Set ( value_str. parse ( ) . ok ( ) ?) ,
64
+ MetricType :: Gauge => MetricValue :: Gauge ( value_str. parse ( ) . ok ( ) ?) ,
65
+ } ;
66
+
67
+ let mut builder = client. add_metric ( name. to_owned ( ) , value) ;
68
+ builder. with_unit ( unit) ;
69
+
70
+ for component in components {
71
+ match component. chars ( ) . next ( ) {
72
+ Some ( '#' ) => {
73
+ for pair in string. get ( 1 ..) ?. split ( ',' ) {
74
+ let mut key_value = pair. splitn ( 2 , ':' ) ;
75
+
76
+ let key = key_value. next ( ) ?. to_owned ( ) ;
77
+ let value = key_value. next ( ) . unwrap_or_default ( ) . to_owned ( ) ;
78
+
79
+ builder. with_tag ( key, value) ;
80
+ }
81
+ }
82
+ _ => ( ) ,
83
+ }
84
+ }
85
+
86
+ builder. finish ( ) ;
87
+ Some ( ( ) )
88
+ }
89
+
48
90
#[ cfg( test) ]
49
91
mod tests {
50
92
use cadence:: { Counted , Distributed } ;
0 commit comments