Skip to content

Commit

Permalink
feat: add support for counter vectors
Browse files Browse the repository at this point in the history
Co-Authored-By: Mykhailo Yeromko <[email protected]>
  • Loading branch information
maxmoehl and mike-jc committed Feb 12, 2025
1 parent 381e68d commit 222b848
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
26 changes: 26 additions & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ type Counter interface {
Add(float64)
}

// counterVec allows us to hide the prometheus logic from the user. [prometheus.CounterVec] is not
// an actual metric but more of a metric factory which returns a metric for each set of labels.
// To not leak the returned prometheus types, this type is used.
type counterVec struct {
vec *prometheus.CounterVec
}

func (c counterVec) Add(f float64, labels []string) {
c.vec.WithLabelValues(labels...).Add(f)
}

type CounterVec interface {
// Add to metric, the number of labels must match the number of label names that were
// given when the [CounterVec] was created.
Add(float64, []string)
}

// A single numerical value that can arbitrarily go up and down.
type Gauge interface {
Add(float64)
Expand Down Expand Up @@ -77,6 +94,15 @@ func (p *Registry) NewCounter(name, helpText string, opts ...MetricOption) Count
return p.registerCollector(name, c).(Counter)
}

// Creates new counter vector. When a duplicate is registered, the Registry will return
// the previously created metric.
func (p *Registry) NewCounterVec(name, helpText string, labelNames []string, opts ...MetricOption) CounterVec {
opt := toPromOpt(name, helpText, opts...)
c := prometheus.NewCounterVec(prometheus.CounterOpts(opt), labelNames)
// See [counterVec] for details.
return counterVec{vec: p.registerCollector(name, c).(*prometheus.CounterVec)}
}

// Creates new gauge. When a duplicate is registered, the Registry will return
// the previously created metric.
func (p *Registry) NewGauge(name, helpText string, opts ...MetricOption) Gauge {
Expand Down
14 changes: 14 additions & 0 deletions prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import (
"code.cloudfoundry.org/tlsconfig/certtest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
)

func init() {
// Prometheus output is long, disable diff truncation.
format.MaxLength = 0
}

var _ = Describe("PrometheusMetrics", func() {
var (
l = log.New(GinkgoWriter, "", log.LstdFlags)
Expand All @@ -23,10 +29,13 @@ var _ = Describe("PrometheusMetrics", func() {
r := metrics.NewRegistry(l, metrics.WithServer(0))

c := r.NewCounter("test_counter", "a counter help text for test_counter", metrics.WithMetricLabels(map[string]string{"foo": "bar"}))
cv := r.NewCounterVec("test_counter_vector", "a counter vector to test", []string{"a", "b"})
g := r.NewGauge("test_gauge", "a gauge help text for test_gauge", metrics.WithMetricLabels(map[string]string{"bar": "baz"}))
h := r.NewHistogram("test_histogram", "a histogram help text for test_histogram", []float64{1.0}, metrics.WithMetricLabels(map[string]string{"aaa": "bbb"}))

c.Add(10)
cv.Add(1, []string{"1", "2"})
cv.Add(2, []string{"2", "1"})
g.Set(10)
g.Add(1)
h.Observe(0.5)
Expand All @@ -35,6 +44,11 @@ var _ = Describe("PrometheusMetrics", func() {
Expect(getMetrics(r.Port())).To(ContainSubstring("a gauge help text for test_gauge"))
Expect(getMetrics(r.Port())).To(ContainSubstring(`test_counter{foo="bar"} 10`))
Expect(getMetrics(r.Port())).To(ContainSubstring("a counter help text for test_counter"))

Expect(getMetrics(r.Port())).To(ContainSubstring(`test_counter_vector{a="1",b="2"} 1`))
Expect(getMetrics(r.Port())).To(ContainSubstring("a counter vector to test"))
Expect(getMetrics(r.Port())).To(ContainSubstring(`test_counter_vector{a="2",b="1"} 2`))

Expect(getMetrics(r.Port())).To(ContainSubstring(`test_histogram_bucket{aaa="bbb",le="1"} 1`))
Expect(getMetrics(r.Port())).To(ContainSubstring("a histogram help text for test_histogram"))

Expand Down

0 comments on commit 222b848

Please sign in to comment.