vendoring: Update client_golang/prometheus

This is mostly required to enable summaries without quantiles
This commit is contained in:
beorn7 2017-04-11 12:51:50 +02:00
parent 92d4cf7663
commit 1ae50b1d1b
13 changed files with 304 additions and 219 deletions

View file

@ -30,16 +30,8 @@ type Counter interface {
Metric Metric
Collector Collector
// Set is used to set the Counter to an arbitrary value. It is only used // Inc increments the counter by 1. Use Add to increment it by arbitrary
// if you have to transfer a value from an external counter into this // non-negative values.
// Prometheus metric. Do not use it for regular handling of a
// Prometheus counter (as it can be used to break the contract of
// monotonically increasing values).
//
// Deprecated: Use NewConstMetric to create a counter for an external
// value. A Counter should never be set.
Set(float64)
// Inc increments the counter by 1.
Inc() Inc()
// Add adds the given value to the counter. It panics if the value is < // Add adds the given value to the counter. It panics if the value is <
// 0. // 0.

View file

@ -16,20 +16,15 @@ package prometheus
import ( import (
"errors" "errors"
"fmt" "fmt"
"regexp"
"sort" "sort"
"strings" "strings"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/prometheus/common/model"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
) )
var (
metricNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_:]*$`)
labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
)
// reservedLabelPrefix is a prefix which is not legal in user-supplied // reservedLabelPrefix is a prefix which is not legal in user-supplied
// label names. // label names.
const reservedLabelPrefix = "__" const reservedLabelPrefix = "__"
@ -78,7 +73,7 @@ type Desc struct {
// Help string. Each Desc with the same fqName must have the same // Help string. Each Desc with the same fqName must have the same
// dimHash. // dimHash.
dimHash uint64 dimHash uint64
// err is an error that occured during construction. It is reported on // err is an error that occurred during construction. It is reported on
// registration time. // registration time.
err error err error
} }
@ -103,7 +98,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
d.err = errors.New("empty help string") d.err = errors.New("empty help string")
return d return d
} }
if !metricNameRE.MatchString(fqName) { if !model.IsValidMetricName(model.LabelValue(fqName)) {
d.err = fmt.Errorf("%q is not a valid metric name", fqName) d.err = fmt.Errorf("%q is not a valid metric name", fqName)
return d return d
} }
@ -200,6 +195,6 @@ func (d *Desc) String() string {
} }
func checkLabelName(l string) bool { func checkLabelName(l string) bool {
return labelNameRE.MatchString(l) && return model.LabelName(l).IsValid() &&
!strings.HasPrefix(l, reservedLabelPrefix) !strings.HasPrefix(l, reservedLabelPrefix)
} }

View file

@ -59,7 +59,7 @@
// // The Handler function provides a default handler to expose metrics // // The Handler function provides a default handler to expose metrics
// // via an HTTP server. "/metrics" is the usual endpoint for that. // // via an HTTP server. "/metrics" is the usual endpoint for that.
// http.Handle("/metrics", promhttp.Handler()) // http.Handle("/metrics", promhttp.Handler())
// http.ListenAndServe(":8080", nil) // log.Fatal(http.ListenAndServe(":8080", nil))
// } // }
// //
// //
@ -69,7 +69,7 @@
// Metrics // Metrics
// //
// The number of exported identifiers in this package might appear a bit // The number of exported identifiers in this package might appear a bit
// overwhelming. Hovever, in addition to the basic plumbing shown in the example // overwhelming. However, in addition to the basic plumbing shown in the example
// above, you only need to understand the different metric types and their // above, you only need to understand the different metric types and their
// vector versions for basic usage. // vector versions for basic usage.
// //
@ -95,8 +95,8 @@
// SummaryVec, HistogramVec, and UntypedVec are not. // SummaryVec, HistogramVec, and UntypedVec are not.
// //
// To create instances of Metrics and their vector versions, you need a suitable // To create instances of Metrics and their vector versions, you need a suitable
// …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, // …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, HistogramOpts, or
// HistogramOpts, or UntypedOpts. // UntypedOpts.
// //
// Custom Collectors and constant Metrics // Custom Collectors and constant Metrics
// //
@ -114,8 +114,8 @@
// Metric instances “on the fly” using NewConstMetric, NewConstHistogram, and // Metric instances “on the fly” using NewConstMetric, NewConstHistogram, and
// NewConstSummary (and their respective Must… versions). That will happen in // NewConstSummary (and their respective Must… versions). That will happen in
// the Collect method. The Describe method has to return separate Desc // the Collect method. The Describe method has to return separate Desc
// instances, representative of the “throw-away” metrics to be created // instances, representative of the “throw-away” metrics to be created later.
// later. NewDesc comes in handy to create those Desc instances. // NewDesc comes in handy to create those Desc instances.
// //
// The Collector example illustrates the use case. You can also look at the // The Collector example illustrates the use case. You can also look at the
// source code of the processCollector (mirroring process metrics), the // source code of the processCollector (mirroring process metrics), the
@ -129,32 +129,32 @@
// Advanced Uses of the Registry // Advanced Uses of the Registry
// //
// While MustRegister is the by far most common way of registering a Collector, // While MustRegister is the by far most common way of registering a Collector,
// sometimes you might want to handle the errors the registration might // sometimes you might want to handle the errors the registration might cause.
// cause. As suggested by the name, MustRegister panics if an error occurs. With // As suggested by the name, MustRegister panics if an error occurs. With the
// the Register function, the error is returned and can be handled. // Register function, the error is returned and can be handled.
// //
// An error is returned if the registered Collector is incompatible or // An error is returned if the registered Collector is incompatible or
// inconsistent with already registered metrics. The registry aims for // inconsistent with already registered metrics. The registry aims for
// consistency of the collected metrics according to the Prometheus data // consistency of the collected metrics according to the Prometheus data model.
// model. Inconsistencies are ideally detected at registration time, not at // Inconsistencies are ideally detected at registration time, not at collect
// collect time. The former will usually be detected at start-up time of a // time. The former will usually be detected at start-up time of a program,
// program, while the latter will only happen at scrape time, possibly not even // while the latter will only happen at scrape time, possibly not even on the
// on the first scrape if the inconsistency only becomes relevant later. That is // first scrape if the inconsistency only becomes relevant later. That is the
// the main reason why a Collector and a Metric have to describe themselves to // main reason why a Collector and a Metric have to describe themselves to the
// the registry. // registry.
// //
// So far, everything we did operated on the so-called default registry, as it // So far, everything we did operated on the so-called default registry, as it
// can be found in the global DefaultRegistry variable. With NewRegistry, you // can be found in the global DefaultRegistry variable. With NewRegistry, you
// can create a custom registry, or you can even implement the Registerer or // can create a custom registry, or you can even implement the Registerer or
// Gatherer interfaces yourself. The methods Register and Unregister work in // Gatherer interfaces yourself. The methods Register and Unregister work in the
// the same way on a custom registry as the global functions Register and // same way on a custom registry as the global functions Register and Unregister
// Unregister on the default registry. // on the default registry.
// //
// There are a number of uses for custom registries: You can use registries // There are a number of uses for custom registries: You can use registries with
// with special properties, see NewPedanticRegistry. You can avoid global state, // special properties, see NewPedanticRegistry. You can avoid global state, as
// as it is imposed by the DefaultRegistry. You can use multiple registries at // it is imposed by the DefaultRegistry. You can use multiple registries at the
// the same time to expose different metrics in different ways. You can use // same time to expose different metrics in different ways. You can use separate
// separate registries for testing purposes. // registries for testing purposes.
// //
// Also note that the DefaultRegistry comes registered with a Collector for Go // Also note that the DefaultRegistry comes registered with a Collector for Go
// runtime metrics (via NewGoCollector) and a Collector for process metrics (via // runtime metrics (via NewGoCollector) and a Collector for process metrics (via
@ -166,16 +166,20 @@
// The Registry implements the Gatherer interface. The caller of the Gather // The Registry implements the Gatherer interface. The caller of the Gather
// method can then expose the gathered metrics in some way. Usually, the metrics // method can then expose the gathered metrics in some way. Usually, the metrics
// are served via HTTP on the /metrics endpoint. That's happening in the example // are served via HTTP on the /metrics endpoint. That's happening in the example
// above. The tools to expose metrics via HTTP are in the promhttp // above. The tools to expose metrics via HTTP are in the promhttp sub-package.
// sub-package. (The top-level functions in the prometheus package are // (The top-level functions in the prometheus package are deprecated.)
// deprecated.)
// //
// Pushing to the Pushgateway // Pushing to the Pushgateway
// //
// Function for pushing to the Pushgateway can be found in the push sub-package. // Function for pushing to the Pushgateway can be found in the push sub-package.
// //
// Graphite Bridge
//
// Functions and examples to push metrics from a Gatherer to Graphite can be
// found in the graphite sub-package.
//
// Other Means of Exposition // Other Means of Exposition
// //
// More ways of exposing metrics can easily be added. Sending metrics to // More ways of exposing metrics can easily be added by following the approaches
// Graphite would be an example that will soon be implemented. // of the existing implementations.
package prometheus package prometheus

View file

@ -27,16 +27,21 @@ type Gauge interface {
// Set sets the Gauge to an arbitrary value. // Set sets the Gauge to an arbitrary value.
Set(float64) Set(float64)
// Inc increments the Gauge by 1. // Inc increments the Gauge by 1. Use Add to increment it by arbitrary
// values.
Inc() Inc()
// Dec decrements the Gauge by 1. // Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
// values.
Dec() Dec()
// Add adds the given value to the Gauge. (The value can be // Add adds the given value to the Gauge. (The value can be negative,
// negative, resulting in a decrease of the Gauge.) // resulting in a decrease of the Gauge.)
Add(float64) Add(float64)
// Sub subtracts the given value from the Gauge. (The value can be // Sub subtracts the given value from the Gauge. (The value can be
// negative, resulting in an increase of the Gauge.) // negative, resulting in an increase of the Gauge.)
Sub(float64) Sub(float64)
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
SetToCurrentTime()
} }
// GaugeOpts is an alias for Opts. See there for doc comments. // GaugeOpts is an alias for Opts. See there for doc comments.

View file

@ -8,7 +8,8 @@ import (
) )
type goCollector struct { type goCollector struct {
goroutines Gauge goroutinesDesc *Desc
threadsDesc *Desc
gcDesc *Desc gcDesc *Desc
// metrics to describe and collect // metrics to describe and collect
@ -19,11 +20,14 @@ type goCollector struct {
// go process. // go process.
func NewGoCollector() Collector { func NewGoCollector() Collector {
return &goCollector{ return &goCollector{
goroutines: NewGauge(GaugeOpts{ goroutinesDesc: NewDesc(
Namespace: "go", "go_goroutines",
Name: "goroutines", "Number of goroutines that currently exist.",
Help: "Number of goroutines that currently exist.", nil, nil),
}), threadsDesc: NewDesc(
"go_threads",
"Number of OS threads created",
nil, nil),
gcDesc: NewDesc( gcDesc: NewDesc(
"go_gc_duration_seconds", "go_gc_duration_seconds",
"A summary of the GC invocation durations.", "A summary of the GC invocation durations.",
@ -48,7 +52,7 @@ func NewGoCollector() Collector {
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("sys_bytes"), memstatNamespace("sys_bytes"),
"Number of bytes obtained by system. Sum of all system allocations.", "Number of bytes obtained from system.",
nil, nil, nil, nil,
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
@ -111,12 +115,12 @@ func NewGoCollector() Collector {
valType: GaugeValue, valType: GaugeValue,
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("heap_released_bytes_total"), memstatNamespace("heap_released_bytes"),
"Total number of heap bytes released to OS.", "Number of heap bytes released to OS.",
nil, nil, nil, nil,
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
valType: CounterValue, valType: GaugeValue,
}, { }, {
desc: NewDesc( desc: NewDesc(
memstatNamespace("heap_objects"), memstatNamespace("heap_objects"),
@ -213,6 +217,14 @@ func NewGoCollector() Collector {
), ),
eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 }, eval: func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 },
valType: GaugeValue, valType: GaugeValue,
}, {
desc: NewDesc(
memstatNamespace("gc_cpu_fraction"),
"The fraction of this program's available CPU time used by the GC since the program started.",
nil, nil,
),
eval: func(ms *runtime.MemStats) float64 { return ms.GCCPUFraction },
valType: GaugeValue,
}, },
}, },
} }
@ -224,9 +236,9 @@ func memstatNamespace(s string) string {
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *goCollector) Describe(ch chan<- *Desc) { func (c *goCollector) Describe(ch chan<- *Desc) {
ch <- c.goroutines.Desc() ch <- c.goroutinesDesc
ch <- c.threadsDesc
ch <- c.gcDesc ch <- c.gcDesc
for _, i := range c.metrics { for _, i := range c.metrics {
ch <- i.desc ch <- i.desc
} }
@ -234,8 +246,9 @@ func (c *goCollector) Describe(ch chan<- *Desc) {
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
func (c *goCollector) Collect(ch chan<- Metric) { func (c *goCollector) Collect(ch chan<- Metric) {
c.goroutines.Set(float64(runtime.NumGoroutine())) ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
ch <- c.goroutines n, _ := runtime.ThreadCreateProfile(nil)
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, float64(n))
var stats debug.GCStats var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5) stats.PauseQuantiles = make([]time.Duration, 5)

View file

@ -62,7 +62,7 @@ func giveBuf(buf *bytes.Buffer) {
// //
// Deprecated: Please note the issues described in the doc comment of // Deprecated: Please note the issues described in the doc comment of
// InstrumentHandler. You might want to consider using promhttp.Handler instead // InstrumentHandler. You might want to consider using promhttp.Handler instead
// (which is non instrumented). // (which is not instrumented).
func Handler() http.Handler { func Handler() http.Handler {
return InstrumentHandler("prometheus", UninstrumentedHandler()) return InstrumentHandler("prometheus", UninstrumentedHandler())
} }
@ -172,6 +172,9 @@ func nowSeries(t ...time.Time) nower {
// httputil.ReverseProxy is a prominent example for a handler // httputil.ReverseProxy is a prominent example for a handler
// performing such writes. // performing such writes.
// //
// - It has additional issues with HTTP/2, cf.
// https://github.com/prometheus/client_golang/issues/272.
//
// Upcoming versions of this package will provide ways of instrumenting HTTP // Upcoming versions of this package will provide ways of instrumenting HTTP
// handlers that are more flexible and have fewer issues. Please prefer direct // handlers that are more flexible and have fewer issues. Please prefer direct
// instrumentation in the meantime. // instrumentation in the meantime.
@ -190,6 +193,7 @@ func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWri
SummaryOpts{ SummaryOpts{
Subsystem: "http", Subsystem: "http",
ConstLabels: Labels{"handler": handlerName}, ConstLabels: Labels{"handler": handlerName},
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, },
handlerFunc, handlerFunc,
) )
@ -245,34 +249,52 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
}, },
instLabels, instLabels,
) )
if err := Register(reqCnt); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqCnt = are.ExistingCollector.(*CounterVec)
} else {
panic(err)
}
}
opts.Name = "request_duration_microseconds" opts.Name = "request_duration_microseconds"
opts.Help = "The HTTP request latencies in microseconds." opts.Help = "The HTTP request latencies in microseconds."
reqDur := NewSummary(opts) reqDur := NewSummary(opts)
if err := Register(reqDur); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqDur = are.ExistingCollector.(Summary)
} else {
panic(err)
}
}
opts.Name = "request_size_bytes" opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes." opts.Help = "The HTTP request sizes in bytes."
reqSz := NewSummary(opts) reqSz := NewSummary(opts)
if err := Register(reqSz); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqSz = are.ExistingCollector.(Summary)
} else {
panic(err)
}
}
opts.Name = "response_size_bytes" opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes." opts.Help = "The HTTP response sizes in bytes."
resSz := NewSummary(opts) resSz := NewSummary(opts)
if err := Register(resSz); err != nil {
regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec) if are, ok := err.(AlreadyRegisteredError); ok {
regReqDur := MustRegisterOrGet(reqDur).(Summary) resSz = are.ExistingCollector.(Summary)
regReqSz := MustRegisterOrGet(reqSz).(Summary) } else {
regResSz := MustRegisterOrGet(resSz).(Summary) panic(err)
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
delegate := &responseWriterDelegator{ResponseWriter: w} delegate := &responseWriterDelegator{ResponseWriter: w}
out := make(chan int) out := computeApproximateRequestSize(r)
urlLen := 0
if r.URL != nil {
urlLen = len(r.URL.String())
}
go computeApproximateRequestSize(r, out, urlLen)
_, cn := w.(http.CloseNotifier) _, cn := w.(http.CloseNotifier)
_, fl := w.(http.Flusher) _, fl := w.(http.Flusher)
@ -290,14 +312,24 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
method := sanitizeMethod(r.Method) method := sanitizeMethod(r.Method)
code := sanitizeCode(delegate.status) code := sanitizeCode(delegate.status)
regReqCnt.WithLabelValues(method, code).Inc() reqCnt.WithLabelValues(method, code).Inc()
regReqDur.Observe(elapsed) reqDur.Observe(elapsed)
regResSz.Observe(float64(delegate.written)) resSz.Observe(float64(delegate.written))
regReqSz.Observe(float64(<-out)) reqSz.Observe(float64(<-out))
}) })
} }
func computeApproximateRequestSize(r *http.Request, out chan int, s int) { func computeApproximateRequestSize(r *http.Request) <-chan int {
// Get URL length in current go routine for avoiding a race condition.
// HandlerFunc that runs in parallel may modify the URL.
s := 0
if r.URL != nil {
s += len(r.URL.String())
}
out := make(chan int, 1)
go func() {
s += len(r.Method) s += len(r.Method)
s += len(r.Proto) s += len(r.Proto)
for name, values := range r.Header { for name, values := range r.Header {
@ -314,6 +346,10 @@ func computeApproximateRequestSize(r *http.Request, out chan int, s int) {
s += int(r.ContentLength) s += int(r.ContentLength)
} }
out <- s out <- s
close(out)
}()
return out
} }
type responseWriterDelegator struct { type responseWriterDelegator struct {

View file

@ -19,10 +19,10 @@ type processCollector struct {
pid int pid int
collectFn func(chan<- Metric) collectFn func(chan<- Metric)
pidFn func() (int, error) pidFn func() (int, error)
cpuTotal Counter cpuTotal *Desc
openFDs, maxFDs Gauge openFDs, maxFDs *Desc
vsize, rss Gauge vsize, rss *Desc
startTime Gauge startTime *Desc
} }
// NewProcessCollector returns a collector which exports the current state of // NewProcessCollector returns a collector which exports the current state of
@ -44,40 +44,45 @@ func NewProcessCollectorPIDFn(
pidFn func() (int, error), pidFn func() (int, error),
namespace string, namespace string,
) Collector { ) Collector {
ns := ""
if len(namespace) > 0 {
ns = namespace + "_"
}
c := processCollector{ c := processCollector{
pidFn: pidFn, pidFn: pidFn,
collectFn: func(chan<- Metric) {}, collectFn: func(chan<- Metric) {},
cpuTotal: NewCounter(CounterOpts{ cpuTotal: NewDesc(
Namespace: namespace, ns+"process_cpu_seconds_total",
Name: "process_cpu_seconds_total", "Total user and system CPU time spent in seconds.",
Help: "Total user and system CPU time spent in seconds.", nil, nil,
}), ),
openFDs: NewGauge(GaugeOpts{ openFDs: NewDesc(
Namespace: namespace, ns+"process_open_fds",
Name: "process_open_fds", "Number of open file descriptors.",
Help: "Number of open file descriptors.", nil, nil,
}), ),
maxFDs: NewGauge(GaugeOpts{ maxFDs: NewDesc(
Namespace: namespace, ns+"process_max_fds",
Name: "process_max_fds", "Maximum number of open file descriptors.",
Help: "Maximum number of open file descriptors.", nil, nil,
}), ),
vsize: NewGauge(GaugeOpts{ vsize: NewDesc(
Namespace: namespace, ns+"process_virtual_memory_bytes",
Name: "process_virtual_memory_bytes", "Virtual memory size in bytes.",
Help: "Virtual memory size in bytes.", nil, nil,
}), ),
rss: NewGauge(GaugeOpts{ rss: NewDesc(
Namespace: namespace, ns+"process_resident_memory_bytes",
Name: "process_resident_memory_bytes", "Resident memory size in bytes.",
Help: "Resident memory size in bytes.", nil, nil,
}), ),
startTime: NewGauge(GaugeOpts{ startTime: NewDesc(
Namespace: namespace, ns+"process_start_time_seconds",
Name: "process_start_time_seconds", "Start time of the process since unix epoch in seconds.",
Help: "Start time of the process since unix epoch in seconds.", nil, nil,
}), ),
} }
// Set up process metric collection if supported by the runtime. // Set up process metric collection if supported by the runtime.
@ -90,12 +95,12 @@ func NewProcessCollectorPIDFn(
// Describe returns all descriptions of the collector. // Describe returns all descriptions of the collector.
func (c *processCollector) Describe(ch chan<- *Desc) { func (c *processCollector) Describe(ch chan<- *Desc) {
ch <- c.cpuTotal.Desc() ch <- c.cpuTotal
ch <- c.openFDs.Desc() ch <- c.openFDs
ch <- c.maxFDs.Desc() ch <- c.maxFDs
ch <- c.vsize.Desc() ch <- c.vsize
ch <- c.rss.Desc() ch <- c.rss
ch <- c.startTime.Desc() ch <- c.startTime
} }
// Collect returns the current state of all metrics of the collector. // Collect returns the current state of all metrics of the collector.
@ -117,26 +122,19 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
} }
if stat, err := p.NewStat(); err == nil { if stat, err := p.NewStat(); err == nil {
c.cpuTotal.Set(stat.CPUTime()) ch <- MustNewConstMetric(c.cpuTotal, CounterValue, stat.CPUTime())
ch <- c.cpuTotal ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(stat.VirtualMemory()))
c.vsize.Set(float64(stat.VirtualMemory())) ch <- MustNewConstMetric(c.rss, GaugeValue, float64(stat.ResidentMemory()))
ch <- c.vsize
c.rss.Set(float64(stat.ResidentMemory()))
ch <- c.rss
if startTime, err := stat.StartTime(); err == nil { if startTime, err := stat.StartTime(); err == nil {
c.startTime.Set(startTime) ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime)
ch <- c.startTime
} }
} }
if fds, err := p.FileDescriptorsLen(); err == nil { if fds, err := p.FileDescriptorsLen(); err == nil {
c.openFDs.Set(float64(fds)) ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(fds))
ch <- c.openFDs
} }
if limits, err := p.NewLimits(); err == nil { if limits, err := p.NewLimits(); err == nil {
c.maxFDs.Set(float64(limits.OpenFiles)) ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(limits.OpenFiles))
ch <- c.maxFDs
} }
} }

View file

@ -152,38 +152,6 @@ func MustRegister(cs ...Collector) {
DefaultRegisterer.MustRegister(cs...) DefaultRegisterer.MustRegister(cs...)
} }
// 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.
//
// Deprecated: RegisterOrGet is merely a convenience function for the
// implementation as described in the documentation for
// AlreadyRegisteredError. As the use case is relatively rare, this function
// will be removed in a future version of this package to clean up the
// namespace.
func RegisterOrGet(c Collector) (Collector, error) {
if err := Register(c); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
return are.ExistingCollector, nil
}
return nil, err
}
return c, nil
}
// MustRegisterOrGet behaves like RegisterOrGet but panics instead of returning
// an error.
//
// Deprecated: This is deprecated for the same reason RegisterOrGet is. See
// there for details.
func MustRegisterOrGet(c Collector) Collector {
c, err := RegisterOrGet(c)
if err != nil {
panic(err)
}
return c
}
// Unregister removes the registration of the provided Collector from the // Unregister removes the registration of the provided Collector from the
// DefaultRegisterer. // DefaultRegisterer.
// //
@ -201,25 +169,6 @@ func (gf GathererFunc) Gather() ([]*dto.MetricFamily, error) {
return gf() return gf()
} }
// SetMetricFamilyInjectionHook replaces the DefaultGatherer with one that
// gathers from the previous DefaultGatherers but then merges the MetricFamily
// protobufs returned from the provided hook function with the MetricFamily
// protobufs returned from the original DefaultGatherer.
//
// Deprecated: This function manipulates the DefaultGatherer variable. Consider
// the implications, i.e. don't do this concurrently with any uses of the
// DefaultGatherer. In the rare cases where you need to inject MetricFamily
// protobufs directly, it is recommended to use a custom Registry and combine it
// with a custom Gatherer using the Gatherers type (see
// there). SetMetricFamilyInjectionHook only exists for compatibility reasons
// with previous versions of this package.
func SetMetricFamilyInjectionHook(hook func() []*dto.MetricFamily) {
DefaultGatherer = Gatherers{
DefaultGatherer,
GathererFunc(func() ([]*dto.MetricFamily, error) { return hook(), nil }),
}
}
// AlreadyRegisteredError is returned by the Register method if the Collector to // AlreadyRegisteredError is returned by the Register method if the Collector to
// be registered has already been registered before, or a different Collector // be registered has already been registered before, or a different Collector
// that collects the same metrics has been registered before. Registration fails // that collects the same metrics has been registered before. Registration fails
@ -294,7 +243,7 @@ func (r *Registry) Register(c Collector) error {
}() }()
r.mtx.Lock() r.mtx.Lock()
defer r.mtx.Unlock() defer r.mtx.Unlock()
// Coduct various tests... // Conduct various tests...
for desc := range descChan { for desc := range descChan {
// Is the descriptor valid at all? // Is the descriptor valid at all?
@ -447,7 +396,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
// Drain metricChan in case of premature return. // Drain metricChan in case of premature return.
defer func() { defer func() {
for _ = range metricChan { for range metricChan {
} }
}() }()
@ -683,7 +632,7 @@ func (s metricSorter) Less(i, j int) bool {
return s[i].GetTimestampMs() < s[j].GetTimestampMs() return s[i].GetTimestampMs() < s[j].GetTimestampMs()
} }
// normalizeMetricFamilies returns a MetricFamily slice whith empty // normalizeMetricFamilies returns a MetricFamily slice with empty
// MetricFamilies pruned and the remaining MetricFamilies sorted by name within // MetricFamilies pruned and the remaining MetricFamilies sorted by name within
// the slice, with the contained Metrics sorted within each MetricFamily. // the slice, with the contained Metrics sorted within each MetricFamily.
func normalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily { func normalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {

View file

@ -54,6 +54,9 @@ type Summary interface {
} }
// DefObjectives are the default Summary quantile values. // DefObjectives are the default Summary quantile values.
//
// Deprecated: DefObjectives will not be used as the default objectives in
// v0.10 of the library. The default Summary will have no quantiles then.
var ( 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}
@ -113,9 +116,15 @@ type SummaryOpts struct {
ConstLabels Labels ConstLabels Labels
// Objectives defines the quantile rank estimates with their respective // Objectives defines the quantile rank estimates with their respective
// absolute error. If Objectives[q] = e, then the value reported // absolute error. If Objectives[q] = e, then the value reported for q
// for q will be the φ-quantile value for some φ between q-e and q+e. // will be the φ-quantile value for some φ between q-e and q+e. The
// The default value is DefObjectives. // default value is DefObjectives. It is used if Objectives is left at
// its zero value (i.e. nil). To create a Summary without Objectives,
// set it to an empty map (i.e. map[float64]float64{}).
//
// Deprecated: Note that the current value of DefObjectives is
// deprecated. It will be replaced by an empty map in v0.10 of the
// library. Please explicitly set Objectives to the desired value.
Objectives map[float64]float64 Objectives map[float64]float64
// MaxAge defines the duration for which an observation stays relevant // MaxAge defines the duration for which an observation stays relevant
@ -183,7 +192,7 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
} }
} }
if len(opts.Objectives) == 0 { if opts.Objectives == nil {
opts.Objectives = DefObjectives opts.Objectives = DefObjectives
} }

View file

@ -0,0 +1,74 @@
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheus
import "time"
// Observer is the interface that wraps the Observe method, which is used by
// Histogram and Summary to add observations.
type Observer interface {
Observe(float64)
}
// The ObserverFunc type is an adapter to allow the use of ordinary
// functions as Observers. If f is a function with the appropriate
// signature, ObserverFunc(f) is an Observer that calls f.
//
// This adapter is usually used in connection with the Timer type, and there are
// two general use cases:
//
// The most common one is to use a Gauge as the Observer for a Timer.
// See the "Gauge" Timer example.
//
// The more advanced use case is to create a function that dynamically decides
// which Observer to use for observing the duration. See the "Complex" Timer
// example.
type ObserverFunc func(float64)
// Observe calls f(value). It implements Observer.
func (f ObserverFunc) Observe(value float64) {
f(value)
}
// Timer is a helper type to time functions. Use NewTimer to create new
// instances.
type Timer struct {
begin time.Time
observer Observer
}
// NewTimer creates a new Timer. The provided Observer is used to observe a
// duration in seconds. Timer is usually used to time a function call in the
// following way:
// func TimeMe() {
// timer := NewTimer(myHistogram)
// defer timer.ObserveDuration()
// // Do actual work.
// }
func NewTimer(o Observer) *Timer {
return &Timer{
begin: time.Now(),
observer: o,
}
}
// ObserveDuration records the duration passed since the Timer was created with
// NewTimer. It calls the Observe method of the Observer provided during
// construction with the duration in seconds as an argument. ObserveDuration is
// usually called with a defer statement.
func (t *Timer) ObserveDuration() {
if t.observer != nil {
t.observer.Observe(time.Since(t.begin).Seconds())
}
}

View file

@ -20,6 +20,11 @@ package prometheus
// no type information is implied. // no type information is implied.
// //
// To create Untyped instances, use NewUntyped. // To create Untyped instances, use NewUntyped.
//
// Deprecated: The Untyped type is deprecated because it doesn't make sense in
// direct instrumentation. If you need to mirror an external metric of unknown
// type (usually while writing exporters), Use MustNewConstMetric to create an
// untyped metric instance on the fly.
type Untyped interface { type Untyped interface {
Metric Metric
Collector Collector

View file

@ -19,6 +19,7 @@ import (
"math" "math"
"sort" "sort"
"sync/atomic" "sync/atomic"
"time"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
@ -43,7 +44,7 @@ var errInconsistentCardinality = errors.New("inconsistent label cardinality")
// ValueType. This is a low-level building block used by the library to back the // ValueType. This is a low-level building block used by the library to back the
// implementations of Counter, Gauge, and Untyped. // implementations of Counter, Gauge, and Untyped.
type value struct { type value struct {
// valBits containst the bits of the represented float64 value. It has // valBits contains the bits of the represented float64 value. It has
// to go first in the struct to guarantee alignment for atomic // to go first in the struct to guarantee alignment for atomic
// operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG // operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG
valBits uint64 valBits uint64
@ -80,6 +81,10 @@ func (v *value) Set(val float64) {
atomic.StoreUint64(&v.valBits, math.Float64bits(val)) atomic.StoreUint64(&v.valBits, math.Float64bits(val))
} }
func (v *value) SetToCurrentTime() {
v.Set(float64(time.Now().UnixNano()) / 1e9)
}
func (v *value) Inc() { func (v *value) Inc() {
v.Add(1) v.Add(1)
} }

18
vendor/vendor.json vendored
View file

@ -519,10 +519,10 @@
"revisionTime": "2016-06-15T09:26:46Z" "revisionTime": "2016-06-15T09:26:46Z"
}, },
{ {
"checksumSHA1": "KkB+77Ziom7N6RzSbyUwYGrmDeU=", "checksumSHA1": "d2irkxoHgazkTuLIvJGiYwagl8o=",
"path": "github.com/prometheus/client_golang/prometheus", "path": "github.com/prometheus/client_golang/prometheus",
"revision": "c5b7fccd204277076155f10851dad72b76a49317", "revision": "08fd2e12372a66e68e30523c7642e0cbc3e4fbde",
"revisionTime": "2016-08-17T15:48:24Z" "revisionTime": "2017-04-01T10:34:46Z"
}, },
{ {
"checksumSHA1": "DvwvOlPNAgRntBzt3b3OSRMS2N4=", "checksumSHA1": "DvwvOlPNAgRntBzt3b3OSRMS2N4=",
@ -531,10 +531,10 @@
"revisionTime": "2015-02-12T10:17:44Z" "revisionTime": "2015-02-12T10:17:44Z"
}, },
{ {
"checksumSHA1": "jG8qYuDUuaZeflt4JxBBdyQBsXw=", "checksumSHA1": "Wtpzndm/+bdwwNU5PCTfb4oUhc8=",
"path": "github.com/prometheus/common/expfmt", "path": "github.com/prometheus/common/expfmt",
"revision": "dd2f054febf4a6c00f2343686efb775948a8bff4", "revision": "49fee292b27bfff7f354ee0f64e1bc4850462edf",
"revisionTime": "2017-01-08T23:12:12Z" "revisionTime": "2017-02-20T10:38:46Z"
}, },
{ {
"checksumSHA1": "GWlM3d2vPYyNATtTFgftS10/A9w=", "checksumSHA1": "GWlM3d2vPYyNATtTFgftS10/A9w=",
@ -549,10 +549,10 @@
"revisionTime": "2017-01-08T23:12:12Z" "revisionTime": "2017-01-08T23:12:12Z"
}, },
{ {
"checksumSHA1": "vopCLXHzYm+3l5fPKOf4/fQwrCM=", "checksumSHA1": "0LL9u9tfv1KPBjNEiMDP6q7lpog=",
"path": "github.com/prometheus/common/model", "path": "github.com/prometheus/common/model",
"revision": "3007b6072c17c8d985734e6e19b1dea9174e13d3", "revision": "49fee292b27bfff7f354ee0f64e1bc4850462edf",
"revisionTime": "2017-02-19T00:35:58+01:00" "revisionTime": "2017-02-20T10:38:46Z"
}, },
{ {
"checksumSHA1": "ZbbESWBHHcPUJ/A5yrzKhTHuPc8=", "checksumSHA1": "ZbbESWBHHcPUJ/A5yrzKhTHuPc8=",