Fixes prometheus start metric

This commit is contained in:
Evan Cordell 2016-09-30 12:56:53 -04:00
parent 2d9ce6dbe3
commit 68c5384473
2 changed files with 17 additions and 4 deletions

View file

@ -542,8 +542,9 @@ class EphemeralBuilderManager(BaseManager):
try:
# log start time to prometheus
realm_data = yield From(self._etcd_client.read(self._etcd_realm_key(build_component.builder_realm)))
start_time = json.loads(realm_data.value)['start_time']
metric_queue.builder_time_to_build(time.time() - start_time, labelvalues=[realm_data.executor_name])
parsed_realm_data = json.loads(realm_data.value)
start_time = parsed_realm_data['start_time']
metric_queue.builder_time_to_build(time.time() - start_time, labelvalues=[parsed_realm_data.get('executor_name', 'unknown')])
except (KeyError, etcd.EtcdKeyError):
logger.warning('Could not read realm key %s', build_component.builder_realm)

View file

@ -2,7 +2,7 @@ import time
import unittest
from mock import Mock
from trollius import coroutine, Return, get_event_loop
from trollius import coroutine, Return, get_event_loop, From
from util.metrics.metricqueue import duration_collector_async
@ -25,6 +25,10 @@ def duration_decorated():
def duration_decorated_error():
raise NonReturn("not a Return error")
@coroutine
def calls_decorated():
yield From(duration_decorated())
class DurationDecoratorTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
@ -45,6 +49,14 @@ class DurationDecoratorTestCase(unittest.TestCase):
self.loop.run_until_complete(duration_decorated_error())
assert not mock_histogram.Observe.called
def test_duration_decorator_caller(self):
mock_histogram.reset_mock()
self.loop.run_until_complete(calls_decorated())
assert mock_histogram.Observe.called
assert 1 - mock_histogram.Observe.call_args[0][0] < 1 # duration should be close to 1s
assert mock_histogram.Observe.call_args[1]["labelvalues"] == ["testlabel"]
if __name__ == '__main__':
unittest.main()