Passed
Push — master ( 77bb10...a1fcf2 )
by Vlad
04:33
created

helpers.wrapper()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily
2
3
4
class TimestampMixin(object):
5
    """Add timestamps to *MetricFamily
6
7
    The goal is to be a generic drop-in replacement for *MetricFamily.
8
    That means that we can be called with the exact same signature as the original class.
9
    We don't care about any other argument.
10
    The sole assumption is that timestamp is not an argument expected by *MetricFamily.
11
    """
12
    def __init__(self, *args, **kwargs):
13
        try:
14
            self._timestamp = kwargs.pop('timestamp')
15
        except KeyError:
16
            self._timestamp = None
17
        super(TimestampMixin, self).__init__(*args, **kwargs)
18
19
    def add_metric(self, *args, **kwargs):
20
        if 'timestamp' not in kwargs:
21
            kwargs['timestamp'] = self._timestamp
22
        super(TimestampMixin, self).add_metric(*args, **kwargs)
23
24
25
class TimestampGaugeMetricFamily(TimestampMixin, GaugeMetricFamily):
26
    pass
27
28
29
class TimestampCounterMetricFamily(TimestampMixin, CounterMetricFamily):
30
    pass
31
32
33
class C():
34
    def f(self, truc=None):
35
        print('x' + truc)
36
37
38
def wrapper(cls, truc=None):
39
    new_cls = cls
40
    new_cls.f = cls.f()
41