Adds OSMMON_VCA_USER and adds timeout and max.poll.interval to collector
[osm/MON.git] / osm_mon / collector / prometheus_exporter.py
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 asyncio
24 import logging
25 import threading
26 import time
27
28 from prometheus_client import start_http_server
29 from prometheus_client.core import REGISTRY
30
31 from osm_mon.collector.collector import MonCollector
32 from osm_mon.core.settings import Config
33
34 log = logging.getLogger(__name__)
35
36
37 class MonPrometheusExporter:
38
39 def __init__(self):
40 self.custom_collector = CustomCollector()
41
42 def _run_exporter(self):
43 log.debug('_run_exporter')
44 REGISTRY.register(self.custom_collector)
45 log.info("Starting MON Prometheus exporter at port %s", 8000)
46 start_http_server(8000)
47
48 def run(self):
49 log.debug('_run')
50 collector_thread = threading.Thread(target=self._run_collector)
51 collector_thread.setDaemon(True)
52 collector_thread.start()
53 exporter_thread = threading.Thread(target=self._run_exporter)
54 exporter_thread.setDaemon(True)
55 exporter_thread.start()
56 collector_thread.join()
57 exporter_thread.join()
58
59 def _run_collector(self):
60 log.debug('_run_collector')
61 asyncio.set_event_loop(asyncio.new_event_loop())
62 mon_collector = MonCollector()
63 cfg = Config.instance()
64 while True:
65 try:
66 log.debug('_run_collector_loop')
67 metrics = asyncio.get_event_loop().run_until_complete(mon_collector.collect_metrics())
68 self.custom_collector.metrics = metrics
69 time.sleep(cfg.OSMMON_COLLECTOR_INTERVAL)
70 except Exception:
71 log.exception("Error collecting metrics")
72
73
74
75 class CustomCollector(object):
76
77 def __init__(self):
78 self.mon_collector = MonCollector()
79 self.metrics = []
80
81 def describe(self):
82 log.debug('describe')
83 return []
84
85 def collect(self):
86 log.debug("collect")
87 return self.metrics
88
89
90 if __name__ == '__main__':
91 MonPrometheusExporter().run()