blob: ad166e4154f82a3267e9b52d6cf9af9df0d8b9d9 [file] [log] [blame]
Benjamin Diaz4da14662018-10-17 14:44:36 -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##
Benjamin Diaz4da14662018-10-17 14:44:36 -030023import logging
Benjamin Diaz0e342442018-11-09 17:52:08 -030024import time
Benjamin Diaz4da14662018-10-17 14:44:36 -030025
Benjamin Diaz0e342442018-11-09 17:52:08 -030026from osm_mon.collector.backends.prometheus import PrometheusBackend
Benjamin Diaza97bdb32019-04-10 15:22:22 -030027from osm_mon.collector.service import CollectorService
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030028from osm_mon.core.config import Config
Benjamin Diaz4da14662018-10-17 14:44:36 -030029
30log = logging.getLogger(__name__)
31
Benjamin Diaz51f44862018-11-15 10:27:12 -030032METRIC_BACKENDS = [
33 PrometheusBackend
34]
Benjamin Diaz4da14662018-10-17 14:44:36 -030035
Benjamin Diaz0e342442018-11-09 17:52:08 -030036
37class Collector:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030038 def __init__(self, config: Config):
39 self.conf = config
Benjamin Diaza97bdb32019-04-10 15:22:22 -030040 self.service = CollectorService(config)
41 self.backends = []
Benjamin Diaz83038622019-01-28 19:03:39 -030042 self._init_backends()
Benjamin Diaz0e342442018-11-09 17:52:08 -030043
44 def collect_forever(self):
45 log.debug('collect_forever')
Benjamin Diaz0e342442018-11-09 17:52:08 -030046 while True:
47 try:
48 self.collect_metrics()
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030049 time.sleep(int(self.conf.get('collector', 'interval')))
Benjamin Diaz0e342442018-11-09 17:52:08 -030050 except Exception:
51 log.exception("Error collecting metrics")
52
Benjamin Diaz0e342442018-11-09 17:52:08 -030053 def collect_metrics(self):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030054 metrics = self.service.collect_metrics()
55 for backend in self.backends:
56 backend.handle(metrics)
Benjamin Diaz83038622019-01-28 19:03:39 -030057
58 def _init_backends(self):
59 for backend in METRIC_BACKENDS:
Benjamin Diaza97bdb32019-04-10 15:22:22 -030060 self.backends.append(backend())