Skip to content

Commit 16f0083

Browse files
committed
Add shell --profile option to trigger osprofiler from CLI
This will allow to trigger profiling of various services that allow it currently and which APIs support is added to openstackclient. Cinder and Glance have osprofiler support already, Nova and Keystone are in progress. To use this functionality osprofiler (and its storage backend) needs to be installed in the environment. If so, you will be able to trigger profiling via the following command, for example: $ openstack --profile SECRET_KEY user list At the end of output there will be message with <trace_id>, and to plot nice HTML graphs the following command should be used: $ osprofiler trace show <trace_id> --html --out result.html Related Keystone change: https://review.openstack.org/#/c/103368/ Related Nova change: https://review.openstack.org/#/c/254703/ The similar change to the keystoneclient (https://review.openstack.org/#/c/255308/) was abandoned as new CLI extenstions are not more accepted to python-keystoneclient. Change-Id: I3d6ac613e5da70619d0a4781e5d066fde073b407
1 parent 624c39a commit 16f0083

5 files changed

Lines changed: 76 additions & 0 deletions

File tree

doc/source/man/openstack.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ OPTIONS
123123
:option:`--os-interface` <interface>
124124
Interface type. Valid options are `public`, `admin` and `internal`.
125125

126+
:option: `--profile` <hmac-key>
127+
HMAC key to use for encrypting context data for performance profiling of
128+
requested operation. This key should be the value of one of the HMAC keys
129+
defined in the configuration files of OpenStack services, user would like
130+
to trace through.
131+
126132
:option:`--log-file` <LOGFILE>
127133
Specify a file to log output. Disabled by default.
128134

openstackclient/shell.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from cliff import command
2626
from cliff import complete
2727
from cliff import help
28+
from oslo_utils import importutils
2829
from oslo_utils import strutils
2930

3031
import openstackclient
@@ -37,6 +38,8 @@
3738

3839
from os_client_config import config as cloud_config
3940

41+
osprofiler_profiler = importutils.try_import("osprofiler.profiler")
42+
4043

4144
DEFAULT_DOMAIN = 'default'
4245

@@ -101,6 +104,8 @@ def __init__(self):
101104
self.client_manager = None
102105
self.command_options = None
103106

107+
self.do_profile = False
108+
104109
def configure_logging(self):
105110
"""Configure logging for the app."""
106111
self.log_configurator = logs.LogConfigurator(self.options)
@@ -125,6 +130,39 @@ def run(self, argv):
125130
finally:
126131
self.log.info("END return value: %s", ret_val)
127132

133+
def init_profile(self):
134+
self.do_profile = osprofiler_profiler and self.options.profile
135+
if self.do_profile:
136+
osprofiler_profiler.init(self.options.profile)
137+
138+
def close_profile(self):
139+
if self.do_profile:
140+
trace_id = osprofiler_profiler.get().get_base_id()
141+
142+
# NOTE(dbelova): let's use warning log level to see these messages
143+
# printed. In fact we can define custom log level here with value
144+
# bigger than most big default one (CRITICAL) or something like
145+
# that (PROFILE = 60 for instance), but not sure we need it here.
146+
self.log.warning("Trace ID: %s" % trace_id)
147+
self.log.warning("To display trace use next command:\n"
148+
"osprofiler trace show --html %s " % trace_id)
149+
150+
def run_subcommand(self, argv):
151+
self.init_profile()
152+
try:
153+
ret_value = super(OpenStackShell, self).run_subcommand(argv)
154+
finally:
155+
self.close_profile()
156+
return ret_value
157+
158+
def interact(self):
159+
self.init_profile()
160+
try:
161+
ret_value = super(OpenStackShell, self).run_subcommand()
162+
finally:
163+
self.close_profile()
164+
return ret_value
165+
128166
def build_option_parser(self, description, version):
129167
parser = super(OpenStackShell, self).build_option_parser(
130168
description,
@@ -190,6 +228,19 @@ def build_option_parser(self, description, version):
190228
help="Print API call timing info",
191229
)
192230

231+
# osprofiler HMAC key argument
232+
if osprofiler_profiler:
233+
parser.add_argument('--profile',
234+
metavar='hmac-key',
235+
help='HMAC key to use for encrypting context '
236+
'data for performance profiling of operation. '
237+
'This key should be the value of one of the '
238+
'HMAC keys configured in osprofiler '
239+
'middleware in the projects user would like '
240+
'to profile. It needs to be specified in '
241+
'configuration files of the required '
242+
'projects.')
243+
193244
return clientmanager.build_plugin_option_parser(parser)
194245

195246
def initialize_app(self, argv):

openstackclient/tests/test_shell.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
'--os-default-domain': (DEFAULT_DOMAIN_NAME, True, True),
110110
'--os-cacert': ('/dev/null', True, True),
111111
'--timing': (True, True, False),
112+
'--profile': ('SECRET_KEY', True, False),
112113
'--os-interface': (DEFAULT_INTERFACE, True, True)
113114
}
114115

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
features:
3+
- |
4+
OSprofiler support was added. To initiate OpenStack request tracing
5+
``--profile <HMAC_KEY>`` option needs to be added to the CLI command. This
6+
key needs to present one of the secret keys defined in the OpenStack
7+
projects configuration files (if there is a wish to generate cross-project
8+
trace, the chosen key needs to be presented in all these configuration
9+
files). By default all OpenStack projects, that support OSprofiler,
10+
are using ``SECRET_KEY`` HMAC key.
11+
12+
To use tracing functionality OSprofiler (and its storage backend)
13+
needs to be installed in the environment. If so, you will be able to
14+
trigger profiling via `openstack --profile SECRET_KEY <operation>` command.
15+
At the end of output there will be message with <trace_id>, and to plot
16+
human-readable HTML chart the following command should be used -
17+
``osprofiler trace show <trace_id> --html --out result.html``.

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ testrepository>=0.0.18 # Apache-2.0/BSD
1717
testtools>=1.4.0 # MIT
1818
WebOb>=1.2.3 # MIT
1919
tempest-lib>=0.14.0 # Apache-2.0
20+
osprofiler>=1.1.0 # Apache-2.0
2021

2122
# Install these to generate sphinx autodocs
2223
python-barbicanclient>=3.3.0 # Apache-2.0

0 commit comments

Comments
 (0)