c5b935ee3dd2cf99034bb7ba2b2cef0569cae9b0
1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
25 from typing
import Dict
, List
29 from osm_mon
.core
.config
import Config
30 from osm_mon
.evaluator
.backends
.base
import BaseBackend
32 log
= logging
.getLogger(__name__
)
34 OSM_METRIC_PREFIX
= "osm_"
36 DEFAULT_QUERY_METRICS
= ["ns_id", "vnf_member_index", "vdu_name"]
39 class PrometheusBackend(BaseBackend
):
40 def __init__(self
, config
: Config
):
41 super().__init
__(config
)
44 def get_metric_data(self
, metric_name
: str, tags
: dict):
45 query
= self
._build
_query
(metric_name
, tags
)
46 request_url
= self
._build
_url
(query
)
47 request_headers
= self
._build
_headers
()
48 return self
._query
_metric
(request_url
, request_headers
)
50 def _query_metric(self
, request_url
: str, headers
: Dict
[str, str]):
51 log
.info("Querying Prometheus: %s", request_url
)
54 timeout
=int(self
.conf
.get("global", "request_timeout")),
57 if r
.status_code
== 200:
58 json_response
= r
.json()
59 if json_response
["status"] == "success":
60 return self
._get
_metric
_data
_from
_response
(json_response
)
63 "Prometheus response is not success. Got status %s",
64 json_response
["status"],
68 "Error contacting Prometheus. Got status code %s: %s",
74 def _build_query(self
, metric_name
: str, tags
: dict) -> str:
75 query_section_tags
= []
76 for k
, v
in tags
.items():
77 query_section_tags
.append(k
+ '="' + v
+ '"')
78 query_section
= "query={0}{{{1}}}".format(
79 OSM_METRIC_PREFIX
+ metric_name
, ",".join(query_section_tags
)
83 def _build_url(self
, query
: str):
84 return self
.conf
.get("prometheus", "url") + "/api/v1/query?" + query
86 def _build_headers(self
) -> Dict
[str, str]:
88 user
= self
.conf
.get("prometheus", "user")
89 password
= self
.conf
.get("prometheus", "password")
91 _phrase
= f
"{user}:{password}".encode("utf-8")
92 token
= base64
.b64encode(_phrase
).decode("utf-8")
93 headers
["Authorization"] = f
"Basic {token}"
96 def _get_metric_data_from_response(self
, json_response
) -> List
[Dict
[str, str]]:
97 result
= json_response
["data"]["result"]
100 for metric
in result
:
101 metrics_labels
= metric
["metric"]
102 metric_value
= float(metric
["value"][1])
103 log
.info("Metric value: %s", metric_value
)
104 metrics_data
.append({"labels": metrics_labels
, "value": metric_value
})