diff options
| author | Jenkins <jenkins@review.openstack.org> | 2016-02-26 19:49:21 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2016-02-26 19:49:21 +0000 |
| commit | 2819450be5d4fa57c6efdb4cd225f59bab894fba (patch) | |
| tree | 2841300de1f8857dd32b9339f4eceaa7c0b79b2f /openstackclient/shell.py | |
| parent | b5b5fdd78ad0d191bdf07a56293fecaa43002e75 (diff) | |
| parent | 16f00833a70893979ccdf7ffb7f6e1e7653cdc24 (diff) | |
| download | python-openstackclient-2819450be5d4fa57c6efdb4cd225f59bab894fba.tar.gz | |
Merge "Add shell --profile option to trigger osprofiler from CLI"
Diffstat (limited to 'openstackclient/shell.py')
| -rw-r--r-- | openstackclient/shell.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/openstackclient/shell.py b/openstackclient/shell.py index dfec40af..659bbee7 100644 --- a/openstackclient/shell.py +++ b/openstackclient/shell.py @@ -25,6 +25,7 @@ from cliff import app from cliff import command from cliff import complete from cliff import help +from oslo_utils import importutils from oslo_utils import strutils import openstackclient @@ -37,6 +38,8 @@ from openstackclient.common import utils from os_client_config import config as cloud_config +osprofiler_profiler = importutils.try_import("osprofiler.profiler") + DEFAULT_DOMAIN = 'default' @@ -101,6 +104,8 @@ class OpenStackShell(app.App): self.client_manager = None self.command_options = None + self.do_profile = False + def configure_logging(self): """Configure logging for the app.""" self.log_configurator = logs.LogConfigurator(self.options) @@ -125,6 +130,39 @@ class OpenStackShell(app.App): finally: self.log.info("END return value: %s", ret_val) + def init_profile(self): + self.do_profile = osprofiler_profiler and self.options.profile + if self.do_profile: + osprofiler_profiler.init(self.options.profile) + + def close_profile(self): + if self.do_profile: + trace_id = osprofiler_profiler.get().get_base_id() + + # NOTE(dbelova): let's use warning log level to see these messages + # printed. In fact we can define custom log level here with value + # bigger than most big default one (CRITICAL) or something like + # that (PROFILE = 60 for instance), but not sure we need it here. + self.log.warning("Trace ID: %s" % trace_id) + self.log.warning("To display trace use next command:\n" + "osprofiler trace show --html %s " % trace_id) + + def run_subcommand(self, argv): + self.init_profile() + try: + ret_value = super(OpenStackShell, self).run_subcommand(argv) + finally: + self.close_profile() + return ret_value + + def interact(self): + self.init_profile() + try: + ret_value = super(OpenStackShell, self).run_subcommand() + finally: + self.close_profile() + return ret_value + def build_option_parser(self, description, version): parser = super(OpenStackShell, self).build_option_parser( description, @@ -190,6 +228,19 @@ class OpenStackShell(app.App): help="Print API call timing info", ) + # osprofiler HMAC key argument + if osprofiler_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 one of the ' + 'HMAC keys configured in osprofiler ' + 'middleware in the projects user would like ' + 'to profile. It needs to be specified in ' + 'configuration files of the required ' + 'projects.') + return clientmanager.build_plugin_option_parser(parser) def initialize_app(self, argv): |
