Add possibility to disable sqlalchemy tracing
Engine that is used in migraiton shouldn't be traced. So add built-in support for disable tracing. This simplifies a lot code in OpenStack projects
This commit is contained in:
parent
a105156eb0
commit
ee2469d047
@ -49,14 +49,14 @@ def get_profiler():
|
||||
|
||||
|
||||
def start(name, info=None):
|
||||
"""Send new start notification if profiler instance is preseneted."""
|
||||
"""Send new start notification if profiler instance is presented."""
|
||||
profiler = get_profiler()
|
||||
if profiler:
|
||||
profiler.start(name, info=info)
|
||||
|
||||
|
||||
def stop(info=None):
|
||||
"""Send new stop notification if profiler instance is preseneted."""
|
||||
"""Send new stop notification if profiler instance is presented."""
|
||||
profiler = get_profiler()
|
||||
if profiler:
|
||||
profiler.stop(info=info)
|
||||
|
@ -37,8 +37,22 @@ def after_execute():
|
||||
return handler
|
||||
|
||||
|
||||
_DISABLED = False
|
||||
|
||||
|
||||
def disable():
|
||||
global _DISABLED
|
||||
_DISABLED = True
|
||||
|
||||
|
||||
def enable():
|
||||
global _DISABLED
|
||||
_DISABLED = False
|
||||
|
||||
|
||||
def add_tracing(sqlalchemy, engine, name):
|
||||
"""Add tracing to all sqlalchemy calls."""
|
||||
|
||||
sqlalchemy.event.listen(engine, 'before_execute', before_execute(name))
|
||||
sqlalchemy.event.listen(engine, 'after_execute', after_execute())
|
||||
if not _DISABLED:
|
||||
sqlalchemy.event.listen(engine, 'before_execute', before_execute(name))
|
||||
sqlalchemy.event.listen(engine, 'after_execute', after_execute())
|
||||
|
@ -58,3 +58,19 @@ class SqlalchemyTracingTestCase(test.TestCase):
|
||||
mock.call(engine, "after_execute", "after")
|
||||
]
|
||||
self.assertEqual(sa.event.listen.call_args_list, expected_calls)
|
||||
|
||||
@mock.patch("osprofiler.sqlalchemy.before_execute")
|
||||
@mock.patch("osprofiler.sqlalchemy.after_execute")
|
||||
def test_disable_and_enable(self, mock_after_exc, mock_before_exc):
|
||||
sqlalchemy.disable()
|
||||
|
||||
sa = mock.MagicMock()
|
||||
engine = mock.MagicMock()
|
||||
sqlalchemy.add_tracing(sa, engine, "sql")
|
||||
self.assertFalse(mock_after_exc.called)
|
||||
self.assertFalse(mock_before_exc.called)
|
||||
|
||||
sqlalchemy.enable()
|
||||
sqlalchemy.add_tracing(sa, engine, "sql")
|
||||
self.assertTrue(mock_after_exc.called)
|
||||
self.assertTrue(mock_before_exc.called)
|
||||
|
Loading…
Reference in New Issue
Block a user