Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Set and Histogram #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions circonus/circonus.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (s *CirconusSink) AddSampleWithLabels(key []string, val float32, labels []m
s.metrics.RecordValue(flatKey, float64(val))
}

func (*CirconusSink) Histogram(key []string, val float32) {}
func (*CirconusSink) HistogramWithLabels(key []string, val float32, labels []metrics.Label) {}
func (*CirconusSink) Set(key []string, val string) {}
func (*CirconusSink) SetWithLabels(key []string, val string, labels []metrics.Label) {}

// Flattens key to Circonus metric name
func (s *CirconusSink) flattenKey(parts []string) string {
joined := strings.Join(parts, "`")
Expand Down
6 changes: 6 additions & 0 deletions circonus/circonus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import (
"net/http/httptest"
"strings"
"testing"

"github.com/armon/go-metrics"
)

func TestImplementsMetricSink(t *testing.T) {
var _ metrics.MetricSink = &CirconusSink{}
}

func TestNewCirconusSink(t *testing.T) {

// test with invalid config (nil)
Expand Down
20 changes: 20 additions & 0 deletions datadog/dogstatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func (s *DogStatsdSink) AddSample(key []string, val float32) {
s.AddSampleWithLabels(key, val, nil)
}

func (s *DogStatsdSink) Histogram(key []string, val float32) {
s.HistogramWithLabels(key, val, nil)
}

func (s *DogStatsdSink) Set(key []string, val string) {
s.SetWithLabels(key, val, nil)
}

// The following ...WithLabels methods correspond to Datadog's Tag extension to Statsd.
// http://docs.datadoghq.com/guides/dogstatsd/#tags
func (s *DogStatsdSink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) {
Expand All @@ -120,6 +128,18 @@ func (s *DogStatsdSink) AddSampleWithLabels(key []string, val float32, labels []
s.client.TimeInMilliseconds(flatKey, float64(val), tags, rate)
}

func (s *DogStatsdSink) HistogramWithLabels(key []string, val float32, labels []metrics.Label) {
flatKey, tags := s.getFlatkeyAndCombinedLabels(key, labels)
rate := 1.0
s.client.Histogram(flatKey, float64(val), tags, rate)
}

func (s *DogStatsdSink) SetWithLabels(key []string, val string, labels []metrics.Label) {
flatKey, tags := s.getFlatkeyAndCombinedLabels(key, labels)
rate := 1.0
s.client.Set(flatKey, val, tags, rate)
}

func (s *DogStatsdSink) getFlatkeyAndCombinedLabels(key []string, labels []metrics.Label) (string, []string) {
key, parsedLabels := s.parseKey(key)
flatKey := s.flattenKey(key)
Expand Down
9 changes: 9 additions & 0 deletions datadog/dogstatsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
TestHostname = "test_hostname"
)

func TestImplementsMetricSink(t *testing.T) {
var _ metrics.MetricSink = &DogStatsdSink{}
}

func MockGetHostname() string {
return TestHostname
}
Expand Down Expand Up @@ -58,6 +62,11 @@ var MetricSinkTests = []struct {
{"SetGauge", []string{"foo", "baz"}, float32(42), []metrics.Label{{"my tag", "my_value"}}, HostnameDisabled, "foo.baz:42.000000|g|#my_tag:my_value"},
{"SetGauge", []string{"foo", "bar"}, float32(42), []metrics.Label{{"my_tag", "my_value"}, {"other_tag", "other_value"}}, HostnameDisabled, "foo.bar:42.000000|g|#my_tag:my_value,other_tag:other_value"},
{"SetGauge", []string{"foo", "bar"}, float32(42), []metrics.Label{{"my_tag", "my_value"}, {"other_tag", "other_value"}}, HostnameEnabled, "foo.bar:42.000000|g|#my_tag:my_value,other_tag:other_value,host:test_hostname"},

{"Histogram", []string{"foo", "bar"}, float32(42), EmptyTags, HostnameDisabled, "foo.bar:42.000000|h"},
{"Histogram", []string{"foo", "bar"}, float32(42), []metrics.Label{{"my_tag", "my_value"}}, HostnameDisabled, "foo.bar:42.000000|h|#my_tag:my_value"},
{"Set", []string{"foo", "bar"}, "baz", EmptyTags, HostnameDisabled, "foo.bar:baz|s"},
{"Set", []string{"foo", "bar"}, "baz", []metrics.Label{{"my_tag", "my_value"}}, HostnameDisabled, "foo.bar:baz|s|#my_tag:my_value"},
}

func mockNewDogStatsdSink(addr string, labels []metrics.Label, tagWithHostname bool) *DogStatsdSink {
Expand Down
5 changes: 5 additions & 0 deletions inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ func (i *InmemSink) AddSampleWithLabels(key []string, val float32, labels []Labe
agg.Ingest(float64(val), i.rateDenom)
}

func (*InmemSink) Histogram(key []string, val float32) {}
func (*InmemSink) HistogramWithLabels(key []string, val float32, labels []Label) {}
func (*InmemSink) Set(key []string, val string) {}
func (*InmemSink) SetWithLabels(key []string, val string, labels []Label) {}

// Data is used to retrieve all the aggregated metrics
// Intervals may be in use, and a read lock should be acquired
func (i *InmemSink) Data() []*IntervalMetrics {
Expand Down
4 changes: 4 additions & 0 deletions inmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"time"
)

func TestInmemImplementsMetricSink(t *testing.T) {
var _ MetricSink = &InmemSink{}
}

func TestInmemSink(t *testing.T) {
inm := NewInmemSink(10*time.Millisecond, 50*time.Millisecond)

Expand Down
5 changes: 5 additions & 0 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,8 @@ func (p *PrometheusSink) IncrCounterWithLabels(parts []string, val float32, labe
g.Add(float64(val))
p.updates[hash] = time.Now()
}

func (*PrometheusSink) Histogram(key []string, val float32) {}
func (*PrometheusSink) HistogramWithLabels(key []string, val float32, labels []metrics.Label) {}
func (*PrometheusSink) Set(key []string, val string) {}
func (*PrometheusSink) SetWithLabels(key []string, val string, labels []metrics.Label) {}
11 changes: 11 additions & 0 deletions prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package prometheus

import (
"testing"

"github.com/armon/go-metrics"
)

func TestImplementsMetricSink(t *testing.T) {
var _ metrics.MetricSink = &PrometheusSink{}
}
12 changes: 12 additions & 0 deletions sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type MetricSink interface {
// Samples are for timing information, where quantiles are used
AddSample(key []string, val float32)
AddSampleWithLabels(key []string, val float32, labels []Label)

// Histogram tracks the statistical distribution of a set of values
Histogram(key []string, val float32)
HistogramWithLabels(key []string, val float32, labels []Label)

// Set counts the number of unique elements in a group.
Set(key []string, val string)
SetWithLabels(key []string, val string, labels []Label)
}

// BlackholeSink is used to just blackhole messages
Expand All @@ -34,6 +42,10 @@ func (*BlackholeSink) IncrCounter(key []string, val float32)
func (*BlackholeSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {}
func (*BlackholeSink) AddSample(key []string, val float32) {}
func (*BlackholeSink) AddSampleWithLabels(key []string, val float32, labels []Label) {}
func (*BlackholeSink) Histogram(key []string, val float32) {}
func (*BlackholeSink) HistogramWithLabels(key []string, val float32, labels []Label) {}
func (*BlackholeSink) Set(key []string, val string) {}
func (*BlackholeSink) SetWithLabels(key []string, val string, labels []Label) {}

// FanoutSink is used to sink to fanout values to multiple sinks
type FanoutSink []MetricSink
Expand Down
4 changes: 4 additions & 0 deletions sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (m *MockSink) AddSampleWithLabels(key []string, val float32, labels []Label
m.vals = append(m.vals, val)
m.labels = append(m.labels, labels)
}
func (*MockSink) Histogram(key []string, val float32) {}
func (*MockSink) HistogramWithLabels(key []string, val float32, labels []Label) {}
func (*MockSink) Set(key []string, val string) {}
func (*MockSink) SetWithLabels(key []string, val string, labels []Label) {}

func TestFanoutSink_Gauge(t *testing.T) {
m1 := &MockSink{}
Expand Down
5 changes: 5 additions & 0 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (s *StatsdSink) AddSampleWithLabels(key []string, val float32, labels []Lab
s.pushMetric(fmt.Sprintf("%s:%f|ms\n", flatKey, val))
}

func (*StatsdSink) Histogram(key []string, val float32) {}
func (*StatsdSink) HistogramWithLabels(key []string, val float32, labels []Label) {}
func (*StatsdSink) Set(key []string, val string) {}
func (*StatsdSink) SetWithLabels(key []string, val string, labels []Label) {}

// Flattens the key for formatting, removes spaces
func (s *StatsdSink) flattenKey(parts []string) string {
joined := strings.Join(parts, ".")
Expand Down
4 changes: 4 additions & 0 deletions statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"time"
)

func TestStatsdImplementsMetricSink(t *testing.T) {
var _ MetricSink = &StatsdSink{}
}

func TestStatsd_Flatten(t *testing.T) {
s := &StatsdSink{}
flat := s.flattenKey([]string{"a", "b", "c", "d"})
Expand Down
5 changes: 5 additions & 0 deletions statsite.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (s *StatsiteSink) AddSampleWithLabels(key []string, val float32, labels []L
s.pushMetric(fmt.Sprintf("%s:%f|ms\n", flatKey, val))
}

func (*StatsiteSink) Histogram(key []string, val float32) {}
func (*StatsiteSink) HistogramWithLabels(key []string, val float32, labels []Label) {}
func (*StatsiteSink) Set(key []string, val string) {}
func (*StatsiteSink) SetWithLabels(key []string, val string, labels []Label) {}

// Flattens the key for formatting, removes spaces
func (s *StatsiteSink) flattenKey(parts []string) string {
joined := strings.Join(parts, ".")
Expand Down
4 changes: 4 additions & 0 deletions statsite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"time"
)

func TestStatsiteImplementsMetricSink(t *testing.T) {
var _ MetricSink = &StatsiteSink{}
}

func TestStatsite_Flatten(t *testing.T) {
s := &StatsiteSink{}
flat := s.flattenKey([]string{"a", "b", "c", "d"})
Expand Down