Add profiling support to manilaclient

To be able to create profiling traces for Manila, client should be
able to send special HTTP header that contains trace info.

Don't worry no security issue here, trace information is signed by HMAC
key that is setted in api-paste.ini. So only person that knows HMAC key
is able to send proper header.

Main patch (in Manila) is:
https://review.opendev.org/c/openstack/manila/+/769729

Change-Id: I20cdb404c6df1be51321afca90aac33ec7eb3475
This commit is contained in:
kpdev 2021-01-07 15:01:25 +01:00
parent 68052cd26b
commit a34aca6b3d
3 changed files with 45 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import logging
from urllib import parse
from oslo_serialization import jsonutils
from oslo_utils import importutils
from oslo_utils import strutils
import re
import requests
@ -33,6 +34,11 @@ try:
except ImportError:
from time import sleep # noqa
try:
osprofiler_web = importutils.try_import("osprofiler.web")
except Exception:
pass
class HTTPClient(object):
"""HTTP Client class used by multiple clients.
@ -108,6 +114,9 @@ class HTTPClient(object):
options = copy.deepcopy(self.request_options)
if osprofiler_web:
headers.update(osprofiler_web.get_trace_id_headers())
if 'body' in kwargs:
headers['Content-Type'] = 'application/json'
options['data'] = jsonutils.dumps(kwargs['body'])

View File

@ -28,12 +28,14 @@ import pkgutil
import sys
from oslo_utils import encodeutils
from oslo_utils import importutils
import six
from manilaclient import api_versions
from manilaclient import client
from manilaclient.common import cliutils
from manilaclient.common import constants
from manilaclient import exceptions as exc
import manilaclient.extension
from manilaclient.v2 import shell as shell_v2
@ -44,6 +46,11 @@ DEFAULT_MAJOR_OS_SHARE_API_VERSION = "2"
V1_MAJOR_VERSION = '1'
V2_MAJOR_VERSION = '2'
try:
osprofiler_profiler = importutils.try_import("osprofiler.profiler")
except Exception:
pass
logger = logging.getLogger(__name__)
@ -347,6 +354,19 @@ class OpenStackManilaShell(object):
parser.add_argument('--os_cert',
help=argparse.SUPPRESS)
if osprofiler_profiler:
parser.add_argument('--profile',
metavar='HMAC_KEY',
default=cliutils.env('OS_PROFILE'),
help='HMAC key to use for encrypting '
'context data for performance profiling '
'of operation. This key needs to match the '
'one configured on the manila api server. '
'Without key the profiling will not be '
'triggered even if osprofiler is enabled '
'on server side. Defaults to '
'env[OS_PROFILE].')
parser.set_defaults(func=self.do_help)
parser.set_defaults(command='')
@ -577,8 +597,18 @@ class OpenStackManilaShell(object):
argv,
options)
profile = osprofiler_profiler and options.profile
if profile:
osprofiler_profiler.init(options.profile)
args.func(self.cs, args)
if profile:
trace_id = osprofiler_profiler.get().get_base_id()
print("Profiling trace ID: %s" % trace_id)
print("To display trace use next command:\n"
"osprofiler trace show --html %s " % trace_id)
def _discover_client(self,
current_client,
os_api_version,

View File

@ -0,0 +1,6 @@
---
features:
- |
OS profiler is now supported within the v2 client and the
manilaclient shell.