From 906433f7b67ce8b1f822a2e61d2a1d74a5c66298 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Sun, 6 Aug 2017 11:52:52 +0200 Subject: [PATCH] metrics: add raw statsd reporter This patch adds a statds reporter which does not aggregate values. Fixes #327 --- fabio.properties | 6 +++-- metrics/metrics.go | 4 +++ metrics/statsd_raw.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 metrics/statsd_raw.go diff --git a/fabio.properties b/fabio.properties index 0e5336363..321a4f280 100644 --- a/fabio.properties +++ b/fabio.properties @@ -638,7 +638,8 @@ # : do not report metrics # stdout: report metrics to stdout # graphite: report metrics to Graphite on ${metrics.graphite.addr} -# statsd: report metrics to StatsD on ${metrics.statsd.addr} +# statsd: report metrics to StatsD on ${metrics.statsd.addr} +# statsd_raw: report metrics to StatsD on ${metrics.statsd.addr} without aggregation # circonus: report metrics to Circonus (http://circonus.com/) # # The default is @@ -723,7 +724,8 @@ # # The default is # -# metrics.interval = 30s +# metrics.interval = 100ms # (statsd_raw target) +# metrics.interval = 30s # (other targets) # metrics.graphite.addr configures the host:port of the Graphite diff --git a/metrics/metrics.go b/metrics/metrics.go index 2be4c475b..d010aa6ca 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -66,6 +66,10 @@ func NewRegistry(cfg config.Metrics) (r Registry, err error) { log.Printf("[INFO] Sending metrics to StatsD on %s as %q", cfg.StatsDAddr, prefix) return gmStatsDRegistry(prefix, cfg.StatsDAddr, cfg.Interval) + case "statsd_raw": + log.Printf("[INFO] Sending metrics to StatsD (raw) on %s as %q", cfg.StatsDAddr, prefix) + return newRawStatsDRegistry(prefix, cfg.StatsDAddr, cfg.Interval) + case "circonus": return circonusRegistry(prefix, cfg.Circonus, cfg.Interval) diff --git a/metrics/statsd_raw.go b/metrics/statsd_raw.go new file mode 100644 index 000000000..5c7a60898 --- /dev/null +++ b/metrics/statsd_raw.go @@ -0,0 +1,63 @@ +package metrics + +import ( + "errors" + "fmt" + "time" + + alstatsd "github.com/alexcesaro/statsd" +) + +func newRawStatsDRegistry(prefix, addr string, interval time.Duration) (Registry, error) { + if addr == "" { + return nil, errors.New(" statsd addr missing") + } + + c, err := alstatsd.New(alstatsd.Address(addr), alstatsd.FlushPeriod(interval)) + if err != nil { + return nil, fmt.Errorf(" cannot init statsd client: %s", err) + } + + return &rawStatsDRegistry{c}, nil +} + +type rawStatsDRegistry struct { + c *alstatsd.Client +} + +func (r *rawStatsDRegistry) Names() []string { return nil } +func (r *rawStatsDRegistry) Unregister(name string) {} +func (r *rawStatsDRegistry) UnregisterAll() {} + +func (r *rawStatsDRegistry) GetCounter(name string) Counter { + return &rawStatsDCounter{r.c, name} +} + +func (r *rawStatsDRegistry) GetTimer(name string) Timer { + return &rawStatsDTimer{r.c, name} +} + +type rawStatsDCounter struct { + c *alstatsd.Client + name string +} + +func (c *rawStatsDCounter) Inc(n int64) { + c.c.Increment(c.name) +} + +type rawStatsDTimer struct { + c *alstatsd.Client + name string +} + +func (t *rawStatsDTimer) Update(d time.Duration) { + t.c.Timing(t.name, int(d/time.Millisecond)) +} + +func (t *rawStatsDTimer) UpdateSince(start time.Time) { + t.Update(time.Now().Sub(start)) +} + +func (t *rawStatsDTimer) Rate1() float64 { return 0 } +func (t *rawStatsDTimer) Percentile(nth float64) float64 { return 0 }