1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
16 from oslo_config import cfg
18 from yardstick.dispatcher.base import Base as DispatchBase
19 from yardstick.dispatcher.influxdb_line_protocol import make_lines
21 LOG = logging.getLogger(__name__)
24 influx_dispatcher_opts = [
26 default='http://127.0.0.1:8086',
27 help='The target where the http request will be sent. '
28 'If this is not set, no data will be posted. For '
29 'example: target = http://hostname:1234/path'),
32 help='The database name to store test results.'),
35 help='The max time in seconds to wait for a request to '
39 CONF.register_opts(influx_dispatcher_opts, group="dispatcher_influxdb")
42 class InfluxdbDispatcher(DispatchBase):
43 """Dispatcher class for posting data into an influxdb target.
46 __dispatcher_type__ = "Influxdb"
48 def __init__(self, conf):
49 super(InfluxdbDispatcher, self).__init__(conf)
50 self.timeout = CONF.dispatcher_influxdb.timeout
51 self.target = CONF.dispatcher_influxdb.target
52 self.db_name = CONF.dispatcher_influxdb.db_name
53 self.influxdb_url = "%s/write?db=%s" % (self.target, self.db_name)
58 self.runners_info = {}
60 "pod_name": os.environ.get('POD_NAME', 'unknown'),
61 "installer": os.environ.get('INSTALLER_TYPE', 'unknown'),
62 "version": os.environ.get('YARDSTICK_VERSION', 'unknown')
65 def _dict_key_flatten(self, data):
68 if not [v for v in data.values()
69 if type(v) == dict or type(v) == list]:
72 for k, v in data.iteritems():
74 for n_k, n_v in v.iteritems():
75 next_data["%s.%s" % (k, n_k)] = n_v
77 for index, item in enumerate(v):
78 next_data["%s%d" % (k, index)] = item
82 return self._dict_key_flatten(next_data)
84 def _get_nano_timestamp(self, results):
86 timestamp = results["benchmark"]["timestamp"]
88 timestamp = time.time()
90 return str(int(float(timestamp) * 1000000000))
92 def _get_extended_tags(self, data):
93 runner_info = self.runners_info[data["runner_id"]]
95 "runner_id": data["runner_id"],
96 "task_id": self.task_id,
97 "scenarios": runner_info["scenarios"]
99 if "host" in runner_info:
100 tags["host"] = runner_info["host"]
101 if "target" in runner_info:
102 tags["target"] = runner_info["target"]
106 def _data_to_line_protocol(self, data):
109 point["measurement"] = self.tc
110 point["fields"] = self._dict_key_flatten(data["benchmark"]["data"])
111 point["time"] = self._get_nano_timestamp(data)
112 point["tags"] = self._get_extended_tags(data)
113 msg["points"] = [point]
114 msg["tags"] = self.static_tags
116 return make_lines(msg).encode('utf-8')
118 def record_result_data(self, data):
119 LOG.debug('Test result : %s' % json.dumps(data))
120 self.raw_result.append(data)
121 if self.target == '':
122 # if the target was not set, do not do anything
123 LOG.error('Dispatcher target was not set, no data will'
127 if isinstance(data, dict) and "scenario_cfg" in data:
128 self.tc = data["scenario_cfg"]["tc"]
129 self.task_id = data["scenario_cfg"]["task_id"]
130 scenario_cfg = data["scenario_cfg"]
131 runner_id = data["runner_id"]
132 self.runners_info[runner_id] = {"scenarios": scenario_cfg["type"]}
133 if "host" in scenario_cfg:
134 self.runners_info[runner_id]["host"] = scenario_cfg["host"]
135 if "target" in scenario_cfg:
136 self.runners_info[runner_id]["target"] = scenario_cfg["target"]
140 LOG.error('Test result : %s' % json.dumps(data))
141 LOG.error('The case_name cannot be found, no data will be posted.')
145 line = self._data_to_line_protocol(data)
146 LOG.debug('Test result line format : %s' % line)
147 res = requests.post(self.influxdb_url,
149 timeout=self.timeout)
150 if res.status_code != 204:
151 LOG.error('Test result posting finished with status code'
152 ' %d.' % res.status_code)
153 except Exception as err:
154 LOG.exception('Failed to record result data: %s',
159 def flush_result_data(self):
160 LOG.debug('Test result all : %s' % json.dumps(self.raw_result))