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 _total suffix for counters reported to prometheus #54

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 1 deletion metrics/prometheus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func newFactory(parent *Factory, scope string, tags map[string]string) *Factory

// Counter implements Counter of metrics.Factory.
func (f *Factory) Counter(name string, tags map[string]string) metrics.Counter {
name = f.subScope(name)
name = counterNamingConvention(f.subScope(name))
tags = f.mergeTags(tags)
labelNames := f.tagNames(tags)
opts := prometheus.CounterOpts{
Expand Down Expand Up @@ -244,3 +244,10 @@ func (f *Factory) tagsAsLabelValues(labels []string, tags map[string]string) []s
}
return ret
}

func counterNamingConvention(name string) string {
if !strings.HasSuffix(name, "_total") {
name += "_total"
}
return name
}
6 changes: 3 additions & 3 deletions metrics/prometheus/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSeparator(t *testing.T) {
c1.Inc(1)
snapshot, err := registry.Gather()
require.NoError(t, err)
m1 := findMetric(t, snapshot, "bender:rodriguez", map[string]string{"a": "b"})
m1 := findMetric(t, snapshot, "bender:rodriguez_total", map[string]string{"a": "b"})
assert.EqualValues(t, 1, m1.GetCounter().GetValue(), "%+v", m1)
}

Expand All @@ -63,10 +63,10 @@ func TestCounter(t *testing.T) {
snapshot, err := registry.Gather()
require.NoError(t, err)

m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"})
m1 := findMetric(t, snapshot, "bender_rodriguez_total", map[string]string{"a": "b", "x": "y"})
assert.EqualValues(t, 3, m1.GetCounter().GetValue(), "%+v", m1)

m2 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "z"})
m2 := findMetric(t, snapshot, "bender_rodriguez_total", map[string]string{"a": "b", "x": "z"})
assert.EqualValues(t, 7, m2.GetCounter().GetValue(), "%+v", m2)
}

Expand Down