Skip to content

Commit

Permalink
add delete-gauges flag to support re-sending a value when there was n…
Browse files Browse the repository at this point in the history
…o update
  • Loading branch information
markrechler committed Apr 23, 2015
1 parent f5225c1 commit 98c338e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
15 changes: 11 additions & 4 deletions statsdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var (
flushInterval = flag.Int64("flush-interval", 10, "Flush interval (seconds)")
debug = flag.Bool("debug", false, "print statistics sent to graphite")
showVersion = flag.Bool("version", false, "print version string")
deleteGauges = flag.Bool("delete-gauges", true, "don't send values to graphite for inactive gauges, as opposed to sending the previous value")
persistCountKeys = flag.Int64("persist-count-keys", 60, "number of flush-intervals to persist count keys")
receiveCounter = flag.String("receive-counter", "", "Metric name for total metrics received per interval")
percentThreshold = Percentiles{}
Expand Down Expand Up @@ -269,12 +270,18 @@ func processGauges(buffer *bytes.Buffer, now int64) int64 {
for g, c := range gauges {
lastValue, ok := trackedGauges[g]

if ok && c == lastValue {
if ok && c == math.MaxUint64 && *deleteGauges {
continue
} else if ok && lastValue > 0 && c == math.MaxUint64 && *deleteGauges == false {
fmt.Fprintf(buffer, "%s %d %d\n", g, lastValue, now)
num++
} else if c != math.MaxUint64 {
fmt.Fprintf(buffer, "%s %d %d\n", g, c, now)
trackedGauges[g] = c
gauges[g] = math.MaxUint64
num++
}
fmt.Fprintf(buffer, "%s %d %d\n", g, c, now)
trackedGauges[g] = c
num++

}
return num
}
Expand Down
35 changes: 33 additions & 2 deletions statsdaemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"flag"
"github.com/bmizerany/assert"
"math"
"math/rand"
Expand Down Expand Up @@ -384,19 +385,49 @@ func TestProcessTimers(t *testing.T) {

func TestProcessGauges(t *testing.T) {
// Some data with expected mean of 20
flag.Set("delete-gauges", "false")
gauges = make(map[string]uint64)
gauges["gaugor"] = math.MaxUint64

now := int64(1418052649)

var buffer bytes.Buffer

num := processGauges(&buffer, now)
assert.Equal(t, num, int64(0))
assert.Equal(t, buffer.String(), "")

gauges["gaugor"] = 12345
num = processGauges(&buffer, now)
assert.Equal(t, num, int64(1))

gauges["gaugor"] = math.MaxUint64
num = processGauges(&buffer, now)
assert.Equal(t, buffer.String(), "gaugor 12345 1418052649\ngaugor 12345 1418052649\n")
assert.Equal(t, num, int64(1))
}

func TestProcessDeleteGauges(t *testing.T) {
// Some data with expected mean of 20
flag.Set("delete-gauges", "true")
gauges = make(map[string]uint64)
gauges["gaugordelete"] = math.MaxUint64

now := int64(1418052649)

var buffer bytes.Buffer

num := processGauges(&buffer, now)
assert.Equal(t, num, int64(0))
assert.Equal(t, buffer.String(), "")

gauges["gaugordelete"] = 12345
num = processGauges(&buffer, now)
assert.Equal(t, num, int64(1))
assert.Equal(t, buffer.String(), "gaugor 12345 1418052649\n")

gauges["gaugor"] = 12345
gauges["gaugordelete"] = math.MaxUint64
num = processGauges(&buffer, now)
assert.Equal(t, buffer.String(), "gaugordelete 12345 1418052649\n")
assert.Equal(t, num, int64(0))
}

Expand Down

0 comments on commit 98c338e

Please sign in to comment.