| Tatyana Leontovich | 09b7b01 | 2017-07-10 12:53:45 +0300 | [diff] [blame] | 1 | import json | 
|  | 2 |  | 
|  | 3 | from tcp_tests.managers.clients import http_client | 
|  | 4 |  | 
|  | 5 |  | 
|  | 6 | class PrometheusClient(object): | 
|  | 7 | def __init__(self, host, port, proto): | 
|  | 8 | self.url = '{0}://{1}:{2}'.format(proto, host, port) | 
|  | 9 | self.client = http_client.HttpClient(base_url=self.url) | 
|  | 10 |  | 
|  | 11 | def get_targets(self): | 
|  | 12 | _, resp = self.client.get("/api/v1/targets") | 
|  | 13 | targets = json.loads(resp) | 
|  | 14 | return targets["data"]["activeTargets"] | 
|  | 15 |  | 
|  | 16 | def get_query(self, query, timestamp=None): | 
|  | 17 | params = { | 
|  | 18 | "query": query | 
|  | 19 | } | 
|  | 20 |  | 
|  | 21 | if timestamp is not None: | 
|  | 22 | params.update({"time": timestamp}) | 
|  | 23 |  | 
|  | 24 | _, resp = self.client.get("/api/v1/query", params=params) | 
|  | 25 |  | 
|  | 26 | query_result = json.loads(resp) | 
|  | 27 | if query_result["status"] != "success": | 
|  | 28 | raise Exception("Failed resp: {}".format(resp)) | 
|  | 29 |  | 
|  | 30 | if query_result["data"]["resultType"] == "vector": | 
|  | 31 | return query_result["data"]["result"] |