@@ -40,18 +40,24 @@ impl Metric {
4040 }
4141
4242 // TODO: formatting of floats
43- pub fn to_string ( & self ) -> String {
43+ // Convert to prometheus exposition format
44+ pub fn to_prom_string ( & self ) -> String {
4445 let labels_str = if self . labels . is_empty ( ) {
4546 String :: new ( )
4647 } else {
47- let labels: Vec < String > = self . labels . iter ( )
48+ let labels: Vec < String > = self
49+ . labels
50+ . iter ( )
4851 . map ( |( k, v) | format ! ( "{}=\" {}\" " , k, v) )
4952 . collect ( ) ;
5053 format ! ( "{{{}}}" , labels. join( "," ) )
5154 } ;
52- format ! ( "# HELP {} {}\n \
55+ format ! (
56+ "# HELP {} {}\n \
5357 # TYPE {} counter\n \
54- {}{} {}", self . name, self . annotation, self . name, self . name, labels_str, self . value)
58+ {}{} {}",
59+ self . name, self . annotation, self . name, self . name, labels_str, self . value
60+ )
5561 }
5662}
5763
@@ -70,7 +76,7 @@ impl MetricsWriter {
7076 let path = Path :: new ( & self . file_path ) ;
7177 let mut file = File :: create ( & path) ?;
7278 for metric in metrics {
73- writeln ! ( file, "{}" , metric. to_string ( ) ) ?;
79+ writeln ! ( file, "{}" , metric. to_prom_string ( ) ) ?;
7480 }
7581 Ok ( ( ) )
7682 }
@@ -84,9 +90,12 @@ mod tests {
8490 let metric = Metric :: new ( "test_metric" , 123.45 )
8591 . add_label ( "label1" , "value1" )
8692 . add_label ( "label2" , "value2" ) ;
87- assert_eq ! ( metric. to_string( ) , "# HELP test_metric Custom metric\n \
93+ assert_eq ! (
94+ metric. to_prom_string( ) ,
95+ "# HELP test_metric Custom metric\n \
8896 # TYPE test_metric counter\n \
89- test_metric{label1=\" value1\" ,label2=\" value2\" } 123.45") ;
97+ test_metric{label1=\" value1\" ,label2=\" value2\" } 123.45"
98+ ) ;
9099 }
91100
92101 #[ test]
@@ -98,37 +107,49 @@ mod tests {
98107 let writer = MetricsWriter :: new ( "/tmp/test_metrics.prom" ) ;
99108 writer. write_metrics ( & metrics) . unwrap ( ) ;
100109 let content = std:: fs:: read_to_string ( "/tmp/test_metrics.prom" ) . unwrap ( ) ;
101- assert ! ( content. contains( "# HELP metric1 Custom metric\n \
110+ assert ! ( content. contains(
111+ "# HELP metric1 Custom metric\n \
102112 # TYPE metric1 counter\n \
103- metric1 1") ) ;
104- assert ! ( content. contains( "# HELP metric2 Custom metric\n \
113+ metric1 1"
114+ ) ) ;
115+ assert ! ( content. contains(
116+ "# HELP metric2 Custom metric\n \
105117 # TYPE metric2 counter\n \
106- metric2{label=\" value\" } 2") ) ;
118+ metric2{label=\" value\" } 2"
119+ ) ) ;
107120 }
108121
109122 #[ test]
110123 fn test_metric_large_value ( ) {
111124 let metric = Metric :: new ( "large_value_metric" , 1.0e64 ) ;
112- assert_eq ! ( metric. to_string( ) , "# HELP large_value_metric Custom metric\n \
125+ assert_eq ! (
126+ metric. to_prom_string( ) ,
127+ "# HELP large_value_metric Custom metric\n \
113128 # TYPE large_value_metric counter\n \
114- large_value_metric 10000000000000000000000000000000000000000000000000000000000000000") ;
129+ large_value_metric 10000000000000000000000000000000000000000000000000000000000000000"
130+ ) ;
115131 }
116132
117-
118133 #[ test]
119134 fn test_metric_without_labels ( ) {
120135 let metric = Metric :: new ( "no_label_metric" , 42.0 ) ;
121- assert_eq ! ( metric. to_string( ) , "# HELP no_label_metric Custom metric\n \
136+ assert_eq ! (
137+ metric. to_prom_string( ) ,
138+ "# HELP no_label_metric Custom metric\n \
122139 # TYPE no_label_metric counter\n \
123- no_label_metric 42") ;
140+ no_label_metric 42"
141+ ) ;
124142 }
125143
126144 #[ test]
127145 fn test_metric_with_annotation ( ) {
128146 let metric = Metric :: with_annotation ( "annotated_metric" , 99.9 , "This is a test metric" ) ;
129- assert_eq ! ( metric. to_string( ) , "# HELP annotated_metric This is a test metric\n \
147+ assert_eq ! (
148+ metric. to_prom_string( ) ,
149+ "# HELP annotated_metric This is a test metric\n \
130150 # TYPE annotated_metric counter\n \
131- annotated_metric 99.9") ;
151+ annotated_metric 99.9"
152+ ) ;
132153 }
133154
134155 #[ test]
@@ -145,16 +166,22 @@ mod tests {
145166 let metric = Metric :: new ( "multi_label_metric" , 10.0 )
146167 . add_label ( "foo" , "bar" )
147168 . add_label ( "version" , "1.0.0" ) ;
148- assert_eq ! ( metric. to_string( ) , "# HELP multi_label_metric Custom metric\n \
169+ assert_eq ! (
170+ metric. to_prom_string( ) ,
171+ "# HELP multi_label_metric Custom metric\n \
149172 # TYPE multi_label_metric counter\n \
150- multi_label_metric{foo=\" bar\" ,version=\" 1.0.0\" } 10") ;
173+ multi_label_metric{foo=\" bar\" ,version=\" 1.0.0\" } 10"
174+ ) ;
151175 }
152176
153177 #[ test]
154178 fn test_metric_with_empty_annotation ( ) {
155179 let metric = Metric :: with_annotation ( "empty_annotation_metric" , 5.5 , "" ) ;
156- assert_eq ! ( metric. to_string( ) , "# HELP empty_annotation_metric \n \
180+ assert_eq ! (
181+ metric. to_prom_string( ) ,
182+ "# HELP empty_annotation_metric \n \
157183 # TYPE empty_annotation_metric counter\n \
158- empty_annotation_metric 5.5") ;
184+ empty_annotation_metric 5.5"
185+ ) ;
159186 }
160- }
187+ }
0 commit comments