Skip to content

Commit 9c8fc7e

Browse files
move general methods to wrapper class
1 parent cec8d65 commit 9c8fc7e

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

synapse/metrics/__init__.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
GaugeMetricFamily,
6161
Sample,
6262
)
63-
from typing_extensions import Self
63+
from typing_extensions import Dict, Self
6464

6565
from twisted.python.threadpool import ThreadPool
6666
from twisted.web.resource import Resource
@@ -191,37 +191,50 @@ def _build_full_name(
191191
return full_name
192192

193193

194-
class SynapseCounter:
195-
_type: str = "counter"
194+
T = TypeVar("T", bound="SynapseMetricWrapperBase")
196195

196+
197+
class SynapseMetricWrapperBase:
197198
def __init__(
198-
self,
199+
self: T,
199200
name: str,
200201
documentation: str,
201202
labelnames: Iterable[str] = (),
202203
namespace: str = "",
203204
subsystem: str = "",
204205
unit: str = "",
205-
registry: Optional[CollectorRegistry] = None,
206+
registry: Optional[CollectorRegistry] = REGISTRY,
206207
_labelvalues: Optional[Sequence[str]] = None,
207208
) -> None:
208-
# Here is where we grab the global meter to create a FauxCounter
209-
self._counter = meter.create_counter(name, unit=unit, description=documentation)
209+
self._type: str = ""
210+
self._original_name = name
211+
self._namespace = namespace
212+
self._subsystem = subsystem
210213
self._name = _build_full_name(self._type, name, namespace, subsystem, unit)
211-
self._documentation = documentation
212-
self._unit = unit
213-
214214
# prom validates these, should we do that?
215215
# labelnames provide a simple way to register that a given set of kwargs call
216216
# from labels can be used. All should be used in a call?
217217
self._labelnames = tuple(labelnames or ())
218-
219218
self._labelvalues = tuple(_labelvalues or ())
219+
self._kwargs: Dict[str, Any] = {}
220+
self._documentation = documentation
221+
self._unit = unit
220222
self._metrics = {} # type: ignore[var-annotated]
221-
222-
self._current_attributes = ()
223223
self._lock = threading.Lock()
224224

225+
# if self._is_parent():
226+
# # Prepare the fields needed for child metrics.
227+
# self._lock = Lock()
228+
# self._metrics: Dict[Sequence[str], T] = {}
229+
230+
# if self._is_observable():
231+
# self._metric_init()
232+
233+
# if not self._labelvalues:
234+
# # Register the multi-wrapper parent metric, or if a label-less metric, the whole shebang.
235+
# if registry:
236+
# registry.register(self)
237+
225238
def labels(self, *labelvalues: Any, **labelkwargs: Any) -> Self:
226239
if labelkwargs:
227240
if sorted(labelkwargs) != sorted(self._labelnames):
@@ -237,12 +250,12 @@ def labels(self, *labelvalues: Any, **labelkwargs: Any) -> Self:
237250

238251
return self
239252

240-
def _child_samples(self) -> Iterable[Sample]: # pragma: no cover
241-
raise NotImplementedError("_child_samples() must be implemented by %r" % self)
242-
243253
def _is_parent(self): # type: ignore[no-untyped-def]
244254
return self._labelnames and not self._labelvalues
245255

256+
def _child_samples(self) -> Iterable[Sample]: # pragma: no cover
257+
raise NotImplementedError("_child_samples() must be implemented by %r" % self)
258+
246259
def _samples(self) -> Iterable[Sample]:
247260
if self._is_parent():
248261
return self._multi_samples()
@@ -271,6 +284,37 @@ def _multi_samples(self) -> Iterable[Sample]:
271284
native_histogram_value,
272285
)
273286

287+
288+
class SynapseCounter(SynapseMetricWrapperBase):
289+
def __init__(
290+
self,
291+
name: str,
292+
documentation: str,
293+
labelnames: Iterable[str] = (),
294+
namespace: str = "",
295+
subsystem: str = "",
296+
unit: str = "",
297+
registry: Optional[CollectorRegistry] = REGISTRY,
298+
_labelvalues: Optional[Sequence[str]] = None,
299+
) -> None:
300+
super().__init__(
301+
name,
302+
documentation,
303+
labelnames,
304+
namespace,
305+
subsystem,
306+
unit,
307+
registry,
308+
_labelvalues,
309+
)
310+
self._type = "counter"
311+
# Here is where we grab the global meter to create a FauxCounter
312+
self._counter = meter.create_counter(
313+
self._name, unit=self._unit, description=self._documentation
314+
)
315+
316+
self._current_attributes = ()
317+
274318
def _get_metric(self): # type: ignore[no-untyped-def]
275319
return Metric(self._name, self._documentation, self._type, self._unit)
276320

@@ -299,9 +343,9 @@ def inc(self, amount: float = 1.0) -> None:
299343
# this may not be appropriate in those cases. Can probably just leave the
300344
# attributes param as empty in that case?
301345
self._counter.add(amount, dict(zip(self._labelnames, self._current_attributes)))
302-
# If this was a "child" metric, then the lock will have been taken in labels()
303-
if self._lock.locked():
304-
self._lock.release()
346+
# # If this was a "child" metric, then the lock will have been taken in labels()
347+
# if self._lock.locked():
348+
# self._lock.release()
305349

306350

307351
@attr.s(slots=True, hash=True, auto_attribs=True, kw_only=True)

0 commit comments

Comments
 (0)