|
35 | 35 | // "github.com/prometheus/client_golang/prometheus/promhttp"
|
36 | 36 | // )
|
37 | 37 | //
|
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 | +// } |
51 | 42 | //
|
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 |
56 | 60 | // }
|
57 | 61 | //
|
58 | 62 | // 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})) |
65 | 75 | // log.Fatal(http.ListenAndServe(":8080", nil))
|
66 | 76 | // }
|
67 | 77 | //
|
68 | 78 | //
|
69 | 79 | // This is a complete program that exports two metrics, a Gauge and a Counter,
|
70 | 80 | // 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. |
71 | 83 | //
|
72 | 84 | // Metrics
|
73 | 85 | //
|
|
0 commit comments