diff --git a/statsdaemon.go b/statsdaemon.go index d18594a..dde0353 100644 --- a/statsdaemon.go +++ b/statsdaemon.go @@ -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{} @@ -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 } diff --git a/statsdaemon_test.go b/statsdaemon_test.go index 65cdbe5..575fea0 100644 --- a/statsdaemon_test.go +++ b/statsdaemon_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "flag" "github.com/bmizerany/assert" "math" "math/rand" @@ -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)) }