blob: a69e651a6c3a4bc0af382dacb055c96a1c7a9179 [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
garciadeblas8e4179f2021-05-14 16:47:03 +020032METRIC_BACKENDS = [PrometheusBackend]
Benjamin Diaz4da14662018-10-17 14:44:36 -030033
Benjamin Diaz0e342442018-11-09 17:52:08 -030034
35class Collector:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030036 def __init__(self, config: Config):
37 self.conf = config
Benjamin Diaza97bdb32019-04-10 15:22:22 -030038 self.service = CollectorService(config)
39 self.backends = []
Benjamin Diaz83038622019-01-28 19:03:39 -030040 self._init_backends()
Benjamin Diaz0e342442018-11-09 17:52:08 -030041
42 def collect_forever(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020043 log.debug("collect_forever")
Benjamin Diaz0e342442018-11-09 17:52:08 -030044 while True:
45 try:
46 self.collect_metrics()
garciadeblas8e4179f2021-05-14 16:47:03 +020047 time.sleep(int(self.conf.get("collector", "interval")))
Benjamin Diaz0e342442018-11-09 17:52:08 -030048 except Exception:
49 log.exception("Error collecting metrics")
50
Benjamin Diaz0e342442018-11-09 17:52:08 -030051 def collect_metrics(self):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030052 metrics = self.service.collect_metrics()
53 for backend in self.backends:
54 backend.handle(metrics)
Benjamin Diaz83038622019-01-28 19:03:39 -030055
56 def _init_backends(self):
57 for backend in METRIC_BACKENDS:
Benjamin Diaza97bdb32019-04-10 15:22:22 -030058 self.backends.append(backend())