You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

histogram.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // Copyright 2015 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
  14. import (
  15. "fmt"
  16. "math"
  17. "runtime"
  18. "sort"
  19. "sync"
  20. "sync/atomic"
  21. "github.com/golang/protobuf/proto"
  22. dto "github.com/prometheus/client_model/go"
  23. )
  24. // A Histogram counts individual observations from an event or sample stream in
  25. // configurable buckets. Similar to a summary, it also provides a sum of
  26. // observations and an observation count.
  27. //
  28. // On the Prometheus server, quantiles can be calculated from a Histogram using
  29. // the histogram_quantile function in the query language.
  30. //
  31. // Note that Histograms, in contrast to Summaries, can be aggregated with the
  32. // Prometheus query language (see the documentation for detailed
  33. // procedures). However, Histograms require the user to pre-define suitable
  34. // buckets, and they are in general less accurate. The Observe method of a
  35. // Histogram has a very low performance overhead in comparison with the Observe
  36. // method of a Summary.
  37. //
  38. // To create Histogram instances, use NewHistogram.
  39. type Histogram interface {
  40. Metric
  41. Collector
  42. // Observe adds a single observation to the histogram.
  43. Observe(float64)
  44. }
  45. // bucketLabel is used for the label that defines the upper bound of a
  46. // bucket of a histogram ("le" -> "less or equal").
  47. const bucketLabel = "le"
  48. // DefBuckets are the default Histogram buckets. The default buckets are
  49. // tailored to broadly measure the response time (in seconds) of a network
  50. // service. Most likely, however, you will be required to define buckets
  51. // customized to your use case.
  52. var (
  53. DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
  54. errBucketLabelNotAllowed = fmt.Errorf(
  55. "%q is not allowed as label name in histograms", bucketLabel,
  56. )
  57. )
  58. // LinearBuckets creates 'count' buckets, each 'width' wide, where the lowest
  59. // bucket has an upper bound of 'start'. The final +Inf bucket is not counted
  60. // and not included in the returned slice. The returned slice is meant to be
  61. // used for the Buckets field of HistogramOpts.
  62. //
  63. // The function panics if 'count' is zero or negative.
  64. func LinearBuckets(start, width float64, count int) []float64 {
  65. if count < 1 {
  66. panic("LinearBuckets needs a positive count")
  67. }
  68. buckets := make([]float64, count)
  69. for i := range buckets {
  70. buckets[i] = start
  71. start += width
  72. }
  73. return buckets
  74. }
  75. // ExponentialBuckets creates 'count' buckets, where the lowest bucket has an
  76. // upper bound of 'start' and each following bucket's upper bound is 'factor'
  77. // times the previous bucket's upper bound. The final +Inf bucket is not counted
  78. // and not included in the returned slice. The returned slice is meant to be
  79. // used for the Buckets field of HistogramOpts.
  80. //
  81. // The function panics if 'count' is 0 or negative, if 'start' is 0 or negative,
  82. // or if 'factor' is less than or equal 1.
  83. func ExponentialBuckets(start, factor float64, count int) []float64 {
  84. if count < 1 {
  85. panic("ExponentialBuckets needs a positive count")
  86. }
  87. if start <= 0 {
  88. panic("ExponentialBuckets needs a positive start value")
  89. }
  90. if factor <= 1 {
  91. panic("ExponentialBuckets needs a factor greater than 1")
  92. }
  93. buckets := make([]float64, count)
  94. for i := range buckets {
  95. buckets[i] = start
  96. start *= factor
  97. }
  98. return buckets
  99. }
  100. // HistogramOpts bundles the options for creating a Histogram metric. It is
  101. // mandatory to set Name to a non-empty string. All other fields are optional
  102. // and can safely be left at their zero value, although it is strongly
  103. // encouraged to set a Help string.
  104. type HistogramOpts struct {
  105. // Namespace, Subsystem, and Name are components of the fully-qualified
  106. // name of the Histogram (created by joining these components with
  107. // "_"). Only Name is mandatory, the others merely help structuring the
  108. // name. Note that the fully-qualified name of the Histogram must be a
  109. // valid Prometheus metric name.
  110. Namespace string
  111. Subsystem string
  112. Name string
  113. // Help provides information about this Histogram.
  114. //
  115. // Metrics with the same fully-qualified name must have the same Help
  116. // string.
  117. Help string
  118. // ConstLabels are used to attach fixed labels to this metric. Metrics
  119. // with the same fully-qualified name must have the same label names in
  120. // their ConstLabels.
  121. //
  122. // ConstLabels are only used rarely. In particular, do not use them to
  123. // attach the same labels to all your metrics. Those use cases are
  124. // better covered by target labels set by the scraping Prometheus
  125. // server, or by one specific metric (e.g. a build_info or a
  126. // machine_role metric). See also
  127. // https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels,-not-static-scraped-labels
  128. ConstLabels Labels
  129. // Buckets defines the buckets into which observations are counted. Each
  130. // element in the slice is the upper inclusive bound of a bucket. The
  131. // values must be sorted in strictly increasing order. There is no need
  132. // to add a highest bucket with +Inf bound, it will be added
  133. // implicitly. The default value is DefBuckets.
  134. Buckets []float64
  135. }
  136. // NewHistogram creates a new Histogram based on the provided HistogramOpts. It
  137. // panics if the buckets in HistogramOpts are not in strictly increasing order.
  138. func NewHistogram(opts HistogramOpts) Histogram {
  139. return newHistogram(
  140. NewDesc(
  141. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  142. opts.Help,
  143. nil,
  144. opts.ConstLabels,
  145. ),
  146. opts,
  147. )
  148. }
  149. func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogram {
  150. if len(desc.variableLabels) != len(labelValues) {
  151. panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, labelValues))
  152. }
  153. for _, n := range desc.variableLabels {
  154. if n == bucketLabel {
  155. panic(errBucketLabelNotAllowed)
  156. }
  157. }
  158. for _, lp := range desc.constLabelPairs {
  159. if lp.GetName() == bucketLabel {
  160. panic(errBucketLabelNotAllowed)
  161. }
  162. }
  163. if len(opts.Buckets) == 0 {
  164. opts.Buckets = DefBuckets
  165. }
  166. h := &histogram{
  167. desc: desc,
  168. upperBounds: opts.Buckets,
  169. labelPairs: makeLabelPairs(desc, labelValues),
  170. counts: [2]*histogramCounts{&histogramCounts{}, &histogramCounts{}},
  171. }
  172. for i, upperBound := range h.upperBounds {
  173. if i < len(h.upperBounds)-1 {
  174. if upperBound >= h.upperBounds[i+1] {
  175. panic(fmt.Errorf(
  176. "histogram buckets must be in increasing order: %f >= %f",
  177. upperBound, h.upperBounds[i+1],
  178. ))
  179. }
  180. } else {
  181. if math.IsInf(upperBound, +1) {
  182. // The +Inf bucket is implicit. Remove it here.
  183. h.upperBounds = h.upperBounds[:i]
  184. }
  185. }
  186. }
  187. // Finally we know the final length of h.upperBounds and can make buckets
  188. // for both counts:
  189. h.counts[0].buckets = make([]uint64, len(h.upperBounds))
  190. h.counts[1].buckets = make([]uint64, len(h.upperBounds))
  191. h.init(h) // Init self-collection.
  192. return h
  193. }
  194. type histogramCounts struct {
  195. // sumBits contains the bits of the float64 representing the sum of all
  196. // observations. sumBits and count have to go first in the struct to
  197. // guarantee alignment for atomic operations.
  198. // http://golang.org/pkg/sync/atomic/#pkg-note-BUG
  199. sumBits uint64
  200. count uint64
  201. buckets []uint64
  202. }
  203. type histogram struct {
  204. // countAndHotIdx enables lock-free writes with use of atomic updates.
  205. // The most significant bit is the hot index [0 or 1] of the count field
  206. // below. Observe calls update the hot one. All remaining bits count the
  207. // number of Observe calls. Observe starts by incrementing this counter,
  208. // and finish by incrementing the count field in the respective
  209. // histogramCounts, as a marker for completion.
  210. //
  211. // Calls of the Write method (which are non-mutating reads from the
  212. // perspective of the histogram) swap the hot–cold under the writeMtx
  213. // lock. A cooldown is awaited (while locked) by comparing the number of
  214. // observations with the initiation count. Once they match, then the
  215. // last observation on the now cool one has completed. All cool fields must
  216. // be merged into the new hot before releasing writeMtx.
  217. //
  218. // Fields with atomic access first! See alignment constraint:
  219. // http://golang.org/pkg/sync/atomic/#pkg-note-BUG
  220. countAndHotIdx uint64
  221. selfCollector
  222. desc *Desc
  223. writeMtx sync.Mutex // Only used in the Write method.
  224. // Two counts, one is "hot" for lock-free observations, the other is
  225. // "cold" for writing out a dto.Metric. It has to be an array of
  226. // pointers to guarantee 64bit alignment of the histogramCounts, see
  227. // http://golang.org/pkg/sync/atomic/#pkg-note-BUG.
  228. counts [2]*histogramCounts
  229. upperBounds []float64
  230. labelPairs []*dto.LabelPair
  231. }
  232. func (h *histogram) Desc() *Desc {
  233. return h.desc
  234. }
  235. func (h *histogram) Observe(v float64) {
  236. // TODO(beorn7): For small numbers of buckets (<30), a linear search is
  237. // slightly faster than the binary search. If we really care, we could
  238. // switch from one search strategy to the other depending on the number
  239. // of buckets.
  240. //
  241. // Microbenchmarks (BenchmarkHistogramNoLabels):
  242. // 11 buckets: 38.3 ns/op linear - binary 48.7 ns/op
  243. // 100 buckets: 78.1 ns/op linear - binary 54.9 ns/op
  244. // 300 buckets: 154 ns/op linear - binary 61.6 ns/op
  245. i := sort.SearchFloat64s(h.upperBounds, v)
  246. // We increment h.countAndHotIdx so that the counter in the lower
  247. // 63 bits gets incremented. At the same time, we get the new value
  248. // back, which we can use to find the currently-hot counts.
  249. n := atomic.AddUint64(&h.countAndHotIdx, 1)
  250. hotCounts := h.counts[n>>63]
  251. if i < len(h.upperBounds) {
  252. atomic.AddUint64(&hotCounts.buckets[i], 1)
  253. }
  254. for {
  255. oldBits := atomic.LoadUint64(&hotCounts.sumBits)
  256. newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
  257. if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
  258. break
  259. }
  260. }
  261. // Increment count last as we take it as a signal that the observation
  262. // is complete.
  263. atomic.AddUint64(&hotCounts.count, 1)
  264. }
  265. func (h *histogram) Write(out *dto.Metric) error {
  266. // For simplicity, we protect this whole method by a mutex. It is not in
  267. // the hot path, i.e. Observe is called much more often than Write. The
  268. // complication of making Write lock-free isn't worth it, if possible at
  269. // all.
  270. h.writeMtx.Lock()
  271. defer h.writeMtx.Unlock()
  272. // Adding 1<<63 switches the hot index (from 0 to 1 or from 1 to 0)
  273. // without touching the count bits. See the struct comments for a full
  274. // description of the algorithm.
  275. n := atomic.AddUint64(&h.countAndHotIdx, 1<<63)
  276. // count is contained unchanged in the lower 63 bits.
  277. count := n & ((1 << 63) - 1)
  278. // The most significant bit tells us which counts is hot. The complement
  279. // is thus the cold one.
  280. hotCounts := h.counts[n>>63]
  281. coldCounts := h.counts[(^n)>>63]
  282. // Await cooldown.
  283. for count != atomic.LoadUint64(&coldCounts.count) {
  284. runtime.Gosched() // Let observations get work done.
  285. }
  286. his := &dto.Histogram{
  287. Bucket: make([]*dto.Bucket, len(h.upperBounds)),
  288. SampleCount: proto.Uint64(count),
  289. SampleSum: proto.Float64(math.Float64frombits(atomic.LoadUint64(&coldCounts.sumBits))),
  290. }
  291. var cumCount uint64
  292. for i, upperBound := range h.upperBounds {
  293. cumCount += atomic.LoadUint64(&coldCounts.buckets[i])
  294. his.Bucket[i] = &dto.Bucket{
  295. CumulativeCount: proto.Uint64(cumCount),
  296. UpperBound: proto.Float64(upperBound),
  297. }
  298. }
  299. out.Histogram = his
  300. out.Label = h.labelPairs
  301. // Finally add all the cold counts to the new hot counts and reset the cold counts.
  302. atomic.AddUint64(&hotCounts.count, count)
  303. atomic.StoreUint64(&coldCounts.count, 0)
  304. for {
  305. oldBits := atomic.LoadUint64(&hotCounts.sumBits)
  306. newBits := math.Float64bits(math.Float64frombits(oldBits) + his.GetSampleSum())
  307. if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
  308. atomic.StoreUint64(&coldCounts.sumBits, 0)
  309. break
  310. }
  311. }
  312. for i := range h.upperBounds {
  313. atomic.AddUint64(&hotCounts.buckets[i], atomic.LoadUint64(&coldCounts.buckets[i]))
  314. atomic.StoreUint64(&coldCounts.buckets[i], 0)
  315. }
  316. return nil
  317. }
  318. // HistogramVec is a Collector that bundles a set of Histograms that all share the
  319. // same Desc, but have different values for their variable labels. This is used
  320. // if you want to count the same thing partitioned by various dimensions
  321. // (e.g. HTTP request latencies, partitioned by status code and method). Create
  322. // instances with NewHistogramVec.
  323. type HistogramVec struct {
  324. *metricVec
  325. }
  326. // NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and
  327. // partitioned by the given label names.
  328. func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec {
  329. desc := NewDesc(
  330. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  331. opts.Help,
  332. labelNames,
  333. opts.ConstLabels,
  334. )
  335. return &HistogramVec{
  336. metricVec: newMetricVec(desc, func(lvs ...string) Metric {
  337. return newHistogram(desc, opts, lvs...)
  338. }),
  339. }
  340. }
  341. // GetMetricWithLabelValues returns the Histogram for the given slice of label
  342. // values (same order as the VariableLabels in Desc). If that combination of
  343. // label values is accessed for the first time, a new Histogram is created.
  344. //
  345. // It is possible to call this method without using the returned Histogram to only
  346. // create the new Histogram but leave it at its starting value, a Histogram without
  347. // any observations.
  348. //
  349. // Keeping the Histogram for later use is possible (and should be considered if
  350. // performance is critical), but keep in mind that Reset, DeleteLabelValues and
  351. // Delete can be used to delete the Histogram from the HistogramVec. In that case, the
  352. // Histogram will still exist, but it will not be exported anymore, even if a
  353. // Histogram with the same label values is created later. See also the CounterVec
  354. // example.
  355. //
  356. // An error is returned if the number of label values is not the same as the
  357. // number of VariableLabels in Desc (minus any curried labels).
  358. //
  359. // Note that for more than one label value, this method is prone to mistakes
  360. // caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as
  361. // an alternative to avoid that type of mistake. For higher label numbers, the
  362. // latter has a much more readable (albeit more verbose) syntax, but it comes
  363. // with a performance overhead (for creating and processing the Labels map).
  364. // See also the GaugeVec example.
  365. func (v *HistogramVec) GetMetricWithLabelValues(lvs ...string) (Observer, error) {
  366. metric, err := v.metricVec.getMetricWithLabelValues(lvs...)
  367. if metric != nil {
  368. return metric.(Observer), err
  369. }
  370. return nil, err
  371. }
  372. // GetMetricWith returns the Histogram for the given Labels map (the label names
  373. // must match those of the VariableLabels in Desc). If that label map is
  374. // accessed for the first time, a new Histogram is created. Implications of
  375. // creating a Histogram without using it and keeping the Histogram for later use
  376. // are the same as for GetMetricWithLabelValues.
  377. //
  378. // An error is returned if the number and names of the Labels are inconsistent
  379. // with those of the VariableLabels in Desc (minus any curried labels).
  380. //
  381. // This method is used for the same purpose as
  382. // GetMetricWithLabelValues(...string). See there for pros and cons of the two
  383. // methods.
  384. func (v *HistogramVec) GetMetricWith(labels Labels) (Observer, error) {
  385. metric, err := v.metricVec.getMetricWith(labels)
  386. if metric != nil {
  387. return metric.(Observer), err
  388. }
  389. return nil, err
  390. }
  391. // WithLabelValues works as GetMetricWithLabelValues, but panics where
  392. // GetMetricWithLabelValues would have returned an error. Not returning an
  393. // error allows shortcuts like
  394. // myVec.WithLabelValues("404", "GET").Observe(42.21)
  395. func (v *HistogramVec) WithLabelValues(lvs ...string) Observer {
  396. h, err := v.GetMetricWithLabelValues(lvs...)
  397. if err != nil {
  398. panic(err)
  399. }
  400. return h
  401. }
  402. // With works as GetMetricWith but panics where GetMetricWithLabels would have
  403. // returned an error. Not returning an error allows shortcuts like
  404. // myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Observe(42.21)
  405. func (v *HistogramVec) With(labels Labels) Observer {
  406. h, err := v.GetMetricWith(labels)
  407. if err != nil {
  408. panic(err)
  409. }
  410. return h
  411. }
  412. // CurryWith returns a vector curried with the provided labels, i.e. the
  413. // returned vector has those labels pre-set for all labeled operations performed
  414. // on it. The cardinality of the curried vector is reduced accordingly. The
  415. // order of the remaining labels stays the same (just with the curried labels
  416. // taken out of the sequence – which is relevant for the
  417. // (GetMetric)WithLabelValues methods). It is possible to curry a curried
  418. // vector, but only with labels not yet used for currying before.
  419. //
  420. // The metrics contained in the HistogramVec are shared between the curried and
  421. // uncurried vectors. They are just accessed differently. Curried and uncurried
  422. // vectors behave identically in terms of collection. Only one must be
  423. // registered with a given registry (usually the uncurried version). The Reset
  424. // method deletes all metrics, even if called on a curried vector.
  425. func (v *HistogramVec) CurryWith(labels Labels) (ObserverVec, error) {
  426. vec, err := v.curryWith(labels)
  427. if vec != nil {
  428. return &HistogramVec{vec}, err
  429. }
  430. return nil, err
  431. }
  432. // MustCurryWith works as CurryWith but panics where CurryWith would have
  433. // returned an error.
  434. func (v *HistogramVec) MustCurryWith(labels Labels) ObserverVec {
  435. vec, err := v.CurryWith(labels)
  436. if err != nil {
  437. panic(err)
  438. }
  439. return vec
  440. }
  441. type constHistogram struct {
  442. desc *Desc
  443. count uint64
  444. sum float64
  445. buckets map[float64]uint64
  446. labelPairs []*dto.LabelPair
  447. }
  448. func (h *constHistogram) Desc() *Desc {
  449. return h.desc
  450. }
  451. func (h *constHistogram) Write(out *dto.Metric) error {
  452. his := &dto.Histogram{}
  453. buckets := make([]*dto.Bucket, 0, len(h.buckets))
  454. his.SampleCount = proto.Uint64(h.count)
  455. his.SampleSum = proto.Float64(h.sum)
  456. for upperBound, count := range h.buckets {
  457. buckets = append(buckets, &dto.Bucket{
  458. CumulativeCount: proto.Uint64(count),
  459. UpperBound: proto.Float64(upperBound),
  460. })
  461. }
  462. if len(buckets) > 0 {
  463. sort.Sort(buckSort(buckets))
  464. }
  465. his.Bucket = buckets
  466. out.Histogram = his
  467. out.Label = h.labelPairs
  468. return nil
  469. }
  470. // NewConstHistogram returns a metric representing a Prometheus histogram with
  471. // fixed values for the count, sum, and bucket counts. As those parameters
  472. // cannot be changed, the returned value does not implement the Histogram
  473. // interface (but only the Metric interface). Users of this package will not
  474. // have much use for it in regular operations. However, when implementing custom
  475. // Collectors, it is useful as a throw-away metric that is generated on the fly
  476. // to send it to Prometheus in the Collect method.
  477. //
  478. // buckets is a map of upper bounds to cumulative counts, excluding the +Inf
  479. // bucket.
  480. //
  481. // NewConstHistogram returns an error if the length of labelValues is not
  482. // consistent with the variable labels in Desc or if Desc is invalid.
  483. func NewConstHistogram(
  484. desc *Desc,
  485. count uint64,
  486. sum float64,
  487. buckets map[float64]uint64,
  488. labelValues ...string,
  489. ) (Metric, error) {
  490. if desc.err != nil {
  491. return nil, desc.err
  492. }
  493. if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil {
  494. return nil, err
  495. }
  496. return &constHistogram{
  497. desc: desc,
  498. count: count,
  499. sum: sum,
  500. buckets: buckets,
  501. labelPairs: makeLabelPairs(desc, labelValues),
  502. }, nil
  503. }
  504. // MustNewConstHistogram is a version of NewConstHistogram that panics where
  505. // NewConstMetric would have returned an error.
  506. func MustNewConstHistogram(
  507. desc *Desc,
  508. count uint64,
  509. sum float64,
  510. buckets map[float64]uint64,
  511. labelValues ...string,
  512. ) Metric {
  513. m, err := NewConstHistogram(desc, count, sum, buckets, labelValues...)
  514. if err != nil {
  515. panic(err)
  516. }
  517. return m
  518. }
  519. type buckSort []*dto.Bucket
  520. func (s buckSort) Len() int {
  521. return len(s)
  522. }
  523. func (s buckSort) Swap(i, j int) {
  524. s[i], s[j] = s[j], s[i]
  525. }
  526. func (s buckSort) Less(i, j int) bool {
  527. return s[i].GetUpperBound() < s[j].GetUpperBound()
  528. }