From 516166881ea029c302682977372bbf66c145a9fa Mon Sep 17 00:00:00 2001 From: Mark Rechler Date: Wed, 22 Apr 2015 15:35:06 -0400 Subject: [PATCH] add delete-gauges flag to support re-sending a value when there was no update --- statsdaemon.go | 25 +++++++++++++++++++------ statsdaemon_test.go | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/statsdaemon.go b/statsdaemon.go index d18594a..dc886ee 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{} @@ -100,7 +101,7 @@ var ( In = make(chan *Packet, MAX_UNPROCESSED_PACKETS) counters = make(map[string]int64) gauges = make(map[string]uint64) - trackedGauges = make(map[string]uint64) + lastGaugeValue = make(map[string]uint64) timers = make(map[string]Uint64Slice) countInactivity = make(map[string]int64) sets = make(map[string][]string) @@ -267,14 +268,26 @@ func processGauges(buffer *bytes.Buffer, now int64) int64 { var num int64 for g, c := range gauges { - lastValue, ok := trackedGauges[g] + currentValue := c + lastValue, hasLastValue := lastGaugeValue[g] + var hasChanged bool - if ok && c == lastValue { + if c != math.MaxUint64 { + hasChanged = true + } + + switch { + case hasChanged: + fmt.Fprintf(buffer, "%s %d %d\n", g, currentValue, now) + lastGaugeValue[g] = currentValue + gauges[g] = math.MaxUint64 + num++ + case hasLastValue && !hasChanged && !*deleteGauges: + fmt.Fprintf(buffer, "%s %d %d\n", g, lastValue, now) + num++ + default: continue } - 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)) }