Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoylan committed Jan 25, 2019
1 parent 97c14c0 commit 8ebda1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ func DefaultSample() metrics.Sample {
}

func (r *rootRegistry) registerMetric(name string, tags Tags) string {
// Copy the list of tags so subsequent mutations do not break the world
// Copy the list of tags so subsequent mutations (e.g. sort, append) do not affect other metrics.
tagsCopy := make(Tags, len(tags))
copy(tagsCopy, tags)

metricID := toMetricTagsID(name, tagsCopy)
r.idToMetricMutex.Lock()
r.idToMetricWithTags[metricID] = metricWithTags{
Expand Down
11 changes: 11 additions & 0 deletions metrics/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func TestMetricsWithTags(t *testing.T) {
assert.Equal(t, wantTags, gotTags)
}

func TestMetricDoesNotMutateInputTagSlice(t *testing.T) {
root := metrics.NewRootMetricsRegistry()

unsortedTags := metrics.Tags{metrics.MustNewTag("b", "b"), metrics.MustNewTag("a", "a")}

root.Counter("my-counter", unsortedTags...).Inc(1)

assert.Equal(t, metrics.Tags{metrics.MustNewTag("b", "b"), metrics.MustNewTag("a", "a")}, unsortedTags)
}


// Prefix should be used as provided (no case conversion/normalization), while tags should always be converted to
// lowercase.
func TestMetricsCasing(t *testing.T) {
Expand Down

0 comments on commit 8ebda1a

Please sign in to comment.