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 tag support in statsd reporter | Upgrade go-statsd-client #163

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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 17 additions & 6 deletions statsd/example/statsd_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is deprecated now

"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)
}
Expand All @@ -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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closer.close returns an error

_ = closer.Close()
}(closer)

counter := scope.Tagged(map[string]string{
"host": "local",
"env": "dev",
}).Counter("test-counter")

gauge := scope.Gauge("test-gauge")

Expand Down
23 changes: 15 additions & 8 deletions statsd/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Copy link
Author

@isopropylcyanide isopropylcyanide Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Keeping it as a func and not a method.

  • 1 obvious allocation here. Open for feedback.
    image

~/gocode/src/github.com/uber-go/tally/statsd master !4 ?1 ❯ go test . -run none -bench BenchmarkGetStatsdTagPairs --benchtime 3s -benchmem      ▼
goos: darwin
goarch: amd64
pkg: github.com/uber-go/tally/statsd
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkGetStatsdTagPairs-12    	31455973	       105.0 ns/op	      64 B/op	       1 allocs/op
PASS
ok  	github.com/uber-go/tally/statsd	3.943s
  • Benchmark

func BenchmarkGetStatsdTagPairs(b *testing.B) {
	tags := map[string]string{
		"K1": "V1",
		"K2": "V2",
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = getStatsdTagPairs(tags)
	}
}

tagPairs := make([]statsd.Tag, 0, len(tags))
for k, v := range tags {
tagPairs = append(tagPairs, statsd.Tag{k, v})
}
return tagPairs
}
14 changes: 13 additions & 1 deletion statsd/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}