Merge pull request #738 from colega/colega/gauge-func-const-labels-godoc

GaugeFunc: ConstLabels godoc example
This commit is contained in:
Björn Rabenstein 2020-04-24 14:29:25 +02:00 committed by GitHub
commit a200f1930c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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{