The `statsd` library is a simple Python client for the StatsD daemon, used for sending various types of metrics (counters, timers, gauges, sets) to monitoring systems like Graphite, Datadog, and Telegraf. It is actively maintained, with the current version being 4.0.1.
pip install statsdVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate `StatsClient` and send common metric types: counters, timings, and gauges. It also shows how to use the timing decorator. Ensure a StatsD server is running and accessible at the specified host and port.
Upgrade your Python environment to 3.7 or higher.
Explicitly instantiate `StatsClient` (e.g., `c = StatsClient('localhost', 8125)`) or use configuration-specific imports like `from statsd.defaults.env import statsd` if relying on environment variables.If your monitoring system requires tags, you must adapt your metric naming strategy (e.g., `metric_name.tag_key.tag_value`) or use a client library specifically designed for that system, as this library explicitly avoids 'magic' tagging to prevent silent failures.
Ensure that UDP port 8125 (or your configured StatsD port) is open in your firewall rules on both the client and server machines. Verify connectivity between the client and the StatsD server.
Double-check the `host` and `port` parameters when instantiating `StatsClient` against your StatsD server's configuration.
Upgrade to `statsd` version 4.0.0 or later to ensure correct timing of asynchronous functions.
pip install statsd
import statsd
c = statsd.StatsClient()
c.incr('my_metric')
# Incorrect: statsd.StatsClient.incr('my_metric')from statsd import StatsClient c = StatsClient() # Alternatively: # import statsd # c = statsd.StatsClient()
import statsd c = statsd.StatsClient(host='localhost', port=8125) # Incorrect: c = statsd.StatsClient(host='localhost', port='8125')