8000 Use timeit.default_timer instead of time.time by acdha · Pull Request #106 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Use timeit.default_timer instead of time.time #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions prometheus_client/bridge/graphite.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket
import time
import threading
from timeit import default_timer

from .. import core

Expand All @@ -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:
Expand All @@ -44,14 +45,14 @@ def run(self):


class GraphiteBridge(object):
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time):
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=default_timer):
self._address = address
self._registry = registry
self._timeout = timeout_seconds
self._time = _time
self._timer = _timer

def push(self, prefix=''):
now = int(self._time.time())
now = int(self._timer())
output = []

prefixstr = ''
Expand Down
16 changes: 8 additions & 8 deletions prometheus_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import copy
import math
import re
import time
import types
from timeit import default_timer

try:
from BaseHTTPServer import BaseHTTPRequestHandler
Expand Down Expand Up @@ -466,7 +466,7 @@ def set(self, value):

def set_to_current_time(self):
'''Set gauge to the current unixtime.'''
self.set(time.time())
self.set(default_timer())

def track_inprogress(self):
'''Track inprogress blocks of code or functions.
Expand Down Expand Up @@ -654,11 +654,11 @@ 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):
Expand Down Expand Up @@ -708,11 +708,11 @@ 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):
Expand All @@ -726,11 +726,11 @@ 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):
Expand Down
9 changes: 4 additions & 5 deletions tests/test_graphite_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from prometheus_client import Counter, CollectorRegistry
from prometheus_client.bridge.graphite import GraphiteBridge

class FakeTime(object):
def time(self):
return 1434898897.5
def fake_timer():
return 1434898897.5

class TestGraphiteBridge(unittest.TestCase):
def setUp(self):
Expand All @@ -27,10 +26,10 @@ def run(self):
server.socket.close()
self.t = ServingThread()
self.t.start()

# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
address = ('localhost', server.server_address[1])
self.gb = GraphiteBridge(address, self.registry, _time=FakeTime())
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)

def test_nolabels(self):
counter = Counter('c', 'help', registry=self.registry)
Expand Down
0