13
13
from .metrics_core import Metric
14
14
from .registry import Collector , CollectorRegistry , REGISTRY
15
15
from .samples import Exemplar , Sample
16
- from .utils import floatToGoString , INF
16
+ from .utils import floatToGoString , getMultiprocDir , INF
17
17
from .validation import (
18
18
_validate_exemplar , _validate_labelnames , _validate_metric_name ,
19
19
)
@@ -108,13 +108,15 @@ def __init__(self: T,
108
108
unit : str = '' ,
109
109
registry : Optional [CollectorRegistry ] = REGISTRY ,
110
110
_labelvalues : Optional [Sequence [str ]] = None ,
111
+ multiprocess_dir : Optional [str ] = ''
111
112
) -> None :
112
113
self ._name = _build_full_name (self ._type , name , namespace , subsystem , unit )
113
114
self ._labelnames = _validate_labelnames (self , labelnames )
114
115
self ._labelvalues = tuple (_labelvalues or ())
115
116
self ._kwargs : Dict [str , Any ] = {}
116
117
self ._documentation = documentation
117
118
self ._unit = unit
119
+ self ._multiprocess_dir = multiprocess_dir
118
120
119
121
_validate_metric_name (self ._name )
120
122
@@ -182,12 +184,13 @@ def labels(self: T, *labelvalues: Any, **labelkwargs: Any) -> T:
182
184
labelnames = self ._labelnames ,
183
185
unit = self ._unit ,
184
186
_labelvalues = labelvalues ,
187
+ multiprocess_dir = self ._multiprocess_dir ,
185
188
** self ._kwargs
186
189
)
187
190
return self ._metrics [labelvalues ]
188
191
189
192
def remove (self , * labelvalues : Any ) -> None :
190
- if 'prometheus_multiproc_dir' in os . environ or 'PROMETHEUS_MULTIPROC_DIR' in os . environ :
193
+ if getMultiprocDir () :
191
194
warnings .warn (
192
195
"Removal of labels has not been implemented in multi-process mode yet." ,
193
196
UserWarning )
@@ -205,7 +208,7 @@ def remove(self, *labelvalues: Any) -> None:
205
208
206
209
def clear (self ) -> None :
207
210
"""Remove all labelsets from the metric"""
208
- if 'prometheus_multiproc_dir' in os . environ or 'PROMETHEUS_MULTIPROC_DIR' in os . environ :
211
+ if getMultiprocDir () :
209
212
warnings .warn (
210
213
"Clearing labels has not been implemented in multi-process mode yet" ,
211
214
UserWarning )
@@ -269,7 +272,7 @@ def f():
269
272
# Count only one type of exception
270
273
with c.count_exceptions(ValueError):
271
274
pass
272
-
275
+
273
276
You can also reset the counter to zero in case your logical "process" restarts
274
277
without restarting the actual python process.
275
278
@@ -280,7 +283,7 @@ def f():
280
283
281
284
def _metric_init (self ) -> None :
282
285
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 )
284
287
self ._created = time .time ()
285
288
286
289
def inc (self , amount : float = 1 , exemplar : Optional [Dict [str , str ]] = None ) -> None :
@@ -369,6 +372,7 @@ def __init__(self,
369
372
registry : Optional [CollectorRegistry ] = REGISTRY ,
370
373
_labelvalues : Optional [Sequence [str ]] = None ,
371
374
multiprocess_mode : Literal ['all' , 'liveall' , 'min' , 'livemin' , 'max' , 'livemax' , 'sum' , 'livesum' , 'mostrecent' , 'livemostrecent' ] = 'all' ,
375
+ multiprocess_dir : Optional [str ] = ''
372
376
):
373
377
self ._multiprocess_mode = multiprocess_mode
374
378
if multiprocess_mode not in self ._MULTIPROC_MODES :
@@ -382,14 +386,15 @@ def __init__(self,
382
386
unit = unit ,
383
387
registry = registry ,
384
388
_labelvalues = _labelvalues ,
389
+ multiprocess_dir = multiprocess_dir
385
390
)
386
391
self ._kwargs ['multiprocess_mode' ] = self ._multiprocess_mode
387
392
self ._is_most_recent = self ._multiprocess_mode in self ._MOST_RECENT_MODES
388
393
389
394
def _metric_init (self ) -> None :
390
395
self ._value = values .ValueClass (
391
396
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
393
398
)
394
399
395
400
def inc (self , amount : float = 1 ) -> None :
@@ -488,8 +493,9 @@ def create_response(request):
488
493
489
494
def _metric_init (self ) -> None :
490
495
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 )
493
499
self ._created = time .time ()
494
500
495
501
def observe (self , amount : float ) -> None :
@@ -572,6 +578,7 @@ def __init__(self,
572
578
registry : Optional [CollectorRegistry ] = REGISTRY ,
573
579
_labelvalues : Optional [Sequence [str ]] = None ,
574
580
buckets : Sequence [Union [float , str ]] = DEFAULT_BUCKETS ,
581
+ multiprocess_dir : Optional [str ] = ''
575
582
):
576
583
self ._prepare_buckets (buckets )
577
584
super ().__init__ (
@@ -583,6 +590,7 @@ def __init__(self,
583
590
unit = unit ,
584
591
registry = registry ,
585
592
_labelvalues = _labelvalues ,
593
+ multiprocess_dir = multiprocess_dir
586
594
)
587
595
self ._kwargs ['buckets' ] = buckets
588
596
@@ -602,15 +610,16 @@ def _metric_init(self) -> None:
602
610
self ._buckets : List [values .ValueClass ] = []
603
611
self ._created = time .time ()
604
612
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 )
606
614
for b in self ._upper_bounds :
607
615
self ._buckets .append (values .ValueClass (
608
616
self ._type ,
609
617
self ._name ,
610
618
self ._name + '_bucket' ,
611
619
bucket_labelnames ,
612
620
self ._labelvalues + (floatToGoString (b ),),
613
- self ._documentation )
621
+ self ._documentation ,
622
+ multiprocess_dir = self ._multiprocess_dir )
614
623
)
615
624
616
625
def observe (self , amount : float , exemplar : Optional [Dict [str , str ]] = None ) -> None :
@@ -717,6 +726,7 @@ def __init__(self,
717
726
registry : Optional [CollectorRegistry ] = REGISTRY ,
718
727
_labelvalues : Optional [Sequence [str ]] = None ,
719
728
states : Optional [Sequence [str ]] = None ,
729
+ multiprocess_dir : Optional [str ] = ''
720
730
):
721
731
super ().__init__ (
722
732
name = name ,
@@ -727,6 +737,7 @@ def __init__(self,
727
737
unit = unit ,
728
738
registry = registry ,
729
739
_labelvalues = _labelvalues ,
740
+ multiprocess_dir = multiprocess_dir
730
741
)
731
742
if name in labelnames :
732
743
raise ValueError (f'Overlapping labels for Enum metric: { name } ' )
0 commit comments