-
Notifications
You must be signed in to change notification settings - Fork 800
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added