@@ -35,9 +35,11 @@ impl Encoder for TextEncoder {
3535 check_metric_family ( mf) ?;
3636
3737 // Write `# HELP` header.
38- let name = mf. get_name ( ) ;
39- let help = mf. get_help ( ) ;
40- if !help. is_empty ( ) {
38+ let name = match & mf. name {
39+ Some ( v) => & * * v,
40+ None => "" ,
41+ } ;
42+ if let Some ( help) = & mf. help {
4143 writer. write_all ( b"# HELP " ) ?;
4244 writer. write_all ( name. as_bytes ( ) ) ?;
4345 writer. write_all ( b" " ) ?;
@@ -46,88 +48,114 @@ impl Encoder for TextEncoder {
4648 }
4749
4850 // Write `# TYPE` header.
49- let metric_type = mf. get_field_type ( ) ;
51+ let metric_type = mf
52+ . r#type
53+ . map ( MetricType :: from_i32)
54+ . flatten ( )
55+ . unwrap_or_default ( ) ;
5056 let lowercase_type = format ! ( "{:?}" , metric_type) . to_lowercase ( ) ;
5157 writer. write_all ( b"# TYPE " ) ?;
5258 writer. write_all ( name. as_bytes ( ) ) ?;
5359 writer. write_all ( b" " ) ?;
5460 writer. write_all ( lowercase_type. as_bytes ( ) ) ?;
5561 writer. write_all ( b"\n " ) ?;
5662
57- for m in mf. get_metric ( ) {
63+ for m in & mf. metric {
5864 match metric_type {
59- MetricType :: COUNTER => {
60- write_sample ( writer, name, None , m, None , m. get_counter ( ) . get_value ( ) ) ?;
65+ MetricType :: Counter => {
66+ let value = match & m. counter {
67+ Some ( v) => v. value . unwrap_or_default ( ) ,
68+ None => 0.0 ,
69+ } ;
70+ write_sample ( writer, & name, None , m, None , value) ?;
6171 }
62- MetricType :: GAUGE => {
63- write_sample ( writer, name, None , m, None , m. get_gauge ( ) . get_value ( ) ) ?;
72+ MetricType :: Gauge => {
73+ let value = match & m. gauge {
74+ Some ( v) => v. value . unwrap_or_default ( ) ,
75+ None => 0.0 ,
76+ } ;
77+ write_sample ( writer, & name, None , m, None , value) ?;
6478 }
65- MetricType :: HISTOGRAM => {
66- let h = m. get_histogram ( ) ;
79+ MetricType :: Histogram => {
80+ if let Some ( h) = & m. histogram {
81+ let mut inf_seen = false ;
82+ for b in & h. bucket {
83+ let upper_bound = b. upper_bound . unwrap_or_default ( ) ;
84+ write_sample (
85+ writer,
86+ & name,
87+ Some ( "_bucket" ) ,
88+ m,
89+ Some ( ( BUCKET_LABEL , & upper_bound. to_string ( ) ) ) ,
90+ b. cumulative_count . unwrap_or_default ( ) as f64 ,
91+ ) ?;
92+ if upper_bound. is_sign_positive ( ) && upper_bound. is_infinite ( ) {
93+ inf_seen = true ;
94+ }
95+ }
96+ if !inf_seen {
97+ write_sample (
98+ writer,
99+ & name,
100+ Some ( "_bucket" ) ,
101+ m,
102+ Some ( ( BUCKET_LABEL , POSITIVE_INF ) ) ,
103+ h. sample_count . unwrap_or_default ( ) as f64 ,
104+ ) ?;
105+ }
67106
68- let mut inf_seen = false ;
69- for b in h. get_bucket ( ) {
70- let upper_bound = b. get_upper_bound ( ) ;
71107 write_sample (
72108 writer,
73- name,
74- Some ( "_bucket " ) ,
109+ & name,
110+ Some ( "_sum " ) ,
75111 m,
76- Some ( ( BUCKET_LABEL , & upper_bound . to_string ( ) ) ) ,
77- b . get_cumulative_count ( ) as f64 ,
112+ None ,
113+ h . sample_sum . unwrap_or_default ( ) ,
78114 ) ?;
79- if upper_bound. is_sign_positive ( ) && upper_bound. is_infinite ( ) {
80- inf_seen = true ;
81- }
82- }
83- if !inf_seen {
115+
84116 write_sample (
85117 writer,
86- name,
87- Some ( "_bucket " ) ,
118+ & name,
119+ Some ( "_count " ) ,
88120 m,
89- Some ( ( BUCKET_LABEL , POSITIVE_INF ) ) ,
90- h. get_sample_count ( ) as f64 ,
121+ None ,
122+ h. sample_count . unwrap_or_default ( ) as f64 ,
91123 ) ?;
92124 }
93-
94- write_sample ( writer, name, Some ( "_sum" ) , m, None , h. get_sample_sum ( ) ) ?;
95-
96- write_sample (
97- writer,
98- name,
99- Some ( "_count" ) ,
100- m,
101- None ,
102- h. get_sample_count ( ) as f64 ,
103- ) ?;
104125 }
105- MetricType :: SUMMARY => {
106- let s = m. get_summary ( ) ;
126+ MetricType :: Summary => {
127+ if let Some ( s) = & m. summary {
128+ for q in & s. quantile {
129+ write_sample (
130+ writer,
131+ & name,
132+ None ,
133+ m,
134+ Some ( ( QUANTILE , & q. quantile . unwrap_or_default ( ) . to_string ( ) ) ) ,
135+ q. value . unwrap_or_default ( ) ,
136+ ) ?;
137+ }
107138
108- for q in s. get_quantile ( ) {
109139 write_sample (
110140 writer,
111- name,
141+ & name,
142+ Some ( "_sum" ) ,
143+ m,
112144 None ,
145+ s. sample_sum . unwrap_or_default ( ) ,
146+ ) ?;
147+
148+ write_sample (
149+ writer,
150+ & name,
151+ Some ( "_count" ) ,
113152 m,
114- Some ( ( QUANTILE , & q . get_quantile ( ) . to_string ( ) ) ) ,
115- q . get_value ( ) ,
153+ None ,
154+ s . sample_count . unwrap_or_default ( ) as f64 ,
116155 ) ?;
117156 }
118-
119- write_sample ( writer, name, Some ( "_sum" ) , m, None , s. get_sample_sum ( ) ) ?;
120-
121- write_sample (
122- writer,
123- name,
124- Some ( "_count" ) ,
125- m,
126- None ,
127- s. get_sample_count ( ) as f64 ,
128- ) ?;
129157 }
130- MetricType :: UNTYPED => {
158+ MetricType :: Untyped => {
131159 unimplemented ! ( ) ;
132160 }
133161 }
@@ -160,13 +188,12 @@ fn write_sample(
160188 writer. write_all ( postfix. as_bytes ( ) ) ?;
161189 }
162190
163- label_pairs_to_text ( mc. get_label ( ) , additional_label, writer) ?;
191+ label_pairs_to_text ( & * mc. label , additional_label, writer) ?;
164192
165193 writer. write_all ( b" " ) ?;
166194 writer. write_all ( value. to_string ( ) . as_bytes ( ) ) ?;
167195
168- let timestamp = mc. get_timestamp_ms ( ) ;
169- if timestamp != 0 {
196+ if let Some ( timestamp) = mc. timestamp_ms {
170197 writer. write_all ( b" " ) ?;
171198 writer. write_all ( timestamp. to_string ( ) . as_bytes ( ) ) ?;
172199 }
@@ -194,10 +221,18 @@ fn label_pairs_to_text(
194221
195222 let mut separator = b"{" ;
196223 for lp in pairs {
224+ let name = match & lp. name {
225+ Some ( v) => & * * v,
226+ None => "" ,
227+ } ;
228+ let value = match & lp. value {
229+ Some ( v) => & * * v,
230+ None => "" ,
231+ } ;
197232 writer. write_all ( separator) ?;
198- writer. write_all ( lp . get_name ( ) . as_bytes ( ) ) ?;
233+ writer. write_all ( name . as_bytes ( ) ) ?;
199234 writer. write_all ( b"=\" " ) ?;
200- writer. write_all ( escape_string ( lp . get_value ( ) , true ) . as_bytes ( ) ) ?;
235+ writer. write_all ( escape_string ( value , true ) . as_bytes ( ) ) ?;
201236 writer. write_all ( b"\" " ) ?;
202237
203238 separator = b"," ;
@@ -267,7 +302,6 @@ fn escape_string(v: &str, include_double_quote: bool) -> Cow<'_, str> {
267302
268303#[ cfg( test) ]
269304mod tests {
270-
271305 use super :: * ;
272306 use crate :: counter:: Counter ;
273307 use crate :: gauge:: Gauge ;
@@ -365,27 +399,27 @@ test_histogram_count{a="1"} 1
365399 use std:: str;
366400
367401 let mut metric_family = MetricFamily :: default ( ) ;
368- metric_family. set_name ( "test_summary" . to_string ( ) ) ;
369- metric_family. set_help ( "This is a test summary statistic" . to_string ( ) ) ;
370- metric_family. set_field_type ( MetricType :: SUMMARY ) ;
402+ metric_family. name = Some ( "test_summary" . to_string ( ) ) ;
403+ metric_family. help = Some ( "This is a test summary statistic" . to_string ( ) ) ;
404+ metric_family. r#type = Some ( MetricType :: Summary ) ;
371405
372406 let mut summary = Summary :: default ( ) ;
373- summary. set_sample_count ( 5.0 as u64 ) ;
374- summary. set_sample_sum ( 15.0 ) ;
407+ summary. sample_count = Some ( 5.0 as u64 ) ;
408+ summary. sample_sum = Some ( 15.0 ) ;
375409
376410 let mut quantile1 = Quantile :: default ( ) ;
377- quantile1. set_quantile ( 50.0 ) ;
378- quantile1. set_value ( 3.0 ) ;
411+ quantile1. quantile = Some ( 50.0 ) ;
412+ quantile1. value = Some ( 3.0 ) ;
379413
380414 let mut quantile2 = Quantile :: default ( ) ;
381- quantile2. set_quantile ( 100.0 ) ;
382- quantile2. set_value ( 5.0 ) ;
415+ quantile2. quantile = Some ( 100.0 ) ;
416+ quantile2. value = Some ( 5.0 ) ;
383417
384- summary. set_quantile ( from_vec ! ( vec!( quantile1, quantile2) ) ) ;
418+ summary. quantile = vec ! [ quantile1, quantile2] ;
385419
386420 let mut metric = Metric :: default ( ) ;
387- metric. set_summary ( summary) ;
388- metric_family. set_metric ( from_vec ! ( vec!( metric) ) ) ;
421+ metric. summary = Some ( summary) ;
422+ metric_family. metric = vec ! [ metric] ;
389423
390424 let mut writer = Vec :: < u8 > :: new ( ) ;
391425 let encoder = TextEncoder :: new ( ) ;
0 commit comments