From a08f1ea348b997b9a4d2215726e20b60e8e07419 Mon Sep 17 00:00:00 2001 From: Mike Cutalo Date: Mon, 13 Dec 2021 16:10:33 -0800 Subject: [PATCH] statsd separator --- common/metrics/tally/statsd/reporter.go | 42 ++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/common/metrics/tally/statsd/reporter.go b/common/metrics/tally/statsd/reporter.go index 3a5e47e4e0a0..9fd0c5e82256 100644 --- a/common/metrics/tally/statsd/reporter.go +++ b/common/metrics/tally/statsd/reporter.go @@ -27,6 +27,7 @@ package statsd import ( "bytes" "sort" + "strings" "time" "github.com/cactus/go-statsd-client/statsd" @@ -37,6 +38,8 @@ import ( type temporalTallyStatsdReporter struct { //Wrapper on top of "github.com/uber-go/tally/statsd" tallystatsd tally.StatsReporter + + separator string } func (r *temporalTallyStatsdReporter) metricNameWithTags(originalName string, tags map[string]string) string { @@ -63,22 +66,23 @@ func (r *temporalTallyStatsdReporter) metricNameWithTags(originalName string, ta func NewReporter(statsd statsd.Statter, opts tallystatsdreporter.Options) tally.StatsReporter { return &temporalTallyStatsdReporter{ tallystatsd: tallystatsdreporter.NewReporter(statsd, opts), + separator: ".__", } } func (r *temporalTallyStatsdReporter) ReportCounter(name string, tags map[string]string, value int64) { newName := r.metricNameWithTags(name, tags) - r.tallystatsd.ReportCounter(newName, map[string]string{}, value) + r.tallystatsd.ReportCounter(r.taggedName(newName, tags), map[string]string{}, value) } func (r *temporalTallyStatsdReporter) ReportGauge(name string, tags map[string]string, value float64) { newName := r.metricNameWithTags(name, tags) - r.tallystatsd.ReportGauge(newName, map[string]string{}, value) + r.tallystatsd.ReportGauge(r.taggedName(newName, tags), map[string]string{}, value) } func (r *temporalTallyStatsdReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) { newName := r.metricNameWithTags(name, tags) - r.tallystatsd.ReportTimer(newName, map[string]string{}, interval) + r.tallystatsd.ReportTimer(r.taggedName(newName, tags), map[string]string{}, interval) } func (r *temporalTallyStatsdReporter) ReportHistogramValueSamples( @@ -90,7 +94,7 @@ func (r *temporalTallyStatsdReporter) ReportHistogramValueSamples( samples int64, ) { newName := r.metricNameWithTags(name, tags) - r.tallystatsd.ReportHistogramValueSamples(newName, map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples) + r.tallystatsd.ReportHistogramValueSamples(r.taggedName(newName, tags), map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples) } func (r *temporalTallyStatsdReporter) ReportHistogramDurationSamples( @@ -102,7 +106,7 @@ func (r *temporalTallyStatsdReporter) ReportHistogramDurationSamples( samples int64, ) { newName := r.metricNameWithTags(name, tags) - r.tallystatsd.ReportHistogramDurationSamples(newName, map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples) + r.tallystatsd.ReportHistogramDurationSamples(r.taggedName(newName, tags), map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples) } func (r *temporalTallyStatsdReporter) Capabilities() tally.Capabilities { @@ -112,3 +116,31 @@ func (r *temporalTallyStatsdReporter) Capabilities() tally.Capabilities { func (r *temporalTallyStatsdReporter) Flush() { r.tallystatsd.Flush() } + +// https://github.com/influxdata/telegraf/blob/master/plugins/inputs/statsd/README.md#influx-statsd +func (r *temporalTallyStatsdReporter) taggedName(name string, tags map[string]string) string { + var b strings.Builder + b.WriteString(name) + for k, v := range tags { + b.WriteString(r.separator) + b.WriteString(replaceChars(k)) + b.WriteByte('=') + b.WriteString(replaceChars(v)) + } + return b.String() +} + +// Replace problematic characters in tags. +func replaceChars(s string) string { + var b strings.Builder + b.Grow(len(s)) + for i := 0; i < len(s); i++ { + switch s[i] { + case '.', ':', '|', '-', '=': + b.WriteByte('_') + default: + b.WriteByte(s[i]) + } + } + return b.String() +}