Skip to content

Commit dde9057

Browse files
committed
Use custom Lock class that provides locked() method (for tests).
Signed-off-by: Ben Timby <[email protected]>
1 parent b1aba02 commit dde9057

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

prometheus_client/metrics.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from threading import RLock
32
import time
43
import types
54
from typing import (
@@ -16,7 +15,7 @@
1615
)
1716
from .registry import Collector, CollectorRegistry, REGISTRY
1817
from .samples import Exemplar, Sample
19-
from .utils import floatToGoString, INF
18+
from .utils import floatToGoString, INF, Lock
2019

2120
T = TypeVar('T', bound='MetricWrapperBase')
2221
F = TypeVar("F", bound=Callable[..., Any])
@@ -144,7 +143,7 @@ def __init__(self: T,
144143

145144
if self._is_parent():
146145
# Prepare the fields needed for child metrics.
147-
self._lock = RLock()
146+
self._lock = Lock()
148147
self._metrics: Dict[Sequence[str], T] = {}
149148

150149
if self._is_observable():
@@ -697,7 +696,7 @@ class Info(MetricWrapperBase):
697696

698697
def _metric_init(self):
699698
self._labelname_set = set(self._labelnames)
700-
self._lock = RLock()
699+
self._lock = Lock()
701700
self._value = {}
702701

703702
def info(self, val: Dict[str, str]) -> None:
@@ -757,7 +756,7 @@ def __init__(self,
757756

758757
def _metric_init(self) -> None:
759758
self._value = 0
760-
self._lock = RLock()
759+
self._lock = Lock()
761760

762761
def state(self, state: str) -> None:
763762
"""Set enum metric state."""

prometheus_client/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from abc import ABC, abstractmethod
22
import copy
3-
from threading import RLock
43
from typing import Dict, Iterable, List, Optional
54

65
from .metrics_core import Metric
6+
from .utils import Lock
77

88

99
# Ideally this would be a Protocol, but Protocols are only available in Python >= 3.8.
@@ -30,7 +30,7 @@ def __init__(self, auto_describe: bool = False, target_info: Optional[Dict[str,
3030
self._collector_to_names: Dict[Collector, List[str]] = {}
3131
self._names_to_collectors: Dict[str, Collector] = {}
3232
self._auto_describe = auto_describe
33-
self._lock = RLock()
33+
self._lock = Lock()
3434
self._target_info: Optional[Dict[str, str]] = {}
3535
self.set_target_info(target_info)
3636

prometheus_client/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from threading import _PyRLock
23

34
INF = float("inf")
45
MINUS_INF = float("-inf")
@@ -22,3 +23,8 @@ def floatToGoString(d):
2223
mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.')
2324
return f'{mantissa}e+0{dot - 1}'
2425
return s
26+
27+
28+
class Lock(_PyRLock):
29+
def locked(self):
30+
return bool(self._count)

prometheus_client/values.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
2-
from threading import RLock
32
import warnings
43

54
from .mmap_dict import mmap_key, MmapedDict
5+
from .utils import Lock
66

77

88
class MutexValue:
@@ -13,7 +13,7 @@ class MutexValue:
1313
def __init__(self, typ, metric_name, name, labelnames, labelvalues, help_text, **kwargs):
1414
self._value = 0.0
1515
self._exemplar = None
16-
self._lock = RLock()
16+
self._lock = Lock()
1717

1818
def inc(self, amount):
1919
with self._lock:
@@ -50,7 +50,7 @@ def MultiProcessValue(process_identifier=os.getpid):
5050
# Use a single global lock when in multi-processing mode
5151
# as we presume this means there is no threading going on.
5252
# This avoids the need to also have mutexes in __MmapDict.
53-
lock = RLock()
53+
lock = Lock()
5454

5555
class MmapedValue:
5656
"""A float protected by a mutex backed by a per-process mmaped file."""

0 commit comments

Comments
 (0)