MCPcopy
hub / github.com/prometheus/client_golang / ExampleGaugeFunc_constLabels

Function ExampleGaugeFunc_constLabels

prometheus/examples_test.go:94–130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

RegisterFunction · 0.92
NewGaugeFuncFunction · 0.92
StatsMethod · 0.80
PrintlnMethod · 0.65

Tested by

no test coverage detected