@@ -2,6 +2,8 @@ use std::sync::Arc;
2
2
3
3
use cadence:: MetricSink ;
4
4
5
+ use crate :: metrics:: { Metric , MetricType , MetricValue } ;
6
+ use crate :: units:: MetricUnit ;
5
7
use crate :: { Client , Hub } ;
6
8
7
9
/// A [`cadence`] compatible [`MetricSink`].
@@ -28,9 +30,12 @@ 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
+ if let Some ( metric) = parse_metric ( string) {
35
+ self . client . add_metric ( metric) ;
36
+ }
37
+
38
+ self . sink . emit ( string)
34
39
}
35
40
36
41
fn flush ( & self ) -> std:: io:: Result < ( ) > {
45
50
}
46
51
}
47
52
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
+
48
88
#[ cfg( test) ]
49
89
mod tests {
50
90
use cadence:: { Counted , Distributed } ;
0 commit comments