@@ -72,7 +72,7 @@ func ExampleGaugeVec() {
72
72
opsQueued .With (prometheus.Labels {"type" : "delete" , "user" : "alice" }).Inc ()
73
73
}
74
74
75
- func ExampleGaugeFunc () {
75
+ func ExampleGaugeFunc_simple () {
76
76
if err := prometheus .Register (prometheus .NewGaugeFunc (
77
77
prometheus.GaugeOpts {
78
78
Subsystem : "runtime" ,
@@ -90,6 +90,44 @@ func ExampleGaugeFunc() {
90
90
// GaugeFunc 'goroutines_count' registered.
91
91
}
92
92
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
+
93
131
func ExampleCounterVec () {
94
132
httpReqs := prometheus .NewCounterVec (
95
133
prometheus.CounterOpts {
0 commit comments