$ show prometheus/statsd_exporter
The prometheus/statsd_exporter is a production-grade Go application that receives StatsD metrics over UDP, TCP, or Unix sockets and translates them into Prometheus metrics. It acts as a drop-in replacement for StatsD, supporting multiple tagging formats (DogStatsD, InfluxDB, Librato, SignalFX) and providing flexible metric mapping via glob or regex patterns. The exporter is designed to run as a sidecar alongside application instances, converting ephemeral StatsD events into persistent Prometheus time series that can be scraped by a Prometheus server. Architecturally, the application consists of several well-separated layers: network listeners (UDP, TCP, Unixgram) that receive raw lines, a line parser that converts text into typed events (counter, gauge, observer), a metric mapper that applies user-defined mapping rules using a finite state machine (FSM) for glob patterns or regular expressions, and an exporter that registers and updates Prometheus metrics via a custom registry. Events are batched through an internal queue to improve throughput. The mapper supports caching (LRU or random replacement) to accelerate repeated lookups. Key entry points include the main.go binary with CLI flags for configuring listeners, mapping files, cache size, relay targets, and tagging options. The primary data flow is: incoming StatsD line → line parser → event queue → mapper → exporter → Prometheus registry. External dependencies are limited to Prometheus client libraries, a YAML parser, and the Go standard library. The codebase is well-tested with unit tests, integration tests, and benchmarks, and follows Prometheus project conventions.
The LLM identified 4 files worth highlighting for reuse:
Implements a finite state machine for efficient glob-style metric name matching. It builds a trie of metric segments and supports backtracking for wildcard patterns. The FSM is self-contained, well-documented with a README and DOT dump capability, and can be extracted for any application needing fast glob matching with capture groups.
A robust parser for StatsD protocol lines that handles multiple tagging formats (DogStatsD, InfluxDB, Librato, SignalFX), sampling factors, extended aggregation (DogStatsD v1.2), container IDs, and metric timestamps. It produces typed event objects and is designed for high throughput with minimal allocations. The parser is configurable to enable/disable specific tagging styles and can be reused in any Go project that needs to ingest StatsD data.
Provides a thread-safe LRU cache implementation with Prometheus instrumentation for cache length, gets, and hits. It wraps the groupcache LRU with a RWMutex and exposes metrics via a CacheMetrics struct. This pattern is reusable for any caching need where eviction policy and observability are required.
Implements an event queue that batches events and flushes them either when a threshold is reached or at a fixed interval. It uses a mutex-protected slice and a ticker for periodic flushing. The queue is generic (accepts any Event interface) and can be extracted for any application needing to batch and process items asynchronously.