@@ -98,6 +98,16 @@ impl<'a> From<protobuf::DescriptorEncoder<'a>> for DescriptorEncoder<'a> {
9898 }
9999}
100100
101+ /// Select the pattern of metric serialization
102+ #[ derive( Debug , PartialEq , Eq , Default , Clone , Copy ) ]
103+ pub enum OutputFormat {
104+ /// Used for the Prometheus format which does not add the suffix to the metric names.
105+ Prometheus ,
106+ /// Used for the OpenMetrics format which adds the suffix for the metric names.
107+ #[ default]
108+ OpenMetrics ,
109+ }
110+
101111impl DescriptorEncoder < ' _ > {
102112 pub ( crate ) fn with_prefix_and_labels < ' s > (
103113 & ' s mut self ,
@@ -120,12 +130,16 @@ impl DescriptorEncoder<'_> {
120130 help : & str ,
121131 unit : Option < & ' s Unit > ,
122132 metric_type : MetricType ,
133+ output_format : OutputFormat ,
123134 ) -> Result < MetricEncoder < ' s > , std:: fmt:: Error > {
124135 for_both_mut ! (
125136 self ,
126137 DescriptorEncoderInner ,
127138 e,
128- Ok ( e. encode_descriptor( name, help, unit, metric_type) ?. into( ) )
139+ Ok (
140+ e. encode_descriptor( name, help, unit, metric_type, output_format) ?
141+ . into( )
142+ )
129143 )
130144 }
131145}
@@ -203,13 +217,22 @@ impl MetricEncoder<'_> {
203217 & ' s mut self ,
204218 label_set : & ' s S ,
205219 ) -> Result < MetricEncoder < ' s > , std:: fmt:: Error > {
220+ let output_format = self . output_format ( ) ;
206221 for_both_mut ! (
207222 self ,
208223 MetricEncoderInner ,
209224 e,
210- e. encode_family( label_set) . map( Into :: into)
225+ e. encode_family( label_set, output_format ) . map( Into :: into)
211226 )
212227 }
228+
229+ /// Returns the `OutputFormat` selected for the encoder.
230+ pub fn output_format ( & self ) -> OutputFormat {
231+ match & self . 0 {
232+ MetricEncoderInner :: Text ( metric_encoder) => metric_encoder. output_format ,
233+ MetricEncoderInner :: Protobuf ( _) => Default :: default ( ) ,
234+ }
235+ }
213236}
214237
215238/// An encodable label set.
0 commit comments