This commit is contained in:
Iain Learmonth 2022-10-08 19:26:50 +01:00
parent 3c5abee77c
commit a189e8756c
2 changed files with 33 additions and 23 deletions

View file

@ -8,7 +8,6 @@ import sys
from app.extensions import db from app.extensions import db
from app.extensions import migrate from app.extensions import migrate
from app.extensions import bootstrap from app.extensions import bootstrap
from app.models.automation import Automation, AutomationState
from app.portal import portal from app.portal import portal
from app.tfstate import tfstate from app.tfstate import tfstate
@ -16,8 +15,8 @@ from app.tfstate import tfstate
app = Flask(__name__) app = Flask(__name__)
app.config.from_file("../config.yaml", load=yaml.safe_load) app.config.from_file("../config.yaml", load=yaml.safe_load)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[assignment]
'/metrics': make_wsgi_app() '/metrics': make_wsgi_app() # type: ignore[no-untyped-call]
}) })
db.init_app(app) db.init_app(app)
@ -31,9 +30,9 @@ app.register_blueprint(tfstate, url_prefix="/tfstate")
if sys.argv[1] != "db": if sys.argv[1] != "db":
from app.metrics import DefinedProxiesCollector, BlockedProxiesCollector, AutomationCollector from app.metrics import DefinedProxiesCollector, BlockedProxiesCollector, AutomationCollector
REGISTRY.register(DefinedProxiesCollector()) REGISTRY.register(DefinedProxiesCollector()) # type: ignore[no-untyped-call]
REGISTRY.register(BlockedProxiesCollector()) REGISTRY.register(BlockedProxiesCollector()) # type: ignore[no-untyped-call]
REGISTRY.register(AutomationCollector()) REGISTRY.register(AutomationCollector()) # type: ignore[no-untyped-call]
@app.route('/') @app.route('/')

View file

@ -1,4 +1,6 @@
from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily from typing import Iterator
from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily, Metric
from sqlalchemy import text from sqlalchemy import text
from app import app from app import app
@ -7,55 +9,64 @@ from app.models.automation import Automation, AutomationState
class DefinedProxiesCollector: class DefinedProxiesCollector:
def collect(self): def collect(self) -> Iterator[Metric]:
with app.app_context(): with app.app_context():
conn = db.engine.connect() conn = db.engine.connect()
result = conn.execute(text(""" 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" 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 WHERE proxy.origin_id = origin.id
AND origin.group_id = "group".id AND origin.group_id = "group".id
AND proxy.pool_id = pool.id AND proxy.pool_id = pool.id
AND proxy.destroyed IS NULL AND proxy.destroyed IS NULL
GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name; 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']) c = GaugeMetricFamily("defined_proxies", "Number of proxies currently defined for deployment",
labels=['group_id', 'group_name', 'provider', 'pool_id',
'pool_name']) # type: ignore[no-untyped-call]
for row in result: for row in result:
c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]], row[5]) c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]],
row[5]) # type: ignore[no-untyped-call]
yield c yield c
class BlockedProxiesCollector: class BlockedProxiesCollector:
def collect(self): def collect(self) -> Iterator[Metric]:
with app.app_context(): with app.app_context():
with db.engine.connect() as conn: with db.engine.connect() as conn:
result = conn.execute(text(""" 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" 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 WHERE proxy.origin_id = origin.id
AND origin.group_id = "group".id AND origin.group_id = "group".id
AND proxy.pool_id = pool.id AND proxy.pool_id = pool.id
AND proxy.deprecated IS NOT NULL 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; GROUP BY origin.group_id, "group".group_name, proxy.provider, proxy.pool_id, pool.pool_name,
proxy.deprecation_reason;
""")) """))
c = CounterMetricFamily(f"deprecated_proxies", c = CounterMetricFamily("deprecated_proxies",
f"Number of proxies deprecated", "Number of proxies deprecated",
labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name', 'deprecation_reason']) labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name',
'deprecation_reason']) # type: ignore[no-untyped-call]
for row in result: for row in result:
c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]], row[6]) c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4], row[5]],
row[6]) # type: ignore[no-untyped-call]
yield c yield c
class AutomationCollector: class AutomationCollector:
def collect(self): def collect(self) -> Iterator[Metric]:
with app.app_context(): with app.app_context():
c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)", labels=['automation_name']) c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)",
labels=['automation_name']) # type: ignore[no-untyped-call]
automations = Automation.query.all() automations = Automation.query.all()
for automation in automations: for automation in automations:
if automation.short_name in app.config['HIDDEN_AUTOMATIONS']: if automation.short_name in app.config['HIDDEN_AUTOMATIONS']:
continue continue
if automation.state == AutomationState.IDLE: if automation.state == AutomationState.IDLE:
c.add_metric([automation.short_name], 0) c.add_metric([automation.short_name], 0) # type: ignore[no-untyped-call]
elif automation.state == AutomationState.RUNNING: elif automation.state == AutomationState.RUNNING:
c.add_metric([automation.short_name], 1) c.add_metric([automation.short_name], 1) # type: ignore[no-untyped-call]
else: else:
c.add_metric([automation.short_name], 2) c.add_metric([automation.short_name], 2) # type: ignore[no-untyped-call]
yield c yield c