|
21 | 21 | // All exported functions and methods are safe to be used concurrently unless
|
22 | 22 | // specified otherwise.
|
23 | 23 | //
|
24 |
| -// A Basic Example |
| 24 | +// # A Basic Example |
25 | 25 | //
|
26 | 26 | // As a starting point, a very basic usage example:
|
27 | 27 | //
|
28 |
| -// package main |
29 |
| -// |
30 |
| -// import ( |
31 |
| -// "log" |
32 |
| -// "net/http" |
33 |
| -// |
34 |
| -// "github.com/prometheus/client_golang/prometheus" |
35 |
| -// "github.com/prometheus/client_golang/prometheus/promhttp" |
36 |
| -// ) |
37 |
| -// |
38 |
| -// type metrics struct { |
39 |
| -// cpuTemp prometheus.Gauge |
40 |
| -// hdFailures *prometheus.CounterVec |
41 |
| -// } |
42 |
| -// |
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 |
60 |
| -// } |
61 |
| -// |
62 |
| -// func main() { |
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})) |
75 |
| -// log.Fatal(http.ListenAndServe(":8080", nil)) |
76 |
| -// } |
77 |
| -// |
| 28 | +// package main |
| 29 | +// |
| 30 | +// import ( |
| 31 | +// "log" |
| 32 | +// "net/http" |
| 33 | +// |
| 34 | +// "github.com/prometheus/client_golang/prometheus" |
| 35 | +// "github.com/prometheus/client_golang/prometheus/promhttp" |
| 36 | +// ) |
| 37 | +// |
| 38 | +// type metrics struct { |
| 39 | +// cpuTemp prometheus.Gauge |
| 40 | +// hdFailures *prometheus.CounterVec |
| 41 | +// } |
| 42 | +// |
| 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 |
| 60 | +// } |
| 61 | +// |
| 62 | +// func main() { |
| 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})) |
| 75 | +// log.Fatal(http.ListenAndServe(":8080", nil)) |
| 76 | +// } |
78 | 77 | //
|
79 | 78 | // This is a complete program that exports two metrics, a Gauge and a Counter,
|
80 | 79 | // the latter with a label attached to turn it into a (one-dimensional) vector.
|
81 | 80 | // It register the metrics using a custom registry and exposes them via an HTTP server
|
82 | 81 | // on the /metrics endpoint.
|
83 | 82 | //
|
84 |
| -// Metrics |
| 83 | +// # Metrics |
85 | 84 | //
|
86 | 85 | // The number of exported identifiers in this package might appear a bit
|
87 | 86 | // overwhelming. However, in addition to the basic plumbing shown in the example
|
|
112 | 111 | // To create instances of Metrics and their vector versions, you need a suitable
|
113 | 112 | // …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, or HistogramOpts.
|
114 | 113 | //
|
115 |
| -// Custom Collectors and constant Metrics |
| 114 | +// # Custom Collectors and constant Metrics |
116 | 115 | //
|
117 | 116 | // While you could create your own implementations of Metric, most likely you
|
118 | 117 | // will only ever implement the Collector interface on your own. At a first
|
|
153 | 152 | // a metric, GaugeFunc, CounterFunc, or UntypedFunc might be interesting
|
154 | 153 | // shortcuts.
|
155 | 154 | //
|
156 |
| -// Advanced Uses of the Registry |
| 155 | +// # Advanced Uses of the Registry |
157 | 156 | //
|
158 | 157 | // While MustRegister is the by far most common way of registering a Collector,
|
159 | 158 | // sometimes you might want to handle the errors the registration might cause.
|
|
188 | 187 | // NewProcessCollector). With a custom registry, you are in control and decide
|
189 | 188 | // yourself about the Collectors to register.
|
190 | 189 | //
|
191 |
| -// HTTP Exposition |
| 190 | +// # HTTP Exposition |
192 | 191 | //
|
193 | 192 | // The Registry implements the Gatherer interface. The caller of the Gather
|
194 | 193 | // method can then expose the gathered metrics in some way. Usually, the metrics
|
195 | 194 | // are served via HTTP on the /metrics endpoint. That's happening in the example
|
196 | 195 | // above. The tools to expose metrics via HTTP are in the promhttp sub-package.
|
197 | 196 | //
|
198 |
| -// Pushing to the Pushgateway |
| 197 | +// # Pushing to the Pushgateway |
199 | 198 | //
|
200 | 199 | // Function for pushing to the Pushgateway can be found in the push sub-package.
|
201 | 200 | //
|
202 |
| -// Graphite Bridge |
| 201 | +// # Graphite Bridge |
203 | 202 | //
|
204 | 203 | // Functions and examples to push metrics from a Gatherer to Graphite can be
|
205 | 204 | // found in the graphite sub-package.
|
206 | 205 | //
|
207 |
| -// Other Means of Exposition |
| 206 | +// # Other Means of Exposition |
208 | 207 | //
|
209 | 208 | // More ways of exposing metrics can easily be added by following the approaches
|
210 | 209 | // of the existing implementations.
|
|
0 commit comments