def process_request(self, request): if 'profile_page' in request.COOKIES: hmac_key = PROFILER_SETTINGS.get('keys')[0] profiler.init(hmac_key) for hdr_key, hdr_value in web.get_trace_id_headers().items(): request.META[hdr_key] = hdr_value return None
def __call__(self, request): if (_ENABLED is not None and not _ENABLED or _ENABLED is None and not self.enabled): return request.get_response(self.application) trace_info = utils.signed_unpack(request.headers.get(X_TRACE_INFO), request.headers.get(X_TRACE_HMAC), _HMAC_KEYS or self.hmac_keys) if not self._trace_is_valid(trace_info): return request.get_response(self.application) profiler.init(**trace_info) info = { "request": { "path": request.path, "query": request.query_string, "method": request.method, "scheme": request.scheme } } try: with profiler.Trace(self.name, info=info): return request.get_response(self.application) finally: profiler.clean()
def _init_profiler(self, context): """Inits the profiler.""" if not CONF.openstack.enable_profiler: return if context is not None: cred = None profiler_hmac_key = None profiler_conn_str = None if context.get("admin"): cred = context["admin"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if context.get("user"): cred = context["user"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if profiler_hmac_key is None: return profiler.init(profiler_hmac_key) trace_id = profiler.get().get_base_id() complete_data = {"title": "OSProfiler Trace-ID", "chart_plugin": "OSProfiler", "data": {"trace_id": [trace_id], "conn_str": profiler_conn_str}} self.add_output(complete=complete_data)
def _init_profiler(self, context): """Inits the profiler.""" if not CONF.benchmark.enable_profiler: return if context is not None: cred = None profiler_hmac_key = None if context.get("admin"): cred = context["admin"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key if context.get("user"): cred = context["user"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key if profiler_hmac_key is None: return profiler.init(profiler_hmac_key) trace_id = profiler.get().get_base_id() self.add_output( complete={ "title": "OSProfiler Trace-ID", "chart_plugin": "TextArea", "data": [trace_id] })
def __call__(self, request): if (_ENABLED is not None and not _ENABLED or _ENABLED is None and not self.enabled): return request.get_response(self.application) trace_info = utils.signed_unpack(request.headers.get(X_TRACE_INFO), request.headers.get(X_TRACE_HMAC), _HMAC_KEYS or self.hmac_keys) if not self._trace_is_valid(trace_info): return request.get_response(self.application) profiler.init(**trace_info) info = { "request": { "path": request.path, "query": request.query_string, "method": request.method, "scheme": request.scheme } } try: with profiler.Trace(self.name, info=info): return request.get_response(self.application) finally: profiler._clean()
def process_view(self, request, view_func, view_args, view_kwargs): # do not profile ajax requests for now if not self.is_enabled(request) or request.is_ajax(): return None trace_info = profiler_utils.signed_unpack( request.META.get('X-Trace-Info'), request.META.get('X-Trace-HMAC'), self.hmac_keys) if not self._trace_is_valid(trace_info): return None profiler.init(**trace_info) info = { 'request': { 'path': request.path, 'query': request.GET.urlencode(), 'method': request.method, 'scheme': request.scheme } } with api.traced(request, view_func.__name__, info) as trace_id: response = view_func(request, *view_args, **view_kwargs) url = reverse('horizon:developer:profiler:index') message = safestring.mark_safe( _('Traced with id %(id)s. Go to <a href="%(url)s">page</a>') % {'id': trace_id, 'url': url}) messages.info(request, message) return response
def process_request(self, request): if 'profile_page' in request.COOKIES: hmac_key = PROFILER_CONF.get('keys')[0] profiler.init(hmac_key) for hdr_key, hdr_value in web.get_trace_id_headers().items(): request.META[hdr_key] = hdr_value return None
def test_get_report(self): initializer.init_from_conf(CONF, None, self.PROJECT, self.SERVICE, "host") profiler.init("SECRET_KEY", project=self.PROJECT, service=self.SERVICE) foo = DriverTestCase.Foo() foo.bar(1) engine = base.get_driver(CONF.profiler.connection_string, project=self.PROJECT, service=self.SERVICE, host="host", conf=CONF) base_id = profiler.get().get_base_id() res = engine.get_report(base_id) self.assertEqual("total", res["info"]["name"]) self.assertEqual(2, res["stats"]["rpc"]["count"]) self.assertEqual(1, len(res["children"])) cbar = res["children"][0] self._assert_child_dict( cbar, base_id, base_id, "rpc", "osprofiler.tests.functional.test_driver.Foo.bar") self.assertEqual(1, len(cbar["children"])) cbaz = cbar["children"][0] self._assert_child_dict( cbaz, base_id, cbar["trace_id"], "rpc", "osprofiler.tests.functional.test_driver.Foo.baz")
def test_list_traces(self): # initialize profiler notifier (the same way as in services) initializer.init_from_conf( CONF, {}, self.PROJECT, self.SERVICE, "host") profiler.init("SECRET_KEY") # grab base_id base_id = profiler.get().get_base_id() # execute profiled code foo = Foo() foo.bar(1) # instantiate report engine (the same way as in osprofiler CLI) engine = base.get_driver(CONF.profiler.connection_string, project=self.PROJECT, service=self.SERVICE, host="host", conf=CONF) # generate the report traces = engine.list_traces() LOG.debug("Collected traces: %s", traces) # ensure trace with base_id is in the list of traces self.assertIn(base_id, [t["base_id"] for t in traces])
def _init_profiler(self, context): """Inits the profiler.""" if not CONF.openstack.enable_profiler: return if context is not None: cred = None profiler_hmac_key = None profiler_conn_str = None if context.get("admin"): cred = context["admin"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if context.get("user"): cred = context["user"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if profiler_hmac_key is None: return profiler.init(profiler_hmac_key) trace_id = profiler.get().get_base_id() complete_data = { "title": "OSProfiler Trace-ID", "chart_plugin": "OSProfiler", "data": { "trace_id": [trace_id], "conn_str": profiler_conn_str } } self.add_output(complete=complete_data)
def thread_with_no_leaked_profiler(): if init_profiler: profiler.init(hmac_key='fake secret') self.spawn_variant( lambda: q.put(is_profiler_initialized('in-child'))) q.put(is_profiler_initialized('in-parent'))
def process_view(self, request, view_func, view_args, view_kwargs): if not self.is_enabled(request): return None trace_info = profiler_utils.signed_unpack( request.META.get('X-Trace-Info'), request.META.get('X-Trace-HMAC'), self.hmac_keys) if not self._trace_is_valid(trace_info): return None profiler.init(**trace_info) info = { 'request': { 'path': request.path, 'query': request.GET.urlencode(), 'method': request.method, 'scheme': request.scheme } } with api.traced(request, view_func.__name__, info) as trace_id: request.META[api.ROOT_HEADER] = profiler.get().get_id() response = view_func(request, *view_args, **view_kwargs) url = reverse('horizon:developer:profiler:index') message = safestring.mark_safe( _('Traced with id %(id)s. Go to <a href="%(url)s">page</a>') % { 'id': trace_id, 'url': url }) messages.info(request, message) return response
def deserialize_context(self, context): trace_info = context.pop('trace_info', None) if trace_info: profiler.init(**trace_info) return self._serializer.deserialize_context(context)\ if self._serializer else context
def test_get_trace_id_headers(self): profiler.init("key", base_id="y", parent_id="z") headers = web.get_trace_id_headers() self.assertEqual(sorted(headers.keys()), sorted(["X-Trace-Info", "X-Trace-HMAC"])) trace_info = utils.signed_unpack(headers["X-Trace-Info"], headers["X-Trace-HMAC"], ["key"]) self.assertIn("hmac_key", trace_info) self.assertEqual("key", trace_info.pop("hmac_key")) self.assertEqual({"parent_id": "z", "base_id": "y"}, trace_info)
def deserialize_context(self, context): trace_info = context.pop('trace_info', None) if trace_info: profiler.init(**trace_info) ctx = MistralContext(**context) set_ctx(ctx) return ctx
def deserialize_context(self, ctxt): rpc_ctxt_dict = ctxt.copy() trace_info = rpc_ctxt_dict.pop("trace_info", None) if trace_info: profiler.init(**trace_info) user_id = rpc_ctxt_dict.pop('user_id', None) if not user_id: user_id = rpc_ctxt_dict.pop('user', None) tenant_id = rpc_ctxt_dict.pop('tenant_id', None) if not tenant_id: tenant_id = rpc_ctxt_dict.pop('project_id', None) return context.Context(user_id, tenant_id, **rpc_ctxt_dict)
def process_request(self, request): if self.is_async_profiling(request): for src_header, dst_header in self.profiler_headers: request.META[dst_header] = request.META.get(src_header) return None if 'profile_page' in request.COOKIES: hmac_key = PROFILER_CONF.get('keys')[0] profiler.init(hmac_key) for hdr_key, hdr_value in web.get_trace_id_headers().items(): request.META[hdr_key] = hdr_value return None
def process_request(self, request): if self.is_async_profiling(request): for src_header, dst_header in self.profiler_headers: request.META[dst_header] = request.META.get(src_header) return None if 'profile_page' in request.COOKIES: hmac_key = horizon_settings.get_dict_config( 'OPENSTACK_PROFILER', 'keys')[0] profiler.init(hmac_key) for hdr_key, hdr_value in web.get_trace_id_headers().items(): request.META[hdr_key] = hdr_value return None
def test_get_profiler_and_init(self): p = profiler.init("secret", base_id="1", parent_id="2") self.assertEqual(profiler.get(), p) self.assertEqual(p.get_base_id(), "1") # NOTE(boris-42): until we make first start we don't have self.assertEqual(p.get_id(), "2")
def __call__(self, request): if not self.enabled: return request.get_response(self.application) trace_info_enc = request.headers.get("X-Trace-Info") trace_hmac = request.headers.get("X-Trace-HMAC") if trace_hmac: trace_hmac = trace_hmac.strip() if trace_info_enc: trace_raw = base64.b64decode(trace_info_enc) try: validate_hmac(trace_raw, trace_hmac, self.hmac_key) except IOError: pass else: trace_info = json.loads(trace_raw) p = profiler.init(trace_info.get("base_id"), trace_info.get("parent_id"), self.hmac_key) 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)
def _within_new_thread(): # This is a new thread so we need to init a profiler again. if cfg.CONF.profiler.enabled: profiler.init(cfg.CONF.profiler.hmac_keys) old_auth_ctx = context.ctx() if context.has_ctx() else None context.set_ctx(auth_ctx) try: if tx_queue: _process_tx_queue(tx_queue) if non_tx_queue: _process_non_tx_queue(non_tx_queue) finally: context.set_ctx(old_auth_ctx)
def _get_profiler_instance(): # If profiler does not exist or not enabled if profiler is None or not CONF.profiler.enabled: return None instance = profiler.get() # Try to initialize an instance if instance is None: instance = profiler.init(CONF.profiler.hmac_keys) LOG.debug("Initialized osprofiler, base trace ID: %s", instance.get_id()) return instance
def __call__(self, environ, start_response): request = webob.Request(environ) trace_info = utils.signed_unpack(request.headers.get(web.X_TRACE_INFO), request.headers.get(web.X_TRACE_HMAC), self.hmac_keys) if not self._trace_is_valid(trace_info): return self.application(environ, start_response) profiler.init(**trace_info) info = { "request": { "path": request.path, "query": request.query_string, "method": request.method, "scheme": request.scheme } } with profiler.Trace(self.name, info=info): return self.application(environ, start_response)
def _invoke_job(auth_ctx, func, args): # Scheduler runs jobs in an separate thread that's neither related # to an RPC nor a REST request processing thread. So we need to # initialize a profiler specifically for this thread. if cfg.CONF.profiler.enabled: profiler.init(cfg.CONF.profiler.hmac_keys) ctx_serializer = context.RpcContextSerializer() try: # Set the correct context for the function. ctx_serializer.deserialize_context(auth_ctx) # Invoke the function. func(**args) except Exception as e: LOG.exception("Scheduled job failed, method: %s, exception: %s", func, e) finally: # Remove context. context.set_ctx(None)
def test_get_report(self): # initialize profiler notifier (the same way as in services) initializer.init_from_conf( CONF, {}, self.PROJECT, self.SERVICE, "host") profiler.init("SECRET_KEY") # grab base_id base_id = profiler.get().get_base_id() # execute profiled code foo = Foo() foo.bar(1) # instantiate report engine (the same way as in osprofiler CLI) engine = base.get_driver(CONF.profiler.connection_string, project=self.PROJECT, service=self.SERVICE, host="host", conf=CONF) # generate the report report = engine.get_report(base_id) LOG.debug("OSProfiler report: %s", report) # verify the report self.assertEqual("total", report["info"]["name"]) self.assertEqual(2, report["stats"]["rpc"]["count"]) self.assertEqual(1, len(report["children"])) cbar = report["children"][0] self._assert_child_dict( cbar, base_id, base_id, "rpc", "osprofiler.tests.functional.test_driver.Foo.bar") self.assertEqual(1, len(cbar["children"])) cbaz = cbar["children"][0] self._assert_child_dict( cbaz, base_id, cbar["trace_id"], "rpc", "osprofiler.tests.functional.test_driver.Foo.baz")
def test_get_report(self): # initialize profiler notifier (the same way as in services) initializer.init_from_conf(CONF, {}, self.PROJECT, self.SERVICE, "host") profiler.init("SECRET_KEY") # grab base_id base_id = profiler.get().get_base_id() # execute profiled code foo = Foo() foo.bar(1) # instantiate report engine (the same way as in osprofiler CLI) engine = base.get_driver(CONF.profiler.connection_string, project=self.PROJECT, service=self.SERVICE, host="host", conf=CONF) # generate the report report = engine.get_report(base_id) LOG.debug("OSProfiler report: %s", report) # verify the report self.assertEqual("total", report["info"]["name"]) self.assertEqual(2, report["stats"]["rpc"]["count"]) self.assertEqual(1, len(report["children"])) cbar = report["children"][0] self._assert_child_dict( cbar, base_id, base_id, "rpc", "osprofiler.tests.functional.test_driver.Foo.bar") self.assertEqual(1, len(cbar["children"])) cbaz = cbar["children"][0] self._assert_child_dict( cbaz, base_id, cbar["trace_id"], "rpc", "osprofiler.tests.functional.test_driver.Foo.baz")
def __call__(self, request): if _DISABLED or not self.enabled: return request.get_response(self.application) trace_info = utils.signed_unpack(request.headers.get("X-Trace-Info"), request.headers.get("X-Trace-HMAC"), self.hmac_keys) if not self._trace_is_valid(trace_info): return request.get_response(self.application) profiler.init(**trace_info) info = { "request": { "host_url": request.host_url, "path": request.path, "query": request.query_string, "method": request.method, "scheme": request.scheme } } with profiler.Trace(self.name, info=info): return request.get_response(self.application)
def _init_profiler(self, context): """Inits the profiler.""" if not CONF.openstack.enable_profiler: return # False statement here means that Scenario class is used outside the # runner as some kind of utils if context is not None and "iteration" in context: profiler_hmac_key = None profiler_conn_str = None if context.get("admin"): cred = context["admin"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if context.get("user"): cred = context["user"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if profiler_hmac_key is None: return profiler.init(profiler_hmac_key) trace_id = profiler.get().get_base_id() complete_data = { "title": "OSProfiler Trace-ID", "chart_plugin": "OSProfiler", "data": { "trace_id": trace_id, "conn_str": profiler_conn_str, "taskID": context["task"]["uuid"], "workload_uuid": context["owner_id"], "iteration": context["iteration"] } } self.add_output(complete=complete_data)
def _init_profiler(self, context): """Inits the profiler.""" LOG.warning("DEBUG: _init_profiler entry point") if not CONF.openstack.enable_profiler: return if context is not None: cred = None profiler_hmac_key = None profiler_conn_str = None if context.get("admin"): cred = context["admin"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str if context.get("user"): cred = context["user"]["credential"] if cred.profiler_hmac_key is not None: profiler_hmac_key = cred.profiler_hmac_key profiler_conn_str = cred.profiler_conn_str # NOTE(jethro): changes to add the sampling decision if profiler_hmac_key is None: if is_sampled(SAMPLING_RATE) is True: LOG.warning("DEBUG: sampled to enable tracing") profiler_hmac_key = "Devstack1" pass else: return profiler.init(profiler_hmac_key) trace_id = profiler.get().get_base_id() print("TRACE: ID %s") % trace_id LOG.info("TRACE: ID %s" % (trace_id)) complete_data = {"title": "OSProfiler Trace-ID", "chart_plugin": "OSProfiler", "data": {"trace_id": [trace_id], "conn_str": profiler_conn_str}} self.add_output(complete=complete_data)
def test_wsgi_middleware(self): request = mock.MagicMock() request.get_response.return_value = "yeah!" request.url = "someurl" trace_info = {"base_id": "1", "parent_id": "2"} request.headers = { "a": "1", "b": "2", "X-Trace-Info": base64.b64encode(json.dumps(trace_info)) } p = profiler.init() p.start = mock.MagicMock() p.stop = mock.MagicMock() with mock.patch("osprofiler.web.profiler.init") as mock_profiler_init: mock_profiler_init.return_value = p middleware = web.WsgiMiddleware("app", service_name="ss", name="WSGI", enabled=True) self.assertEqual("yeah!", middleware(request)) mock_profiler_init.assert_called_once_with("1", "2", "ss") p.start.assert_called_once_with("WSGI", info={"url": request.url}) p.stop.assert_called_once_with()
def deserialize_context(self, ctxt): rpc_ctxt_dict = ctxt.copy() trace_info = rpc_ctxt_dict.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return context.Context.from_dict(rpc_ctxt_dict)
def _deserialize_context(self, context): trace_info = context.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return TroveContext.from_dict(context)
def test_get_trace_id_headers_no_hmac(self): profiler.init(None, base_id="y", parent_id="z") headers = web.get_trace_id_headers() self.assertEqual(headers, {})
def deserialize_context(self, context): trace_info = context.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return TroveContext.from_dict(context)
def deserialize_context(self, context): trace_info = context.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return <project_name>.context.RequestContext.from_dict(context)
def _start_with_trace(self, cnxt, trace, func, *args, **kwargs): if trace: profiler.init(**trace) if cnxt is not None: cnxt.update_store() return func(*args, **kwargs)
def test_start(self): p = profiler.init("secret", base_id="1", parent_id="2") p.start = mock.MagicMock() profiler.start("name", info="info") p.start.assert_called_once_with("name", info="info")
def test_stop(self): p = profiler.init("secret", base_id="1", parent_id="2") p.stop = mock.MagicMock() profiler.stop(info="info") p.stop.assert_called_once_with(info="info")
def wrapper(*args, **kwargs): if profiler_info: profiler.init(**profiler_info) return func(*args, **kwargs)
def _start_with_trace(self, trace, func, *args, **kwargs): if trace: profiler.init(**trace) return func(*args, **kwargs)
def deserialize_context(ctxt): trace_info = ctxt.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return context.RequestContext.from_dict(ctxt)
def deserialize_context(self, context): trace_info = context.pop("trace_info", None) if trace_info: profiler.init(**trace_info) return cinder.context.RequestContext.from_dict(context)