From f55db194876e08ef4671d31c52b8665fb540c00e Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Wed, 10 Feb 2016 14:27:49 -0500 Subject: [PATCH] Profiler: make it possible to run without loading osprofiler This makes it possible to run Cinder without having osprofiler installed. This is primarily to make it easier to debug issues and ensure that osprofiler is not introducing problems when debugging CI failures. Note that this requires disabling osprofiler in api-paste.ini as well. Related-Bug: #1541996 Change-Id: If00451524b87b949a2d5888a1157184f64cd0d59 --- cinder/db/sqlalchemy/api.py | 5 +++-- cinder/rpc.py | 23 +++++++++++++---------- cinder/service.py | 20 +++++++++++++------- cinder/volume/manager.py | 4 ++-- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index e82edd890..2390a46d7 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -33,9 +33,10 @@ from oslo_db import exception as db_exc from oslo_db import options from oslo_db.sqlalchemy import session as db_session from oslo_log import log as logging +from oslo_utils import importutils from oslo_utils import timeutils from oslo_utils import uuidutils -import osprofiler.sqlalchemy +osprofiler_sqlalchemy = importutils.try_import('osprofiler.sqlalchemy') import six import sqlalchemy from sqlalchemy import MetaData @@ -85,7 +86,7 @@ def _create_facade_lazily(): CONF.import_group("profiler", "cinder.service") if CONF.profiler.profiler_enabled: if CONF.profiler.trace_sqlalchemy: - osprofiler.sqlalchemy.add_tracing(sqlalchemy, + osprofiler_sqlalchemy.add_tracing(sqlalchemy, _FACADE.get_engine(), "db") diff --git a/cinder/rpc.py b/cinder/rpc.py index 9ade49d14..a92fc0dd0 100644 --- a/cinder/rpc.py +++ b/cinder/rpc.py @@ -30,7 +30,8 @@ from oslo_config import cfg from oslo_log import log as logging import oslo_messaging as messaging from oslo_serialization import jsonutils -from osprofiler import profiler +from oslo_utils import importutils +profiler = importutils.try_import('osprofiler.profiler') import cinder.context import cinder.exception @@ -123,20 +124,22 @@ class RequestContextSerializer(messaging.Serializer): 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}) + if profiler is not None: + 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 deserialize_context(self, context): trace_info = context.pop("trace_info", None) if trace_info: - profiler.init(**trace_info) + if profiler is not None: + profiler.init(**trace_info) return cinder.context.RequestContext.from_dict(context) diff --git a/cinder/service.py b/cinder/service.py index 08ca100d5..5d03ee55b 100644 --- a/cinder/service.py +++ b/cinder/service.py @@ -31,9 +31,9 @@ from oslo_service import loopingcall from oslo_service import service from oslo_service import wsgi from oslo_utils import importutils -import osprofiler.notifier -from osprofiler import profiler -import osprofiler.web +osprofiler_notifier = importutils.try_import('osprofiler.notifier') +profiler = importutils.try_import('osprofiler.profiler') +osprofiler_web = importutils.try_import('osprofiler.web') from cinder import context from cinder import exception @@ -84,12 +84,18 @@ CONF.register_opts(profiler_opts, group="profiler") def setup_profiler(binary, host): + if (osprofiler_notifier is None or + profiler is None or + osprofiler_web is None): + LOG.debug('osprofiler is not present') + return + if CONF.profiler.profiler_enabled: - _notifier = osprofiler.notifier.create( + _notifier = osprofiler_notifier.create( "Messaging", messaging, context.get_admin_context().to_dict(), rpc.TRANSPORT, "cinder", binary, host) - osprofiler.notifier.set(_notifier) - osprofiler.web.enable(CONF.profiler.hmac_keys) + osprofiler_notifier.set(_notifier) + osprofiler_web.enable(CONF.profiler.hmac_keys) LOG.warning( _LW("OSProfiler is enabled.\nIt means that person who knows " "any of hmac_keys that are specified in " @@ -101,7 +107,7 @@ def setup_profiler(binary, host): "To disable OSprofiler set in cinder.conf:\n" "[profiler]\nprofiler_enabled=false")) else: - osprofiler.web.disable() + osprofiler_web.disable() class Service(service.Service): diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index f78ac4d44..b5b15a658 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -49,7 +49,7 @@ from oslo_utils import importutils from oslo_utils import timeutils from oslo_utils import units from oslo_utils import uuidutils -from osprofiler import profiler +profiler = importutils.try_import('osprofiler.profiler') import six from taskflow import exceptions as tfe @@ -238,7 +238,7 @@ class VolumeManager(manager.SchedulerDependentManager): host=self.host, is_vol_db_empty=vol_db_empty) - if CONF.profiler.profiler_enabled: + if CONF.profiler.profiler_enabled and profiler is not None: self.driver = profiler.trace_cls("driver")(self.driver) try: self.extra_capabilities = jsonutils.loads( -- 2.45.2