~sircmpwn/core.sr.ht

e41ac24a9a2169a7498251179ab08c3c5ae6f47c — Ignas Kiela 6 months ago d2b5ead 0.67.0
Improve recorded metrics

Summary currently doesn't expose quintile information and Histogram
records occurrence count, so the Counters are no longer necessary.
2 files changed, 6 insertions(+), 15 deletions(-)

M srht/database.py
M srht/flask.py
M srht/database.py => srht/database.py +2 -4
@@ 5,7 5,7 @@ from alembic.config import Config, CommandLine
from argparse import ArgumentParser
from datetime import datetime
from logging.config import dictConfig
from prometheus_client import Counter, Summary
from prometheus_client import Histogram
from sqlalchemy import create_engine, event, engine_from_config, pool
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker


@@ 21,8 21,7 @@ db = LocalProxy(lambda: _db)
_metrics = type("metrics", tuple(), {
    m.describe()[0].name: m
    for m in [
        Counter("sql_roundtrips", "Number of SQL round-trips"),
        Summary("sql_query_duration", "Duration of SQL queries"),
        Histogram("sql_query_duration", "Duration of SQL queries"),
    ]
})



@@ 63,7 62,6 @@ class DbSession():
        @event.listens_for(self.engine, 'after_cursor_execute')
        def after_cursor_execute(conn, cursor, statement,
                    parameters, context, executemany):
            _metrics.sql_roundtrips.inc()
            _metrics.sql_query_duration.observe(
                    max(default_timer() - self._execute_start_time, 0))


M srht/flask.py => srht/flask.py +4 -11
@@ 12,7 12,7 @@ from srht.validation import Validation
from datetime import datetime, timedelta
from jinja2 import Markup, FileSystemLoader, ChoiceLoader, contextfunction
from jinja2 import escape
from prometheus_client import Counter, Summary, CollectorRegistry, REGISTRY, make_wsgi_app
from prometheus_client import Histogram, CollectorRegistry, REGISTRY, make_wsgi_app
from prometheus_client.multiprocess import MultiProcessCollector
from timeit import default_timer
from urllib.parse import urlparse, quote_plus


@@ 206,11 206,8 @@ class SrhtFlask(Flask):
        self.metrics = type("metrics", tuple(), {
            m.describe()[0].name: m
            for m in [
                Counter("http_requests", "Number of HTTP requests", [
                    "method", "route", "status",
                ]),
                Summary("request_time", "Duration of HTTP requests", [
                    "method", "route",
                Histogram("request_time", "Duration of HTTP requests", [
                    "method", "route", "status"
                ]),
            ]
        })


@@ 398,14 395,10 @@ class SrhtFlask(Flask):
        def track_request(resp):
            if not hasattr(request, "_srht_start_time"):
                return resp
            self.metrics.http_requests.labels(
                method=request.method,
                route=request.endpoint,
                status=resp.status_code,
            ).inc()
            self.metrics.request_time.labels(
                method=request.method,
                route=request.endpoint,
                status=resp.status_code,
            ).observe(max(default_timer() - request._srht_start_time, 0))
            return inject_rtl_direction(resp)