| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -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 | ## |
| 23 | import logging |
| 24 | from typing import List |
| 25 | |
| 26 | from prometheus_client import start_http_server |
| 27 | from prometheus_client.core import REGISTRY, GaugeMetricFamily |
| 28 | |
| 29 | from osm_mon.collector.backends.base import BaseBackend |
| 30 | from osm_mon.collector.metric import Metric |
| 31 | |
| 32 | log = logging.getLogger(__name__) |
| 33 | |
| 34 | OSM_METRIC_PREFIX = 'osm_' |
| 35 | |
| 36 | |
| 37 | class PrometheusBackend(BaseBackend): |
| 38 | |
| 39 | def __init__(self): |
| 40 | self.custom_collector = CustomCollector() |
| 41 | self._start_exporter(8000) |
| 42 | |
| 43 | def handle(self, metrics: List[Metric]): |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 44 | log.debug('handle') |
| 45 | log.debug('metrics: %s', metrics) |
| Benjamin Diaz | 9604945 | 2018-11-20 11:56:43 -0300 | [diff] [blame] | 46 | prometheus_metrics = {} |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 47 | for metric in metrics: |
| Benjamin Diaz | 9604945 | 2018-11-20 11:56:43 -0300 | [diff] [blame] | 48 | if metric.name not in prometheus_metrics: |
| 49 | prometheus_metrics[metric.name] = GaugeMetricFamily( |
| 50 | OSM_METRIC_PREFIX + metric.name, |
| 51 | 'OSM metric', |
| Benjamin Diaz | b525e6c | 2019-02-04 13:38:11 -0300 | [diff] [blame] | 52 | labels=list(metric.tags.keys()) |
| Benjamin Diaz | 9604945 | 2018-11-20 11:56:43 -0300 | [diff] [blame] | 53 | ) |
| Benjamin Diaz | b525e6c | 2019-02-04 13:38:11 -0300 | [diff] [blame] | 54 | prometheus_metrics[metric.name].add_metric(list(metric.tags.values()), metric.value) |
| Benjamin Diaz | 9604945 | 2018-11-20 11:56:43 -0300 | [diff] [blame] | 55 | self.custom_collector.metrics = prometheus_metrics.values() |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 56 | |
| 57 | def _start_exporter(self, port): |
| 58 | log.debug('_start_exporter') |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 59 | log.debug('port: %s', port) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 60 | REGISTRY.register(self.custom_collector) |
| 61 | log.info("Starting MON Prometheus exporter at port %s", port) |
| 62 | start_http_server(port) |
| 63 | |
| 64 | |
| 65 | class CustomCollector(object): |
| 66 | |
| 67 | def __init__(self): |
| 68 | self.metrics = [] |
| 69 | |
| 70 | def describe(self): |
| 71 | log.debug('describe') |
| 72 | return [] |
| 73 | |
| 74 | def collect(self): |
| 75 | log.debug("collect") |
| 76 | return self.metrics |