Merge pull request #66 from vikingco/feature/log-db-types
Log update/select/insert queries separately.
This commit is contained in:
commit
2f482b4518
|
@ -25,19 +25,22 @@ def pre_django_1_6_cursorwrapper_getattr(self, attr):
|
||||||
return getattr(self.cursor, attr)
|
return getattr(self.cursor, attr)
|
||||||
|
|
||||||
|
|
||||||
def patched_execute(orig_execute, self, *args, **kwargs):
|
def _get_query_type(query):
|
||||||
with statsd.timer(key(self.db, 'execute')):
|
return (query.split(None, 1) or ['__empty__'])[0].lower()
|
||||||
return orig_execute(self, *args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def patched_executemany(orig_executemany, self, *args, **kwargs):
|
def patched_execute(orig_execute, self, query, *args, **kwargs):
|
||||||
with statsd.timer(key(self.db, 'executemany')):
|
with statsd.timer(key(self.db, 'execute.%s' % _get_query_type(query))):
|
||||||
return orig_executemany(self, *args, **kwargs)
|
return orig_execute(self, query, *args, **kwargs)
|
||||||
|
|
||||||
|
def patched_executemany(orig_executemany, self, query, *args, **kwargs):
|
||||||
|
with statsd.timer(key(self.db, 'executemany.%s' % _get_query_type(query))):
|
||||||
|
return orig_executemany(self, query, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def patched_callproc(orig_callproc, self, *args, **kwargs):
|
def patched_callproc(orig_callproc, self, query, *args, **kwargs):
|
||||||
with statsd.timer(key(self.db, 'callproc')):
|
with statsd.timer(key(self.db, 'callproc.%s' % _get_query_type(query))):
|
||||||
return orig_callproc(self, *args, **kwargs)
|
return orig_callproc(self, query, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def patch():
|
def patch():
|
||||||
|
|
|
@ -476,27 +476,47 @@ class TestPatchMethod(TestCase):
|
||||||
|
|
||||||
|
|
||||||
class TestCursorWrapperPatching(TestCase):
|
class TestCursorWrapperPatching(TestCase):
|
||||||
|
example_queries = {
|
||||||
|
'select': 'select * from something;',
|
||||||
|
'insert': 'insert (1, 2) into something;',
|
||||||
|
'update': 'update something set a=1;',
|
||||||
|
}
|
||||||
|
|
||||||
def test_patched_callproc_calls_timer(self):
|
def test_patched_callproc_calls_timer(self):
|
||||||
with mock.patch.object(statsd, 'timer') as timer:
|
for operation, query in self.example_queries.items():
|
||||||
db = mock.Mock(executable_name='name', alias='alias')
|
with mock.patch.object(statsd, 'timer') as timer:
|
||||||
instance = mock.Mock(db=db)
|
client = mock.Mock(executable_name='client_executable_name')
|
||||||
patched_callproc(lambda *args, **kwargs: None, instance)
|
db = mock.Mock(executable_name='name', alias='alias', client=client)
|
||||||
self.assertEqual(timer.call_count, 1)
|
instance = mock.Mock(db=db)
|
||||||
|
|
||||||
|
patched_callproc(lambda *args, **kwargs: None, instance, query)
|
||||||
|
|
||||||
|
self.assertEqual(timer.call_count, 1)
|
||||||
|
self.assertEqual(timer.call_args[0][0], 'db.client_executable_name.alias.callproc.%s' % operation)
|
||||||
|
|
||||||
def test_patched_execute_calls_timer(self):
|
def test_patched_execute_calls_timer(self):
|
||||||
with mock.patch.object(statsd, 'timer') as timer:
|
for operation, query in self.example_queries.items():
|
||||||
db = mock.Mock(executable_name='name', alias='alias')
|
with mock.patch.object(statsd, 'timer') as timer:
|
||||||
instance = mock.Mock(db=db)
|
client = mock.Mock(executable_name='client_executable_name')
|
||||||
patched_execute(lambda *args, **kwargs: None, instance)
|
db = mock.Mock(executable_name='name', alias='alias', client=client)
|
||||||
self.assertEqual(timer.call_count, 1)
|
instance = mock.Mock(db=db)
|
||||||
|
|
||||||
|
patched_execute(lambda *args, **kwargs: None, instance, query)
|
||||||
|
|
||||||
|
self.assertEqual(timer.call_count, 1)
|
||||||
|
self.assertEqual(timer.call_args[0][0], 'db.client_executable_name.alias.execute.%s' % operation)
|
||||||
|
|
||||||
def test_patched_executemany_calls_timer(self):
|
def test_patched_executemany_calls_timer(self):
|
||||||
with mock.patch.object(statsd, 'timer') as timer:
|
for operation, query in self.example_queries.items():
|
||||||
db = mock.Mock(executable_name='name', alias='alias')
|
with mock.patch.object(statsd, 'timer') as timer:
|
||||||
instance = mock.Mock(db=db)
|
client = mock.Mock(executable_name='client_executable_name')
|
||||||
patched_executemany(lambda *args, **kwargs: None, instance)
|
db = mock.Mock(executable_name='name', alias='alias', client=client)
|
||||||
self.assertEqual(timer.call_count, 1)
|
instance = mock.Mock(db=db)
|
||||||
|
|
||||||
|
patched_executemany(lambda *args, **kwargs: None, instance, query)
|
||||||
|
|
||||||
|
self.assertEqual(timer.call_count, 1)
|
||||||
|
self.assertEqual(timer.call_args[0][0], 'db.client_executable_name.alias.executemany.%s' % operation)
|
||||||
|
|
||||||
@mock.patch(
|
@mock.patch(
|
||||||
'django_statsd.patches.db.pre_django_1_6_cursorwrapper_getattr')
|
'django_statsd.patches.db.pre_django_1_6_cursorwrapper_getattr')
|
||||||
|
|
Loading…
Reference in New Issue