X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fevaluator%2Fbackends%2Fprometheus.py;fp=osm_mon%2Fevaluator%2Fbackends%2Fprometheus.py;h=94024d7b7bdba906c18bc372735704a51f379441;hb=8e4179facf22c8096992f0a83caeec9f2f4996c7;hp=070cf691e0ddd1449efb3597d4f67f2b448ecc6c;hpb=a2eeb474200b8f9ebcaee6fa68fe52b6e1a5e337;p=osm%2FMON.git diff --git a/osm_mon/evaluator/backends/prometheus.py b/osm_mon/evaluator/backends/prometheus.py index 070cf69..94024d7 100644 --- a/osm_mon/evaluator/backends/prometheus.py +++ b/osm_mon/evaluator/backends/prometheus.py @@ -29,11 +29,10 @@ from osm_mon.evaluator.backends.base import BaseBackend log = logging.getLogger(__name__) -OSM_METRIC_PREFIX = 'osm_' +OSM_METRIC_PREFIX = "osm_" class PrometheusBackend(BaseBackend): - def __init__(self, config: Config): super().__init__(config) self.conf = config @@ -42,31 +41,42 @@ class PrometheusBackend(BaseBackend): query = self._build_query(metric_name, tags) request_url = self._build_url(query) log.info("Querying Prometheus: %s", request_url) - r = requests.get(request_url, timeout=int(self.conf.get('global', 'request_timeout'))) + r = requests.get( + request_url, timeout=int(self.conf.get("global", "request_timeout")) + ) if r.status_code == 200: json_response = r.json() - if json_response['status'] == 'success': + if json_response["status"] == "success": return self._get_metric_value_from_response(json_response) else: - log.warning("Prometheus response is not success. Got status %s", json_response['status']) + log.warning( + "Prometheus response is not success. Got status %s", + json_response["status"], + ) else: - log.warning("Error contacting Prometheus. Got status code %s: %s", r.status_code, r.text) + log.warning( + "Error contacting Prometheus. Got status code %s: %s", + r.status_code, + r.text, + ) return None def _build_query(self, metric_name: str, tags: dict) -> str: query_section_tags = [] for k, v in tags.items(): - query_section_tags.append(k + '=\"' + v + '\"') - query_section = "query={0}{{{1}}}".format(OSM_METRIC_PREFIX + metric_name, ','.join(query_section_tags)) + query_section_tags.append(k + '="' + v + '"') + query_section = "query={0}{{{1}}}".format( + OSM_METRIC_PREFIX + metric_name, ",".join(query_section_tags) + ) return query_section def _build_url(self, query: str): - return self.conf.get('prometheus', 'url') + "/api/v1/query?" + query + return self.conf.get("prometheus", "url") + "/api/v1/query?" + query def _get_metric_value_from_response(self, json_response): - result = json_response['data']['result'] + result = json_response["data"]["result"] if len(result): - metric_value = float(result[0]['value'][1]) + metric_value = float(result[0]["value"][1]) log.info("Metric value: %s", metric_value) return metric_value else: