GaugeFunc: ConstLabels godoc example

Document the example usage of ConstLabels to register several GaugeFuncs
on the same metric name. Also reference it from the NewCounterFunc
documentation as it's similar.

Ref: https://github.com/prometheus/client_golang/pull/736

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This commit is contained in:
Oleg Zaytsev 2020-04-21 10:56:51 +02:00
parent efb148ca4d
commit 38045061c3
No known key found for this signature in database
GPG Key ID: 7E9FE9FD48F512EF
2 changed files with 41 additions and 1 deletions

View File

@ -309,6 +309,8 @@ type CounterFunc interface {
// provided function must be concurrency-safe. The function should also honor // provided function must be concurrency-safe. The function should also honor
// the contract for a Counter (values only go up, not down), but compliance will // the contract for a Counter (values only go up, not down), but compliance will
// not be checked. // not be checked.
//
// Check out the ExampleGaugeFunc examples for the similar GaugeFunc.
func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc { func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
return newValueFunc(NewDesc( return newValueFunc(NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name), BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),

View File

@ -72,7 +72,7 @@ func ExampleGaugeVec() {
opsQueued.With(prometheus.Labels{"type": "delete", "user": "alice"}).Inc() opsQueued.With(prometheus.Labels{"type": "delete", "user": "alice"}).Inc()
} }
func ExampleGaugeFunc() { func ExampleGaugeFunc_simple() {
if err := prometheus.Register(prometheus.NewGaugeFunc( if err := prometheus.Register(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Subsystem: "runtime", Subsystem: "runtime",
@ -90,6 +90,44 @@ func ExampleGaugeFunc() {
// GaugeFunc 'goroutines_count' registered. // GaugeFunc 'goroutines_count' registered.
} }
func ExampleGaugeFunc_constLabels() {
// primaryDB and secondaryDB represent two example *sql.DB connections we want to instrument.
var primaryDB, secondaryDB interface {
Stats() struct{ OpenConnections int }
}
if err := prometheus.Register(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "mysql",
Name: "connections_open",
Help: "Number of mysql connections open.",
ConstLabels: prometheus.Labels{"destination": "primary"},
},
func() float64 { return float64(primaryDB.Stats().OpenConnections) },
)); err == nil {
fmt.Println(`GaugeFunc 'connections_open' for primary DB connection registered with labels {destination="primary"}`)
}
if err := prometheus.Register(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "mysql",
Name: "connections_open",
Help: "Number of mysql connections open.",
ConstLabels: prometheus.Labels{"destination": "secondary"},
},
func() float64 { return float64(secondaryDB.Stats().OpenConnections) },
)); err == nil {
fmt.Println(`GaugeFunc 'connections_open' for secondary DB connection registered with labels {destination="secondary"}`)
}
// Note that we can register more than once GaugeFunc with same metric name
// as long as their const labels are consistent.
// Output:
// GaugeFunc 'connections_open' for primary DB connection registered with labels {destination="primary"}
// GaugeFunc 'connections_open' for secondary DB connection registered with labels {destination="secondary"}
}
func ExampleCounterVec() { func ExampleCounterVec() {
httpReqs := prometheus.NewCounterVec( httpReqs := prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{