blob: a9bb93803818653c70cbd51b33af035b2f250bc1 [file] [log] [blame]
Benjamin Diaz0e342442018-11-09 17:52:08 -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
24from typing import List
25
26from prometheus_client import start_http_server
27from prometheus_client.core import REGISTRY, GaugeMetricFamily
28
29from osm_mon.collector.backends.base import BaseBackend
30from osm_mon.collector.metric import Metric
31
32log = logging.getLogger(__name__)
33
garciadeblas8e4179f2021-05-14 16:47:03 +020034OSM_METRIC_PREFIX = "osm_"
Benjamin Diaz0e342442018-11-09 17:52:08 -030035
36
37class PrometheusBackend(BaseBackend):
Benjamin Diaz0e342442018-11-09 17:52:08 -030038 def __init__(self):
39 self.custom_collector = CustomCollector()
40 self._start_exporter(8000)
41
42 def handle(self, metrics: List[Metric]):
garciadeblas8e4179f2021-05-14 16:47:03 +020043 log.debug("handle")
44 log.debug("metrics: %s", metrics)
Benjamin Diaz96049452018-11-20 11:56:43 -030045 prometheus_metrics = {}
Benjamin Diaz0e342442018-11-09 17:52:08 -030046 for metric in metrics:
Benjamin Diaz96049452018-11-20 11:56:43 -030047 if metric.name not in prometheus_metrics:
48 prometheus_metrics[metric.name] = GaugeMetricFamily(
49 OSM_METRIC_PREFIX + metric.name,
garciadeblas8e4179f2021-05-14 16:47:03 +020050 "OSM metric",
51 labels=list(metric.tags.keys()),
Benjamin Diaz96049452018-11-20 11:56:43 -030052 )
garciadeblas8e4179f2021-05-14 16:47:03 +020053 prometheus_metrics[metric.name].add_metric(
54 list(metric.tags.values()), metric.value
55 )
Benjamin Diaz96049452018-11-20 11:56:43 -030056 self.custom_collector.metrics = prometheus_metrics.values()
Benjamin Diaz0e342442018-11-09 17:52:08 -030057
58 def _start_exporter(self, port):
garciadeblas8e4179f2021-05-14 16:47:03 +020059 log.debug("_start_exporter")
60 log.debug("port: %s", port)
Benjamin Diaz0e342442018-11-09 17:52:08 -030061 REGISTRY.register(self.custom_collector)
62 log.info("Starting MON Prometheus exporter at port %s", port)
63 start_http_server(port)
64
65
66class CustomCollector(object):
Benjamin Diaz0e342442018-11-09 17:52:08 -030067 def __init__(self):
68 self.metrics = []
69
70 def describe(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020071 log.debug("describe")
Benjamin Diaz0e342442018-11-09 17:52:08 -030072 return []
73
74 def collect(self):
75 log.debug("collect")
76 return self.metrics