StatsD Tags Client

3.2.1.post1 · active · verified Fri Apr 17

A lightweight Python client for StatsD, extending the basic StatsD protocol with DogStatsD-compatible tags. It simplifies sending metrics with key-value tags to StatsD servers that support the DataDog extension. The current version is 3.2.1.post1, and the project is actively maintained with a stable release cadence.

Common errors

Warnings

Install

Imports

Quickstart

Initialize the `StatsClient` with a host, port, optional prefix, and default tags. Then use methods like `incr`, `gauge`, and `timing` to send various metric types, providing specific tags for each metric call that override or merge with default client tags.

import os
from statsd_tags import StatsClient

# Configure host/port via environment variables for easy deployment
statsd_host = os.environ.get('STATSD_HOST', 'localhost')
statsd_port = int(os.environ.get('STATSD_PORT', '8125'))

# Initialize the StatsD client with a prefix and default tags
client = StatsClient(
    host=statsd_host,
    port=statsd_port,
    prefix='my_application',
    tags={'environment': 'production'}
)

# Increment a counter metric with additional, specific tags
client.incr('request_count', tags={'endpoint': '/api/v1/data', 'status': '200'})

# Set a gauge metric value with custom tags
client.gauge('cpu_utilization', 45.7, tags={'server': 'web_01'})

# Record a timing metric (milliseconds) with event-specific tags
client.timing('db_query_time', 123.45, tags={'query_type': 'read'})

print(f"Sent metrics (incr, gauge, timing) to StatsD at {statsd_host}:{statsd_port}")
print("Note: StatsD uses UDP, so there's no direct confirmation of receipt.")

view raw JSON →