Added Unix socket support for the Prometheus metrics server.
This commit is contained in:
+25
-3
@@ -33,6 +33,7 @@ from crabstero.__main__ import (
|
||||
_watchdog_interval,
|
||||
_watchdog_loop,
|
||||
)
|
||||
from crabstero.metrics import TcpMetricsAddress, UnixMetricsAddress
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable, Coroutine
|
||||
@@ -141,7 +142,7 @@ class TestParseArgs:
|
||||
def test_listen_metrics_parses_address(self) -> None:
|
||||
"""--listen-metrics HOST:PORT sets listen_metrics to (host, port)."""
|
||||
args = _parse_args(["--token", "test", "--listen-metrics", "127.0.0.1:9090"])
|
||||
assert args.listen_metrics == ("127.0.0.1", 9090)
|
||||
assert args.listen_metrics == TcpMetricsAddress("127.0.0.1", 9090)
|
||||
|
||||
def test_listen_metrics_default_none(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Omitting --listen-metrics defaults to None."""
|
||||
@@ -152,13 +153,29 @@ class TestParseArgs:
|
||||
def test_listen_metrics_ipv6(self) -> None:
|
||||
"""--listen-metrics [::1]:PORT parses IPv6 address correctly."""
|
||||
args = _parse_args(["--token", "test", "--listen-metrics", "[::1]:9090"])
|
||||
assert args.listen_metrics == ("[::1]", 9090)
|
||||
assert args.listen_metrics == TcpMetricsAddress("[::1]", 9090)
|
||||
|
||||
def test_listen_metrics_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""LISTEN_METRICS env var is used when --listen-metrics is omitted."""
|
||||
monkeypatch.setenv("LISTEN_METRICS", "127.0.0.1:8080")
|
||||
args = _parse_args(["--token", "test"])
|
||||
assert args.listen_metrics == ("127.0.0.1", 8080)
|
||||
assert args.listen_metrics == TcpMetricsAddress("127.0.0.1", 8080)
|
||||
|
||||
def test_listen_metrics_unix_socket(self) -> None:
|
||||
"""--listen-metrics unix:/path sets listen_metrics to a Unix socket."""
|
||||
args = _parse_args(
|
||||
["--token", "test", "--listen-metrics", "unix:/run/crabstero.sock"],
|
||||
)
|
||||
assert args.listen_metrics == UnixMetricsAddress("/run/crabstero.sock")
|
||||
|
||||
def test_listen_metrics_unix_socket_from_env(
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""LISTEN_METRICS can enable metrics on a Unix socket."""
|
||||
monkeypatch.setenv("LISTEN_METRICS", "unix:/run/crabstero.sock")
|
||||
args = _parse_args(["--token", "test"])
|
||||
assert args.listen_metrics == UnixMetricsAddress("/run/crabstero.sock")
|
||||
|
||||
def test_listen_metrics_invalid_format(self) -> None:
|
||||
"""--listen-metrics with no colon raises SystemExit."""
|
||||
@@ -170,6 +187,11 @@ class TestParseArgs:
|
||||
with pytest.raises(SystemExit):
|
||||
_parse_args(["--token", "test", "--listen-metrics", "127.0.0.1:abc"])
|
||||
|
||||
def test_listen_metrics_empty_unix_socket_path(self) -> None:
|
||||
"""--listen-metrics unix: with no path raises SystemExit."""
|
||||
with pytest.raises(SystemExit):
|
||||
_parse_args(["--token", "test", "--listen-metrics", "unix:"])
|
||||
|
||||
def test_missing_token_exits(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Missing token causes SystemExit."""
|
||||
monkeypatch.delenv("TOKEN", raising=False)
|
||||
|
||||
Reference in New Issue
Block a user