2014-12-11 02:01:46 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-02-26 02:36:30 +03:00
|
|
|
dto "github.com/prometheus/client_golang/Godeps/_workspace/src/github.com/prometheus/client_model/go"
|
2014-12-11 02:01:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGoCollector(t *testing.T) {
|
|
|
|
var (
|
|
|
|
c = NewGoCollector()
|
|
|
|
ch = make(chan Metric)
|
|
|
|
waitc = make(chan struct{})
|
|
|
|
closec = make(chan struct{})
|
|
|
|
old = -1
|
|
|
|
)
|
|
|
|
defer close(closec)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
c.Collect(ch)
|
|
|
|
go func(c <-chan struct{}) {
|
|
|
|
<-c
|
|
|
|
}(closec)
|
|
|
|
<-waitc
|
|
|
|
c.Collect(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case metric := <-ch:
|
|
|
|
switch m := metric.(type) {
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
// Attention, this also catches Counter...
|
2014-12-11 02:01:46 +03:00
|
|
|
case Gauge:
|
|
|
|
pb := &dto.Metric{}
|
|
|
|
m.Write(pb)
|
|
|
|
|
|
|
|
if old == -1 {
|
|
|
|
old = int(pb.GetGauge().GetValue())
|
|
|
|
close(waitc)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := int(pb.GetGauge().GetValue()) - old; diff != 1 {
|
2015-01-21 15:44:43 +03:00
|
|
|
// TODO: This is flaky in highly concurrent situations.
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
t.Errorf("want 1 new goroutine, got %d", diff)
|
2014-12-11 02:01:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
t.Errorf("want type Gauge, got %s", reflect.TypeOf(metric))
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("expected collect timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|