{"library":"statsd-python","title":"StatsD Python Client","type":"library","description":"The `statsd-python` library (version 0.6.1) provides a simple Python client for sending metrics to a StatsD server via UDP. It supports incrementing counters, setting gauges, and recording timings. The library has a stable API and sees infrequent updates, making it a reliable choice for basic StatsD integration.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install statsd-python"],"cli":null},"imports":["import statsd\nc = statsd.StatsClient(...)"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/lirsacc/statsd_client_python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/statsd-python/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import statsd\nimport time\nimport os\n\n# Configure StatsD client\n# Default StatsD host is usually 'localhost', port 8125\n# Replace with your StatsD server's host and port if different\nstatsd_host = os.environ.get('STATSD_HOST', 'localhost')\nstatsd_port = int(os.environ.get('STATSD_PORT', '8125'))\n\nc = statsd.StatsClient(statsd_host, statsd_port)\n\nprint(f\"Sending metrics to StatsD server at {statsd_host}:{statsd_port}...\")\n\n# Increment a counter\nc.incr('my_app.requests_total')\nprint(\"Incremented 'my_app.requests_total'\")\n\n# Set a gauge value\nc.gauge('my_app.active_users', 15)\nc.gauge('my_app.memory_usage_mb', 256.7)\nprint(\"Set gauges for 'my_app.active_users' and 'my_app.memory_usage_mb'\")\n\n# Record a timing (in milliseconds)\nstart_time = time.time()\ntime.sleep(0.05) # Simulate some work\nend_time = time.time()\nduration_ms = (end_time - start_time) * 1000\nc.timing('my_app.process_duration_ms', duration_ms)\nprint(f\"Recorded timing for 'my_app.process_duration_ms': {duration_ms:.2f}ms\")\n\n# Send with a sample rate (e.g., send only 50% of the time)\nc.incr('my_app.sampled_events', rate=0.5)\nprint(\"Incremented 'my_app.sampled_events' with a 50% sample rate\")\n\nprint(\"Metrics sent. Check your StatsD server for reception.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the `StatsClient` and send common metric types: counters (`incr`), gauges (`gauge`), and timings (`timing`). It also shows how to use a sample rate for counters.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}