From 23802dade54c78618542d19e9463ffa6bcec54f2 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Mon, 30 Jan 2017 17:05:50 +0000 Subject: [PATCH] Add GaugeRelative to support Telegraf statsd implementation Telegraf doesn't support support negative counters, requiring relative gauge values instead. This adds the new method GaugeRelative to allow for this. --- conn.go | 15 +++++++++++++++ statsd.go | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/conn.go b/conn.go index 4dbda63..3004722 100644 --- a/conn.go +++ b/conn.go @@ -1,6 +1,7 @@ package statsd import ( + "fmt" "io" "math/rand" "net" @@ -93,6 +94,20 @@ func (c *conn) metric(prefix, bucket string, n interface{}, typ string, rate flo c.mu.Unlock() } +func (c *conn) gaugeRelative(prefix, bucket string, value interface{}, tags string) { + c.mu.Lock() + l := len(c.buf) + c.appendBucket(prefix, bucket, tags) + if isNegative(value) { + c.appendNumber(value) + } else { + c.appendString(fmt.Sprintf("+%v", value)) + } + c.appendType("g") + c.flushIfBufferFull(l) + c.mu.Unlock() +} + func (c *conn) gauge(prefix, bucket string, value interface{}, tags string) { c.mu.Lock() l := len(c.buf) diff --git a/statsd.go b/statsd.go index f19204d..28885b5 100644 --- a/statsd.go +++ b/statsd.go @@ -100,6 +100,14 @@ func (c *Client) Gauge(bucket string, value interface{}) { c.conn.gauge(c.prefix, bucket, value, c.tags) } +// GaugeRelative records an relative value for the given bucket. +func (c *Client) GaugeRelative(bucket string, value interface{}) { + if c.skip() { + return + } + c.conn.gaugeRelative(c.prefix, bucket, value, c.tags) +} + // Timing sends a timing value to a bucket. func (c *Client) Timing(bucket string, value interface{}) { if c.skip() {