@@ -126,7 +126,39 @@ impl Registry {
126126 help : H ,
127127 metric : impl Metric ,
128128 ) {
129- self . priv_register ( name, help, metric, None )
129+ self . priv_register ( name, help, metric, None , true )
130+ }
131+
132+ /// Register a metric with the [`Registry`].
133+ ///
134+ /// Note: In the Open Metrics text exposition format some metric types have
135+ /// a special suffix, e.g. the
136+ /// [`Counter`](crate::metrics::counter::Counter`) metric with `_total`.
137+ /// These suffixes are inferred through the metric type and must not be
138+ /// appended to the metric name manually by the user.
139+ ///
140+ /// Note: A full stop punctuation mark (`.`) is automatically added to the
141+ /// passed help text.
142+ ///
143+ /// Use [`Registry::register_with_unit`] whenever a unit for the given
144+ /// metric is known.
145+ ///
146+ /// ```
147+ /// # use prometheus_client::metrics::counter::{Atomic as _, Counter};
148+ /// # use prometheus_client::registry::{Registry, Unit};
149+ /// #
150+ /// let mut registry = Registry::default();
151+ /// let counter: Counter = Counter::default();
152+ ///
153+ /// registry.register_without_auto_suffix("my_counter", "This is my counter", counter.clone());
154+ /// ```
155+ pub fn register_without_auto_suffix < N : Into < String > , H : Into < String > > (
156+ & mut self ,
157+ name : N ,
158+ help : H ,
159+ metric : impl Metric ,
160+ ) {
161+ self . priv_register ( name, help, metric, None , false )
130162 }
131163
132164 /// Register a metric with the [`Registry`] specifying the metric's unit.
@@ -158,7 +190,7 @@ impl Registry {
158190 unit : Unit ,
159191 metric : impl Metric ,
160192 ) {
161- self . priv_register ( name, help, metric, Some ( unit) )
193+ self . priv_register ( name, help, metric, Some ( unit) , false )
162194 }
163195
164196 fn priv_register < N : Into < String > , H : Into < String > > (
@@ -167,8 +199,9 @@ impl Registry {
167199 help : H ,
168200 metric : impl Metric ,
169201 unit : Option < Unit > ,
202+ auto_suffix : bool ,
170203 ) {
171- let descriptor = Descriptor :: new ( name, help, unit) ;
204+ let descriptor = Descriptor :: new ( name, help, unit, auto_suffix ) ;
172205 self . metrics . push ( ( descriptor, Box :: new ( metric) ) ) ;
173206 }
174207
@@ -295,6 +328,7 @@ impl Registry {
295328 & descriptor. help ,
296329 descriptor. unit . as_ref ( ) ,
297330 EncodeMetric :: metric_type ( metric. as_ref ( ) ) ,
331+ descriptor. auto_suffix ,
298332 ) ?;
299333 metric. encode ( metric_encoder) ?;
300334 }
@@ -335,15 +369,17 @@ struct Descriptor {
335369 name : String ,
336370 help : String ,
337371 unit : Option < Unit > ,
372+ auto_suffix : bool ,
338373}
339374
340375impl Descriptor {
341376 /// Create new [`Descriptor`].
342- fn new < N : Into < String > , H : Into < String > > ( name : N , help : H , unit : Option < Unit > ) -> Self {
377+ fn new < N : Into < String > , H : Into < String > > ( name : N , help : H , unit : Option < Unit > , auto_suffix : bool ) -> Self {
343378 Self {
344379 name : name. into ( ) ,
345380 help : help. into ( ) + "." ,
346381 unit,
382+ auto_suffix,
347383 }
348384 }
349385}
0 commit comments