Skip to content

Commit a200f19

Browse files
authored
Merge pull request #738 from colega/colega/gauge-func-const-labels-godoc
GaugeFunc: ConstLabels godoc example
2 parents fe75699 + 3804506 commit a200f19

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

prometheus/counter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ type CounterFunc interface {
309309
// provided function must be concurrency-safe. The function should also honor
310310
// the contract for a Counter (values only go up, not down), but compliance will
311311
// not be checked.
312+
//
313+
// Check out the ExampleGaugeFunc examples for the similar GaugeFunc.
312314
func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
313315
return newValueFunc(NewDesc(
314316
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),

prometheus/examples_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ExampleGaugeVec() {
7272
opsQueued.With(prometheus.Labels{"type": "delete", "user": "alice"}).Inc()
7373
}
7474

75-
func ExampleGaugeFunc() {
75+
func ExampleGaugeFunc_simple() {
7676
if err := prometheus.Register(prometheus.NewGaugeFunc(
7777
prometheus.GaugeOpts{
7878
Subsystem: "runtime",
@@ -90,6 +90,44 @@ func ExampleGaugeFunc() {
9090
// GaugeFunc 'goroutines_count' registered.
9191
}
9292

93+
func ExampleGaugeFunc_constLabels() {
94+
// primaryDB and secondaryDB represent two example *sql.DB connections we want to instrument.
95+
var primaryDB, secondaryDB interface {
96+
Stats() struct{ OpenConnections int }
97+
}
98+
99+
if err := prometheus.Register(prometheus.NewGaugeFunc(
100+
prometheus.GaugeOpts{
101+
Namespace: "mysql",
102+
Name: "connections_open",
103+
Help: "Number of mysql connections open.",
104+
ConstLabels: prometheus.Labels{"destination": "primary"},
105+
},
106+
func() float64 { return float64(primaryDB.Stats().OpenConnections) },
107+
)); err == nil {
108+
fmt.Println(`GaugeFunc 'connections_open' for primary DB connection registered with labels {destination="primary"}`)
109+
}
110+
111+
if err := prometheus.Register(prometheus.NewGaugeFunc(
112+
prometheus.GaugeOpts{
113+
Namespace: "mysql",
114+
Name: "connections_open",
115+
Help: "Number of mysql connections open.",
116+
ConstLabels: prometheus.Labels{"destination": "secondary"},
117+
},
118+
func() float64 { return float64(secondaryDB.Stats().OpenConnections) },
119+
)); err == nil {
120+
fmt.Println(`GaugeFunc 'connections_open' for secondary DB connection registered with labels {destination="secondary"}`)
121+
}
122+
123+
// Note that we can register more than once GaugeFunc with same metric name
124+
// as long as their const labels are consistent.
125+
126+
// Output:
127+
// GaugeFunc 'connections_open' for primary DB connection registered with labels {destination="primary"}
128+
// GaugeFunc 'connections_open' for secondary DB connection registered with labels {destination="secondary"}
129+
}
130+
93131
func ExampleCounterVec() {
94132
httpReqs := prometheus.NewCounterVec(
95133
prometheus.CounterOpts{

0 commit comments

Comments
 (0)