diff options
author | Boris Pavlovic <boris@pavlovic.me> | 2014-07-09 05:02:14 +0400 |
---|---|---|
committer | Boris Pavlovic <boris@pavlovic.me> | 2014-07-10 00:03:10 +0400 |
commit | 988909f112ffe79f8855c4713c2c791dd274bb8d (patch) | |
tree | 765eae8facfe456f9ff75e6e8f4a194c12c1269a | |
parent | a6ad8da299e64beb534ed6eb88c907a4a8ad96e4 (diff) | |
download | osprofiler-0.2.4.tar.gz |
Add alternative way to dissable middleware0.2.4
This is mostly done to be able to disable profiler in project via
oslo.config, instead of chaning api-paste.ini
Change-Id: Ib972aedad31fe6a09b8d9815761a41a0ab28256c
-rw-r--r-- | osprofiler/web.py | 21 | ||||
-rw-r--r-- | tests/test_web.py | 22 |
2 files changed, 42 insertions, 1 deletions
diff --git a/osprofiler/web.py b/osprofiler/web.py index e3db948..4787343 100644 --- a/osprofiler/web.py +++ b/osprofiler/web.py @@ -32,6 +32,25 @@ def get_trace_id_headers(): return {} +_DISABLED = False + + +def disable(): + """Disable middleware. + + This is the alternative way to disable middleware. It will be used to be + able to disable middleware via oslo.config. + """ + global _DISABLED + _DISABLED = True + + +def enable(): + """Enable middleware.""" + global _DISABLED + _DISABLED = False + + class WsgiMiddleware(object): """WSGI Middleware that enables tracing for an application.""" @@ -60,7 +79,7 @@ class WsgiMiddleware(object): @webob.dec.wsgify def __call__(self, request): - if not self.enabled: + if _DISABLED or not self.enabled: return request.get_response(self.application) trace_info = utils.signed_unpack(request.headers.get("X-Trace-Info"), diff --git a/tests/test_web.py b/tests/test_web.py index 6fd8b3a..4aefcc6 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -63,6 +63,10 @@ class WebMiddlewareTestCase(test.TestCase): profiler._clean() self.addCleanup(profiler._clean) + def tearDown(self): + web.enable() + super(WebMiddlewareTestCase, self).tearDown() + def test_factory(self): mock_app = mock.MagicMock() local_conf = {"enabled": True, "hmac_key": "123"} @@ -197,3 +201,21 @@ class WebMiddlewareTestCase(test.TestCase): } } mock_profiler_trace.assert_called_once_with("wsgi", info=expected_info) + + @mock.patch("osprofiler.web.profiler.init") + def test_wsgi_middleware_disable_via_python(self, mock_profiler_init): + request = mock.MagicMock() + request.get_response.return_value = "yeah!" + web.disable() + middleware = web.WsgiMiddleware("app", "hmac_key", enabled=True) + self.assertEqual("yeah!", middleware(request)) + self.assertEqual(mock_profiler_init.call_count, 0) + + def test_disable(self): + web.disable() + self.assertTrue(web._DISABLED) + + def test_enabled(self): + web.disable() + web.enable() + self.assertFalse(web._DISABLED) |