diff --git a/README.md b/README.md index 42bdcd3b..7c48047d 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,13 @@ import ( ) func newScope() (tally.Scope, io.Closer) { - statter, _ := statsd.NewBufferedClient("127.0.0.1:8125", - "stats", 100*time.Millisecond, 1440) + statter, err := statsd.NewClientWithConfig(&statsd.ClientConfig{ + Address: "127.0.0.1:8125", + Prefix: "stats", + FlushInterval: 100 * time.Millisecond, + FlushBytes: 1440, + TagFormat: statsd.InfixComma, + }) reporter := tallystatsd.NewReporter(statter, tallystatsd.Options{ SampleRate: 1.0, diff --git a/glide.lock b/glide.lock index 05853b28..560fb609 100644 --- a/glide.lock +++ b/glide.lock @@ -6,7 +6,7 @@ imports: subpackages: - quantile - name: github.com/cactus/go-statsd-client - version: 138b925ccdf617776955904ba7759fce64406cec + version: 7eb0950793aa9a679bc468eb1da9b5520f73b16f subpackages: - statsd - name: github.com/golang/protobuf diff --git a/glide.yaml b/glide.yaml index 3b31589a..e13a832e 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,7 +1,7 @@ package: github.com/uber-go/tally import: - package: github.com/cactus/go-statsd-client - version: ~3.1.0 + version: ^5.0.0 subpackages: - statsd - package: github.com/m3db/prometheus_client_golang diff --git a/statsd/example/statsd_main.go b/statsd/example/statsd_main.go index 9e3f223d..8e6d6737 100644 --- a/statsd/example/statsd_main.go +++ b/statsd/example/statsd_main.go @@ -21,21 +21,26 @@ package main import ( + "io" "log" "math/rand" "time" + "github.com/cactus/go-statsd-client/statsd" "github.com/uber-go/tally" statsdreporter "github.com/uber-go/tally/statsd" - - "github.com/cactus/go-statsd-client/statsd" ) // To view statsd emitted metrics locally you can use // netcat with "nc 8125 -l -u" func main() { - statter, err := statsd.NewBufferedClient("127.0.0.1:8125", - "stats", 100*time.Millisecond, 1440) + statter, err := statsd.NewClientWithConfig(&statsd.ClientConfig{ + Address: "127.0.0.1:8125", + Prefix: "stats", + FlushInterval: 100 * time.Millisecond, + FlushBytes: 1440, + TagFormat: statsd.InfixComma, + }) if err != nil { log.Fatalf("could not create statsd client: %v", err) } @@ -48,9 +53,15 @@ func main() { Tags: map[string]string{}, Reporter: r, }, 1*time.Second) - defer closer.Close() - counter := scope.Counter("test-counter") + defer func(closer io.Closer) { + _ = closer.Close() + }(closer) + + counter := scope.Tagged(map[string]string{ + "host": "local", + "env": "dev", + }).Counter("test-counter") gauge := scope.Gauge("test-gauge") diff --git a/statsd/reporter.go b/statsd/reporter.go index 6c75f78f..a60eb717 100644 --- a/statsd/reporter.go +++ b/statsd/reporter.go @@ -26,9 +26,8 @@ import ( "strconv" "time" - "github.com/uber-go/tally" - "github.com/cactus/go-statsd-client/statsd" + "github.com/uber-go/tally" ) const ( @@ -74,15 +73,15 @@ func NewReporter(statsd statsd.Statter, opts Options) tally.StatsReporter { } func (r *cactusStatsReporter) ReportCounter(name string, tags map[string]string, value int64) { - r.statter.Inc(name, value, r.sampleRate) + r.statter.Inc(name, value, r.sampleRate, getStatsdTagPairs(tags)...) } func (r *cactusStatsReporter) ReportGauge(name string, tags map[string]string, value float64) { - r.statter.Gauge(name, int64(value), r.sampleRate) + r.statter.Gauge(name, int64(value), r.sampleRate, getStatsdTagPairs(tags)...) } func (r *cactusStatsReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) { - r.statter.TimingDuration(name, interval, r.sampleRate) + r.statter.TimingDuration(name, interval, r.sampleRate, getStatsdTagPairs(tags)...) } func (r *cactusStatsReporter) ReportHistogramValueSamples( @@ -97,7 +96,7 @@ func (r *cactusStatsReporter) ReportHistogramValueSamples( fmt.Sprintf("%s.%s-%s", name, r.valueBucketString(bucketLowerBound), r.valueBucketString(bucketUpperBound)), - samples, r.sampleRate) + samples, r.sampleRate, getStatsdTagPairs(tags)...) } func (r *cactusStatsReporter) ReportHistogramDurationSamples( @@ -112,7 +111,7 @@ func (r *cactusStatsReporter) ReportHistogramDurationSamples( fmt.Sprintf("%s.%s-%s", name, r.durationBucketString(bucketLowerBound), r.durationBucketString(bucketUpperBound)), - samples, r.sampleRate) + samples, r.sampleRate, getStatsdTagPairs(tags)...) } func (r *cactusStatsReporter) valueBucketString( @@ -148,9 +147,17 @@ func (r *cactusStatsReporter) Reporting() bool { } func (r *cactusStatsReporter) Tagging() bool { - return false + return true } func (r *cactusStatsReporter) Flush() { // no-op } + +func getStatsdTagPairs(tags map[string]string) []statsd.Tag { + tagPairs := make([]statsd.Tag, 0, len(tags)) + for k, v := range tags { + tagPairs = append(tagPairs, statsd.Tag{k, v}) + } + return tagPairs +} diff --git a/statsd/reporter_test.go b/statsd/reporter_test.go index 91762fa6..0bded470 100644 --- a/statsd/reporter_test.go +++ b/statsd/reporter_test.go @@ -29,5 +29,17 @@ import ( func TestCapabilities(t *testing.T) { r := NewReporter(nil, Options{}) assert.True(t, r.Capabilities().Reporting()) - assert.False(t, r.Capabilities().Tagging()) + assert.True(t, r.Capabilities().Tagging()) +} + +func TestGetStatsdTagPairs(t *testing.T) { + tags := map[string]string{ + "K1": "V1", + "K2": "V2", + } + tagPairs := getStatsdTagPairs(tags) + assert.Equal(t, "K1", tagPairs[0][0]) + assert.Equal(t, "V1", tagPairs[0][1]) + assert.Equal(t, "K2", tagPairs[1][0]) + assert.Equal(t, "V2", tagPairs[1][1]) }