| 1 |
// Copyright 2014 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package prometheus is the core instrumentation package. It provides metrics |
| 15 |
// primitives to instrument code for monitoring. It also offers a registry for |
| 16 |
// metrics. Sub-packages allow to expose the registered metrics via HTTP |
| 17 |
// (package promhttp) or push them to a Pushgateway (package push). There is |
| 18 |
// also a sub-package promauto, which provides metrics constructors with |
| 19 |
// automatic registration. |
| 20 |
// |
| 21 |
// All exported functions and methods are safe to be used concurrently unless |
| 22 |
// specified otherwise. |
| 23 |
// |
| 24 |
// # A Basic Example |
| 25 |
// |
| 26 |
// As a starting point, a very basic usage example: |
| 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 |
// |
| 78 |
// This is a complete program that exports two metrics, a Gauge and a Counter, |
| 79 |
// the latter with a label attached to turn it into a (one-dimensional) vector. |
| 80 |
// It register the metrics using a custom registry and exposes them via an HTTP server |
| 81 |
// on the /metrics endpoint. |
| 82 |
// |
| 83 |
// # Metrics |
| 84 |
// |
| 85 |
// The number of exported identifiers in this package might appear a bit |
| 86 |
// overwhelming. However, in addition to the basic plumbing shown in the example |
| 87 |
// above, you only need to understand the different metric types and their |
| 88 |
// vector versions for basic usage. Furthermore, if you are not concerned with |
| 89 |
// fine-grained control of when and how to register metrics with the registry, |
| 90 |
// have a look at the promauto package, which will effectively allow you to |
| 91 |
// ignore registration altogether in simple cases. |
| 92 |
// |
| 93 |
// Above, you have already touched the Counter and the Gauge. There are two more |
| 94 |
// advanced metric types: the Summary and Histogram. A more thorough description |
| 95 |
// of those four metric types can be found in the Prometheus docs: |
| 96 |
// https://prometheus.io/docs/concepts/metric_types/ |
| 97 |
// |
| 98 |
// In addition to the fundamental metric types Gauge, Counter, Summary, and |
| 99 |
// Histogram, a very important part of the Prometheus data model is the |
| 100 |
// partitioning of samples along dimensions called labels, which results in |
| 101 |
// metric vectors. The fundamental types are GaugeVec, CounterVec, SummaryVec, |
| 102 |
// and HistogramVec. |
| 103 |
// |
| 104 |
// While only the fundamental metric types implement the Metric interface, both |
| 105 |
// the metrics and their vector versions implement the Collector interface. A |
| 106 |
// Collector manages the collection of a number of Metrics, but for convenience, |
| 107 |
// a Metric can also “collect itself”. Note that Gauge, Counter, Summary, and |
| 108 |
// Histogram are interfaces themselves while GaugeVec, CounterVec, SummaryVec, |
| 109 |
// and HistogramVec are not. |
| 110 |
// |
| 111 |
// To create instances of Metrics and their vector versions, you need a suitable |
| 112 |
// …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, or HistogramOpts. |
| 113 |
// |
| 114 |
// # Custom Collectors and constant Metrics |
| 115 |
// |
| 116 |
// While you could create your own implementations of Metric, most likely you |
| 117 |
// will only ever implement the Collector interface on your own. At a first |
| 118 |
// glance, a custom Collector seems handy to bundle Metrics for common |
| 119 |
// registration (with the prime example of the different metric vectors above, |
| 120 |
// which bundle all the metrics of the same name but with different labels). |
| 121 |
// |
| 122 |
// There is a more involved use case, too: If you already have metrics |
| 123 |
// available, created outside of the Prometheus context, you don't need the |
| 124 |
// interface of the various Metric types. You essentially want to mirror the |
| 125 |
// existing numbers into Prometheus Metrics during collection. An own |
| 126 |
// implementation of the Collector interface is perfect for that. You can create |
| 127 |
// Metric instances “on the fly” using NewConstMetric, NewConstHistogram, and |
| 128 |
// NewConstSummary (and their respective Must… versions). NewConstMetric is used |
| 129 |
// for all metric types with just a float64 as their value: Counter, Gauge, and |
| 130 |
// a special “type” called Untyped. Use the latter if you are not sure if the |
| 131 |
// mirrored metric is a Counter or a Gauge. Creation of the Metric instance |
| 132 |
// happens in the Collect method. The Describe method has to return separate |
| 133 |
// Desc instances, representative of the “throw-away” metrics to be created |
| 134 |
// later. NewDesc comes in handy to create those Desc instances. Alternatively, |
| 135 |
// you could return no Desc at all, which will mark the Collector “unchecked”. |
| 136 |
// No checks are performed at registration time, but metric consistency will |
| 137 |
// still be ensured at scrape time, i.e. any inconsistencies will lead to scrape |
| 138 |
// errors. Thus, with unchecked Collectors, the responsibility to not collect |
| 139 |
// metrics that lead to inconsistencies in the total scrape result lies with the |
| 140 |
// implementer of the Collector. While this is not a desirable state, it is |
| 141 |
// sometimes necessary. The typical use case is a situation where the exact |
| 142 |
// metrics to be returned by a Collector cannot be predicted at registration |
| 143 |
// time, but the implementer has sufficient knowledge of the whole system to |
| 144 |
// guarantee metric consistency. |
| 145 |
// |
| 146 |
// The Collector example illustrates the use case. You can also look at the |
| 147 |
// source code of the processCollector (mirroring process metrics), the |
| 148 |
// goCollector (mirroring Go metrics), or the expvarCollector (mirroring expvar |
| 149 |
// metrics) as examples that are used in this package itself. |
| 150 |
// |
| 151 |
// If you just need to call a function to get a single float value to collect as |
| 152 |
// a metric, GaugeFunc, CounterFunc, or UntypedFunc might be interesting |
| 153 |
// shortcuts. |
| 154 |
// |
| 155 |
// # Advanced Uses of the Registry |
| 156 |
// |
| 157 |
// While MustRegister is the by far most common way of registering a Collector, |
| 158 |
// sometimes you might want to handle the errors the registration might cause. |
| 159 |
// As suggested by the name, MustRegister panics if an error occurs. With the |
| 160 |
// Register function, the error is returned and can be handled. |
| 161 |
// |
| 162 |
// An error is returned if the registered Collector is incompatible or |
| 163 |
// inconsistent with already registered metrics. The registry aims for |
| 164 |
// consistency of the collected metrics according to the Prometheus data model. |
| 165 |
// Inconsistencies are ideally detected at registration time, not at collect |
| 166 |
// time. The former will usually be detected at start-up time of a program, |
| 167 |
// while the latter will only happen at scrape time, possibly not even on the |
| 168 |
// first scrape if the inconsistency only becomes relevant later. That is the |
| 169 |
// main reason why a Collector and a Metric have to describe themselves to the |
| 170 |
// registry. |
| 171 |
// |
| 172 |
// So far, everything we did operated on the so-called default registry, as it |
| 173 |
// can be found in the global DefaultRegisterer variable. With NewRegistry, you |
| 174 |
// can create a custom registry, or you can even implement the Registerer or |
| 175 |
// Gatherer interfaces yourself. The methods Register and Unregister work in the |
| 176 |
// same way on a custom registry as the global functions Register and Unregister |
| 177 |
// on the default registry. |
| 178 |
// |
| 179 |
// There are a number of uses for custom registries: You can use registries with |
| 180 |
// special properties, see NewPedanticRegistry. You can avoid global state, as |
| 181 |
// it is imposed by the DefaultRegisterer. You can use multiple registries at |
| 182 |
// the same time to expose different metrics in different ways. You can use |
| 183 |
// separate registries for testing purposes. |
| 184 |
// |
| 185 |
// Also note that the DefaultRegisterer comes registered with a Collector for Go |
| 186 |
// runtime metrics (via NewGoCollector) and a Collector for process metrics (via |
| 187 |
// NewProcessCollector). With a custom registry, you are in control and decide |
| 188 |
// yourself about the Collectors to register. |
| 189 |
// |
| 190 |
// # HTTP Exposition |
| 191 |
// |
| 192 |
// The Registry implements the Gatherer interface. The caller of the Gather |
| 193 |
// method can then expose the gathered metrics in some way. Usually, the metrics |
| 194 |
// are served via HTTP on the /metrics endpoint. That's happening in the example |
| 195 |
// above. The tools to expose metrics via HTTP are in the promhttp sub-package. |
| 196 |
// |
| 197 |
// # Pushing to the Pushgateway |
| 198 |
// |
| 199 |
// Function for pushing to the Pushgateway can be found in the push sub-package. |
| 200 |
// |
| 201 |
// # Graphite Bridge |
| 202 |
// |
| 203 |
// Functions and examples to push metrics from a Gatherer to Graphite can be |
| 204 |
// found in the graphite sub-package. |
| 205 |
// |
| 206 |
// # Other Means of Exposition |
| 207 |
// |
| 208 |
// More ways of exposing metrics can easily be added by following the approaches |
| 209 |
// of the existing implementations. |
| 210 |
package prometheus |