metrics: remove circular import

This commit is contained in:
Iain Learmonth 2022-11-02 13:24:02 +00:00
parent 8aad3368af
commit 28c07b7d82
2 changed files with 16 additions and 8 deletions

View file

@ -1,16 +1,24 @@
from typing import Iterator
from typing import Iterator, Optional
from flask import Flask
from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily, Metric
from sqlalchemy import text
from app import app
from app.extensions import db
from app.models.automation import Automation, AutomationState
class FlaskCollector:
def __init__(self, app=None):
self.app: Optional[Flask] = None
def init_app(self, app):
self.app = app
class DefinedProxiesCollector:
def collect(self) -> Iterator[Metric]:
with app.app_context():
with self.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,
@ -32,7 +40,7 @@ class DefinedProxiesCollector:
class BlockedProxiesCollector:
def collect(self) -> Iterator[Metric]:
with app.app_context():
with self.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,
@ -56,7 +64,7 @@ class BlockedProxiesCollector:
class AutomationCollector:
def collect(self) -> Iterator[Metric]:
with app.app_context():
with self.app.app_context():
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()