From 2e66d12fe5c6bc326ef8b70742b7f98ba7882674 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Wed, 3 Aug 2016 12:50:39 +0200 Subject: [PATCH] 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. --- prometheus/histogram.go | 8 +++---- prometheus/http.go | 8 +++---- prometheus/registry.go | 46 +++++++++++++++++++++++++---------------- prometheus/summary.go | 2 +- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 160b63c..11a9083 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -51,11 +51,11 @@ type Histogram interface { // bucket of a histogram ("le" -> "less or equal"). const bucketLabel = "le" +// DefBuckets are the default Histogram buckets. The default buckets are +// tailored to broadly measure the response time (in seconds) of a network +// service. Most likely, however, you will be required to define buckets +// customized to your use case. var ( - // DefBuckets are the default Histogram buckets. The default buckets are - // tailored to broadly measure the response time (in seconds) of a - // network service. Most likely, however, you will be required to define - // buckets customized to your use case. DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10} errBucketLabelNotAllowed = fmt.Errorf( diff --git a/prometheus/http.go b/prometheus/http.go index 41d1bd8..7ffe097 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -50,19 +50,19 @@ func Handler() http.Handler { 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 // error, no error logging, and compression if requested by the client. // -// If you want to create a Handler for the DefaultRegistry with different -// HandlerOpts, create it with HandlerFor with the DefaultRegistry and your +// If you want to create a Handler for the DefaultDeliverer with different +// HandlerOpts, create it with HandlerFor with the DefaultDeliverer and your // desired HandlerOpts. // // Note that in future versions of this package, UninstrumentedHandler will be // replaced by Handler (which will then return an uninstrumented handler, see // there for details). func UninstrumentedHandler() http.Handler { - return HandlerFor(DefaultRegistry, HandlerOpts{}) + return HandlerFor(DefaultDeliverer, HandlerOpts{}) } // HandlerFor returns an http.Handler for the provided Deliverer. The behavior ef diff --git a/prometheus/registry.go b/prometheus/registry.go index 9c94cf3..0a0d563 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -32,10 +32,19 @@ const ( capDescChan = 10 ) -// DefaultRegistry is the default registry implicitly used by a number of -// convenience functions. It has a ProcessCollector and a GoCollector -// pre-registered. -var DefaultRegistry = NewRegistry() +// DefaultRegistry is a Registry instance that has a ProcessCollector and a +// GoCollector pre-registered. DefaultRegisterer and DefaultDeliverer are both +// pointing to it. A number of convenience functions in this package act on +// 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() { MustRegister(NewProcessCollector(os.Getpid(), "")) @@ -70,9 +79,9 @@ func NewPedanticRegistry() *Registry { // Registerer is the interface for the part of a registry in charge of // registering and unregistering. Users of custom registries should use -// Registerer as type for registration purposes (rather then Registry). In that -// way, they are free to exchange the Registerer implementation (e.g. for -// testing purposes). +// Registerer as type for registration purposes (rather then the Registry type +// directly). In that way, they are free to exchange the Registerer +// implementation (e.g. for testing purposes). type Registerer interface { // Register registers a new Collector to be included in metrics // 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 -// the collected metrics. +// the collected metrics, wich the same general implication as described for the +// Registerer interface. type Deliverer interface { // Deliver collects metrics from registered Collectors and returns them // as lexicographically sorted MetricFamily protobufs. Even if an error @@ -125,24 +135,24 @@ type Deliverer interface { 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. 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. // -// MustRegister is a shortcut for MustRegisterWith(DefaultRegistry, cs...). See +// MustRegister is a shortcut for DefaultRegisterer.MustRegister(cs...). See // there for more details. 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 // 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 -// 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. func Unregister(c Collector) bool { - return DefaultRegistry.Unregister(c) + return DefaultRegisterer.Unregister(c) } // SetMetricFamilyInjectionHook sets a MetricFamily injection hook on the diff --git a/prometheus/summary.go b/prometheus/summary.go index 4ce13b2..fda213c 100644 --- a/prometheus/summary.go +++ b/prometheus/summary.go @@ -53,8 +53,8 @@ type Summary interface { Observe(float64) } +// DefObjectives are the default Summary quantile values. var ( - // DefObjectives are the default Summary quantile values. DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001} errQuantileLabelNotAllowed = fmt.Errorf(