statshog

1.0.6 · active · verified Sun Apr 12

statshog is a simple Python client for emitting metrics to StatsD servers, including both Etsy's original StatsD and InfluxDB's Telegraf StatsD server. It provides functionalities for incrementing counters, recording gauges, and tracking timings. The library is currently at version 1.0.6, with a stable but infrequent release cadence, reflecting its mature and focused functionality.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `StatsClient` and send common metric types: increments, gauges, and timings. It also shows how to use tags when `telegraf=True` is enabled.

import statshog
import time

# Initialize a StatsD client (defaults to localhost:8125)
# For Telegraf's StatsD with tags, use telegraf=True
statsd = statshog.StatsClient(host='127.0.0.1', port=8125, telegraf=True)

# Increment a counter
statsd.incr('my_app.requests_total')
statsd.incr('my_app.errors', tags={'type': 'database_error'})

# Record a gauge value
statsd.gauge('my_app.cpu_usage', 55)
statsd.gauge('my_app.memory_percent', 72.5, tags={'host': 'server_a'})

# Record a timing
start_time = time.time()
time.sleep(0.05) # Simulate some work
end_time = time.time()
duration_ms = (end_time - start_time) * 1000
statsd.timing('my_app.api_response_time', duration_ms, tags={'endpoint': '/api/data'})

print('Metrics sent to StatsD.')
# In a real application, ensure your StatsD server (e.g., Telegraf) is running on 127.0.0.1:8125
# and listening for UDP packets.

view raw JSON →