Skip to content

Commit c0d5593

Browse files
committed
Drop the use of double locking in values and metrics modules
It's hard to justify the overhead of double locking there. Signed-off-by: Przemysław Suliga <mail@suligap.net>
1 parent fa51a75 commit c0d5593

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

prometheus_client/metrics.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from threading import Lock
23
import time
34
import types
45
from typing import (
@@ -12,7 +13,7 @@
1213
from .metrics_core import Metric
1314
from .registry import Collector, CollectorRegistry, REGISTRY
1415
from .samples import Exemplar, Sample
15-
from .utils import floatToGoString, INF, WarnLock
16+
from .utils import floatToGoString, INF
1617
from .validation import (
1718
_validate_exemplar, _validate_labelnames, _validate_metric_name,
1819
)
@@ -119,7 +120,7 @@ def __init__(self: T,
119120

120121
if self._is_parent():
121122
# Prepare the fields needed for child metrics.
122-
self._lock = WarnLock()
123+
self._lock = Lock()
123124
self._metrics: Dict[Sequence[str], T] = {}
124125

125126
if self._is_observable():
@@ -672,7 +673,7 @@ class Info(MetricWrapperBase):
672673

673674
def _metric_init(self):
674675
self._labelname_set = set(self._labelnames)
675-
self._lock = WarnLock()
676+
self._lock = Lock()
676677
self._value = {}
677678

678679
def info(self, val: Dict[str, str]) -> None:
@@ -734,7 +735,7 @@ def __init__(self,
734735

735736
def _metric_init(self) -> None:
736737
self._value = 0
737-
self._lock = WarnLock()
738+
self._lock = Lock()
738739

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

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 Lock
23
import warnings
34

45
from .mmap_dict import mmap_key, MmapedDict
5-
from .utils import WarnLock
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 = WarnLock()
16+
self._lock = Lock()
1717

1818
def inc(self, amount):
1919
with self._lock:
@@ -49,7 +49,7 @@ def MultiProcessValue(process_identifier=os.getpid):
4949
pid = {'value': process_identifier()}
5050
# Use a single global lock when in multi-processing mode.
5151
# This avoids the need to also have mutexes in __MmapDict.
52-
lock = WarnLock()
52+
lock = Lock()
5353

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

tests/test_core.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,6 @@ def test_exemplar_too_long(self):
135135
'y123456': '7+15 characters',
136136
})
137137

138-
def test_single_thread_deadlock_detection(self):
139-
counter = self.counter
140-
141-
class Tracked(float):
142-
def __radd__(self, other):
143-
counter.inc(10)
144-
return self + other
145-
146-
expected_msg = 'Attempt to enter a non reentrant context from a single thread.'
147-
self.assertRaisesRegex(
148-
PrometheusClientRuntimeError, expected_msg, counter.inc, Tracked(100)
149-
)
150-
151138

152139
class TestDisableCreated(unittest.TestCase):
153140
def setUp(self):

0 commit comments

Comments
 (0)