From 4081a38552d3b70eea1bd25d0e5209dc7877e8b3 Mon Sep 17 00:00:00 2001 From: Iain Learmonth Date: Wed, 5 Oct 2022 15:57:58 +0100 Subject: [PATCH] metrics: refactor into a separate module as much as possible, given we need flask context --- app/__init__.py | 58 +--------------------------------------------- app/metrics.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 app/metrics.py diff --git a/app/__init__.py b/app/__init__.py index aefcbfd..ba97cb5 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,5 @@ from flask import Flask, redirect, url_for from flask.typing import ResponseReturnValue -from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily -from sqlalchemy import text from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import make_wsgi_app, REGISTRY import yaml @@ -28,61 +26,7 @@ bootstrap.init_app(app) app.register_blueprint(portal, url_prefix="/portal") app.register_blueprint(tfstate, url_prefix="/tfstate") - -class DefinedProxiesCollector: - def collect(self): - with app.app_context(): - conn = db.engine.connect() - result = conn.execute(text(""" - SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, COUNT(proxy.id) FROM proxy, origin, pool, "group" - WHERE proxy.origin_id = origin.id - AND origin.group_id = "group".id - AND proxy.pool_id = pool.id - AND proxy.destroyed IS NULL - GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name; - """)) - c = GaugeMetricFamily("defined_proxies", "Number of proxies currently defined for deployment", labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name']) - for row in result: - c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]], row[5]) - yield c - - -class BlockedProxiesCollector: - def collect(self): - with app.app_context(): - with db.engine.connect() as conn: - result = conn.execute(text(""" - SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, proxy.deprecation_reason, COUNT(proxy.id) FROM proxy, origin, pool, "group" - WHERE proxy.origin_id = origin.id - AND origin.group_id = "group".id - AND proxy.pool_id = pool.id - AND proxy.deprecated IS NOT NULL - GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, proxy.deprecation_reason; - """)) - c = CounterMetricFamily(f"deprecated_proxies", - f"Number of proxies deprecated", - labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name', 'deprecation_reason']) - for row in result: - c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]], row[6]) - yield c - - -class AutomationCollector: - def collect(self): - with app.app_context(): - c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)", labels=['automation_name']) - automations = Automation.query.all() - for automation in automations: - if automation.short_name in app.config['HIDDEN_AUTOMATIONS']: - continue - if automation.state == AutomationState.IDLE: - c.add_metric([automation.short_name], 0) - elif automation.state == AutomationState.RUNNING: - c.add_metric([automation.short_name], 1) - else: - c.add_metric([automation.short_name], 2) - yield c - +from app.metrics import DefinedProxiesCollector, BlockedProxiesCollector, AutomationCollector REGISTRY.register(DefinedProxiesCollector()) REGISTRY.register(BlockedProxiesCollector()) diff --git a/app/metrics.py b/app/metrics.py new file mode 100644 index 0000000..c07cb74 --- /dev/null +++ b/app/metrics.py @@ -0,0 +1,61 @@ +from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily +from sqlalchemy import text + +from app import app +from app.extensions import db +from app.models.automation import Automation, AutomationState + + +class DefinedProxiesCollector: + def collect(self): + with app.app_context(): + conn = db.engine.connect() + result = conn.execute(text(""" + SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, COUNT(proxy.id) FROM proxy, origin, pool, "group" + WHERE proxy.origin_id = origin.id + AND origin.group_id = "group".id + AND proxy.pool_id = pool.id + AND proxy.destroyed IS NULL + GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name; + """)) + c = GaugeMetricFamily("defined_proxies", "Number of proxies currently defined for deployment", labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name']) + for row in result: + c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]], row[5]) + yield c + + +class BlockedProxiesCollector: + def collect(self): + with app.app_context(): + with db.engine.connect() as conn: + result = conn.execute(text(""" + SELECT origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, proxy.deprecation_reason, COUNT(proxy.id) FROM proxy, origin, pool, "group" + WHERE proxy.origin_id = origin.id + AND origin.group_id = "group".id + AND proxy.pool_id = pool.id + AND proxy.deprecated IS NOT NULL + GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name, proxy.deprecation_reason; + """)) + c = CounterMetricFamily(f"deprecated_proxies", + f"Number of proxies deprecated", + labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name', 'deprecation_reason']) + for row in result: + c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]], row[6]) + yield c + + +class AutomationCollector: + def collect(self): + with app.app_context(): + c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)", labels=['automation_name']) + automations = Automation.query.all() + for automation in automations: + if automation.short_name in app.config['HIDDEN_AUTOMATIONS']: + continue + if automation.state == AutomationState.IDLE: + c.add_metric([automation.short_name], 0) + elif automation.state == AutomationState.RUNNING: + c.add_metric([automation.short_name], 1) + else: + c.add_metric([automation.short_name], 2) + yield c