Skip to content

Commit 9b5c5b8

Browse files
committed
Update basic example to use custom registry
Signed-off-by: Jéssica Lins <[email protected]>
1 parent 10b0550 commit 9b5c5b8

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

prometheus/doc.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,51 @@
3535
// "github.com/prometheus/client_golang/prometheus/promhttp"
3636
// )
3737
//
38-
// var (
39-
// cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
40-
// Name: "cpu_temperature_celsius",
41-
// Help: "Current temperature of the CPU.",
42-
// })
43-
// hdFailures = prometheus.NewCounterVec(
44-
// prometheus.CounterOpts{
45-
// Name: "hd_errors_total",
46-
// Help: "Number of hard-disk errors.",
47-
// },
48-
// []string{"device"},
49-
// )
50-
// )
38+
// type metrics struct {
39+
// cpuTemp prometheus.Gauge
40+
// hdFailures *prometheus.CounterVec
41+
// }
5142
//
52-
// func init() {
53-
// // Metrics have to be registered to be exposed:
54-
// prometheus.MustRegister(cpuTemp)
55-
// prometheus.MustRegister(hdFailures)
43+
// func NewMetrics(reg prometheus.Registerer) *metrics {
44+
// m := &metrics{
45+
// cpuTemp: prometheus.NewGauge(prometheus.GaugeOpts{
46+
// Name: "cpu_temperature_celsius",
47+
// Help: "Current temperature of the CPU.",
48+
// }),
49+
// hdFailures: prometheus.NewCounterVec(
50+
// prometheus.CounterOpts{
51+
// Name: "hd_errors_total",
52+
// Help: "Number of hard-disk errors.",
53+
// },
54+
// []string{"device"},
55+
// ),
56+
// }
57+
// reg.MustRegister(m.cpuTemp)
58+
// reg.MustRegister(m.hdFailures)
59+
// return m
5660
// }
5761
//
5862
// func main() {
59-
// cpuTemp.Set(65.3)
60-
// hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
61-
//
62-
// // The Handler function provides a default handler to expose metrics
63-
// // via an HTTP server. "/metrics" is the usual endpoint for that.
64-
// http.Handle("/metrics", promhttp.Handler())
63+
// // Create a non-global registry.
64+
// reg := prometheus.NewRegistry()
65+
//
66+
// // Create new metrics and register them using the custom registry.
67+
// m := NewMetrics(reg)
68+
// // Set values for the new created metrics.
69+
// m.cpuTemp.Set(65.3)
70+
// m.hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
71+
//
72+
// // Expose metrics and custom registry via an HTTP server
73+
// // using the HandleFor function. "/metrics" is the usual endpoint for that.
74+
// http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
6575
// log.Fatal(http.ListenAndServe(":8080", nil))
6676
// }
6777
//
6878
//
6979
// This is a complete program that exports two metrics, a Gauge and a Counter,
7080
// the latter with a label attached to turn it into a (one-dimensional) vector.
81+
// It register the metrics using a custom registry and exposes them via an HTTP server
82+
// on the /metrics endpoint.
7183
//
7284
// Metrics
7385
//

0 commit comments

Comments
 (0)