@@ -40,18 +40,24 @@ impl Metric {
40
40
}
41
41
42
42
// 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 {
44
45
let labels_str = if self . labels . is_empty ( ) {
45
46
String :: new ( )
46
47
} else {
47
- let labels: Vec < String > = self . labels . iter ( )
48
+ let labels: Vec < String > = self
49
+ . labels
50
+ . iter ( )
48
51
. map ( |( k, v) | format ! ( "{}=\" {}\" " , k, v) )
49
52
. collect ( ) ;
50
53
format ! ( "{{{}}}" , labels. join( "," ) )
51
54
} ;
52
- format ! ( "# HELP {} {}\n \
55
+ format ! (
56
+ "# HELP {} {}\n \
53
57
# 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
+ )
55
61
}
56
62
}
57
63
@@ -70,7 +76,7 @@ impl MetricsWriter {
70
76
let path = Path :: new ( & self . file_path ) ;
71
77
let mut file = File :: create ( & path) ?;
72
78
for metric in metrics {
73
- writeln ! ( file, "{}" , metric. to_string ( ) ) ?;
79
+ writeln ! ( file, "{}" , metric. to_prom_string ( ) ) ?;
74
80
}
75
81
Ok ( ( ) )
76
82
}
@@ -84,9 +90,12 @@ mod tests {
84
90
let metric = Metric :: new ( "test_metric" , 123.45 )
85
91
. add_label ( "label1" , "value1" )
86
92
. 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 \
88
96
# TYPE test_metric counter\n \
89
- test_metric{label1=\" value1\" ,label2=\" value2\" } 123.45") ;
97
+ test_metric{label1=\" value1\" ,label2=\" value2\" } 123.45"
98
+ ) ;
90
99
}
91
100
92
101
#[ test]
@@ -98,37 +107,49 @@ mod tests {
98
107
let writer = MetricsWriter :: new ( "/tmp/test_metrics.prom" ) ;
99
108
writer. write_metrics ( & metrics) . unwrap ( ) ;
100
109
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 \
102
112
# 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 \
105
117
# TYPE metric2 counter\n \
106
- metric2{label=\" value\" } 2") ) ;
118
+ metric2{label=\" value\" } 2"
119
+ ) ) ;
107
120
}
108
121
109
122
#[ test]
110
123
fn test_metric_large_value ( ) {
111
124
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 \
113
128
# TYPE large_value_metric counter\n \
114
- large_value_metric 10000000000000000000000000000000000000000000000000000000000000000") ;
129
+ large_value_metric 10000000000000000000000000000000000000000000000000000000000000000"
130
+ ) ;
115
131
}
116
132
117
-
118
133
#[ test]
119
134
fn test_metric_without_labels ( ) {
120
135
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 \
122
139
# TYPE no_label_metric counter\n \
123
- no_label_metric 42") ;
140
+ no_label_metric 42"
141
+ ) ;
124
142
}
125
143
126
144
#[ test]
127
145
fn test_metric_with_annotation ( ) {
128
146
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 \
130
150
# TYPE annotated_metric counter\n \
131
- annotated_metric 99.9") ;
151
+ annotated_metric 99.9"
152
+ ) ;
132
153
}
133
154
134
155
#[ test]
@@ -145,16 +166,22 @@ mod tests {
145
166
let metric = Metric :: new ( "multi_label_metric" , 10.0 )
146
167
. add_label ( "foo" , "bar" )
147
168
. 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 \
149
172
# 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
+ ) ;
151
175
}
152
176
153
177
#[ test]
154
178
fn test_metric_with_empty_annotation ( ) {
155
179
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 \
157
183
# TYPE empty_annotation_metric counter\n \
158
- empty_annotation_metric 5.5") ;
184
+ empty_annotation_metric 5.5"
185
+ ) ;
159
186
}
160
- }
187
+ }
0 commit comments