Merge "Integrate OSprofiler in Magnum Client"

This commit is contained in:
Jenkins 2017-01-27 19:06:05 +00:00 committed by Gerrit Code Review
commit 8fee8c50cb
5 changed files with 47 additions and 1 deletions

View File

@ -23,11 +23,13 @@ import socket
import ssl
from keystoneauth1 import adapter
from oslo_utils import importutils
import six
import six.moves.urllib.parse as urlparse
from magnumclient import exceptions
osprofiler_web = importutils.try_import("osprofiler.web")
LOG = logging.getLogger(__name__)
USER_AGENT = 'python-magnumclient'
@ -326,6 +328,10 @@ class SessionClient(adapter.LegacyJsonAdapter):
# Copy the kwargs so we can reuse the original in case of redirects
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
kwargs['headers'].setdefault('User-Agent', self.user_agent)
# NOTE(tovin07): osprofiler_web.get_trace_id_headers does not add any
# headers in case if osprofiler is not initialized.
if osprofiler_web:
kwargs['headers'].update(osprofiler_web.get_trace_id_headers())
if self.api_version:
version_string = 'container-infra %s' % self.api_version
kwargs['headers'].setdefault(

View File

@ -30,9 +30,12 @@ import os
import sys
from oslo_utils import encodeutils
from oslo_utils import importutils
from oslo_utils import strutils
import six
profiler = importutils.try_import("osprofiler.profiler")
HAS_KEYRING = False
all_errors = ValueError
try:
@ -422,6 +425,19 @@ class OpenStackMagnumShell(object):
action='store_true',
help=_("Do not verify https connections"))
if profiler:
parser.add_argument('--profile',
metavar='HMAC_KEY',
help='HMAC key to use for encrypting context '
'data for performance profiling of operation. '
'This key should be the value of the HMAC key '
'configured for the OSprofiler middleware in '
'nova; it is specified in the Nova '
'configuration file at "/etc/nova/nova.conf". '
'Without the key, profiling will not be '
'triggered even if OSprofiler is enabled on '
'the server side.')
return parser
def get_subcommand_parser(self, version):
@ -619,6 +635,10 @@ class OpenStackMagnumShell(object):
if args.os_interface.endswith('URL'):
args.os_interface = args.os_interface[:-3]
kwargs = {}
if profiler:
kwargs["profile"] = args.profile
self.cs = client.Client(
cloud=args.os_cloud,
user_id=args.os_user_id,
@ -638,11 +658,17 @@ class OpenStackMagnumShell(object):
interface=args.os_interface,
insecure=args.insecure,
api_version=args.magnum_api_version,
**kwargs
)
self._check_deprecation(args.func, argv)
args.func(self.cs, args)
if profiler and args.profile:
trace_id = profiler.get().get_base_id()
print("To display trace use the command:\n\n"
" osprofiler trace show --html %s " % trace_id)
def _check_deprecation(self, func, argv):
if not hasattr(func, 'deprecated_groups'):
return

View File

@ -215,7 +215,7 @@ class ShellTest(utils.TestCase):
def _expected_client_kwargs(self):
return {
'password': 'password', 'auth_token': None,
'auth_url': self.AUTH_URL,
'auth_url': self.AUTH_URL, 'profile': None,
'cloud': None, 'interface': 'public',
'insecure': False, 'magnum_url': None,
'project_id': None, 'project_name': 'project_name',

View File

@ -16,6 +16,7 @@
from keystoneauth1.exceptions import catalog
from keystoneauth1 import session as ksa_session
import os_client_config
from oslo_utils import importutils
from magnumclient.common import httpclient
from magnumclient.v1 import baymodels
@ -25,6 +26,8 @@ from magnumclient.v1 import cluster_templates
from magnumclient.v1 import clusters
from magnumclient.v1 import mservices
profiler = importutils.try_import("osprofiler.profiler")
DEFAULT_SERVICE_TYPE = 'container-infra'
LEGACY_DEFAULT_SERVICE_TYPE = 'container'
@ -198,3 +201,13 @@ class Client(object):
self.cluster_templates = \
cluster_templates.ClusterTemplateManager(self.http_client)
self.mservices = mservices.MServiceManager(self.http_client)
profile = kwargs.pop("profile", None)
if profiler and profile:
# Initialize the root of the future trace: the created trace ID
# will be used as the very first parent to which all related
# traces will be bound to. The given HMAC key must correspond to
# the one set in magnum-api magnum.conf, otherwise the latter
# will fail to check the request signature and will skip
# initialization of osprofiler on the server side.
profiler.init(profile)

View File

@ -10,6 +10,7 @@ python-subunit>=0.0.18 # Apache-2.0/BSD
sphinx!=1.3b1,<1.4,>=1.2.1 # BSD
oslosphinx>=4.7.0 # Apache-2.0
oslotest>=1.10.0 # Apache-2.0
osprofiler>=1.4.0 # Apache-2.0
testrepository>=0.0.18 # Apache-2.0/BSD
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=1.4.0 # MIT