InfluxDBMixin: Add test for set_metric_tag.

This commit is contained in:
Dave Shawley 2016-01-27 10:28:30 -05:00
parent 78a59d167d
commit 64c136f635
2 changed files with 26 additions and 0 deletions

View file

@ -32,6 +32,16 @@ class SimpleHandler(metrics.InfluxDBMixin, web.RequestHandler):
""" """
@gen.coroutine
def prepare(self):
maybe_future = super(SimpleHandler, self).prepare()
if gen.is_future(maybe_future):
yield maybe_future
if 'Correlation-ID' in self.request.headers:
self.set_metric_tag('correlation_id',
self.request.headers['Correlation-ID'])
@gen.coroutine @gen.coroutine
def get(self): def get(self):
with self.execution_timer('sleepytime'): with self.execution_timer('sleepytime'):

View file

@ -1,6 +1,7 @@
import logging import logging
import socket import socket
import time import time
import uuid
from tornado import gen, testing, web from tornado import gen, testing, web
import mock import mock
@ -190,3 +191,18 @@ class InfluxDbTests(testing.AsyncHTTPTestCase):
response = self.fetch('/') response = self.fetch('/')
self.assertEqual(response.code, 204) self.assertEqual(response.code, 204)
self.assertIs(cfg['db_connection'], conn) self.assertIs(cfg['db_connection'], conn)
def test_that_metric_tag_is_tracked(self):
cid = str(uuid.uuid4())
response = self.fetch('/', headers={'Correlation-ID': cid})
self.assertEqual(response.code, 204)
for key, fields, timestamp in self.influx_messages:
if key.startswith('my-service,'):
tag_dict = dict(a.split('=') for a in key.split(',')[1:])
self.assertEqual(tag_dict['correlation_id'],
'"{}"'.format(cid))
break
else:
self.fail('Expected to find "request" metric in {!r}'.format(
list(self.application.influx_db['requests'])))