1
1
use prometheus_client:: encoding:: text:: encode;
2
- use prometheus_client:: encoding:: Encode ;
2
+ use prometheus_client:: encoding:: { EncodeLabelSet , EncodeLabelValue } ;
3
3
use prometheus_client:: metrics:: counter:: Counter ;
4
4
use prometheus_client:: metrics:: family:: Family ;
5
5
use prometheus_client:: registry:: Registry ;
6
6
7
- #[ derive( Clone , Hash , PartialEq , Eq , Encode ) ]
7
+ #[ derive( Clone , Hash , PartialEq , Eq , EncodeLabelSet , Debug ) ]
8
8
struct Labels {
9
9
method : Method ,
10
10
path : String ,
11
11
}
12
12
13
- #[ derive( Clone , Hash , PartialEq , Eq , Encode ) ]
13
+ #[ derive( Clone , Hash , PartialEq , Eq , EncodeLabelValue , Debug ) ]
14
14
enum Method {
15
15
Get ,
16
16
#[ allow( dead_code) ]
@@ -33,17 +33,17 @@ fn basic_flow() {
33
33
. inc ( ) ;
34
34
35
35
// Encode all metrics in the registry in the text format.
36
- let mut buffer = vec ! [ ] ;
36
+ let mut buffer = String :: new ( ) ;
37
37
encode ( & mut buffer, & registry) . unwrap ( ) ;
38
38
39
39
let expected = "# HELP my_counter This is my counter.\n " . to_owned ( )
40
40
+ "# TYPE my_counter counter\n "
41
41
+ "my_counter_total{method=\" Get\" ,path=\" /metrics\" } 1\n "
42
42
+ "# EOF\n " ;
43
- assert_eq ! ( expected, String :: from_utf8 ( buffer) . unwrap ( ) ) ;
43
+ assert_eq ! ( expected, buffer) ;
44
44
}
45
45
46
- # [ cfg ( feature = " protobuf" ) ]
46
+ // TODO: Remove protobuf module wrapper
47
47
mod protobuf {
48
48
use crate :: { Labels , Method } ;
49
49
use prometheus_client:: encoding:: proto:: encode;
@@ -66,7 +66,7 @@ mod protobuf {
66
66
. inc ( ) ;
67
67
68
68
// Encode all metrics in the registry in the OpenMetrics protobuf format.
69
- let mut metric_set = encode ( & registry) ;
69
+ let mut metric_set = encode ( & registry) . unwrap ( ) ;
70
70
let mut family: prometheus_client:: encoding:: proto:: MetricFamily =
71
71
metric_set. metric_families . pop ( ) . unwrap ( ) ;
72
72
let metric: prometheus_client:: encoding:: proto:: Metric = family. metrics . pop ( ) . unwrap ( ) ;
@@ -83,14 +83,19 @@ mod protobuf {
83
83
#[ test]
84
84
fn enums ( ) {
85
85
let mut registry = Registry :: default ( ) ;
86
- let family = Family :: < Method , Counter > :: default ( ) ;
86
+ let family = Family :: < Labels , Counter > :: default ( ) ;
87
87
registry. register ( "my_counter" , "This is my counter" , family. clone ( ) ) ;
88
88
89
89
// Record a single HTTP GET request.
90
- family. get_or_create ( & Method :: Get ) . inc ( ) ;
90
+ family
91
+ . get_or_create ( & Labels {
92
+ method : Method :: Get ,
93
+ path : "/metrics" . to_string ( ) ,
94
+ } )
95
+ . inc ( ) ;
91
96
92
97
// Encode all metrics in the registry in the OpenMetrics protobuf format.
93
- let mut metric_set = encode ( & registry) ;
98
+ let mut metric_set = encode ( & registry) . unwrap ( ) ;
94
99
let mut family: prometheus_client:: encoding:: proto:: MetricFamily =
95
100
metric_set. metric_families . pop ( ) . unwrap ( ) ;
96
101
let metric: prometheus_client:: encoding:: proto:: Metric = family. metrics . pop ( ) . unwrap ( ) ;
@@ -103,7 +108,7 @@ mod protobuf {
103
108
104
109
#[ test]
105
110
fn remap_keyword_identifiers ( ) {
106
- #[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
111
+ #[ derive( EncodeLabelSet , Hash , Clone , Eq , PartialEq , Debug ) ]
107
112
struct Labels {
108
113
// `r#type` is problematic as `r#` is not a valid OpenMetrics label name
109
114
// but one needs to use keyword identifier syntax (aka. raw identifiers)
@@ -114,17 +119,20 @@ fn remap_keyword_identifiers() {
114
119
r#type : u64 ,
115
120
}
116
121
117
- let labels = Labels { r#type : 42 } ;
122
+ let mut registry = Registry :: default ( ) ;
123
+ let family = Family :: < Labels , Counter > :: default ( ) ;
124
+ registry. register ( "my_counter" , "This is my counter" , family. clone ( ) ) ;
118
125
119
- let mut buffer = vec ! [ ] ;
126
+ // Record a single HTTP GET request.
127
+ family. get_or_create ( & Labels { r#type : 42 } ) . inc ( ) ;
120
128
121
- {
122
- use prometheus_client:: encoding:: text:: Encode ;
123
- labels. encode ( & mut buffer) . unwrap ( ) ;
124
- }
129
+ // Encode all metrics in the registry in the text format.
130
+ let mut buffer = String :: new ( ) ;
131
+ encode ( & mut buffer, & registry) . unwrap ( ) ;
125
132
126
- assert_eq ! (
127
- "type=\" 42\" " . to_string( ) ,
128
- String :: from_utf8( buffer) . unwrap( )
129
- ) ;
133
+ let expected = "# HELP my_counter This is my counter.\n " . to_owned ( )
134
+ + "# TYPE my_counter counter\n "
135
+ + "my_counter_total{type=\" 42\" } 1\n "
136
+ + "# EOF\n " ;
137
+ assert_eq ! ( expected, buffer) ;
130
138
}
0 commit comments