@@ -198,12 +198,12 @@ class Metric(object):
198198 and SummaryMetricFamily instead.
199199 '''
200200 def __init__ (self , name , documentation , typ , unit = '' ):
201+ if unit and not name .endswith ("_" + unit ):
202+ name += "_" + unit
201203 if not _METRIC_NAME_RE .match (name ):
202204 raise ValueError ('Invalid metric name: ' + name )
203205 self .name = name
204206 self .documentation = documentation
205- if unit and not name .endswith ("_" + unit ):
206- raise ValueError ("Metric name not suffixed by unit: " + name )
207207 self .unit = unit
208208 if typ == 'untyped' :
209209 typ = 'unknown'
@@ -239,8 +239,8 @@ class UnknownMetricFamily(Metric):
239239 '''A single unknwon metric and its samples.
240240 For use by custom collectors.
241241 '''
242- def __init__ (self , name , documentation , value = None , labels = None ):
243- Metric .__init__ (self , name , documentation , 'unknown' )
242+ def __init__ (self , name , documentation , value = None , labels = None , unit = '' ):
243+ Metric .__init__ (self , name , documentation , 'unknown' , unit )
244244 if labels is not None and value is not None :
245245 raise ValueError ('Can only specify at most one of value and labels.' )
246246 if labels is None :
@@ -265,11 +265,11 @@ class CounterMetricFamily(Metric):
265265
266266 For use by custom collectors.
267267 '''
268- def __init__ (self , name , documentation , value = None , labels = None , created = None ):
268+ def __init__ (self , name , documentation , value = None , labels = None , created = None , unit = '' ):
269269 # Glue code for pre-OpenMetrics metrics.
270270 if name .endswith ('_total' ):
271271 name = name [:- 6 ]
272- Metric .__init__ (self , name , documentation , 'counter' )
272+ Metric .__init__ (self , name , documentation , 'counter' , unit )
273273 if labels is not None and value is not None :
274274 raise ValueError ('Can only specify at most one of value and labels.' )
275275 if labels is None :
@@ -735,14 +735,19 @@ def _samples(self):
735735
736736def _MetricWrapper (cls ):
737737 '''Provides common functionality for metrics.'''
738- def init (name , documentation , labelnames = (), namespace = '' , subsystem = '' , registry = REGISTRY , ** kwargs ):
738+ def init (name , documentation , labelnames = (), namespace = '' , subsystem = '' , unit = '' , registry = REGISTRY , ** kwargs ):
739739 full_name = ''
740740 if namespace :
741741 full_name += namespace + '_'
742742 if subsystem :
743743 full_name += subsystem + '_'
744744 full_name += name
745745
746+ if unit and not full_name .endswith ("_" + unit ):
747+ full_name += "_" + unit
748+ if unit and cls ._type in ('info' , 'stateset' ):
749+ raise ValueError ('Metric name is of a type that cannot have a unit: ' + full_name )
750+
746751 if cls ._type == 'counter' and full_name .endswith ('_total' ):
747752 full_name = full_name [:- 6 ] # Munge to OpenMetrics.
748753
@@ -767,7 +772,7 @@ def describe():
767772 collector .describe = describe
768773
769774 def collect ():
770- metric = Metric (full_name , documentation , cls ._type )
775+ metric = Metric (full_name , documentation , cls ._type , unit )
771776 for suffix , labels , value in collector ._samples ():
772777 metric .add_sample (full_name + suffix , labels , value )
773778 return [metric ]
0 commit comments