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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added testutil.CollectAndCount #703

Merged
merged 1 commit into from Jan 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions prometheus/testutil/testutil.go
Expand Up @@ -108,6 +108,33 @@ func ToFloat64(c prometheus.Collector) float64 {
panic(fmt.Errorf("collected a non-gauge/counter/untyped metric: %s", pb))
}

// CollectAndCount collects all Metrics from the provided Collector and returns their number.
//
// This can be used to assert the number of metrics collected by a given collector after certain operations.
//
// This function is only for testing purposes, and even for testing, other approaches
// are often more appropriate (see this package's documentation).
func CollectAndCount(c prometheus.Collector) int {
var (
mCount int
mChan = make(chan prometheus.Metric)
done = make(chan struct{})
)

go func() {
for range mChan {
mCount++
}
close(done)
}()

c.Collect(mChan)
close(mChan)
<-done

return mCount
}

// CollectAndCompare registers the provided Collector with a newly created
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
// metrics from the pedantic Registry.
Expand Down