Files
snake-python/server/blueprints/metrics.py
T
2026-04-07 01:42:25 +02:00

35 lines
1.1 KiB
Python

from quart_common.web.decorators import restrict_ip_addresses
from quart import Blueprint, jsonify
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from server.Server import Server
def create_metrics_blueprint(server:'Server') -> Blueprint:
blueprint = Blueprint('metrics', __name__)
@blueprint.get('/metrics')
@restrict_ip_addresses(allow=['192.168.188.0/24', '192.168.200.0/24'], abort_code=404)
async def metrics():
snapshot = await server.metrics_collector.build_snapshot(
server.game_runtime.game_last_seen_unix,
server.game_runtime.game_move_counts,
)
return jsonify(snapshot)
@blueprint.get('/metrics/prometheus')
@restrict_ip_addresses(allow=['192.168.188.0/24', '192.168.200.0/24'], abort_code=404)
async def metrics_prometheus():
snapshot = await server.metrics_collector.build_snapshot(
server.game_runtime.game_last_seen_unix,
server.game_runtime.game_move_counts,
)
return (
server.metrics_collector.build_prometheus_metrics(snapshot),
200,
{'Content-Type': 'text/plain; version=0.0.4; charset=utf-8'},
)
return blueprint