| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | |
| 6 | # This file is part of OSM Monitoring module |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact: bdiaz@whitestack.com or glavado@whitestack.com |
| 22 | ## |
| David Garcia | 52e6037 | 2021-10-11 15:07:33 +0200 | [diff] [blame] | 23 | import base64 |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 24 | import logging |
| David Garcia | 52e6037 | 2021-10-11 15:07:33 +0200 | [diff] [blame] | 25 | from typing import Dict |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 26 | |
| 27 | import requests |
| 28 | |
| 29 | from osm_mon.core.config import Config |
| 30 | from osm_mon.evaluator.backends.base import BaseBackend |
| 31 | |
| 32 | log = logging.getLogger(__name__) |
| 33 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 34 | OSM_METRIC_PREFIX = "osm_" |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class PrometheusBackend(BaseBackend): |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 38 | def __init__(self, config: Config): |
| 39 | super().__init__(config) |
| 40 | self.conf = config |
| 41 | |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 42 | def get_metric_value(self, metric_name: str, tags: dict): |
| 43 | query = self._build_query(metric_name, tags) |
| 44 | request_url = self._build_url(query) |
| David Garcia | 52e6037 | 2021-10-11 15:07:33 +0200 | [diff] [blame] | 45 | request_headers = self._build_headers() |
| 46 | return self._query_metric(request_url, request_headers) |
| 47 | |
| 48 | def _query_metric(self, request_url: str, headers: Dict[str, str]): |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 49 | log.info("Querying Prometheus: %s", request_url) |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 50 | r = requests.get( |
| David Garcia | 52e6037 | 2021-10-11 15:07:33 +0200 | [diff] [blame] | 51 | request_url, |
| 52 | timeout=int(self.conf.get("global", "request_timeout")), |
| 53 | headers=headers, |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 54 | ) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 55 | if r.status_code == 200: |
| 56 | json_response = r.json() |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 57 | if json_response["status"] == "success": |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 58 | return self._get_metric_value_from_response(json_response) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 59 | else: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 60 | log.warning( |
| 61 | "Prometheus response is not success. Got status %s", |
| 62 | json_response["status"], |
| 63 | ) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 64 | else: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 65 | log.warning( |
| 66 | "Error contacting Prometheus. Got status code %s: %s", |
| 67 | r.status_code, |
| 68 | r.text, |
| 69 | ) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 70 | return None |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 71 | |
| 72 | def _build_query(self, metric_name: str, tags: dict) -> str: |
| 73 | query_section_tags = [] |
| 74 | for k, v in tags.items(): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 75 | query_section_tags.append(k + '="' + v + '"') |
| 76 | query_section = "query={0}{{{1}}}".format( |
| 77 | OSM_METRIC_PREFIX + metric_name, ",".join(query_section_tags) |
| 78 | ) |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 79 | return query_section |
| 80 | |
| 81 | def _build_url(self, query: str): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 82 | return self.conf.get("prometheus", "url") + "/api/v1/query?" + query |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 83 | |
| David Garcia | 52e6037 | 2021-10-11 15:07:33 +0200 | [diff] [blame] | 84 | def _build_headers(self) -> Dict[str, str]: |
| 85 | headers = {} |
| 86 | user = self.conf.get("prometheus", "user") |
| 87 | password = self.conf.get("prometheus", "password") |
| 88 | if user and password: |
| 89 | _phrase = f"{user}:{password}".encode("utf-8") |
| 90 | token = base64.b64encode(_phrase).decode("utf-8") |
| 91 | headers["Authorization"] = f"Basic {token}" |
| 92 | return headers |
| 93 | |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 94 | def _get_metric_value_from_response(self, json_response): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 95 | result = json_response["data"]["result"] |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 96 | if len(result): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 97 | metric_value = float(result[0]["value"][1]) |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 98 | log.info("Metric value: %s", metric_value) |
| 99 | return metric_value |
| 100 | else: |
| 101 | return None |