6060 GaugeMetricFamily ,
6161 Sample ,
6262)
63- from typing_extensions import Self
63+ from typing_extensions import Dict , Self
6464
6565from twisted .python .threadpool import ThreadPool
6666from 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" )
195+
196196
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,38 @@ def _multi_samples(self) -> Iterable[Sample]:
271284 native_histogram_value ,
272285 )
273286
287+
288+ class SynapseCounter (SynapseMetricWrapperBase ):
289+ _type : str = "counter"
290+
291+ def __init__ (
292+ self ,
293+ name : str ,
294+ documentation : str ,
295+ labelnames : Iterable [str ] = (),
296+ namespace : str = "" ,
297+ subsystem : str = "" ,
298+ unit : str = "" ,
299+ registry : Optional [CollectorRegistry ] = REGISTRY ,
300+ _labelvalues : Optional [Sequence [str ]] = None ,
301+ ) -> None :
302+ super ().__init__ (
303+ name ,
304+ documentation ,
305+ labelnames ,
306+ namespace ,
307+ subsystem ,
308+ unit ,
309+ registry ,
310+ _labelvalues ,
311+ )
312+ # Here is where we grab the global meter to create a FauxCounter
313+ self ._counter = meter .create_counter (
314+ self ._name , unit = self ._unit , description = self ._documentation
315+ )
316+
317+ self ._current_attributes = ()
318+
274319 def _get_metric (self ): # type: ignore[no-untyped-def]
275320 return Metric (self ._name , self ._documentation , self ._type , self ._unit )
276321
0 commit comments