8000 Merge pull request #81 from alex/patch-1 · yeahnoob/client_python@f005a78 · GitHub
[go: up one dir, main page]

Skip to content

Commit f005a78

Browse files
committed
Merge pull request prometheus#81 from alex/patch-1
Unnest the Timer class
2 parents 9a188ac + 2c2aa24 commit f005a78

File tree

1 file changed

+99
-99
lines changed

1 file changed

+99
-99
lines changed

prometheus_client/core.py

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -398,26 +398,7 @@ def count_exceptions(self, exception=Exception):
398398
Increments the counter when an exception of the given
399399
type is raised up out of the code.
400400
'''
401-
402-
class ExceptionCounter(object):
403-
def __init__(self, counter):
404-
self._counter = counter
405-
406-
def __enter__(self):
407-
pass
408-
409-
def __exit__(self, typ, value, traceback):
410-
if isinstance(value, exception):
411-
self._counter.inc()
412-
413-
def __call__(self, f):
414-
@wraps(f)
415-
def wrapped(*args, **kwargs):
416-
with self:
417-
return f(*args, **kwargs)
418-
return wrapped
419-
420-
return ExceptionCounter(self)
401+
return _ExceptionCounter(self, exception)
421402

422403
def _samples(self):
423404
return (('', {}, self._value.get()), )
@@ -490,51 +471,14 @@ def track_inprogress(self):
490471
Increments the gauge when the code is entered,
491472
and decrements when it is exited.
492473
'''
493-
494-
class InprogressTracker(object):
495-
def __init__(self, gauge):
496-
self._gauge = gauge
497-
498-
def __enter__(self):
499-
self._gauge.inc()
500-
501-
def __exit__(self, typ, value, traceback):
502-
self._gauge.dec()
503-
504-
def __call__(self, f):
505-
@wraps(f)
506-
def wrapped(*args, **kwargs):
507-
with self:
508-
return f(*args, **kwargs)
509-
return wrapped
510-
511-
return InprogressTracker(self)
474+
return _InprogressTracker(self)
512475

513476
def time(self):
514477
'''Time a block of code or function, and set the duration in seconds.
515478
516479
Can be used as a function decorator or context manager.
517480
'''
518-
519-
class Timer(object):
520-
def __init__(self, gauge):
521-
self._gauge = gauge
522-
523-
def __enter__(self):
524-
self._start = time.time()
525-
526-
def __exit__(self, typ, value, traceback):
527-
# Time can go backwards.
528-
self._gauge.set(max(time.time() - self._start, 0))
529-
530-
def __call__(self, f):
531-
@wraps(f)
532-
def wrapped(*args, **kwargs):
533-
with self:
534-
return f(*args, **kwargs)
535-
return wrapped
536-
537-
return Timer(self)
481+
return _GaugeTimer(self)
538482

539483
def set_function(self, f):
540484
'''Call the provided function to return the Gauge value.
@@ -598,26 +542,7 @@ def time(self):
598542
599543
Can be used as a function decorator or context manager.
600544
'''
601-
602-
class Timer(object):
603-
def __init__(self, summary):
604-
self._summary = summary
605-
606-
def __enter__(self):
607-
self._start = time.time()
608-
609-
def __exit__(self, typ, value, traceback):
610-
# Time can go backwards.
611-
self._summary.observe(max(time.time() - self._start, 0))
612-
613-
def __call__(self, f):
614-
@wraps(f)
615-
def wrapped(*args, **kwargs):
616-
with self:
617-
return f(*args, **kwargs)
< A3D4 code>618-
return wrapped
619-
620-
return Timer(self)
545+
return _SummaryTimer(self)
621546

622547
def _samples(self):
623548
return (
@@ -707,26 +632,7 @@ def time(self):
707632
708633
Can be used as a function decorator or context manager.
709634
'''
710-
711-
class Timer(object):
712-
def __init__(self, histogram):
713-
self._histogram = histogram
714-
715-
def __enter__(self):
716-
self._start = time.time()
717-
718-
def __exit__(self, typ, value, traceback):
719-
# Time can go backwards.
720-
self._histogram.observe(max(time.time() - self._start, 0))
721-
722-
def __call__(self, f):
723-
@wraps(f)
724-
def wrapped(*args, **kwargs):
725-
with self:
726-
return f(*args, **kwargs)
727-
return wrapped
728-
729-
return Timer(self)
635+
return _HistogramTimer(self)
730636

731637
def _samples(self):
732638
samples = []
@@ -738,3 +644,97 @@ def _samples(self):
738644
samples.append(('_sum', {}, self._sum.get()))
739645
return tuple(samples)
740646

647+
648+
class _HistogramTimer(object):
649+
def __init__(self, histogram):
650+
self._histogram = histogram
651+
652+
def __enter__(self):
653+
self._start = time.time()
654+
655+
def __exit__(self, typ, value, traceback):
656+
# Time can go backwards.
657+
self._histogram.observe(max(time.time() - self._start, 0))
658+
659+
def __call__(self, f):
660+
@wraps(f)
661+
def wrapped(*args, **kwargs):
662+
with self:
663+
return f(*args, **kwargs)
664+
return wrapped
665+
666+
667+
class _ExceptionCounter(object):
668+
def __init__(self, counter, exception):
669+
self._counter = counter
670+
self._exception = exception
671+
672+
def __enter__(self):
673+
pass
674+
675+
def __exit__(self, typ, value, traceback):
676+
if isinstance(value, self._exception):
677+
self._counter.inc()
678+
679+
def __call__(self, f):
680+
@wraps(f)
681+
def wrapped(*args, **kwargs):
682+
with self:
683+
return f(*args, **kwargs)
684+
return wrapped
685+
686+
687+
class _InprogressTracker(object):
688+
def __init__(self, gauge):
689+
self._gauge = gauge
690+
691+
def __enter__(self):
692+
self._gauge.inc()
693+
694+
def __exit__(self, typ, value, traceback):
695+
self._gauge.dec()
696+
697+
def __call__(self, f):
698+
@wraps(f)
699+
def wrapped(*args, **kwargs):
700+
with self:
701+
return f(*args, **kwargs)
702+
return wrapped
703+
704+
705+
class _SummaryTimer(object):
706+
def __init__(self, summary):
707+
self._summary = summary
708+
709+
def __enter__(self):
710+
self._start = time.time()
711+
712+
def __exit__(self, typ, value, traceback):
713+
# Time can go backwards.
714+
self._summary.observe(max(time.time() - self._start, 0))
715+
716+
def __call__(self, f):
717+
@wraps(f)
718+
def wrapped(*args, **kwargs):
719+
with self:
720+
return f(*args, **kwargs)
721+
return wrapped
722+
723+
724+
class _GaugeTimer(object):
725+
def __init__(self, gauge):
726+
self._gauge = gauge
727+
728+
def __enter__(self):
729+
self._start = time.time()
730+
731+
def __exit__(self, typ, value, traceback):
732+
# Time can go backwards.
733+
self._gauge.set(max(time.time() - self._start, 0))
734+
735+
def __call__(self, f):
736+
@wraps(f)
737+
def wrapped(*args, **kwargs):
738+
with self:
739+
return f(*args, **kwargs)
740+
return wrapped

0 commit comments

Comments
 (0)
0