1313from .metrics_core import Metric
1414from .registry import Collector , CollectorRegistry , REGISTRY
1515from .samples import Exemplar , Sample
16- from .utils import floatToGoString , INF
16+ from .utils import floatToGoString , getMultiprocDir , INF
1717from .validation import (
1818 _validate_exemplar , _validate_labelnames , _validate_metric_name ,
1919)
@@ -108,13 +108,15 @@ def __init__(self: T,
108108 unit : str = '' ,
109109 registry : Optional [CollectorRegistry ] = REGISTRY ,
110110 _labelvalues : Optional [Sequence [str ]] = None ,
111+ multiprocess_dir : Optional [str ] = ''
111112 ) -> None :
112113 self ._name = _build_full_name (self ._type , name , namespace , subsystem , unit )
113114 self ._labelnames = _validate_labelnames (self , labelnames )
114115 self ._labelvalues = tuple (_labelvalues or ())
115116 self ._kwargs : Dict [str , Any ] = {}
116117 self ._documentation = documentation
117118 self ._unit = unit
119+ self ._multiprocess_dir = multiprocess_dir
118120
119121 _validate_metric_name (self ._name )
120122
@@ -182,12 +184,13 @@ def labels(self: T, *labelvalues: Any, **labelkwargs: Any) -> T:
182184 labelnames = self ._labelnames ,
183185 unit = self ._unit ,
184186 _labelvalues = labelvalues ,
187+ multiprocess_dir = self ._multiprocess_dir ,
185188 ** self ._kwargs
186189 )
187190 return self ._metrics [labelvalues ]
188191
189192 def remove (self , * labelvalues : Any ) -> None :
190- if 'prometheus_multiproc_dir' in os . environ or 'PROMETHEUS_MULTIPROC_DIR' in os . environ :
193+ if getMultiprocDir () :
191194 warnings .warn (
192195 "Removal of labels has not been implemented in multi-process mode yet." ,
193196 UserWarning )
@@ -205,7 +208,7 @@ def remove(self, *labelvalues: Any) -> None:
205208
206209 def clear (self ) -> None :
207210 """Remove all labelsets from the metric"""
208- if 'prometheus_multiproc_dir' in os . environ or 'PROMETHEUS_MULTIPROC_DIR' in os . environ :
211+ if getMultiprocDir () :
209212 warnings .warn (
210213 "Clearing labels has not been implemented in multi-process mode yet" ,
211214 UserWarning )
@@ -269,7 +272,7 @@ def f():
269272 # Count only one type of exception
270273 with c.count_exceptions(ValueError):
271274 pass
272-
275+
273276 You can also reset the counter to zero in case your logical "process" restarts
274277 without restarting the actual python process.
275278
@@ -280,7 +283,7 @@ def f():
280283
281284 def _metric_init (self ) -> None :
282285 self ._value = values .ValueClass (self ._type , self ._name , self ._name + '_total' , self ._labelnames ,
283- self ._labelvalues , self ._documentation )
286+ self ._labelvalues , self ._documentation , multiprocess_dir = self . _multiprocess_dir )
284287 self ._created = time .time ()
285288
286289 def inc (self , amount : float = 1 , exemplar : Optional [Dict [str , str ]] = None ) -> None :
@@ -369,6 +372,7 @@ def __init__(self,
369372 registry : Optional [CollectorRegistry ] = REGISTRY ,
370373 _labelvalues : Optional [Sequence [str ]] = None ,
371374 multiprocess_mode : Literal ['all' , 'liveall' , 'min' , 'livemin' , 'max' , 'livemax' , 'sum' , 'livesum' , 'mostrecent' , 'livemostrecent' ] = 'all' ,
375+ multiprocess_dir : Optional [str ] = ''
372376 ):
373377 self ._multiprocess_mode = multiprocess_mode
374378 if multiprocess_mode not in self ._MULTIPROC_MODES :
@@ -382,14 +386,15 @@ def __init__(self,
382386 unit = unit ,
383387 registry = registry ,
384388 _labelvalues = _labelvalues ,
389+ multiprocess_dir = multiprocess_dir
385390 )
386391 self ._kwargs ['multiprocess_mode' ] = self ._multiprocess_mode
387392 self ._is_most_recent = self ._multiprocess_mode in self ._MOST_RECENT_MODES
388393
389394 def _metric_init (self ) -> None :
390395 self ._value = values .ValueClass (
391396 self ._type , self ._name , self ._name , self ._labelnames , self ._labelvalues ,
392- self ._documentation , multiprocess_mode = self ._multiprocess_mode
397+ self ._documentation , multiprocess_mode = self ._multiprocess_mode , multiprocess_dir = self . _multiprocess_dir
393398 )
394399
395400 def inc (self , amount : float = 1 ) -> None :
@@ -488,8 +493,9 @@ def create_response(request):
488493
489494 def _metric_init (self ) -> None :
490495 self ._count = values .ValueClass (self ._type , self ._name , self ._name + '_count' , self ._labelnames ,
491- self ._labelvalues , self ._documentation )
492- self ._sum = values .ValueClass (self ._type , self ._name , self ._name + '_sum' , self ._labelnames , self ._labelvalues , self ._documentation )
496+ self ._labelvalues , self ._documentation , multiprocess_dir = self ._multiprocess_dir )
497+ self ._sum = values .ValueClass (self ._type , self ._name , self ._name + '_sum' , self ._labelnames ,
498+ self ._labelvalues , self ._documentation , multiprocess_dir = self ._multiprocess_dir )
493499 self ._created = time .time ()
494500
495501 def observe (self , amount : float ) -> None :
@@ -572,6 +578,7 @@ def __init__(self,
572578 registry : Optional [CollectorRegistry ] = REGISTRY ,
573579 _labelvalues : Optional [Sequence [str ]] = None ,
574580 buckets : Sequence [Union [float , str ]] = DEFAULT_BUCKETS ,
581+ multiprocess_dir : Optional [str ] = ''
575582 ):
576583 self ._prepare_buckets (buckets )
577584 super ().__init__ (
@@ -583,6 +590,7 @@ def __init__(self,
583590 unit = unit ,
584591 registry = registry ,
585592 _labelvalues = _labelvalues ,
593+ multiprocess_dir = multiprocess_dir
586594 )
587595 self ._kwargs ['buckets' ] = buckets
588596
@@ -602,15 +610,16 @@ def _metric_init(self) -> None:
602610 self ._buckets : List [values .ValueClass ] = []
603611 self ._created = time .time ()
604612 bucket_labelnames = self ._labelnames + ('le' ,)
605- self ._sum = values .ValueClass (self ._type , self ._name , self ._name + '_sum' , self ._labelnames , self ._labelvalues , self ._documentation )
613+ self ._sum = values .ValueClass (self ._type , self ._name , self ._name + '_sum' , self ._labelnames , self ._labelvalues , self ._documentation , multiprocess_dir = self . _multiprocess_dir )
606614 for b in self ._upper_bounds :
607615 self ._buckets .append (values .ValueClass (
608616 self ._type ,
609617 self ._name ,
610618 self ._name + '_bucket' ,
611619 bucket_labelnames ,
612620 self ._labelvalues + (floatToGoString (b ),),
613- self ._documentation )
621+ self ._documentation ,
622+ multiprocess_dir = self ._multiprocess_dir )
614623 )
615624
616625 def observe (self , amount : float , exemplar : Optional [Dict [str , str ]] = None ) -> None :
@@ -717,6 +726,7 @@ def __init__(self,
717726 registry : Optional [CollectorRegistry ] = REGISTRY ,
718727 _labelvalues : Optional [Sequence [str ]] = None ,
719728 states : Optional [Sequence [str ]] = None ,
729+ multiprocess_dir : Optional [str ] = ''
720730 ):
721731 super ().__init__ (
722732 name = name ,
@@ -727,6 +737,7 @@ def __init__(self,
727737 unit = unit ,
728738 registry = registry ,
729739 _labelvalues = _labelvalues ,
740+ multiprocess_dir = multiprocess_dir
730741 )
731742 if name in labelnames :
732743 raise ValueError (f'Overlapping labels for Enum metric: { name } ' )
0 commit comments