Remove information about service in profiler

Actually only notifier should know information about service.

Fix WsgiMiddleware + add more information about request
This commit is contained in:
Boris Pavlovic 2014-05-09 21:45:18 +04:00
parent d8aa82e3dd
commit 9762127dbe
4 changed files with 24 additions and 20 deletions

View File

@ -21,7 +21,7 @@ class Notifier(object):
like to use profiler lib. like to use profiler lib.
""" """
def notify(self, event_type, payload): def notify(self, payload):
pass pass

View File

@ -20,7 +20,7 @@ import uuid
from osprofiler import notifier from osprofiler import notifier
# NOTE(boris-42): Thread safe storge for profiler instances. # NOTE(boris-42): Thread safe storage for profiler instances.
__local_ctx = threading.local() __local_ctx = threading.local()
@ -28,15 +28,13 @@ def _clean():
__local_ctx.profiler = None __local_ctx.profiler = None
def init(base_id=None, parent_id=None, service='generic'): def init(base_id=None, parent_id=None):
"""Init profiler. """Init profiler.
:param base_id: Used to bind all related traces. :param base_id: Used to bind all related traces.
:param parent_id: Used to build tree of traces. :param parent_id: Used to build tree of traces.
:param service: Service name that sends traces.
:returns: Profiler instance :returns: Profiler instance
""" """
__local_ctx.profiler = Profiler(base_id=base_id, parent_id=parent_id, __local_ctx.profiler = Profiler(base_id=base_id, parent_id=parent_id)
service=service)
return __local_ctx.profiler return __local_ctx.profiler
@ -64,9 +62,8 @@ def stop(info=None):
class Profiler(object): class Profiler(object):
def __init__(self, base_id=None, parent_id=None, service='generic'): def __init__(self, base_id=None, parent_id=None):
self.notifier = notifier.get_notifier() self.notifier = notifier.get_notifier()
self._service = service
if not base_id: if not base_id:
base_id = str(uuid.uuid4()) base_id = str(uuid.uuid4())
self._trace_stack = [base_id, parent_id or base_id] self._trace_stack = [base_id, parent_id or base_id]
@ -121,4 +118,4 @@ class Profiler(object):
if info: if info:
payload['info'] = info payload['info'] = info
self.notifier.notify('profiler.%s' % self._service, payload) self.notifier.notify(payload)

View File

@ -30,10 +30,8 @@ def add_trace_id_header(headers):
class WsgiMiddleware(object): class WsgiMiddleware(object):
"""WSGI Middleware that enables tracing for an application.""" """WSGI Middleware that enables tracing for an application."""
def __init__(self, application, service_name='server', name='WSGI', def __init__(self, application, name='WSGI', enabled=False):
enabled=False):
self.application = application self.application = application
self.service_name = service_name
self.name = name self.name = name
self.enabled = enabled self.enabled = enabled
@ -53,10 +51,19 @@ class WsgiMiddleware(object):
trace_info = pickle.loads(base64.b64decode(trace_info_enc)) trace_info = pickle.loads(base64.b64decode(trace_info_enc))
p = profiler.init(trace_info.get("base_id"), p = profiler.init(trace_info.get("base_id"),
trace_info.get("parent_id"), trace_info.get("parent_id"))
self.service_name)
with p(self.name, info={"url": request.url}): info = {
"request": {
"host_url": request.host_url,
"path": request.path,
"query": request.query_string,
"method": request.method,
"scheme": request.scheme
}
}
with p(self.name, info=info):
return request.get_response(self.application) return request.get_response(self.application)
return request.get_response(self.application) return request.get_response(self.application)

View File

@ -27,7 +27,7 @@ class ProfilerGlobMethodsTestCase(test.TestCase):
self.assertIsNone(profiler.get_profiler()) self.assertIsNone(profiler.get_profiler())
def test_get_profiler_and_init(self): def test_get_profiler_and_init(self):
p = profiler.init(base_id="1", parent_id="2", service="generic2") p = profiler.init(base_id="1", parent_id="2")
self.assertEqual(profiler.get_profiler(), p) self.assertEqual(profiler.get_profiler(), p)
self.assertEqual(p.get_base_id(), "1") self.assertEqual(p.get_base_id(), "1")
@ -39,7 +39,7 @@ class ProfilerGlobMethodsTestCase(test.TestCase):
profiler.start("name") profiler.start("name")
def test_start(self): def test_start(self):
p = profiler.init(base_id="1", parent_id="2", service="generic2") p = profiler.init(base_id="1", parent_id="2")
p.start = mock.MagicMock() p.start = mock.MagicMock()
profiler.start("name", info="info") profiler.start("name", info="info")
p.start.assert_called_once_with("name", info="info") p.start.assert_called_once_with("name", info="info")
@ -49,7 +49,7 @@ class ProfilerGlobMethodsTestCase(test.TestCase):
profiler.stop() profiler.stop()
def test_stop(self): def test_stop(self):
p = profiler.init(base_id="1", parent_id="2", service="generic2") p = profiler.init(base_id="1", parent_id="2")
p.stop = mock.MagicMock() p.stop = mock.MagicMock()
profiler.stop(info="info") profiler.stop(info="info")
p.stop.assert_called_once_with(info="info") p.stop.assert_called_once_with(info="info")
@ -101,7 +101,7 @@ class ProfilerTestCase(test.TestCase):
prof = profiler.Profiler(base_id="1", parent_id="2") prof = profiler.Profiler(base_id="1", parent_id="2")
prof.start("test", info=info) prof.start("test", info=info)
notifier.notify.assert_called_once_with('profiler.generic', payload) notifier.notify.assert_called_once_with(payload)
@mock.patch("osprofiler.profiler.notifier.get_notifier") @mock.patch("osprofiler.profiler.notifier.get_notifier")
def test_profiler_stop(self, mock_get_notfier): def test_profiler_stop(self, mock_get_notfier):
@ -123,7 +123,7 @@ class ProfilerTestCase(test.TestCase):
"info": info "info": info
} }
notifier.notify.assert_called_once_with('profiler.generic', payload) notifier.notify.assert_called_once_with(payload)
self.assertEqual(len(prof._name), 0) self.assertEqual(len(prof._name), 0)
self.assertEqual(prof._trace_stack, ["1", "2"]) self.assertEqual(prof._trace_stack, ["1", "2"])