healthchecks_healthchecks/hc/lib/statsd.py
Pēteris Caune 66d482c4af
Disable a type warning related to statsd having no type hints yet
python-statsd does have a PR for adding type hints, so
hopefully this is temporary.
2024-05-01 11:39:20 +03:00

45 lines
1.0 KiB
Python

from __future__ import annotations
from django.conf import settings
from statsd.client.base import StatsClientBase
from statsd.client.udp import StatsClient
class NoopClient(StatsClientBase): # type: ignore
"""A client for statsd that does nothing."""
_prefix = None
def _send(self, data: str) -> None:
pass
def close(self) -> None:
pass
def pipeline(self) -> None:
pass
def get_client() -> StatsClientBase:
"""Initialize and return statsd client.
If settings.STATSD_HOST is set, initialize a UDP client.
Otherwise, initialize a no-op client.
The syntax for settings.STATSD_HOST is "example.org" (will assume port 8125)
or "example.org:1234" (will use port 1234).
"""
if settings.STATSD_HOST:
parts = settings.STATSD_HOST.split(":", maxsplit=1)
host, port = parts[0], 8125
if len(parts) == 2:
port = int(parts[1])
return StatsClient(host=host, port=port)
return NoopClient()
statsd = get_client()