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

Extend statsd reporter to having tags appended to metric names #378

Merged
merged 3 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
109 changes: 109 additions & 0 deletions common/metrics/tally/statsd/reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package statsd

import (
"bytes"
"github.com/cactus/go-statsd-client/statsd"
"github.com/uber-go/tally"
tallystatsdreporter "github.com/uber-go/tally/statsd"
"sort"
"time"
)

type cadenceTallyStatsdReporter struct {
//Wrapper on top of "github.com/uber-go/tally/statsd"
tallystatsd tally.StatsReporter
}

func (r *cadenceTallyStatsdReporter) metricNameWithTags(original_name string, tags map[string]string) string {
var keys []string
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)

var buffer bytes.Buffer
buffer.WriteString(original_name)

for _, tk := range keys {
// adding "." as delimiter so that it will show as different parts in Graphite/Grafana
buffer.WriteString("." + tk + "." + tags[tk])
}

return buffer.String()
}

// This is a wrapper on top of "github.com/uber-go/tally/statsd"
// The purpose is to support tagging
// The implementation is to append tags as metric name suffixes
func NewReporter(statsd statsd.Statter, opts tallystatsdreporter.Options) tally.StatsReporter {
return &cadenceTallyStatsdReporter{
tallystatsd: tallystatsdreporter.NewReporter(statsd, opts),
}
}

func (r *cadenceTallyStatsdReporter) ReportCounter(name string, tags map[string]string, value int64) {
new_name := r.metricNameWithTags(name, tags)
r.tallystatsd.ReportCounter(new_name, map[string]string{}, value)
}

func (r *cadenceTallyStatsdReporter) ReportGauge(name string, tags map[string]string, value float64) {
new_name := r.metricNameWithTags(name, tags)
r.tallystatsd.ReportGauge(new_name, map[string]string{}, value)
}

func (r *cadenceTallyStatsdReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) {
new_name := r.metricNameWithTags(name, tags)
r.tallystatsd.ReportTimer(new_name, map[string]string{}, interval)
}

func (r *cadenceTallyStatsdReporter) ReportHistogramValueSamples(
name string,
tags map[string]string,
buckets tally.Buckets,
bucketLowerBound,
bucketUpperBound float64,
samples int64,
) {
new_name := r.metricNameWithTags(name, tags)
r.tallystatsd.ReportHistogramValueSamples(new_name, map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples)
}

func (r *cadenceTallyStatsdReporter) ReportHistogramDurationSamples(
name string,
tags map[string]string,
buckets tally.Buckets,
bucketLowerBound,
bucketUpperBound time.Duration,
samples int64,
) {
new_name := r.metricNameWithTags(name, tags)
r.tallystatsd.ReportHistogramDurationSamples(new_name, map[string]string{}, buckets, bucketLowerBound, bucketUpperBound, samples)
}

func (r *cadenceTallyStatsdReporter) Capabilities() tally.Capabilities {
return r.tallystatsd.Capabilities()
}

func (r *cadenceTallyStatsdReporter) Flush() {
r.tallystatsd.Flush()
}
58 changes: 58 additions & 0 deletions common/metrics/tally/statsd/reporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package statsd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMetricNameWithTags(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add a separate test to validate output of metricNameWithTags ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added

r := cadenceTallyStatsdReporter{
tallystatsd: nil,
}
tags := map[string]string{
"tag1": "123",
"tag2": "456",
"tag3": "789",
}
name := "test-metric-name1"

assert.Equal(t, "test-metric-name1.tag1.123.tag2.456.tag3.789", r.metricNameWithTags(name, tags))
}

func TestMetricNameWithTagsStability(t *testing.T) {
r := cadenceTallyStatsdReporter{
tallystatsd: nil,
}
tags := map[string]string{
"tag1": "123",
"tag2": "456",
"tag3": "789",
}
name := "test-metric-name2"

//test the order is stable
for i := 1; i<=16; i++ {
assert.Equal(t, r.metricNameWithTags(name, tags), r.metricNameWithTags(name, tags))
}
}
7 changes: 5 additions & 2 deletions common/service/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ package config
import (
"github.com/cactus/go-statsd-client/statsd"
"github.com/uber-go/tally"
statsdreporter "github.com/uber-go/tally/statsd"
tallystatsdreporter "github.com/uber-go/tally/statsd"
statsdreporter "github.com/uber/cadence/common/metrics/tally/statsd"
"log"
"time"
)
Expand Down Expand Up @@ -72,7 +73,9 @@ func (c *Metrics) newStatsdScope() tally.Scope {
if err != nil {
log.Fatalf("error creating statsd client, err=%v", err)
}
reporter := statsdreporter.NewReporter(statter, statsdreporter.Options{})
//NOTE: according to ( https://github.com/uber-go/tally )Tally's statsd implementation doesn't support tagging.
// Therefore, we implement Tally interface to have a statsd reporter that can support tagging
reporter := statsdreporter.NewReporter(statter, tallystatsdreporter.Options{})
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment here for why we don't use the default tally.statsd implementation

scopeOpts := tally.ScopeOptions{
Tags: c.Tags,
Reporter: reporter,
Expand Down