Skip to content

Commit

Permalink
Fix remaining duplication issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jtblin committed Mar 20, 2016
1 parent 14e21a6 commit 69ce19d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 11 additions & 16 deletions statsd/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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)}
Expand Down
42 changes: 42 additions & 0 deletions types/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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 {
Expand Down

0 comments on commit 69ce19d

Please sign in to comment.