@@ -198,12 +198,12 @@ class Metric(object):
198
198
and SummaryMetricFamily instead.
199
199
'''
200
200
def __init__ (self , name , documentation , typ , unit = '' ):
201
+ if unit and not name .endswith ("_" + unit ):
202
+ name += "_" + unit
201
203
if not _METRIC_NAME_RE .match (name ):
202
204
raise ValueError ('Invalid metric name: ' + name )
203
205
self .name = name
204
206
self .documentation = documentation
205
- if unit and not name .endswith ("_" + unit ):
206
- raise ValueError ("Metric name not suffixed by unit: " + name )
207
207
self .unit = unit
208
208
if typ == 'untyped' :
209
209
typ = 'unknown'
@@ -239,8 +239,8 @@ class UnknownMetricFamily(Metric):
239
239
'''A single unknwon metric and its samples.
240
240
For use by custom collectors.
241
241
'''
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 )
244
244
if labels is not None and value is not None :
245
245
raise ValueError ('Can only specify at most one of value and labels.' )
246
246
if labels is None :
@@ -265,11 +265,11 @@ class CounterMetricFamily(Metric):
265
265
266
266
For use by custom collectors.
267
267
'''
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 = '' ):
269
269
# Glue code for pre-OpenMetrics metrics.
270
270
if name .endswith ('_total' ):
271
271
name = name [:- 6 ]
272
- Metric .__init__ (self , name , documentation , 'counter' )
272
+ Metric .__init__ (self , name , documentation , 'counter' , unit )
273
273
if labels is not None and value is not None :
274
274
raise ValueError ('Can only specify at most one of value and labels.' )
275
275
if labels is None :
@@ -735,14 +735,19 @@ def _samples(self):
735
735
736
736
def _MetricWrapper (cls ):
737
737
'''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 ):
739
739
full_name = ''
740
740
if namespace :
741
741
full_name += namespace + '_'
742
742
if subsystem :
743
743
full_name += subsystem + '_'
744
744
full_name += name
745
745
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
+
746
751
if cls ._type == 'counter' and full_name .endswith ('_total' ):
747
752
full_name = full_name [:- 6 ] # Munge to OpenMetrics.
748
753
@@ -767,7 +772,7 @@ def describe():
767
772
collector .describe = describe
768
773
769
774
def collect ():
770
- metric = Metric (full_name , documentation , cls ._type )
775
+ metric = Metric (full_name , documentation , cls ._type , unit )
771
776
for suffix , labels , value in collector ._samples ():
772
777
metric .add_sample (full_name + suffix , labels , value )
773
778
return [metric ]
0 commit comments