Skip to content

Commit

Permalink
Add _total suffix for counters reported to prometheus (#54)
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Brown <gary@brownuk.com>
  • Loading branch information
objectiser authored and yurishkuro committed Oct 30, 2018
1 parent e0a812e commit 92cc65b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

0 comments on commit 92cc65b

Please sign in to comment.