Update basic example to use custom registry

Signed-off-by: Jéssica Lins <jessicaalins@gmail.com>
This commit is contained in:
Jéssica Lins 2022-10-21 15:47:51 +02:00
parent 10b0550932
commit 9b5c5b8a47
No known key found for this signature in database
GPG Key ID: D81F1550FA3B93C0
1 changed files with 34 additions and 22 deletions

View File

@ -35,39 +35,51 @@
// "github.com/prometheus/client_golang/prometheus/promhttp" // "github.com/prometheus/client_golang/prometheus/promhttp"
// ) // )
// //
// var ( // type metrics struct {
// cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{ // cpuTemp prometheus.Gauge
// Name: "cpu_temperature_celsius", // hdFailures *prometheus.CounterVec
// Help: "Current temperature of the CPU.", // }
// })
// hdFailures = prometheus.NewCounterVec(
// prometheus.CounterOpts{
// Name: "hd_errors_total",
// Help: "Number of hard-disk errors.",
// },
// []string{"device"},
// )
// )
// //
// func init() { // func NewMetrics(reg prometheus.Registerer) *metrics {
// // Metrics have to be registered to be exposed: // m := &metrics{
// prometheus.MustRegister(cpuTemp) // cpuTemp: prometheus.NewGauge(prometheus.GaugeOpts{
// prometheus.MustRegister(hdFailures) // Name: "cpu_temperature_celsius",
// Help: "Current temperature of the CPU.",
// }),
// hdFailures: prometheus.NewCounterVec(
// prometheus.CounterOpts{
// Name: "hd_errors_total",
// Help: "Number of hard-disk errors.",
// },
// []string{"device"},
// ),
// }
// reg.MustRegister(m.cpuTemp)
// reg.MustRegister(m.hdFailures)
// return m
// } // }
// //
// func main() { // func main() {
// cpuTemp.Set(65.3) // // Create a non-global registry.
// hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc() // reg := prometheus.NewRegistry()
// //
// // The Handler function provides a default handler to expose metrics // // Create new metrics and register them using the custom registry.
// // via an HTTP server. "/metrics" is the usual endpoint for that. // m := NewMetrics(reg)
// http.Handle("/metrics", promhttp.Handler()) // // Set values for the new created metrics.
// m.cpuTemp.Set(65.3)
// m.hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
//
// // Expose metrics and custom registry via an HTTP server
// // using the HandleFor function. "/metrics" is the usual endpoint for that.
// http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
// log.Fatal(http.ListenAndServe(":8080", nil)) // log.Fatal(http.ListenAndServe(":8080", nil))
// } // }
// //
// //
// This is a complete program that exports two metrics, a Gauge and a Counter, // This is a complete program that exports two metrics, a Gauge and a Counter,
// the latter with a label attached to turn it into a (one-dimensional) vector. // the latter with a label attached to turn it into a (one-dimensional) vector.
// It register the metrics using a custom registry and exposes them via an HTTP server
// on the /metrics endpoint.
// //
// Metrics // Metrics
// //