histogram.go 22 KB

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