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

Possibility to use different tags format for statsd #183

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ Use the inbuilt statsd reporter:
```go
import (
"io"
"github.com/cactus/go-statsd-client/statsd"
"github.com/cactus/go-statsd-client/v5/statsd"
"github.com/uber-go/tally"
tallystatsd "github.com/uber-go/tally/statsd"
// ...
)

func newScope() (tally.Scope, io.Closer) {
statter, _ := statsd.NewBufferedClient("127.0.0.1:8125",
"stats", 100*time.Millisecond, 1440)
statter, _ := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: "127.0.0.1:8125",
Prefix: "stats",
UseBuffered: true,
FlushInterval: 100 * time.Millisecond,
FlushBytes: 1440,
})

reporter := tallystatsd.NewReporter(statter, tallystatsd.Options{
SampleRate: 1.0,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/uber-go/tally/v4
go 1.15

require (
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c
github.com/cactus/go-statsd-client/v5 v5.0.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_model v0.2.0
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c h1:HIGF0r/56+7fuIZw2V4isE22MK6xpxWx7BbV8dJ290w=
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI=
github.com/cactus/go-statsd-client/v5 v5.0.0 h1:KqvIQtc9qt34uq+nu4nd1PwingWfBt/IISgtUQ2nSJk=
github.com/cactus/go-statsd-client/v5 v5.0.0/go.mod h1:COEvJ1E+/E2L4q6QE5CkjWPi4eeDw9maJBMIuMPBZbY=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -41,6 +41,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
2 changes: 1 addition & 1 deletion statsd/example/statsd_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"math/rand"
"time"

"github.com/cactus/go-statsd-client/statsd"
"github.com/cactus/go-statsd-client/v5/statsd"
tally "github.com/uber-go/tally/v4"
statsdreporter "github.com/uber-go/tally/v4/statsd"
)
Expand Down
28 changes: 22 additions & 6 deletions statsd/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ package statsd
import (
"fmt"
"math"
"sort"
"strconv"
"time"

"github.com/cactus/go-statsd-client/statsd"
statsd "github.com/cactus/go-statsd-client/v5/statsd"
tally "github.com/uber-go/tally/v4"
)

Expand Down Expand Up @@ -73,15 +74,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, convertTags(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, convertTags(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, convertTags(tags)...)
}

func (r *cactusStatsReporter) ReportHistogramValueSamples(
Expand All @@ -96,7 +97,7 @@ func (r *cactusStatsReporter) ReportHistogramValueSamples(
fmt.Sprintf("%s.%s-%s", name,
r.valueBucketString(bucketLowerBound),
r.valueBucketString(bucketUpperBound)),
samples, r.sampleRate)
samples, r.sampleRate, convertTags(tags)...)
}

func (r *cactusStatsReporter) ReportHistogramDurationSamples(
Expand All @@ -111,7 +112,7 @@ func (r *cactusStatsReporter) ReportHistogramDurationSamples(
fmt.Sprintf("%s.%s-%s", name,
r.durationBucketString(bucketLowerBound),
r.durationBucketString(bucketUpperBound)),
samples, r.sampleRate)
samples, r.sampleRate, convertTags(tags)...)
}

func (r *cactusStatsReporter) valueBucketString(
Expand Down Expand Up @@ -153,3 +154,18 @@ func (r *cactusStatsReporter) Tagging() bool {
func (r *cactusStatsReporter) Flush() {
// no-op
}

func convertTags(tags map[string]string) []statsd.Tag {
var keys []string
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)

var statsdTags []statsd.Tag
for _, tk := range keys {
statsdTags = append(statsdTags, statsd.Tag{tk, tags[tk]})
}

return statsdTags
}