From 0ab9fdebc5f8406149b0bd7124b98ae18c1ed618 Mon Sep 17 00:00:00 2001 From: Iain Learmonth Date: Wed, 2 Nov 2022 13:58:54 +0000 Subject: [PATCH] metrics: remove type ignores that are no longer required --- app/__init__.py | 8 ++++---- app/metrics.py | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 5f1a446..80cd053 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,7 +16,7 @@ app = Flask(__name__) app.config.from_file("../config.yaml", load=yaml.safe_load) app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { # type: ignore[assignment] - '/metrics': make_wsgi_app() # type: ignore[no-untyped-call] + '/metrics': make_wsgi_app() }) db.init_app(app) @@ -30,9 +30,9 @@ app.register_blueprint(tfstate, url_prefix="/tfstate") if 'nose' not in sys.modules.keys() and sys.argv[1] != "db": from app.metrics import DefinedProxiesCollector, BlockedProxiesCollector, AutomationCollector - REGISTRY.register(DefinedProxiesCollector(app)) # type: ignore[no-untyped-call] - REGISTRY.register(BlockedProxiesCollector(app)) # type: ignore[no-untyped-call] - REGISTRY.register(AutomationCollector(app)) # type: ignore[no-untyped-call] + REGISTRY.register(DefinedProxiesCollector(app)) + REGISTRY.register(BlockedProxiesCollector(app)) + REGISTRY.register(AutomationCollector(app)) @app.route('/') diff --git a/app/metrics.py b/app/metrics.py index 289373c..ced81d2 100644 --- a/app/metrics.py +++ b/app/metrics.py @@ -3,13 +3,14 @@ from typing import Iterator, Optional from flask import Flask from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily, Metric +from prometheus_client.registry import Collector from sqlalchemy import text from app.extensions import db from app.models.automation import Automation, AutomationState -class FlaskCollector: +class FlaskCollector(Collector): _app: Optional[Flask] def __init__(self, app: Optional[Flask] = None) -> None: @@ -43,10 +44,10 @@ class DefinedProxiesCollector(FlaskCollector): """)) 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] + 'pool_name']) for row in result: c.add_metric([str(row[0]), row[1], row[2], str(row[3]), row[4]], - row[5]) # type: ignore[no-untyped-call] + row[5]) yield c @@ -66,25 +67,25 @@ class BlockedProxiesCollector(FlaskCollector): c = CounterMetricFamily("deprecated_proxies", "Number of proxies deprecated", labels=['group_id', 'group_name', 'provider', 'pool_id', 'pool_name', - 'deprecation_reason']) # type: ignore[no-untyped-call] + '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]) # type: ignore[no-untyped-call] + row[6]) yield c class AutomationCollector(FlaskCollector): def collect_in_ctx(self, app: Flask) -> Iterator[Metric]: c = GaugeMetricFamily("automation_state", "The automation state (0: idle, 1: running, 2: error)", - labels=['automation_name']) # type: ignore[no-untyped-call] + 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) # type: ignore[no-untyped-call] + c.add_metric([automation.short_name], 0) elif automation.state == AutomationState.RUNNING: - c.add_metric([automation.short_name], 1) # type: ignore[no-untyped-call] + c.add_metric([automation.short_name], 1) else: - c.add_metric([automation.short_name], 2) # type: ignore[no-untyped-call] + c.add_metric([automation.short_name], 2) yield c