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 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 serialize_context(self, context): _context = context.to_dict() prof = profiler.get() if prof: trace_info = {"hmac_key": prof.hmac_key, "base_id": prof.get_base_id(), "parent_id": prof.get_id()} _context.update({"trace_info": trace_info}) return _context
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_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 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 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.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 _init_profiler(self, context): """Inits the profiler.""" if not CONF.openstack.enable_profiler: return if context is not 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.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 collect_profiler_info(): p = profiler.get() if p: return { "hmac_key": p.hmac_key, "base_id": p.get_base_id(), "parent_id": p.get_id(), }
def get_trace_id_headers(): """Adds the trace id headers (and any hmac) into provided dictionary.""" p = profiler.get() if p and p.hmac_key: data = {"base_id": p.get_base_id(), "parent_id": p.get_id()} pack = utils.signed_pack(data, p.hmac_key) return {X_TRACE_INFO: pack[0], X_TRACE_HMAC: pack[1]} return {}
def update_trace_headers(keys, **kwargs): trace_headers = web.get_trace_id_headers() trace_info = utils.signed_unpack( trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC], keys) trace_info.update(kwargs) p = profiler.get() trace_data = utils.signed_pack(trace_info, p.hmac_key) return json.dumps({web.X_TRACE_INFO: trace_data[0], web.X_TRACE_HMAC: trace_data[1]})
def _serialize_profile_info(self): prof = profiler.get() trace_info = None if prof: trace_info = { "hmac_key": prof.hmac_key, "base_id": prof.get_base_id(), "parent_id": prof.get_id() } return trace_info
def serialize_context(self, ctxt): _context = ctxt.to_dict() prof = profiler.get() if prof: trace_info = { "hmac_key": prof.hmac_key, "base_id": prof.get_base_id(), "parent_id": prof.get_id() } _context['trace_info'] = trace_info return _context
def traced(request, name, info=None): if info is None: info = {} profiler_instance = profiler.get() if profiler_instance is not None: trace_id = profiler_instance.get_base_id() info['user_id'] = request.user.id with profiler.Trace(name, info=info): yield trace_id else: yield
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 get_trace_id_headers(): """Adds the trace id headers (and any hmac) into provided dictionary.""" p = profiler.get() if p and p.hmac_key: data = {"base_id": p.get_base_id(), "parent_id": p.get_id()} pack = utils.signed_pack(data, p.hmac_key) return { "X-Trace-Info": pack[0], "X-Trace-HMAC": pack[1] } return {}
def serialize_context(ctxt): _context = ctxt.to_dict() prof = profiler.get() if prof: trace_info = { "hmac_key": prof.hmac_key, "base_id": prof.get_base_id(), "parent_id": prof.get_id() } _context.update({"trace_info": trace_info}) return _context
def update_trace_headers(keys, **kwargs): trace_headers = web.get_trace_id_headers() trace_info = utils.signed_unpack( trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC], keys) trace_info.update(kwargs) p = profiler.get() trace_data = utils.signed_pack(trace_info, p.hmac_key) trace_data = [key.decode() if isinstance(key, bytes) else key for key in trace_data] return json.dumps({web.X_TRACE_INFO: trace_data[0], web.X_TRACE_HMAC: trace_data[1]})
def serialize_context(self, context): ctx = context.to_dict() pfr = profiler.get() if pfr: ctx['trace_info'] = { "hmac_key": pfr.hmac_key, "base_id": pfr.get_base_id(), "parent_id": pfr.get_id() } return ctx
def update_trace_headers(keys, **kwargs): trace_headers = web.get_trace_id_headers() trace_info = utils.signed_unpack( trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC], keys) trace_info.update(kwargs) p = profiler.get() trace_data = utils.signed_pack(trace_info, p.hmac_key) if six.PY3: trace_data = [key.decode() if isinstance(key, six.binary_type) else key for key in trace_data] return json.dumps({web.X_TRACE_INFO: trace_data[0], web.X_TRACE_HMAC: trace_data[1]})
def serialize_context(self, context): ctx = self._serializer.serialize_context(context) \ if self._serializer else context pfr = profiler.get() if pfr: ctx['trace_info'] = { "hmac_key": pfr.hmac_key, "base_id": pfr.get_base_id(), "parent_id": pfr.get_id() } return ctx
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 _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_get_profiler_not_inited(self): profiler._clean() self.assertIsNone(profiler.get())
def process_response(self, request, response): if profiler.get() is not None: profiler.clean() self.clear_profiling_cookies(request, response) return response
def is_profiler_initialized(where): # Instead of returning a single boolean add information so we can # identify which thread produced the result without depending on # queue order. return {where: bool(profiler.get())}