From d48e9670270305beeaba57ffcfd61b14792d8097 Mon Sep 17 00:00:00 2001 From: Alexey Yelistratov Date: Thu, 12 May 2016 19:55:08 +0300 Subject: [PATCH] Support new osprofiler API Add connection_string config option that allows to specify the address of the notifier's storage engine. Co-Authored-By: Tovin Seven Change-Id: Ifdfe2649020c610c76b318351e9448ad777352d5 Depends-On: I77d0d3b5e9e725507916724dcd28d5fbc6f7f5c7 --- cinder/service.py | 22 ++++---- cinder/tests/unit/test_setup_profiler.py | 52 +++++++++++++++++++ .../new-osprofiler-call-0bb1a305c8e8f9cc.yaml | 7 +++ 3 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 cinder/tests/unit/test_setup_profiler.py create mode 100644 releasenotes/notes/new-osprofiler-call-0bb1a305c8e8f9cc.yaml diff --git a/cinder/service.py b/cinder/service.py index 06a94d15882..69796c1953e 100644 --- a/cinder/service.py +++ b/cinder/service.py @@ -31,9 +31,8 @@ from oslo_service import loopingcall from oslo_service import service from oslo_service import wsgi from oslo_utils import importutils -osprofiler_notifier = importutils.try_import('osprofiler.notifier') +osprofiler_initializer = importutils.try_import('osprofiler.initializer') profiler = importutils.try_import('osprofiler.profiler') -osprofiler_web = importutils.try_import('osprofiler.web') profiler_opts = importutils.try_import('osprofiler.opts') @@ -87,19 +86,20 @@ if profiler_opts: def setup_profiler(binary, host): - if (osprofiler_notifier is None or + if (osprofiler_initializer is None or profiler is None or - osprofiler_web is None or profiler_opts is None): LOG.debug('osprofiler is not present') return if CONF.profiler.enabled: - _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_initializer.init_from_conf( + conf=CONF, + context=context.get_admin_context().to_dict(), + project="cinder", + service=binary, + host=host + ) LOG.warning( _LW("OSProfiler is enabled.\nIt means that person who knows " "any of hmac_keys that are specified in " @@ -108,10 +108,8 @@ def setup_profiler(binary, host): "is no security issue. Note that even if person can " "trigger profiler, only admin user can retrieve trace " "information.\n" - "To disable OSprofiler set in cinder.conf:\n" + "To disable OSProfiler set in cinder.conf:\n" "[profiler]\nenabled=false")) - else: - osprofiler_web.disable() class Service(service.Service): diff --git a/cinder/tests/unit/test_setup_profiler.py b/cinder/tests/unit/test_setup_profiler.py new file mode 100644 index 00000000000..f29ba96f8db --- /dev/null +++ b/cinder/tests/unit/test_setup_profiler.py @@ -0,0 +1,52 @@ +# Copyright 2016 Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from cinder import service +from cinder import test + + +class SetupProfilerTestCase(test.TestCase): + def setUp(self): + super(SetupProfilerTestCase, self).setUp() + service.osprofiler_initializer = mock.MagicMock() + service.profiler = mock.MagicMock() + service.profiler_opts = mock.MagicMock() + service.osprofiler_initializer.init_from_conf = mock.MagicMock() + + def test_profiler_not_present(self): + service.profiler = None + service.LOG.debug = mock.MagicMock() + service.setup_profiler("cinder-volume", "localhost") + service.LOG.debug.assert_called_once_with("osprofiler is not present") + + @mock.patch("cinder.service.context") + def test_profiler_enabled(self, context): + service.CONF.profiler.enabled = True + return_value = {"Meaning Of Life": 42} + context.get_admin_context().to_dict.return_value = return_value + service.setup_profiler("cinder-volume", "localhost") + service.osprofiler_initializer.init_from_conf.assert_called_once_with( + conf=service.CONF, + context=return_value, + project="cinder", + service="cinder-volume", + host="localhost") + + def test_profiler_disabled(self): + service.CONF.profiler.enabled = False + service.setup_profiler("cinder-volume", "localhost") + service.osprofiler_initializer.init_from_conf.assert_not_called() diff --git a/releasenotes/notes/new-osprofiler-call-0bb1a305c8e8f9cc.yaml b/releasenotes/notes/new-osprofiler-call-0bb1a305c8e8f9cc.yaml new file mode 100644 index 00000000000..15d9819c62e --- /dev/null +++ b/releasenotes/notes/new-osprofiler-call-0bb1a305c8e8f9cc.yaml @@ -0,0 +1,7 @@ +--- +upgrade: + - New config option added. ``"connection_string"`` in [profiler] + section is used to specify OSProfiler driver connection + string, for example, + ``"connection_string = messaging://"``, + ``"connection_string = mongodb://localhost:27017"``