histogram.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 and Help to a non-empty string. All other fields are
  102. // optional and can safely be left at their zero value.
  103. type HistogramOpts struct {
  104. // Namespace, Subsystem, and Name are components of the fully-qualified
  105. // name of the Histogram (created by joining these components with
  106. // "_"). Only Name is mandatory, the others merely help structuring the
  107. // name. Note that the fully-qualified name of the Histogram must be a
  108. // valid Prometheus metric name.
  109. Namespace string
  110. Subsystem string
  111. Name string
  112. // Help provides information about this Histogram. Mandatory!
  113. //
  114. // Metrics with the same fully-qualified name must have the same Help
  115. // string.
  116. Help string
  117. // ConstLabels are used to attach fixed labels to this metric. Metrics
  118. // with the same fully-qualified name must have the same label names in
  119. // their ConstLabels.
  120. //
  121. // ConstLabels are only used rarely. In particular, do not use them to
  122. // attach the same labels to all your metrics. Those use cases are
  123. // better covered by target labels set by the scraping Prometheus
  124. // server, or by one specific metric (e.g. a build_info or a
  125. // machine_role metric). See also
  126. // https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels,-not-static-scraped-labels
  127. ConstLabels Labels
  128. // Buckets defines the buckets into which observations are counted. Each
  129. // element in the slice is the upper inclusive bound of a bucket. The
  130. // values must be sorted in strictly increasing order. There is no need
  131. // to add a highest bucket with +Inf bound, it will be added
  132. // implicitly. The default value is DefBuckets.
  133. Buckets []float64
  134. }
  135. // NewHistogram creates a new Histogram based on the provided HistogramOpts. It
  136. // panics if the buckets in HistogramOpts are not in strictly increasing order.
  137. func NewHistogram(opts HistogramOpts) Histogram {
  138. return newHistogram(
  139. NewDesc(
  140. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  141. opts.Help,
  142. nil,
  143. opts.ConstLabels,
  144. ),
  145. opts,
  146. )
  147. }
  148. func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogram {
  149. if len(desc.variableLabels) != len(labelValues) {
  150. panic(errInconsistentCardinality)
  151. }
  152. for _, n := range desc.variableLabels {
  153. if n == bucketLabel {
  154. panic(errBucketLabelNotAllowed)
  155. }
  156. }
  157. for _, lp := range desc.constLabelPairs {
  158. if lp.GetName() == bucketLabel {
  159. panic(errBucketLabelNotAllowed)
  160. }
  161. }
  162. if len(opts.Buckets) == 0 {
  163. opts.Buckets = DefBuckets
  164. }
  165. h := &histogram{
  166. desc: desc,
  167. upperBounds: opts.Buckets,
  168. labelPairs: makeLabelPairs(desc, labelValues),
  169. }
  170. for i, upperBound := range h.upperBounds {
  171. if i < len(h.upperBounds)-1 {
  172. if upperBound >= h.upperBounds[i+1] {
  173. panic(fmt.Errorf(
  174. "histogram buckets must be in increasing order: %f >= %f",
  175. upperBound, h.upperBounds[i+1],
  176. ))
  177. }
  178. } else {
  179. if math.IsInf(upperBound, +1) {
  180. // The +Inf bucket is implicit. Remove it here.
  181. h.upperBounds = h.upperBounds[:i]
  182. }
  183. }
  184. }
  185. // Finally we know the final length of h.upperBounds and can make counts
  186. // for both states:
  187. h.counts[0].buckets = make([]uint64, len(h.upperBounds))
  188. h.counts[1].buckets = make([]uint64, len(h.upperBounds))
  189. h.init(h) // Init self-collection.
  190. return h
  191. }
  192. type histogramCounts struct {
  193. // sumBits contains the bits of the float64 representing the sum of all
  194. // observations. sumBits and count have to go first in the struct to
  195. // guarantee alignment for atomic operations.
  196. // http://golang.org/pkg/sync/atomic/#pkg-note-BUG
  197. sumBits uint64
  198. count uint64
  199. buckets []uint64
  200. }
  201. type histogram struct {
  202. selfCollector
  203. desc *Desc
  204. writeMtx sync.Mutex // Only used in the Write method.
  205. upperBounds []float64
  206. // Two counts, one is "hot" for lock-free observations, the other is
  207. // "cold" for writing out a dto.Metric.
  208. counts [2]histogramCounts
  209. hotIdx int // Index of currently-hot counts. Only used within Write.
  210. // This is a complicated one. For lock-free yet atomic observations, we
  211. // need to save the total count of observations again, combined with the
  212. // index of the currently-hot counts struct, so that we can perform the
  213. // operation on both values atomically. The least significant bit
  214. // defines the hot counts struct. The remaining 63 bits represent the
  215. // total count of observations. This happens under the assumption that
  216. // the 63bit count will never overflow. Rationale: An observations takes
  217. // about 30ns. Let's assume it could happen in 10ns. Overflowing the
  218. // counter will then take at least (2^63)*10ns, which is about 3000
  219. // years.
  220. countAndHotIdx uint64
  221. labelPairs []*dto.LabelPair
  222. }
  223. func (h *histogram) Desc() *Desc {
  224. return h.desc
  225. }
  226. func (h *histogram) Observe(v float64) {
  227. // TODO(beorn7): For small numbers of buckets (<30), a linear search is
  228. // slightly faster than the binary search. If we really care, we could
  229. // switch from one search strategy to the other depending on the number
  230. // of buckets.
  231. //
  232. // Microbenchmarks (BenchmarkHistogramNoLabels):
  233. // 11 buckets: 38.3 ns/op linear - binary 48.7 ns/op
  234. // 100 buckets: 78.1 ns/op linear - binary 54.9 ns/op
  235. // 300 buckets: 154 ns/op linear - binary 61.6 ns/op
  236. i := sort.SearchFloat64s(h.upperBounds, v)
  237. // We increment h.countAndHotIdx by 2 so that the counter in the upper
  238. // 63 bits gets incremented by 1. At the same time, we get the new value
  239. // back, which we can use to find the currently-hot counts.
  240. n := atomic.AddUint64(&h.countAndHotIdx, 2)
  241. hotCounts := &h.counts[n%2]
  242. if i < len(h.upperBounds) {
  243. atomic.AddUint64(&hotCounts.buckets[i], 1)
  244. }
  245. for {
  246. oldBits := atomic.LoadUint64(&hotCounts.sumBits)
  247. newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
  248. if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
  249. break
  250. }
  251. }
  252. // Increment count last as we take it as a signal that the observation
  253. // is complete.
  254. atomic.AddUint64(&hotCounts.count, 1)
  255. }
  256. func (h *histogram) Write(out *dto.Metric) error {
  257. var (
  258. his = &dto.Histogram{}
  259. buckets = make([]*dto.Bucket, len(h.upperBounds))
  260. hotCounts, coldCounts *histogramCounts
  261. count uint64
  262. )
  263. // For simplicity, we mutex the rest of this method. It is not in the
  264. // hot path, i.e. Observe is called much more often than Write. The
  265. // complication of making Write lock-free isn't worth it.
  266. h.writeMtx.Lock()
  267. defer h.writeMtx.Unlock()
  268. // This is a bit arcane, which is why the following spells out this if
  269. // clause in English:
  270. //
  271. // If the currently-hot counts struct is #0, we atomically increment
  272. // h.countAndHotIdx by 1 so that from now on Observe will use the counts
  273. // struct #1. Furthermore, the atomic increment gives us the new value,
  274. // which, in its most significant 63 bits, tells us the count of
  275. // observations done so far up to and including currently ongoing
  276. // observations still using the counts struct just changed from hot to
  277. // cold. To have a normal uint64 for the count, we bitshift by 1 and
  278. // save the result in count. We also set h.hotIdx to 1 for the next
  279. // Write call, and we will refer to counts #1 as hotCounts and to counts
  280. // #0 as coldCounts.
  281. //
  282. // If the currently-hot counts struct is #1, we do the corresponding
  283. // things the other way round. We have to _decrement_ h.countAndHotIdx
  284. // (which is a bit arcane in itself, as we have to express -1 with an
  285. // unsigned int...).
  286. if h.hotIdx == 0 {
  287. count = atomic.AddUint64(&h.countAndHotIdx, 1) >> 1
  288. h.hotIdx = 1
  289. hotCounts = &h.counts[1]
  290. coldCounts = &h.counts[0]
  291. } else {
  292. count = atomic.AddUint64(&h.countAndHotIdx, ^uint64(0)) >> 1 // Decrement.
  293. h.hotIdx = 0
  294. hotCounts = &h.counts[0]
  295. coldCounts = &h.counts[1]
  296. }
  297. // Now we have to wait for the now-declared-cold counts to actually cool
  298. // down, i.e. wait for all observations still using it to finish. That's
  299. // the case once the count in the cold counts struct is the same as the
  300. // one atomically retrieved from the upper 63bits of h.countAndHotIdx.
  301. for {
  302. if count == atomic.LoadUint64(&coldCounts.count) {
  303. break
  304. }
  305. runtime.Gosched() // Let observations get work done.
  306. }
  307. his.SampleCount = proto.Uint64(count)
  308. his.SampleSum = proto.Float64(math.Float64frombits(atomic.LoadUint64(&coldCounts.sumBits)))
  309. var cumCount uint64
  310. for i, upperBound := range h.upperBounds {
  311. cumCount += atomic.LoadUint64(&coldCounts.buckets[i])
  312. buckets[i] = &dto.Bucket{
  313. CumulativeCount: proto.Uint64(cumCount),
  314. UpperBound: proto.Float64(upperBound),
  315. }
  316. }
  317. his.Bucket = buckets
  318. out.Histogram = his
  319. out.Label = h.labelPairs
  320. // Finally add all the cold counts to the new hot counts and reset the cold counts.
  321. atomic.AddUint64(&hotCounts.count, count)
  322. atomic.StoreUint64(&coldCounts.count, 0)
  323. for {
  324. oldBits := atomic.LoadUint64(&hotCounts.sumBits)
  325. newBits := math.Float64bits(math.Float64frombits(oldBits) + his.GetSampleSum())
  326. if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
  327. atomic.StoreUint64(&coldCounts.sumBits, 0)
  328. break
  329. }
  330. }
  331. for i := range h.upperBounds {
  332. atomic.AddUint64(&hotCounts.buckets[i], atomic.LoadUint64(&coldCounts.buckets[i]))
  333. atomic.StoreUint64(&coldCounts.buckets[i], 0)
  334. }
  335. return nil
  336. }
  337. // HistogramVec is a Collector that bundles a set of Histograms that all share the
  338. // same Desc, but have different values for their variable labels. This is used
  339. // if you want to count the same thing partitioned by various dimensions
  340. // (e.g. HTTP request latencies, partitioned by status code and method). Create
  341. // instances with NewHistogramVec.
  342. type HistogramVec struct {
  343. *metricVec
  344. }
  345. // NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and
  346. // partitioned by the given label names.
  347. func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec {
  348. desc := NewDesc(
  349. BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
  350. opts.Help,
  351. labelNames,
  352. opts.ConstLabels,
  353. )
  354. return &HistogramVec{
  355. metricVec: newMetricVec(desc, func(lvs ...string) Metric {
  356. return newHistogram(desc, opts, lvs...)
  357. }),
  358. }
  359. }
  360. // GetMetricWithLabelValues returns the Histogram for the given slice of label
  361. // values (same order as the VariableLabels in Desc). If that combination of
  362. // label values is accessed for the first time, a new Histogram is created.
  363. //
  364. // It is possible to call this method without using the returned Histogram to only
  365. // create the new Histogram but leave it at its starting value, a Histogram without
  366. // any observations.
  367. //
  368. // Keeping the Histogram for later use is possible (and should be considered if
  369. // performance is critical), but keep in mind that Reset, DeleteLabelValues and
  370. // Delete can be used to delete the Histogram from the HistogramVec. In that case, the
  371. // Histogram will still exist, but it will not be exported anymore, even if a
  372. // Histogram with the same label values is created later. See also the CounterVec
  373. // example.
  374. //
  375. // An error is returned if the number of label values is not the same as the
  376. // number of VariableLabels in Desc (minus any curried labels).
  377. //
  378. // Note that for more than one label value, this method is prone to mistakes
  379. // caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as
  380. // an alternative to avoid that type of mistake. For higher label numbers, the
  381. // latter has a much more readable (albeit more verbose) syntax, but it comes
  382. // with a performance overhead (for creating and processing the Labels map).
  383. // See also the GaugeVec example.
  384. func (v *HistogramVec) GetMetricWithLabelValues(lvs ...string) (Observer, error) {
  385. metric, err := v.metricVec.getMetricWithLabelValues(lvs...)
  386. if metric != nil {
  387. return metric.(Observer), err
  388. }
  389. return nil, err
  390. }
  391. // GetMetricWith returns the Histogram for the given Labels map (the label names
  392. // must match those of the VariableLabels in Desc). If that label map is
  393. // accessed for the first time, a new Histogram is created. Implications of
  394. // creating a Histogram without using it and keeping the Histogram for later use
  395. // are the same as for GetMetricWithLabelValues.
  396. //
  397. // An error is returned if the number and names of the Labels are inconsistent
  398. // with those of the VariableLabels in Desc (minus any curried labels).
  399. //
  400. // This method is used for the same purpose as
  401. // GetMetricWithLabelValues(...string). See there for pros and cons of the two
  402. // methods.
  403. func (v *HistogramVec) GetMetricWith(labels Labels) (Observer, error) {
  404. metric, err := v.metricVec.getMetricWith(labels)
  405. if metric != nil {
  406. return metric.(Observer), err
  407. }
  408. return nil, err
  409. }
  410. // WithLabelValues works as GetMetricWithLabelValues, but panics where
  411. // GetMetricWithLabelValues would have returned an error. Not returning an
  412. // error allows shortcuts like
  413. // myVec.WithLabelValues("404", "GET").Observe(42.21)
  414. func (v *HistogramVec) WithLabelValues(lvs ...string) Observer {
  415. h, err := v.GetMetricWithLabelValues(lvs...)
  416. if err != nil {
  417. panic(err)
  418. }
  419. return h
  420. }
  421. // With works as GetMetricWith but panics where GetMetricWithLabels would have
  422. // returned an error. Not returning an error allows shortcuts like
  423. // myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Observe(42.21)
  424. func (v *HistogramVec) With(labels Labels) Observer {
  425. h, err := v.GetMetricWith(labels)
  426. if err != nil {
  427. panic(err)
  428. }
  429. return h
  430. }
  431. // CurryWith returns a vector curried with the provided labels, i.e. the
  432. // returned vector has those labels pre-set for all labeled operations performed
  433. // on it. The cardinality of the curried vector is reduced accordingly. The
  434. // order of the remaining labels stays the same (just with the curried labels
  435. // taken out of the sequence – which is relevant for the
  436. // (GetMetric)WithLabelValues methods). It is possible to curry a curried
  437. // vector, but only with labels not yet used for currying before.
  438. //
  439. // The metrics contained in the HistogramVec are shared between the curried and
  440. // uncurried vectors. They are just accessed differently. Curried and uncurried
  441. // vectors behave identically in terms of collection. Only one must be
  442. // registered with a given registry (usually the uncurried version). The Reset
  443. // method deletes all metrics, even if called on a curried vector.
  444. func (v *HistogramVec) CurryWith(labels Labels) (ObserverVec, error) {
  445. vec, err := v.curryWith(labels)
  446. if vec != nil {
  447. return &HistogramVec{vec}, err
  448. }
  449. return nil, err
  450. }
  451. // MustCurryWith works as CurryWith but panics where CurryWith would have
  452. // returned an error.
  453. func (v *HistogramVec) MustCurryWith(labels Labels) ObserverVec {
  454. vec, err := v.CurryWith(labels)
  455. if err != nil {
  456. panic(err)
  457. }
  458. return vec
  459. }
  460. type constHistogram struct {
  461. desc *Desc
  462. count uint64
  463. sum float64
  464. buckets map[float64]uint64
  465. labelPairs []*dto.LabelPair
  466. }
  467. func (h *constHistogram) Desc() *Desc {
  468. return h.desc
  469. }
  470. func (h *constHistogram) Write(out *dto.Metric) error {
  471. his := &dto.Histogram{}
  472. buckets := make([]*dto.Bucket, 0, len(h.buckets))
  473. his.SampleCount = proto.Uint64(h.count)
  474. his.SampleSum = proto.Float64(h.sum)
  475. for upperBound, count := range h.buckets {
  476. buckets = append(buckets, &dto.Bucket{
  477. CumulativeCount: proto.Uint64(count),
  478. UpperBound: proto.Float64(upperBound),
  479. })
  480. }
  481. if len(buckets) > 0 {
  482. sort.Sort(buckSort(buckets))
  483. }
  484. his.Bucket = buckets
  485. out.Histogram = his
  486. out.Label = h.labelPairs
  487. return nil
  488. }
  489. // NewConstHistogram returns a metric representing a Prometheus histogram with
  490. // fixed values for the count, sum, and bucket counts. As those parameters
  491. // cannot be changed, the returned value does not implement the Histogram
  492. // interface (but only the Metric interface). Users of this package will not
  493. // have much use for it in regular operations. However, when implementing custom
  494. // Collectors, it is useful as a throw-away metric that is generated on the fly
  495. // to send it to Prometheus in the Collect method.
  496. //
  497. // buckets is a map of upper bounds to cumulative counts, excluding the +Inf
  498. // bucket.
  499. //
  500. // NewConstHistogram returns an error if the length of labelValues is not
  501. // consistent with the variable labels in Desc.
  502. func NewConstHistogram(
  503. desc *Desc,
  504. count uint64,
  505. sum float64,
  506. buckets map[float64]uint64,
  507. labelValues ...string,
  508. ) (Metric, error) {
  509. if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil {
  510. return nil, err
  511. }
  512. return &constHistogram{
  513. desc: desc,
  514. count: count,
  515. sum: sum,
  516. buckets: buckets,
  517. labelPairs: makeLabelPairs(desc, labelValues),
  518. }, nil
  519. }
  520. // MustNewConstHistogram is a version of NewConstHistogram that panics where
  521. // NewConstMetric would have returned an error.
  522. func MustNewConstHistogram(
  523. desc *Desc,
  524. count uint64,
  525. sum float64,
  526. buckets map[float64]uint64,
  527. labelValues ...string,
  528. ) Metric {
  529. m, err := NewConstHistogram(desc, count, sum, buckets, labelValues...)
  530. if err != nil {
  531. panic(err)
  532. }
  533. return m
  534. }
  535. type buckSort []*dto.Bucket
  536. func (s buckSort) Len() int {
  537. return len(s)
  538. }
  539. func (s buckSort) Swap(i, j int) {
  540. s[i], s[j] = s[j], s[i]
  541. }
  542. func (s buckSort) Less(i, j int) bool {
  543. return s[i].GetUpperBound() < s[j].GetUpperBound()
  544. }