Use get_status for status code

This commit is contained in:
Andrew Rabert 2017-03-24 14:47:31 -04:00
parent e470d5ca53
commit a83d5ef771
3 changed files with 17 additions and 17 deletions

View file

@ -93,7 +93,7 @@ class InfluxDBMixin(object):
def on_finish(self): def on_finish(self):
super(InfluxDBMixin, self).on_finish() super(InfluxDBMixin, self).on_finish()
self.set_metric_tag('status_code', self._status_code) self.set_metric_tag('status_code', self.get_status())
self.record_timing(self.request.request_time(), 'duration') self.record_timing(self.request.request_time(), 'duration')
self.application.influxdb.submit( self.application.influxdb.submit(
self.settings[SETTINGS_KEY]['measurement'], self.settings[SETTINGS_KEY]['measurement'],

View file

@ -15,7 +15,6 @@ class StatsdMixin(object):
def initialize(self): def initialize(self):
super(StatsdMixin, self).initialize() super(StatsdMixin, self).initialize()
self.__status_code = None
def set_metric_tag(self, tag, value): def set_metric_tag(self, tag, value):
"""Ignored for statsd since it does not support tagging. """Ignored for statsd since it does not support tagging.
@ -90,21 +89,7 @@ class StatsdMixin(object):
super(StatsdMixin, self).on_finish() super(StatsdMixin, self).on_finish()
self.record_timing(self.request.request_time(), self.record_timing(self.request.request_time(),
self.__class__.__name__, self.request.method, self.__class__.__name__, self.request.method,
self.__status_code) self.get_status())
def set_status(self, status_code, reason=None):
"""Extend tornado `set_status` method to track status code
to avoid referencing the _status internal variable
:param int status_code: Response status code. If ``reason``
is ``None``, it must be present in `httplib.responses
<http.client.responses>`.
:param string reason: Human-readable reason phrase describing
the status code. If ``None``, it will be filled in from
`httplib.responses <http.client.responses>`.
"""
self.__status_code = status_code
super(StatsdMixin, self).set_status(status_code, reason=reason)
class StatsDCollector(object): class StatsDCollector(object):

View file

@ -30,6 +30,12 @@ class CounterBumper(statsd.StatsdMixin, web.RequestHandler):
self.set_status(204) self.set_status(204)
class DefaultStatusCode(statsd.StatsdMixin, web.RequestHandler):
def get(self):
pass
def assert_between(low, value, high): def assert_between(low, value, high):
if not (low <= value < high): if not (low <= value < high):
raise AssertionError('Expected {} to be between {} and {}'.format( raise AssertionError('Expected {} to be between {} and {}'.format(
@ -42,6 +48,7 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
self.application = web.Application([ self.application = web.Application([
web.url('/', examples.statsd.SimpleHandler), web.url('/', examples.statsd.SimpleHandler),
web.url('/counters/(.*)/([.0-9]*)', CounterBumper), web.url('/counters/(.*)/([.0-9]*)', CounterBumper),
web.url('/status_code', DefaultStatusCode),
]) ])
return self.application return self.application
@ -95,6 +102,14 @@ class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
headers={'Correlation-ID': 'does not matter'}) headers={'Correlation-ID': 'does not matter'})
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
def test_that_status_code_is_used_when_not_explicitly_set(self):
response = self.fetch('/status_code')
self.assertEqual(response.code, 200)
expected = 'testing.timers.DefaultStatusCode.GET.200'
self.assertEqual(expected,
list(self.statsd.find_metrics(expected, 'ms'))[0][0])
class StatsdConfigurationTests(testing.AsyncHTTPTestCase): class StatsdConfigurationTests(testing.AsyncHTTPTestCase):