Add duration metric collector decorator (#1885)
Track time-to-start for builders Track time-to-build for builders Track ec2 builder fallbacks Track build time
This commit is contained in:
parent
85d611e2fb
commit
832ee89923
5 changed files with 110 additions and 5 deletions
50
test/test_metricqueue.py
Normal file
50
test/test_metricqueue.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import time
|
||||
import unittest
|
||||
|
||||
from mock import Mock
|
||||
from trollius import coroutine, Return, get_event_loop
|
||||
|
||||
from util.metrics.metricqueue import duration_collector_async
|
||||
|
||||
|
||||
mock_histogram = Mock()
|
||||
|
||||
class NonReturn(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@coroutine
|
||||
@duration_collector_async(mock_histogram, labelvalues=["testlabel"])
|
||||
def duration_decorated():
|
||||
time.sleep(1)
|
||||
raise Return("fin")
|
||||
|
||||
|
||||
@coroutine
|
||||
@duration_collector_async(mock_histogram, labelvalues=["testlabel"])
|
||||
def duration_decorated_error():
|
||||
raise NonReturn("not a Return error")
|
||||
|
||||
|
||||
class DurationDecoratorTestCase(unittest.TestCase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.etcd_client_mock = None
|
||||
self.loop = get_event_loop()
|
||||
super(DurationDecoratorTestCase, self).__init__(*args, **kwargs)
|
||||
|
||||
def test_duration_decorator(self):
|
||||
self.loop.run_until_complete(duration_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"]
|
||||
|
||||
def test_duration_decorator_error(self):
|
||||
mock_histogram.reset_mock()
|
||||
|
||||
with self.assertRaises(NonReturn):
|
||||
self.loop.run_until_complete(duration_decorated_error())
|
||||
assert not mock_histogram.Observe.called
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in a new issue