"""Prometheus metrics helpers that integrate with the observability statement observers."""
from collections.abc import Iterable
from typing import Any
from sqlspec.observability import ObservabilityConfig, StatementEvent, StatementObserver, resolve_db_system
from sqlspec.typing import Counter, Histogram
from sqlspec.utils.module_loader import ensure_prometheus
__all__ = ("PrometheusStatementObserver", "enable_metrics")
class PrometheusStatementObserver:
"""Statement observer that records Prometheus metrics."""
__slots__ = ("_counters", "_duration", "_label_names", "_rows")
def __init__(
self,
*,
namespace: str = "sqlspec",
subsystem: str = "driver",
registry: Any | None = None,
label_names: Iterable[str] = ("db_system", "operation"),
duration_buckets: tuple[float, ...] | None = None,
) -> None:
self._label_names = tuple(label_names)
self._counters = Counter(
"query_total",
"Total SQL statements executed",
labelnames=self._label_names,
namespace=namespace,
subsystem=subsystem,
registry=registry,
)
histogram_kwargs: dict[str, Any] = {}
if duration_buckets is not None:
histogram_kwargs["buckets"] = duration_buckets
self._duration = Histogram(
"query_duration_seconds",
"SQL execution time in seconds",
labelnames=self._label_names,
namespace=namespace,
subsystem=subsystem,
registry=registry,
**histogram_kwargs,
)
self._rows = Histogram(
"query_rows",
"Rows affected per statement",
labelnames=self._label_names,
namespace=namespace,
subsystem=subsystem,
registry=registry,
)