Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Add Application tag to metrics (#16)
Browse files Browse the repository at this point in the history
* Add Application tag to metircs

* Update reporter_test.go

* solving race condition
  • Loading branch information
laullon authored and vikramraman committed May 28, 2019
1 parent a8ea12b commit d106aec
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
7 changes: 7 additions & 0 deletions reporting/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ func (r *reporter) Report() {
lastErrorsCount := r.ErrorsCount()
r.registry.Each(func(key string, metric interface{}) {
name, tags := DecodeKey(key)

for t, v := range r.application.Map() {
if _, ok := tags[t]; !ok {
tags[t] = v
}
}

switch metric.(type) {
case metrics.Counter:
if hasDeltaPrefix(name) {
Expand Down
74 changes: 55 additions & 19 deletions reporting/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package reporting

import (
"fmt"
"github.com/rcrowley/go-metrics"
"github.com/stretchr/testify/assert"
"github.com/wavefronthq/wavefront-sdk-go/application"
"github.com/wavefronthq/wavefront-sdk-go/histogram"
"github.com/wavefronthq/wavefront-sdk-go/senders"
"math/rand"
"sync"
"testing"
"time"

metrics "github.com/rcrowley/go-metrics"
"github.com/stretchr/testify/assert"
"github.com/wavefronthq/wavefront-sdk-go/application"
"github.com/wavefronthq/wavefront-sdk-go/histogram"
"github.com/wavefronthq/wavefront-sdk-go/senders"
)

func TestPrefixAndSuffix(t *testing.T) {
Expand All @@ -30,6 +31,33 @@ func TestPrefixAndSuffix(t *testing.T) {
assert.Equal(t, name, "name")
}

func TestTags(t *testing.T) {
sender := &MockSender{}
reporter := NewReporter(sender, application.New("app", "srv"), DisableAutoStart(),
LogErrors(true), CustomRegistry(metrics.NewRegistry()))

reporter.GetOrRegisterMetric("m1", metrics.NewCounter(), map[string]string{"tag1": "tag"})
reporter.GetOrRegisterMetric("m2", metrics.NewCounter(), map[string]string{"application": "tag"})

reporter.Report()

assert.Equal(t, 2, len(sender.Metrics))
for _, metric := range sender.Metrics {
switch metric.Name {
case "m1.count":
assert.Equal(t, 5, len(metric.Tags))
assert.Equal(t, "app", metric.Tags["application"], "metrics tags: %v", metric.Tags)
case "m2.count":
assert.Equal(t, 4, len(metric.Tags))
assert.Equal(t, "tag", metric.Tags["application"], "metrics tags: %v", metric.Tags)
default:
t.Errorf("unexpected metric: '%v'", metric)
}
}

reporter.Close()
}

func TestError(t *testing.T) {
sender := &MockSender{}
reporter := NewReporter(sender, application.New("app", "srv"), DisableAutoStart(),
Expand All @@ -43,12 +71,14 @@ func TestError(t *testing.T) {
c.Inc(1)

reporter.Report()
reporter.Close()
time.Sleep(time.Second * 2)

_, met, _ := sender.Counters()

assert.Equal(t, 1, met)
assert.Equal(t, int64(1), reporter.ErrorsCount())
assert.NotEqual(t, 0, met, "error count, metrics: %v", sender.Metrics)
assert.NotEqual(t, int64(0), reporter.ErrorsCount(), "error count")

reporter.Close()
}

func TestBasicCounter(t *testing.T) {
Expand All @@ -68,10 +98,11 @@ func TestBasicCounter(t *testing.T) {
for i := 0; i < 3; i++ {
reporter.Report()
}
reporter.Close()

_, met, _ := sender.Counters()
assert.True(t, met >= 2)

reporter.Close()
}

func TestWFHistogram(t *testing.T) {
Expand Down Expand Up @@ -153,16 +184,21 @@ func TestDeltaPoint(t *testing.T) {

func newMockSender() *MockSender {
return &MockSender{
distributions: make([]string, 0),
metrics: make([]string, 0),
deltas: make([]string, 0),
Distributions: make([]MockMetirc, 0),
Metrics: make([]MockMetirc, 0),
Deltas: make([]MockMetirc, 0),
}
}

type MockMetirc struct {
Name string
Tags map[string]string
}

type MockSender struct {
distributions []string
metrics []string
deltas []string
Distributions []MockMetirc
Metrics []MockMetirc
Deltas []MockMetirc
sync.Mutex
}

Expand All @@ -179,14 +215,14 @@ func (s *MockSender) SendSpan(name string, startMillis, durationMillis int64, so
func (s *MockSender) SendDistribution(name string, centroids []histogram.Centroid, hgs map[histogram.Granularity]bool, ts int64, source string, tags map[string]string) error {
s.Lock()
defer s.Unlock()
s.distributions = append(s.distributions, name)
s.Distributions = append(s.Distributions, MockMetirc{Name: name, Tags: tags})
return nil
}

func (s *MockSender) SendDeltaCounter(name string, value float64, source string, tags map[string]string) error {
s.Lock()
defer s.Unlock()
s.deltas = append(s.deltas, name)
s.Deltas = append(s.Deltas, MockMetirc{Name: name, Tags: tags})
return nil
}

Expand All @@ -196,7 +232,7 @@ func (s *MockSender) SendMetric(name string, value float64, ts int64, source str
}
s.Lock()
defer s.Unlock()
s.metrics = append(s.metrics, name)
s.Metrics = append(s.Metrics, MockMetirc{Name: name, Tags: tags})
return nil
}

Expand All @@ -213,5 +249,5 @@ func (s *MockSender) Start() {}
func (s *MockSender) Counters() (int, int, int) {
s.Lock()
defer s.Unlock()
return len(s.distributions), len(s.metrics), len(s.deltas)
return len(s.Distributions), len(s.Metrics), len(s.Deltas)
}

0 comments on commit d106aec

Please sign in to comment.