blob: 94024d7b7bdba906c18bc372735704a51f379441 [file] [log] [blame]
Benjamin Diaza97bdb32019-04-10 15:22:22 -03001# -*- 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##
23import logging
24
25import requests
26
27from osm_mon.core.config import Config
28from osm_mon.evaluator.backends.base import BaseBackend
29
30log = logging.getLogger(__name__)
31
garciadeblas8e4179f2021-05-14 16:47:03 +020032OSM_METRIC_PREFIX = "osm_"
Benjamin Diaza97bdb32019-04-10 15:22:22 -030033
34
35class PrometheusBackend(BaseBackend):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030036 def __init__(self, config: Config):
37 super().__init__(config)
38 self.conf = config
39
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030040 def get_metric_value(self, metric_name: str, tags: dict):
41 query = self._build_query(metric_name, tags)
42 request_url = self._build_url(query)
Benjamin Diaza97bdb32019-04-10 15:22:22 -030043 log.info("Querying Prometheus: %s", request_url)
garciadeblas8e4179f2021-05-14 16:47:03 +020044 r = requests.get(
45 request_url, timeout=int(self.conf.get("global", "request_timeout"))
46 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030047 if r.status_code == 200:
48 json_response = r.json()
garciadeblas8e4179f2021-05-14 16:47:03 +020049 if json_response["status"] == "success":
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030050 return self._get_metric_value_from_response(json_response)
Benjamin Diaza97bdb32019-04-10 15:22:22 -030051 else:
garciadeblas8e4179f2021-05-14 16:47:03 +020052 log.warning(
53 "Prometheus response is not success. Got status %s",
54 json_response["status"],
55 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030056 else:
garciadeblas8e4179f2021-05-14 16:47:03 +020057 log.warning(
58 "Error contacting Prometheus. Got status code %s: %s",
59 r.status_code,
60 r.text,
61 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030062 return None
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030063
64 def _build_query(self, metric_name: str, tags: dict) -> str:
65 query_section_tags = []
66 for k, v in tags.items():
garciadeblas8e4179f2021-05-14 16:47:03 +020067 query_section_tags.append(k + '="' + v + '"')
68 query_section = "query={0}{{{1}}}".format(
69 OSM_METRIC_PREFIX + metric_name, ",".join(query_section_tags)
70 )
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030071 return query_section
72
73 def _build_url(self, query: str):
garciadeblas8e4179f2021-05-14 16:47:03 +020074 return self.conf.get("prometheus", "url") + "/api/v1/query?" + query
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030075
76 def _get_metric_value_from_response(self, json_response):
garciadeblas8e4179f2021-05-14 16:47:03 +020077 result = json_response["data"]["result"]
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030078 if len(result):
garciadeblas8e4179f2021-05-14 16:47:03 +020079 metric_value = float(result[0]["value"][1])
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030080 log.info("Metric value: %s", metric_value)
81 return metric_value
82 else:
83 return None