From 69ce19da298695a9b62f1d483c100a394f922080 Mon Sep 17 00:00:00 2001 From: Jerome Touffe-Blin Date: Sun, 20 Mar 2016 20:46:29 +1100 Subject: [PATCH] Fix remaining duplication issues --- Makefile | 4 ++-- ROADMAP.md | 2 +- statsd/aggregator.go | 27 +++++++++++---------------- types/metrics.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index cbeaf999..a17ca727 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ junit-test: build check: go install gometalinter --deadline=60s ./... --vendor --linter='errcheck:errcheck:-ignore=net:Close' --cyclo-over=20 \ - --linter='vet:go tool vet -composites=false {paths}:PATH:LINE:MESSAGE' --disable=interfacer + --linter='vet:go tool vet -composites=false {paths}:PATH:LINE:MESSAGE' --disable=interfacer --dupl-threshold=65 profile: ./build/bin/$(ARCH)/$(BINARY_NAME) --backends=stdout --cpu-profile=./profile.out --flush-interval=1s @@ -66,7 +66,7 @@ cross: docker: cross cd build && docker build -t $(IMAGE_NAME):$(GIT_HASH) . -release: test docker +release: check test docker docker push $(IMAGE_NAME):$(GIT_HASH) docker tag -f $(IMAGE_NAME):$(GIT_HASH) $(IMAGE_NAME):latest docker push $(IMAGE_NAME):latest diff --git a/ROADMAP.md b/ROADMAP.md index 17ab909f..2db3d95c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -26,7 +26,7 @@ Pri 2 * [x] Use source ip address as hostname for datadog backend * [x] Use datadog and user agent headers for datadog backend * [ ] Add `kinesis` backend -* [ ] Fix gomatelinter issues +* [x] Fix gomatelinter issues * [ ] Implement stats by backend e.g. last flush, last flush error, etc. * [ ] Support `Event` metric * [ ] Fix value of metrics displayed in console diff --git a/statsd/aggregator.go b/statsd/aggregator.go index 9dbc32b0..586bf009 100644 --- a/statsd/aggregator.go +++ b/statsd/aggregator.go @@ -196,6 +196,13 @@ func (a *MetricAggregator) isExpired(now, ts time.Time) bool { return a.ExpiryInterval != time.Duration(0) && now.Sub(ts) > a.ExpiryInterval } +func (a *MetricAggregator) deleteMetric(key, tagsKey string, metrics types.AggregatedMetrics) { + metrics.DeleteChild(key, tagsKey) + if !metrics.HasChildren(key) { + metrics.Delete(key) + } +} + // Reset clears the contents of a MetricAggregator func (a *MetricAggregator) Reset(now time.Time) { a.Lock() @@ -204,10 +211,7 @@ func (a *MetricAggregator) Reset(now time.Time) { a.Counters.Each(func(key, tagsKey string, counter types.Counter) { if a.isExpired(now, counter.Timestamp) { - delete(a.Counters[key], tagsKey) - if len(a.Counters[key]) == 0 { - delete(a.Counters, key) - } + a.deleteMetric(key, tagsKey, a.Counters) } else { interval := counter.Interval a.Counters[key][tagsKey] = types.Counter{Interval: interval} @@ -216,10 +220,7 @@ func (a *MetricAggregator) Reset(now time.Time) { a.Timers.Each(func(key, tagsKey string, timer types.Timer) { if a.isExpired(now, timer.Timestamp) { - delete(a.Timers[key], tagsKey) - if len(a.Timers[key]) == 0 { - delete(a.Timers, key) - } + a.deleteMetric(key, tagsKey, a.Timers) } else { interval := timer.Interval a.Timers[key][tagsKey] = types.Timer{Interval: interval} @@ -228,20 +229,14 @@ func (a *MetricAggregator) Reset(now time.Time) { a.Gauges.Each(func(key, tagsKey string, gauge types.Gauge) { if a.isExpired(now, gauge.Timestamp) { - delete(a.Gauges[key], tagsKey) - if len(a.Gauges[key]) == 0 { - delete(a.Gauges, key) - } + a.deleteMetric(key, tagsKey, a.Gauges) } // No reset for gauges, they keep the last value until expiration }) a.Sets.Each(func(key, tagsKey string, set types.Set) { if a.isExpired(now, set.Timestamp) { - delete(a.Sets[key], tagsKey) - if len(a.Sets[key]) == 0 { - delete(a.Sets, key) - } + a.deleteMetric(key, tagsKey, a.Sets) } else { interval := set.Interval a.Sets[key][tagsKey] = types.Set{Interval: interval, Values: make(map[string]int64)} diff --git a/types/metrics.go b/types/metrics.go index 4a6d64cc..04053aee 100644 --- a/types/metrics.go +++ b/types/metrics.go @@ -148,6 +148,8 @@ func (m Metric) String() string { type AggregatedMetrics interface { MetricsName() string Delete(string) + DeleteChild(string, string) + HasChildren(string) bool } // Counters stores a map of counters by tags @@ -163,6 +165,16 @@ func (c Counters) Delete(k string) { delete(c, k) } +// DeleteChild deletes the metrics from the collection for the given tags +func (c Counters) DeleteChild(k, t string) { + delete(c[k], t) +} + +// HasChildren returns whether there are more children nested under the key +func (c Counters) HasChildren(k string) bool { + return len(c[k]) != 0 +} + // Timers stores a map of timers by tags type Timers map[string]map[string]Timer @@ -176,6 +188,16 @@ func (t Timers) Delete(k string) { delete(t, k) } +// DeleteChild deletes the metrics from the collection for the given tags +func (t Timers) DeleteChild(k, tags string) { + delete(t[k], tags) +} + +// HasChildren returns whether there are more children nested under the key +func (t Timers) HasChildren(k string) bool { + return len(t[k]) != 0 +} + // Gauges stores a map of gauges by tags type Gauges map[string]map[string]Gauge @@ -189,6 +211,16 @@ func (g Gauges) Delete(k string) { delete(g, k) } +// DeleteChild deletes the metrics from the collection for the given tags +func (g Gauges) DeleteChild(k, t string) { + delete(g[k], t) +} + +// HasChildren returns whether there are more children nested under the key +func (g Gauges) HasChildren(k string) bool { + return len(g[k]) != 0 +} + // Sets stores a map of sets by tags type Sets map[string]map[string]Set @@ -202,6 +234,16 @@ func (s Sets) Delete(k string) { delete(s, k) } +// DeleteChild deletes the metrics from the collection for the given tags +func (s Sets) DeleteChild(k, t string) { + delete(s[k], t) +} + +// HasChildren returns whether there are more children nested under the key +func (s Sets) HasChildren(k string) bool { + return len(s[k]) != 0 +} + // MetricMap is used for storing aggregated Metric values. // The keys of each map are metric names. type MetricMap struct {