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
24 from typing
import List
26 from prometheus_client
import start_http_server
27 from prometheus_client
.core
import REGISTRY
, GaugeMetricFamily
29 from osm_mon
.collector
.backends
.base
import BaseBackend
30 from osm_mon
.collector
.metric
import Metric
32 log
= logging
.getLogger(__name__
)
34 OSM_METRIC_PREFIX
= "osm_"
37 class PrometheusBackend(BaseBackend
):
39 self
.custom_collector
= CustomCollector()
40 self
._start
_exporter
(8000)
42 def handle(self
, metrics
: List
[Metric
]):
44 log
.debug("metrics: %s", metrics
)
45 prometheus_metrics
= {}
46 for metric
in metrics
:
47 if metric
.name
not in prometheus_metrics
:
48 prometheus_metrics
[metric
.name
] = GaugeMetricFamily(
49 OSM_METRIC_PREFIX
+ metric
.name
,
51 labels
=list(metric
.tags
.keys()),
53 prometheus_metrics
[metric
.name
].add_metric(
54 list(metric
.tags
.values()), metric
.value
56 self
.custom_collector
.metrics
= prometheus_metrics
.values()
58 def _start_exporter(self
, port
):
59 log
.debug("_start_exporter")
60 log
.debug("port: %s", port
)
61 REGISTRY
.register(self
.custom_collector
)
62 log
.info("Starting MON Prometheus exporter at port %s", port
)
63 start_http_server(port
)
66 class CustomCollector(object):