Skip to content

Commit

Permalink
Add GaugeRelative to support Telegraf statsd implementation
Browse files Browse the repository at this point in the history
Telegraf doesn't support support negative counters, requiring relative gauge values instead. This adds the new method GaugeRelative to allow for this.
  • Loading branch information
stevenh committed Jan 30, 2017
1 parent 7fea3f0 commit 23802da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package statsd

import (
"fmt"
"io"
"math/rand"
"net"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 23802da

Please sign in to comment.