Skip to content

Commit d8b51d4

Browse files
authored
Merge pull request #753 from prometheus/beorn7/test
Improve CollectAndCount
2 parents ef4f037 + 0577ef6 commit d8b51d4

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

prometheus/testutil/testutil.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,43 @@ func ToFloat64(c prometheus.Collector) float64 {
112112
panic(fmt.Errorf("collected a non-gauge/counter/untyped metric: %s", pb))
113113
}
114114

115-
// CollectAndCount collects all Metrics from the provided Collector and returns their number.
116-
//
117-
// This can be used to assert the number of metrics collected by a given collector after certain operations.
118-
//
119-
// This function is only for testing purposes, and even for testing, other approaches
120-
// are often more appropriate (see this package's documentation).
121-
func CollectAndCount(c prometheus.Collector) int {
122-
var (
123-
mCount int
124-
mChan = make(chan prometheus.Metric)
125-
done = make(chan struct{})
126-
)
127-
128-
go func() {
129-
for range mChan {
130-
mCount++
131-
}
132-
close(done)
133-
}()
115+
// CollectAndCount registers the provided Collector with a newly created
116+
// pedantic Registry. It then calls GatherAndCount with that Registry and with
117+
// the provided metricNames. In the unlikely case that the registration or the
118+
// gathering fails, this function panics. (This is inconsistent with the other
119+
// CollectAnd… functions in this package and has historical reasons. Changing
120+
// the function signature would be a breaking change and will therefore only
121+
// happen with the next major version bump.)
122+
func CollectAndCount(c prometheus.Collector, metricNames ...string) int {
123+
reg := prometheus.NewPedanticRegistry()
124+
if err := reg.Register(c); err != nil {
125+
panic(fmt.Errorf("registering collector failed: %s", err))
126+
}
127+
result, err := GatherAndCount(reg, metricNames...)
128+
if err != nil {
129+
panic(err)
130+
}
131+
return result
132+
}
134133

135-
c.Collect(mChan)
136-
close(mChan)
137-
<-done
134+
// GatherAndCount gathers all metrics from the provided Gatherer and counts
135+
// them. It returns the number of metric children in all gathered metric
136+
// families together. If any metricNames are provided, only metrics with those
137+
// names are counted.
138+
func GatherAndCount(g prometheus.Gatherer, metricNames ...string) (int, error) {
139+
got, err := g.Gather()
140+
if err != nil {
141+
return 0, fmt.Errorf("gathering metrics failed: %s", err)
142+
}
143+
if metricNames != nil {
144+
got = filterMetrics(got, metricNames)
145+
}
138146

139-
return mCount
147+
result := 0
148+
for _, mf := range got {
149+
result += len(mf.GetMetric())
150+
}
151+
return result, nil
140152
}
141153

142154
// CollectAndCompare registers the provided Collector with a newly created

prometheus/testutil/testutil_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,30 @@ some_total{label1="value1"} 1
306306
t.Errorf("Expected\n%#+v\nGot:\n%#+v", expectedError, err.Error())
307307
}
308308
}
309+
310+
func TestCollectAndCount(t *testing.T) {
311+
c := prometheus.NewCounterVec(
312+
prometheus.CounterOpts{
313+
Name: "some_total",
314+
Help: "A value that represents a counter.",
315+
},
316+
[]string{"foo"},
317+
)
318+
if got, want := CollectAndCount(c), 0; got != want {
319+
t.Errorf("unexpected metric count, got %d, want %d", got, want)
320+
}
321+
c.WithLabelValues("bar")
322+
if got, want := CollectAndCount(c), 1; got != want {
323+
t.Errorf("unexpected metric count, got %d, want %d", got, want)
324+
}
325+
c.WithLabelValues("baz")
326+
if got, want := CollectAndCount(c), 2; got != want {
327+
t.Errorf("unexpected metric count, got %d, want %d", got, want)
328+
}
329+
if got, want := CollectAndCount(c, "some_total"), 2; got != want {
330+
t.Errorf("unexpected metric count, got %d, want %d", got, want)
331+
}
332+
if got, want := CollectAndCount(c, "some_other_total"), 0; got != want {
333+
t.Errorf("unexpected metric count, got %d, want %d", got, want)
334+
}
335+
}

0 commit comments

Comments
 (0)