doc.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Package prometheus provides metrics primitives to instrument code for
  14. // monitoring. It also offers a registry for metrics. Sub-packages allow to
  15. // expose the registered metrics via HTTP (package promhttp) or push them to a
  16. // Pushgateway (package push).
  17. //
  18. // All exported functions and methods are safe to be used concurrently unless
  19. // specified otherwise.
  20. //
  21. // A Basic Example
  22. //
  23. // As a starting point, a very basic usage example:
  24. //
  25. // package main
  26. //
  27. // import (
  28. // "log"
  29. // "net/http"
  30. //
  31. // "github.com/prometheus/client_golang/prometheus"
  32. // "github.com/prometheus/client_golang/prometheus/promhttp"
  33. // )
  34. //
  35. // var (
  36. // cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
  37. // Name: "cpu_temperature_celsius",
  38. // Help: "Current temperature of the CPU.",
  39. // })
  40. // hdFailures = prometheus.NewCounterVec(
  41. // prometheus.CounterOpts{
  42. // Name: "hd_errors_total",
  43. // Help: "Number of hard-disk errors.",
  44. // },
  45. // []string{"device"},
  46. // )
  47. // )
  48. //
  49. // func init() {
  50. // // Metrics have to be registered to be exposed:
  51. // prometheus.MustRegister(cpuTemp)
  52. // prometheus.MustRegister(hdFailures)
  53. // }
  54. //
  55. // func main() {
  56. // cpuTemp.Set(65.3)
  57. // hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
  58. //
  59. // // The Handler function provides a default handler to expose metrics
  60. // // via an HTTP server. "/metrics" is the usual endpoint for that.
  61. // http.Handle("/metrics", promhttp.Handler())
  62. // log.Fatal(http.ListenAndServe(":8080", nil))
  63. // }
  64. //
  65. //
  66. // This is a complete program that exports two metrics, a Gauge and a Counter,
  67. // the latter with a label attached to turn it into a (one-dimensional) vector.
  68. //
  69. // Metrics
  70. //
  71. // The number of exported identifiers in this package might appear a bit
  72. // overwhelming. However, in addition to the basic plumbing shown in the example
  73. // above, you only need to understand the different metric types and their
  74. // vector versions for basic usage.
  75. //
  76. // Above, you have already touched the Counter and the Gauge. There are two more
  77. // advanced metric types: the Summary and Histogram. A more thorough description
  78. // of those four metric types can be found in the Prometheus docs:
  79. // https://prometheus.io/docs/concepts/metric_types/
  80. //
  81. // A fifth "type" of metric is Untyped. It behaves like a Gauge, but signals the
  82. // Prometheus server not to assume anything about its type.
  83. //
  84. // In addition to the fundamental metric types Gauge, Counter, Summary,
  85. // Histogram, and Untyped, a very important part of the Prometheus data model is
  86. // the partitioning of samples along dimensions called labels, which results in
  87. // metric vectors. The fundamental types are GaugeVec, CounterVec, SummaryVec,
  88. // HistogramVec, and UntypedVec.
  89. //
  90. // While only the fundamental metric types implement the Metric interface, both
  91. // the metrics and their vector versions implement the Collector interface. A
  92. // Collector manages the collection of a number of Metrics, but for convenience,
  93. // a Metric can also “collect itself”. Note that Gauge, Counter, Summary,
  94. // Histogram, and Untyped are interfaces themselves while GaugeVec, CounterVec,
  95. // SummaryVec, HistogramVec, and UntypedVec are not.
  96. //
  97. // To create instances of Metrics and their vector versions, you need a suitable
  98. // …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, HistogramOpts, or
  99. // UntypedOpts.
  100. //
  101. // Custom Collectors and constant Metrics
  102. //
  103. // While you could create your own implementations of Metric, most likely you
  104. // will only ever implement the Collector interface on your own. At a first
  105. // glance, a custom Collector seems handy to bundle Metrics for common
  106. // registration (with the prime example of the different metric vectors above,
  107. // which bundle all the metrics of the same name but with different labels).
  108. //
  109. // There is a more involved use case, too: If you already have metrics
  110. // available, created outside of the Prometheus context, you don't need the
  111. // interface of the various Metric types. You essentially want to mirror the
  112. // existing numbers into Prometheus Metrics during collection. An own
  113. // implementation of the Collector interface is perfect for that. You can create
  114. // Metric instances “on the fly” using NewConstMetric, NewConstHistogram, and
  115. // NewConstSummary (and their respective Must… versions). That will happen in
  116. // the Collect method. The Describe method has to return separate Desc
  117. // instances, representative of the “throw-away” metrics to be created later.
  118. // NewDesc comes in handy to create those Desc instances.
  119. //
  120. // The Collector example illustrates the use case. You can also look at the
  121. // source code of the processCollector (mirroring process metrics), the
  122. // goCollector (mirroring Go metrics), or the expvarCollector (mirroring expvar
  123. // metrics) as examples that are used in this package itself.
  124. //
  125. // If you just need to call a function to get a single float value to collect as
  126. // a metric, GaugeFunc, CounterFunc, or UntypedFunc might be interesting
  127. // shortcuts.
  128. //
  129. // Advanced Uses of the Registry
  130. //
  131. // While MustRegister is the by far most common way of registering a Collector,
  132. // sometimes you might want to handle the errors the registration might cause.
  133. // As suggested by the name, MustRegister panics if an error occurs. With the
  134. // Register function, the error is returned and can be handled.
  135. //
  136. // An error is returned if the registered Collector is incompatible or
  137. // inconsistent with already registered metrics. The registry aims for
  138. // consistency of the collected metrics according to the Prometheus data model.
  139. // Inconsistencies are ideally detected at registration time, not at collect
  140. // time. The former will usually be detected at start-up time of a program,
  141. // while the latter will only happen at scrape time, possibly not even on the
  142. // first scrape if the inconsistency only becomes relevant later. That is the
  143. // main reason why a Collector and a Metric have to describe themselves to the
  144. // registry.
  145. //
  146. // So far, everything we did operated on the so-called default registry, as it
  147. // can be found in the global DefaultRegistry variable. With NewRegistry, you
  148. // can create a custom registry, or you can even implement the Registerer or
  149. // Gatherer interfaces yourself. The methods Register and Unregister work in the
  150. // same way on a custom registry as the global functions Register and Unregister
  151. // on the default registry.
  152. //
  153. // There are a number of uses for custom registries: You can use registries with
  154. // special properties, see NewPedanticRegistry. You can avoid global state, as
  155. // it is imposed by the DefaultRegistry. You can use multiple registries at the
  156. // same time to expose different metrics in different ways. You can use separate
  157. // registries for testing purposes.
  158. //
  159. // Also note that the DefaultRegistry comes registered with a Collector for Go
  160. // runtime metrics (via NewGoCollector) and a Collector for process metrics (via
  161. // NewProcessCollector). With a custom registry, you are in control and decide
  162. // yourself about the Collectors to register.
  163. //
  164. // HTTP Exposition
  165. //
  166. // The Registry implements the Gatherer interface. The caller of the Gather
  167. // method can then expose the gathered metrics in some way. Usually, the metrics
  168. // are served via HTTP on the /metrics endpoint. That's happening in the example
  169. // above. The tools to expose metrics via HTTP are in the promhttp sub-package.
  170. // (The top-level functions in the prometheus package are deprecated.)
  171. //
  172. // Pushing to the Pushgateway
  173. //
  174. // Function for pushing to the Pushgateway can be found in the push sub-package.
  175. //
  176. // Graphite Bridge
  177. //
  178. // Functions and examples to push metrics from a Gatherer to Graphite can be
  179. // found in the graphite sub-package.
  180. //
  181. // Other Means of Exposition
  182. //
  183. // More ways of exposing metrics can easily be added by following the approaches
  184. // of the existing implementations.
  185. package prometheus