Act on DefaultRegisterer and DefaultDeliverer where possible

Both point to the DefaultRegistry anyway, but users can now replace it
with interfaces rather than only other Registry instances.
This commit is contained in:
beorn7 2016-08-03 12:50:39 +02:00
parent e5c6302150
commit 2e66d12fe5
4 changed files with 37 additions and 27 deletions

View File

@ -51,11 +51,11 @@ type Histogram interface {
// bucket of a histogram ("le" -> "less or equal"). // bucket of a histogram ("le" -> "less or equal").
const bucketLabel = "le" const bucketLabel = "le"
var (
// DefBuckets are the default Histogram buckets. The default buckets are // DefBuckets are the default Histogram buckets. The default buckets are
// tailored to broadly measure the response time (in seconds) of a // tailored to broadly measure the response time (in seconds) of a network
// network service. Most likely, however, you will be required to define // service. Most likely, however, you will be required to define buckets
// buckets customized to your use case. // customized to your use case.
var (
DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10} DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
errBucketLabelNotAllowed = fmt.Errorf( errBucketLabelNotAllowed = fmt.Errorf(

View File

@ -50,19 +50,19 @@ func Handler() http.Handler {
return InstrumentHandler("prometheus", UninstrumentedHandler()) return InstrumentHandler("prometheus", UninstrumentedHandler())
} }
// UninstrumentedHandler returns an HTTP handler for the DefaultRegistry. The // UninstrumentedHandler returns an HTTP handler for the DefaultDeliverer. The
// Handler uses the default HandlerOpts, i.e. report the first error as an HTTP // Handler uses the default HandlerOpts, i.e. report the first error as an HTTP
// error, no error logging, and compression if requested by the client. // error, no error logging, and compression if requested by the client.
// //
// If you want to create a Handler for the DefaultRegistry with different // If you want to create a Handler for the DefaultDeliverer with different
// HandlerOpts, create it with HandlerFor with the DefaultRegistry and your // HandlerOpts, create it with HandlerFor with the DefaultDeliverer and your
// desired HandlerOpts. // desired HandlerOpts.
// //
// Note that in future versions of this package, UninstrumentedHandler will be // Note that in future versions of this package, UninstrumentedHandler will be
// replaced by Handler (which will then return an uninstrumented handler, see // replaced by Handler (which will then return an uninstrumented handler, see
// there for details). // there for details).
func UninstrumentedHandler() http.Handler { func UninstrumentedHandler() http.Handler {
return HandlerFor(DefaultRegistry, HandlerOpts{}) return HandlerFor(DefaultDeliverer, HandlerOpts{})
} }
// HandlerFor returns an http.Handler for the provided Deliverer. The behavior ef // HandlerFor returns an http.Handler for the provided Deliverer. The behavior ef

View File

@ -32,10 +32,19 @@ const (
capDescChan = 10 capDescChan = 10
) )
// DefaultRegistry is the default registry implicitly used by a number of // DefaultRegistry is a Registry instance that has a ProcessCollector and a
// convenience functions. It has a ProcessCollector and a GoCollector // GoCollector pre-registered. DefaultRegisterer and DefaultDeliverer are both
// pre-registered. // pointing to it. A number of convenience functions in this package act on
var DefaultRegistry = NewRegistry() // them. This approach to keep a default instance as global state mirrors the
// approach of other packages in the Go standard library. Note that there are
// caveats. Change the variables with caution and only if you understand the
// consequences. Users who want to avoid global state altogether should not
// use the convenience function and act on custom instances instead.
var (
DefaultRegistry = NewRegistry()
DefaultRegisterer Registerer = DefaultRegistry
DefaultDeliverer Deliverer = DefaultRegistry
)
func init() { func init() {
MustRegister(NewProcessCollector(os.Getpid(), "")) MustRegister(NewProcessCollector(os.Getpid(), ""))
@ -70,9 +79,9 @@ func NewPedanticRegistry() *Registry {
// Registerer is the interface for the part of a registry in charge of // Registerer is the interface for the part of a registry in charge of
// registering and unregistering. Users of custom registries should use // registering and unregistering. Users of custom registries should use
// Registerer as type for registration purposes (rather then Registry). In that // Registerer as type for registration purposes (rather then the Registry type
// way, they are free to exchange the Registerer implementation (e.g. for // directly). In that way, they are free to exchange the Registerer
// testing purposes). // implementation (e.g. for testing purposes).
type Registerer interface { type Registerer interface {
// Register registers a new Collector to be included in metrics // Register registers a new Collector to be included in metrics
// collection. It returns an error if the descriptors provided by the // collection. It returns an error if the descriptors provided by the
@ -107,7 +116,8 @@ type Registerer interface {
} }
// Deliverer is the interface for the part of a registry in charge of delivering // Deliverer is the interface for the part of a registry in charge of delivering
// the collected metrics. // the collected metrics, wich the same general implication as described for the
// Registerer interface.
type Deliverer interface { type Deliverer interface {
// Deliver collects metrics from registered Collectors and returns them // Deliver collects metrics from registered Collectors and returns them
// as lexicographically sorted MetricFamily protobufs. Even if an error // as lexicographically sorted MetricFamily protobufs. Even if an error
@ -125,24 +135,24 @@ type Deliverer interface {
Deliver() ([]*dto.MetricFamily, error) Deliver() ([]*dto.MetricFamily, error)
} }
// Register registers the provided Collector with the DefaultRegistry. // Register registers the provided Collector with the DefaultRegisterer.
// //
// Register is a shortcut for DefaultRegistry.Register(c). See there for more // Register is a shortcut for DefaultRegisterer.Register(c). See there for more
// details. // details.
func Register(c Collector) error { func Register(c Collector) error {
return DefaultRegistry.Register(c) return DefaultRegisterer.Register(c)
} }
// MustRegister registers the provided Collectors with the DefaultRegistry and // MustRegister registers the provided Collectors with the DefaultRegisterer and
// panics if any error occurs. // panics if any error occurs.
// //
// MustRegister is a shortcut for MustRegisterWith(DefaultRegistry, cs...). See // MustRegister is a shortcut for DefaultRegisterer.MustRegister(cs...). See
// there for more details. // there for more details.
func MustRegister(cs ...Collector) { func MustRegister(cs ...Collector) {
DefaultRegistry.MustRegister(cs...) DefaultRegisterer.MustRegister(cs...)
} }
// RegisterOrGet registers the provided Collector with the DefaultRegistry and // RegisterOrGet registers the provided Collector with the DefaultRegisterer and
// returns the Collector, unless an equal Collector was registered before, in // returns the Collector, unless an equal Collector was registered before, in
// which case that Collector is returned. // which case that Collector is returned.
// //
@ -175,12 +185,12 @@ func MustRegisterOrGet(c Collector) Collector {
} }
// Unregister removes the registration of the provided Collector from the // Unregister removes the registration of the provided Collector from the
// DefaultRegistry. // DefaultRegisterer.
// //
// Unregister is a shortcut for DefaultRegistry.Unregister(c). See there for // Unregister is a shortcut for DefaultRegisterer.Unregister(c). See there for
// more details. // more details.
func Unregister(c Collector) bool { func Unregister(c Collector) bool {
return DefaultRegistry.Unregister(c) return DefaultRegisterer.Unregister(c)
} }
// SetMetricFamilyInjectionHook sets a MetricFamily injection hook on the // SetMetricFamilyInjectionHook sets a MetricFamily injection hook on the

View File

@ -53,8 +53,8 @@ type Summary interface {
Observe(float64) Observe(float64)
} }
var (
// DefObjectives are the default Summary quantile values. // DefObjectives are the default Summary quantile values.
var (
DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001} DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
errQuantileLabelNotAllowed = fmt.Errorf( errQuantileLabelNotAllowed = fmt.Errorf(