- Add external_logging_init_failures counter - Add external_log_required flag, raise if init fails and required - Health endpoint: add external_logging status check - Blocklist service: enrich with metadata fields, update import logic - Health check task: add runtime_state dependency, fix return typing - Metrics: add Histogram for request latencies - Frontend: align BlocklistImportLogSection props - Docs: update deployment guide, remove stale tasks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
124 lines
2.9 KiB
Python
124 lines
2.9 KiB
Python
"""Prometheus metrics collection for BanGUI backend.
|
|
|
|
This module provides metrics collection for:
|
|
- HTTP request count and latency per endpoint
|
|
- Active concurrent requests
|
|
- Custom application metrics (bans, jails, etc.)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from prometheus_client import (
|
|
CONTENT_TYPE_LATEST,
|
|
CollectorRegistry,
|
|
Counter,
|
|
Gauge,
|
|
Histogram,
|
|
Summary,
|
|
generate_latest,
|
|
)
|
|
|
|
__all__ = [
|
|
"get_metrics_registry",
|
|
"get_metrics",
|
|
"http_request_count",
|
|
"http_request_latency",
|
|
"http_active_requests",
|
|
"bans_total",
|
|
"jails_total",
|
|
"fail2ban_connection_errors",
|
|
"external_logging_init_failures",
|
|
]
|
|
|
|
# Global registry
|
|
_registry: CollectorRegistry | None = None
|
|
|
|
|
|
def get_metrics_registry() -> CollectorRegistry:
|
|
"""Get or create the global metrics registry.
|
|
|
|
Returns:
|
|
The Prometheus CollectorRegistry instance.
|
|
"""
|
|
global _registry
|
|
if _registry is None:
|
|
_registry = CollectorRegistry()
|
|
return _registry
|
|
|
|
|
|
# HTTP Metrics
|
|
|
|
http_request_count = Counter(
|
|
"bangui_http_requests_total",
|
|
"Total HTTP requests by method, endpoint, and status code",
|
|
["method", "endpoint", "status_code"],
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
http_request_latency = Histogram(
|
|
"bangui_http_request_duration_seconds",
|
|
"HTTP request latency in seconds by method and endpoint",
|
|
["method", "endpoint"],
|
|
buckets=(0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0),
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
http_active_requests = Gauge(
|
|
"bangui_http_active_requests",
|
|
"Current number of active HTTP requests by method and endpoint",
|
|
["method", "endpoint"],
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
# Application Metrics
|
|
|
|
bans_total = Gauge(
|
|
"bangui_bans_total",
|
|
"Total number of banned IPs across all jails",
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
jails_total = Gauge(
|
|
"bangui_jails_total",
|
|
"Total number of fail2ban jails",
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
fail2ban_connection_errors = Counter(
|
|
"bangui_fail2ban_connection_errors_total",
|
|
"Total number of fail2ban connection errors",
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
external_logging_init_failures = Counter(
|
|
"bangui_external_logging_init_failures_total",
|
|
"Total number of external logging handler initialization failures",
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
# Application startup and health
|
|
|
|
app_uptime = Summary(
|
|
"bangui_uptime_seconds",
|
|
"Application uptime in seconds",
|
|
registry=get_metrics_registry(),
|
|
)
|
|
|
|
|
|
def get_metrics() -> bytes:
|
|
"""Get all collected metrics in Prometheus text format.
|
|
|
|
Returns:
|
|
Prometheus-formatted metrics as bytes.
|
|
"""
|
|
return generate_latest(get_metrics_registry())
|
|
|
|
|
|
def get_metrics_content_type() -> str:
|
|
"""Get the correct Content-Type for Prometheus metrics.
|
|
|
|
Returns:
|
|
The MIME type for Prometheus metrics.
|
|
"""
|
|
return CONTENT_TYPE_LATEST
|