OpenStack cross service/project profiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
7.7KB

  1. # Copyright 2014 Mirantis Inc.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import json
  16. import os
  17. import prettytable
  18. import six
  19. from oslo_utils import encodeutils
  20. from oslo_utils import uuidutils
  21. from osprofiler.cmd import cliutils
  22. from osprofiler.drivers import base
  23. from osprofiler import exc
  24. class BaseCommand(object):
  25. group_name = None
  26. class TraceCommands(BaseCommand):
  27. group_name = "trace"
  28. @cliutils.arg("trace", help="File with trace or trace id")
  29. @cliutils.arg("--connection-string", dest="conn_str",
  30. default=(cliutils.env("OSPROFILER_CONNECTION_STRING")),
  31. help="Storage driver's connection string. Defaults to "
  32. "env[OSPROFILER_CONNECTION_STRING] if set")
  33. @cliutils.arg("--transport-url", dest="transport_url",
  34. help="Oslo.messaging transport URL (for messaging:// driver "
  35. "only), e.g. rabbit://user:password@host:5672/")
  36. @cliutils.arg("--idle-timeout", dest="idle_timeout", type=int, default=1,
  37. help="How long to wait for the trace to finish, in seconds "
  38. "(for messaging:// driver only)")
  39. @cliutils.arg("--json", dest="use_json", action="store_true",
  40. help="show trace in JSON")
  41. @cliutils.arg("--html", dest="use_html", action="store_true",
  42. help="show trace in HTML")
  43. @cliutils.arg("--local-libs", dest="local_libs", action="store_true",
  44. help="use local static files of html in /libs/")
  45. @cliutils.arg("--dot", dest="use_dot", action="store_true",
  46. help="show trace in DOT language")
  47. @cliutils.arg("--render-dot", dest="render_dot_filename",
  48. help="filename for rendering the dot graph in pdf format")
  49. @cliutils.arg("--out", dest="file_name", help="save output in file")
  50. def show(self, args):
  51. """Display trace results in HTML, JSON or DOT format."""
  52. if not args.conn_str:
  53. raise exc.CommandError(
  54. "You must provide connection string via"
  55. " either --connection-string or "
  56. "via env[OSPROFILER_CONNECTION_STRING]")
  57. trace = None
  58. if not uuidutils.is_uuid_like(args.trace):
  59. trace = json.load(open(args.trace))
  60. else:
  61. try:
  62. engine = base.get_driver(args.conn_str, **args.__dict__)
  63. except Exception as e:
  64. raise exc.CommandError(e.message)
  65. trace = engine.get_report(args.trace)
  66. if not trace or not trace.get("children"):
  67. msg = ("Trace with UUID %s not found. Please check the HMAC key "
  68. "used in the command." % args.trace)
  69. raise exc.CommandError(msg)
  70. # Since datetime.datetime is not JSON serializable by default,
  71. # this method will handle that.
  72. def datetime_json_serialize(obj):
  73. if hasattr(obj, "isoformat"):
  74. return obj.isoformat()
  75. else:
  76. return obj
  77. if args.use_json:
  78. output = json.dumps(trace, default=datetime_json_serialize,
  79. separators=(",", ": "),
  80. indent=2)
  81. elif args.use_html:
  82. with open(os.path.join(os.path.dirname(__file__),
  83. "template.html")) as html_template:
  84. output = html_template.read().replace(
  85. "$DATA", json.dumps(trace, indent=4,
  86. separators=(",", ": "),
  87. default=datetime_json_serialize))
  88. if args.local_libs:
  89. output = output.replace("$LOCAL", "true")
  90. else:
  91. output = output.replace("$LOCAL", "false")
  92. elif args.use_dot:
  93. dot_graph = self._create_dot_graph(trace)
  94. output = dot_graph.source
  95. if args.render_dot_filename:
  96. dot_graph.render(args.render_dot_filename, cleanup=True)
  97. else:
  98. raise exc.CommandError("You should choose one of the following "
  99. "output formats: json, html or dot.")
  100. if args.file_name:
  101. with open(args.file_name, "w+") as output_file:
  102. output_file.write(output)
  103. else:
  104. print(output)
  105. def _create_dot_graph(self, trace):
  106. try:
  107. import graphviz
  108. except ImportError:
  109. raise exc.CommandError(
  110. "graphviz library is required to use this option.")
  111. dot = graphviz.Digraph(format="pdf")
  112. next_id = [0]
  113. def _create_node(info):
  114. time_taken = info["finished"] - info["started"]
  115. service = info["service"] + ":" if "service" in info else ""
  116. name = info["name"]
  117. label = "%s%s - %d ms" % (service, name, time_taken)
  118. if name == "wsgi":
  119. req = info["meta.raw_payload.wsgi-start"]["info"]["request"]
  120. label = "%s\\n%s %s.." % (label, req["method"],
  121. req["path"][:30])
  122. elif name == "rpc" or name == "driver":
  123. raw = info["meta.raw_payload.%s-start" % name]
  124. fn_name = raw["info"]["function"]["name"]
  125. label = "%s\\n%s" % (label, fn_name.split(".")[-1])
  126. node_id = str(next_id[0])
  127. next_id[0] += 1
  128. dot.node(node_id, label)
  129. return node_id
  130. def _create_sub_graph(root):
  131. rid = _create_node(root["info"])
  132. for child in root["children"]:
  133. cid = _create_sub_graph(child)
  134. dot.edge(rid, cid)
  135. return rid
  136. _create_sub_graph(trace)
  137. return dot
  138. @cliutils.arg("--connection-string", dest="conn_str",
  139. default=cliutils.env("OSPROFILER_CONNECTION_STRING"),
  140. help="Storage driver's connection string. Defaults to "
  141. "env[OSPROFILER_CONNECTION_STRING] if set")
  142. @cliutils.arg("--error-trace", dest="error_trace",
  143. type=bool, default=False,
  144. help="List all traces that contain error.")
  145. def list(self, args):
  146. """List all traces"""
  147. if not args.conn_str:
  148. raise exc.CommandError(
  149. "You must provide connection string via"
  150. " either --connection-string or "
  151. "via env[OSPROFILER_CONNECTION_STRING]")
  152. try:
  153. engine = base.get_driver(args.conn_str, **args.__dict__)
  154. except Exception as e:
  155. raise exc.CommandError(e.message)
  156. fields = ("base_id", "timestamp")
  157. pretty_table = prettytable.PrettyTable(fields)
  158. pretty_table.align = "l"
  159. if not args.error_trace:
  160. traces = engine.list_traces(fields)
  161. else:
  162. traces = engine.list_error_traces()
  163. for trace in traces:
  164. row = [trace[field] for field in fields]
  165. pretty_table.add_row(row)
  166. if six.PY3:
  167. print(encodeutils.safe_encode(pretty_table.get_string()).decode())
  168. else:
  169. print(encodeutils.safe_encode(pretty_table.get_string()))