From fc6257098f403c2c78d45b733ea717bd0f600cd2 Mon Sep 17 00:00:00 2001 From: Kirill Melnikov Date: Thu, 6 Jul 2017 01:16:09 +0500 Subject: [PATCH 1/2] Graphite bridge should use standart time.time instead of timeit.default_timer --- prometheus_client/bridge/graphite.py | 11 ++-- prometheus_client/core.py | 95 +++++++++++++++++----------- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/prometheus_client/bridge/graphite.py b/prometheus_client/bridge/graphite.py index 4c715541..e9f983bc 100755 --- a/prometheus_client/bridge/graphite.py +++ b/prometheus_client/bridge/graphite.py @@ -6,7 +6,6 @@ import socket import time import threading -from timeit import default_timer from .. import core @@ -27,10 +26,10 @@ def __init__(self, pusher, interval, prefix): self._prefix = prefix def run(self): - wait_until = default_timer() + wait_until = time.time() while True: while True: - now = default_timer() + now = time.time() if now >= wait_until: # May need to skip some pushes. while wait_until < now: @@ -45,7 +44,7 @@ def run(self): class GraphiteBridge(object): - def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=default_timer): + def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=time.time): self._address = address self._registry = registry self._timeout = timeout_seconds @@ -64,8 +63,8 @@ def push(self, prefix=''): if labels: labelstr = '.' + '.'.join( ['{0}.{1}'.format( - _sanitize(k), _sanitize(v)) - for k, v in sorted(labels.items())]) + _sanitize(k), _sanitize(v)) + for k, v in sorted(labels.items())]) else: labelstr = '' output.append('{0}{1}{2} {3} {4}\n'.format( diff --git a/prometheus_client/core.py b/prometheus_client/core.py index d715af3e..0256d3a0 100644 --- a/prometheus_client/core.py +++ b/prometheus_client/core.py @@ -19,7 +19,6 @@ unicode = str from threading import Lock -from timeit import default_timer from .decorator import decorate @@ -28,7 +27,7 @@ _RESERVED_METRIC_LABEL_NAME_RE = re.compile(r'^__.*$') _INF = float("inf") _MINUS_INF = float("-inf") -_INITIAL_MMAP_SIZE = 1024*1024 +_INITIAL_MMAP_SIZE = 1024 * 1024 class CollectorRegistry(object): @@ -38,6 +37,7 @@ class CollectorRegistry(object): Metric objects. The returned metrics should be consistent with the Prometheus exposition formats. ''' + def __init__(self, auto_describe=False): self._collector_to_names = {} self._names_to_collectors = {} @@ -51,7 +51,7 @@ def register(self, collector): for name in names: if name in self._names_to_collectors: raise ValueError('Timeseries already present ' - 'in CollectorRegistry: ' + name) + 'in CollectorRegistry: ' + name) for name in names: self._names_to_collectors[name] = collector self._collector_to_names[collector] = names @@ -121,9 +121,11 @@ def restricted_registry(self, names): m = Metric(metric.name, metric.documentation, metric.type) m.samples = samples metrics.append(m) + class RestrictedRegistry(object): def collect(self): return metrics + return RestrictedRegistry() def get_sample_value(self, name, labels=None): @@ -154,6 +156,7 @@ class Metric(object): Custom collectors should use GaugeMetricFamily, CounterMetricFamily and SummaryMetricFamily instead. ''' + def __init__(self, name, documentation, typ): self.name = name self.documentation = documentation @@ -181,15 +184,16 @@ class CounterMetricFamily(Metric): For use by custom collectors. ''' + def __init__(self, name, documentation, value=None, labels=None): Metric.__init__(self, name, documentation, 'counter') if labels is not None and value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if value is not None: - self.add_metric([], value) + self.add_metric([], value) def add_metric(self, labels, value): '''Add a metric to the metric family. @@ -206,15 +210,16 @@ class GaugeMetricFamily(Metric): For use by custom collectors. ''' + def __init__(self, name, documentation, value=None, labels=None): Metric.__init__(self, name, documentation, 'gauge') if labels is not None and value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if value is not None: - self.add_metric([], value) + self.add_metric([], value) def add_metric(self, labels, value): '''Add a metric to the metric family. @@ -231,6 +236,7 @@ class SummaryMetricFamily(Metric): For use by custom collectors. ''' + def __init__(self, name, documentation, count_value=None, sum_value=None, labels=None): Metric.__init__(self, name, documentation, 'summary') if (sum_value is None) != (count_value is None): @@ -238,10 +244,10 @@ def __init__(self, name, documentation, count_value=None, sum_value=None, labels if labels is not None and count_value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if count_value is not None: - self.add_metric([], count_value, sum_value) + self.add_metric([], count_value, sum_value) def add_metric(self, labels, count_value, sum_value): '''Add a metric to the metric family. @@ -260,6 +266,7 @@ class HistogramMetricFamily(Metric): For use by custom collectors. ''' + def __init__(self, name, documentation, buckets=None, sum_value=None, labels=None): Metric.__init__(self, name, documentation, 'histogram') if (sum_value is None) != (buckets is None): @@ -267,10 +274,10 @@ def __init__(self, name, documentation, buckets=None, sum_value=None, labels=Non if labels is not None and buckets is not None: raise ValueError('Can only specify at most one of buckets and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if buckets is not None: - self.add_metric([], buckets, sum_value) + self.add_metric([], buckets, sum_value) def add_metric(self, labels, buckets, sum_value): '''Add a metric to the metric family. @@ -282,7 +289,8 @@ def add_metric(self, labels, buckets, sum_value): sum_value: The sum value of the metric. ''' for bucket, value in buckets: - self.samples.append((self.name + '_bucket', dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), value)) + self.samples.append( + (self.name + '_bucket', dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), value)) # +Inf is last and provides the count value. self.samples.append((self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1])) self.samples.append((self.name + '_sum', dict(zip(self._labelnames, labels)), sum_value)) @@ -294,20 +302,20 @@ class _MutexValue(object): _multiprocess = False def __init__(self, typ, metric_name, name, labelnames, labelvalues, **kwargs): - self._value = 0.0 - self._lock = Lock() + self._value = 0.0 + self._lock = Lock() def inc(self, amount): - with self._lock: - self._value += amount + with self._lock: + self._value += amount def set(self, value): - with self._lock: - self._value = value + with self._lock: + self._value = value def get(self): - with self._lock: - return self._value + with self._lock: + return self._value class _MmapedDict(object): @@ -321,6 +329,7 @@ class _MmapedDict(object): Not thread safe. """ + def __init__(self, filename): self._f = open(filename, 'a+b') if os.fstat(self._f.fileno()).st_size == 0: @@ -407,13 +416,13 @@ class _MmapedValue(object): def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess_mode='', **kwargs): if typ == 'gauge': - file_prefix = typ + '_' + multiprocess_mode + file_prefix = typ + '_' + multiprocess_mode else: file_prefix = typ with lock: if file_prefix not in files: filename = os.path.join( - os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid)) + os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid)) files[file_prefix] = _MmapedDict(filename) self._file = files[file_prefix] self._key = json.dumps((metric_name, name, labelnames, labelvalues)) @@ -448,6 +457,7 @@ def get(self): class _LabelWrapper(object): '''Handles labels for the wrapped metric.''' + def __init__(self, wrappedClass, name, labelnames, **kwargs): self._wrappedClass = wrappedClass self._type = wrappedClass._type @@ -497,7 +507,8 @@ def labels(self, *labelvalues, **labelkwargs): labelvalues = tuple([unicode(l) for l in labelvalues]) with self._lock: if labelvalues not in self._metrics: - self._metrics[labelvalues] = self._wrappedClass(self._name, self._labelnames, labelvalues, **self._kwargs) + self._metrics[labelvalues] = self._wrappedClass(self._name, self._labelnames, labelvalues, + **self._kwargs) return self._metrics[labelvalues] def remove(self, *labelvalues): @@ -519,6 +530,7 @@ def _samples(self): def _MetricWrapper(cls): '''Provides common functionality for metrics.''' + def init(name, documentation, labelnames=(), namespace='', subsystem='', registry=REGISTRY, **kwargs): full_name = '' if namespace: @@ -545,6 +557,7 @@ def init(name, documentation, labelnames=(), namespace='', subsystem='', registr def describe(): return [Metric(full_name, documentation, cls._type)] + collector.describe = describe def collect(): @@ -552,6 +565,7 @@ def collect(): for suffix, labels, value in collector._samples(): metric.add_sample(full_name + suffix, labels, value) return [metric] + collector.collect = collect if registry: @@ -617,7 +631,7 @@ def count_exceptions(self, exception=Exception): return _ExceptionCounter(self, exception) def _samples(self): - return (('', {}, self._value.get()), ) + return (('', {}, self._value.get()),) @_MetricWrapper @@ -663,10 +677,10 @@ def f(): def __init__(self, name, labelnames, labelvalues, multiprocess_mode='all'): if (_ValueClass._multiprocess - and multiprocess_mode not in ['min', 'max', 'livesum', 'liveall', 'all']): + and multiprocess_mode not in ['min', 'max', 'livesum', 'liveall', 'all']): raise ValueError('Invalid multiprocess mode: ' + multiprocess_mode) self._value = _ValueClass(self._type, name, name, labelnames, - labelvalues, multiprocess_mode=multiprocess_mode) + labelvalues, multiprocess_mode=multiprocess_mode) def inc(self, amount=1): '''Increment gauge by the given amount.''' @@ -706,12 +720,14 @@ def set_function(self, f): The function must return a float, and may be called from multiple threads. All other methods of the Gauge become NOOPs. ''' + def samples(self): - return (('', {}, float(f())), ) + return (('', {}, float(f())),) + self._samples = types.MethodType(samples, self) def _samples(self): - return (('', {}, self._value.get()), ) + return (('', {}, self._value.get()),) @_MetricWrapper @@ -822,7 +838,8 @@ def create_response(request): _type = 'histogram' _reserved_labelnames = ['histogram'] - def __init__(self, name, labelnames, labelvalues, buckets=(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, _INF)): + def __init__(self, name, labelnames, labelvalues, + buckets=(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, _INF)): self._sum = _ValueClass(self._type, name, name + '_sum', labelnames, labelvalues) buckets = [float(b) for b in buckets] if buckets != sorted(buckets): @@ -837,7 +854,8 @@ def __init__(self, name, labelnames, labelvalues, buckets=(.005, .01, .025, .05, self._buckets = [] bucket_labelnames = labelnames + ('le',) for b in buckets: - self._buckets.append(_ValueClass(self._type, name, name + '_bucket', bucket_labelnames, labelvalues + (_floatToGoString(b),))) + self._buckets.append(_ValueClass(self._type, name, name + '_bucket', bucket_labelnames, + labelvalues + (_floatToGoString(b),))) def observe(self, amount): '''Observe the given amount.''' @@ -870,16 +888,17 @@ def __init__(self, histogram): self._histogram = histogram def __enter__(self): - self._start = default_timer() + self._start = time.time() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._histogram.observe(max(default_timer() - self._start, 0)) + self._histogram.observe(max(time.time() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) + return decorate(f, wrapped) @@ -899,6 +918,7 @@ def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) + return decorate(f, wrapped) @@ -916,6 +936,7 @@ def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) + return decorate(f, wrapped) @@ -924,16 +945,17 @@ def __init__(self, summary): self._summary = summary def __enter__(self): - self._start = default_timer() + self._start = time.time() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._summary.observe(max(default_timer() - self._start, 0)) + self._summary.observe(max(time.time() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) + return decorate(f, wrapped) @@ -942,14 +964,15 @@ def __init__(self, gauge): self._gauge = gauge def __enter__(self): - self._start = default_timer() + self._start = time.time() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._gauge.set(max(default_timer() - self._start, 0)) + self._gauge.set(max(time.time() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) + return decorate(f, wrapped) From 2ce21383f6afa5e079b5149feaa767a9d21336a4 Mon Sep 17 00:00:00 2001 From: Kirill Melnikov Date: Thu, 6 Jul 2017 14:27:28 +0500 Subject: [PATCH 2/2] back up core functionality --- prometheus_client/bridge/graphite.py | 5 +- prometheus_client/core.py | 95 +++++++++++----------------- 2 files changed, 39 insertions(+), 61 deletions(-) diff --git a/prometheus_client/bridge/graphite.py b/prometheus_client/bridge/graphite.py index e9f983bc..6b8f2417 100755 --- a/prometheus_client/bridge/graphite.py +++ b/prometheus_client/bridge/graphite.py @@ -6,6 +6,7 @@ import socket import time import threading +from timeit import default_timer from .. import core @@ -26,10 +27,10 @@ def __init__(self, pusher, interval, prefix): self._prefix = prefix def run(self): - wait_until = time.time() + wait_until = default_timer() while True: while True: - now = time.time() + now = default_timer() if now >= wait_until: # May need to skip some pushes. while wait_until < now: diff --git a/prometheus_client/core.py b/prometheus_client/core.py index 0256d3a0..d715af3e 100644 --- a/prometheus_client/core.py +++ b/prometheus_client/core.py @@ -19,6 +19,7 @@ unicode = str from threading import Lock +from timeit import default_timer from .decorator import decorate @@ -27,7 +28,7 @@ _RESERVED_METRIC_LABEL_NAME_RE = re.compile(r'^__.*$') _INF = float("inf") _MINUS_INF = float("-inf") -_INITIAL_MMAP_SIZE = 1024 * 1024 +_INITIAL_MMAP_SIZE = 1024*1024 class CollectorRegistry(object): @@ -37,7 +38,6 @@ class CollectorRegistry(object): Metric objects. The returned metrics should be consistent with the Prometheus exposition formats. ''' - def __init__(self, auto_describe=False): self._collector_to_names = {} self._names_to_collectors = {} @@ -51,7 +51,7 @@ def register(self, collector): for name in names: if name in self._names_to_collectors: raise ValueError('Timeseries already present ' - 'in CollectorRegistry: ' + name) + 'in CollectorRegistry: ' + name) for name in names: self._names_to_collectors[name] = collector self._collector_to_names[collector] = names @@ -121,11 +121,9 @@ def restricted_registry(self, names): m = Metric(metric.name, metric.documentation, metric.type) m.samples = samples metrics.append(m) - class RestrictedRegistry(object): def collect(self): return metrics - return RestrictedRegistry() def get_sample_value(self, name, labels=None): @@ -156,7 +154,6 @@ class Metric(object): Custom collectors should use GaugeMetricFamily, CounterMetricFamily and SummaryMetricFamily instead. ''' - def __init__(self, name, documentation, typ): self.name = name self.documentation = documentation @@ -184,16 +181,15 @@ class CounterMetricFamily(Metric): For use by custom collectors. ''' - def __init__(self, name, documentation, value=None, labels=None): Metric.__init__(self, name, documentation, 'counter') if labels is not None and value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if value is not None: - self.add_metric([], value) + self.add_metric([], value) def add_metric(self, labels, value): '''Add a metric to the metric family. @@ -210,16 +206,15 @@ class GaugeMetricFamily(Metric): For use by custom collectors. ''' - def __init__(self, name, documentation, value=None, labels=None): Metric.__init__(self, name, documentation, 'gauge') if labels is not None and value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if value is not None: - self.add_metric([], value) + self.add_metric([], value) def add_metric(self, labels, value): '''Add a metric to the metric family. @@ -236,7 +231,6 @@ class SummaryMetricFamily(Metric): For use by custom collectors. ''' - def __init__(self, name, documentation, count_value=None, sum_value=None, labels=None): Metric.__init__(self, name, documentation, 'summary') if (sum_value is None) != (count_value is None): @@ -244,10 +238,10 @@ def __init__(self, name, documentation, count_value=None, sum_value=None, labels if labels is not None and count_value is not None: raise ValueError('Can only specify at most one of value and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if count_value is not None: - self.add_metric([], count_value, sum_value) + self.add_metric([], count_value, sum_value) def add_metric(self, labels, count_value, sum_value): '''Add a metric to the metric family. @@ -266,7 +260,6 @@ class HistogramMetricFamily(Metric): For use by custom collectors. ''' - def __init__(self, name, documentation, buckets=None, sum_value=None, labels=None): Metric.__init__(self, name, documentation, 'histogram') if (sum_value is None) != (buckets is None): @@ -274,10 +267,10 @@ def __init__(self, name, documentation, buckets=None, sum_value=None, labels=Non if labels is not None and buckets is not None: raise ValueError('Can only specify at most one of buckets and labels.') if labels is None: - labels = [] + labels = [] self._labelnames = labels if buckets is not None: - self.add_metric([], buckets, sum_value) + self.add_metric([], buckets, sum_value) def add_metric(self, labels, buckets, sum_value): '''Add a metric to the metric family. @@ -289,8 +282,7 @@ def add_metric(self, labels, buckets, sum_value): sum_value: The sum value of the metric. ''' for bucket, value in buckets: - self.samples.append( - (self.name + '_bucket', dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), value)) + self.samples.append((self.name + '_bucket', dict(list(zip(self._labelnames, labels)) + [('le', bucket)]), value)) # +Inf is last and provides the count value. self.samples.append((self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1])) self.samples.append((self.name + '_sum', dict(zip(self._labelnames, labels)), sum_value)) @@ -302,20 +294,20 @@ class _MutexValue(object): _multiprocess = False def __init__(self, typ, metric_name, name, labelnames, labelvalues, **kwargs): - self._value = 0.0 - self._lock = Lock() + self._value = 0.0 + self._lock = Lock() def inc(self, amount): - with self._lock: - self._value += amount + with self._lock: + self._value += amount def set(self, value): - with self._lock: - self._value = value + with self._lock: + self._value = value def get(self): - with self._lock: - return self._value + with self._lock: + return self._value class _MmapedDict(object): @@ -329,7 +321,6 @@ class _MmapedDict(object): Not thread safe. """ - def __init__(self, filename): self._f = open(filename, 'a+b') if os.fstat(self._f.fileno()).st_size == 0: @@ -416,13 +407,13 @@ class _MmapedValue(object): def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess_mode='', **kwargs): if typ == 'gauge': - file_prefix = typ + '_' + multiprocess_mode + file_prefix = typ + '_' + multiprocess_mode else: file_prefix = typ with lock: if file_prefix not in files: filename = os.path.join( - os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid)) + os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid)) files[file_prefix] = _MmapedDict(filename) self._file = files[file_prefix] self._key = json.dumps((metric_name, name, labelnames, labelvalues)) @@ -457,7 +448,6 @@ def get(self): class _LabelWrapper(object): '''Handles labels for the wrapped metric.''' - def __init__(self, wrappedClass, name, labelnames, **kwargs): self._wrappedClass = wrappedClass self._type = wrappedClass._type @@ -507,8 +497,7 @@ def labels(self, *labelvalues, **labelkwargs): labelvalues = tuple([unicode(l) for l in labelvalues]) with self._lock: if labelvalues not in self._metrics: - self._metrics[labelvalues] = self._wrappedClass(self._name, self._labelnames, labelvalues, - **self._kwargs) + self._metrics[labelvalues] = self._wrappedClass(self._name, self._labelnames, labelvalues, **self._kwargs) return self._metrics[labelvalues] def remove(self, *labelvalues): @@ -530,7 +519,6 @@ def _samples(self): def _MetricWrapper(cls): '''Provides common functionality for metrics.''' - def init(name, documentation, labelnames=(), namespace='', subsystem='', registry=REGISTRY, **kwargs): full_name = '' if namespace: @@ -557,7 +545,6 @@ def init(name, documentation, labelnames=(), namespace='', subsystem='', registr def describe(): return [Metric(full_name, documentation, cls._type)] - collector.describe = describe def collect(): @@ -565,7 +552,6 @@ def collect(): for suffix, labels, value in collector._samples(): metric.add_sample(full_name + suffix, labels, value) return [metric] - collector.collect = collect if registry: @@ -631,7 +617,7 @@ def count_exceptions(self, exception=Exception): return _ExceptionCounter(self, exception) def _samples(self): - return (('', {}, self._value.get()),) + return (('', {}, self._value.get()), ) @_MetricWrapper @@ -677,10 +663,10 @@ def f(): def __init__(self, name, labelnames, labelvalues, multiprocess_mode='all'): if (_ValueClass._multiprocess - and multiprocess_mode not in ['min', 'max', 'livesum', 'liveall', 'all']): + and multiprocess_mode not in ['min', 'max', 'livesum', 'liveall', 'all']): raise ValueError('Invalid multiprocess mode: ' + multiprocess_mode) self._value = _ValueClass(self._type, name, name, labelnames, - labelvalues, multiprocess_mode=multiprocess_mode) + labelvalues, multiprocess_mode=multiprocess_mode) def inc(self, amount=1): '''Increment gauge by the given amount.''' @@ -720,14 +706,12 @@ def set_function(self, f): The function must return a float, and may be called from multiple threads. All other methods of the Gauge become NOOPs. ''' - def samples(self): - return (('', {}, float(f())),) - + return (('', {}, float(f())), ) self._samples = types.MethodType(samples, self) def _samples(self): - return (('', {}, self._value.get()),) + return (('', {}, self._value.get()), ) @_MetricWrapper @@ -838,8 +822,7 @@ def create_response(request): _type = 'histogram' _reserved_labelnames = ['histogram'] - def __init__(self, name, labelnames, labelvalues, - buckets=(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, _INF)): + def __init__(self, name, labelnames, labelvalues, buckets=(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, _INF)): self._sum = _ValueClass(self._type, name, name + '_sum', labelnames, labelvalues) buckets = [float(b) for b in buckets] if buckets != sorted(buckets): @@ -854,8 +837,7 @@ def __init__(self, name, labelnames, labelvalues, self._buckets = [] bucket_labelnames = labelnames + ('le',) for b in buckets: - self._buckets.append(_ValueClass(self._type, name, name + '_bucket', bucket_labelnames, - labelvalues + (_floatToGoString(b),))) + self._buckets.append(_ValueClass(self._type, name, name + '_bucket', bucket_labelnames, labelvalues + (_floatToGoString(b),))) def observe(self, amount): '''Observe the given amount.''' @@ -888,17 +870,16 @@ def __init__(self, histogram): self._histogram = histogram def __enter__(self): - self._start = time.time() + self._start = default_timer() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._histogram.observe(max(time.time() - self._start, 0)) + self._histogram.observe(max(default_timer() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) - return decorate(f, wrapped) @@ -918,7 +899,6 @@ def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) - return decorate(f, wrapped) @@ -936,7 +916,6 @@ def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) - return decorate(f, wrapped) @@ -945,17 +924,16 @@ def __init__(self, summary): self._summary = summary def __enter__(self): - self._start = time.time() + self._start = default_timer() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._summary.observe(max(time.time() - self._start, 0)) + self._summary.observe(max(default_timer() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) - return decorate(f, wrapped) @@ -964,15 +942,14 @@ def __init__(self, gauge): self._gauge = gauge def __enter__(self): - self._start = time.time() + self._start = default_timer() def __exit__(self, typ, value, traceback): # Time can go backwards. - self._gauge.set(max(time.time() - self._start, 0)) + self._gauge.set(max(default_timer() - self._start, 0)) def __call__(self, f): def wrapped(func, *args, **kwargs): with self: return func(*args, **kwargs) - return decorate(f, wrapped)