@@ -342,3 +342,102 @@ func (m *HistogramVec) WithLabelValues(lvs ...string) Histogram {
342
342
func (m * HistogramVec ) With (labels Labels ) Histogram {
343
343
return m .MetricVec .With (labels ).(Histogram )
344
344
}
345
+
346
+ type constHistogram struct {
347
+ desc * Desc
348
+ count uint64
349
+ sum float64
350
+ buckets map [float64 ]uint64
351
+ labelPairs []* dto.LabelPair
352
+ }
353
+
354
+ func (h * constHistogram ) Desc () * Desc {
355
+ return h .desc
356
+ }
357
+
358
+ func (h * constHistogram ) Write (out * dto.Metric ) error {
359
+ his := & dto.Histogram {}
360
+ buckets := make ([]* dto.Bucket , 0 , len (h .buckets ))
361
+
362
+ his .SampleCount = proto .Uint64 (h .count )
363
+ his .SampleSum = proto .Float64 (h .sum )
364
+
365
+ for upperBound , count := range h .buckets {
366
+ buckets = append (buckets , & dto.Bucket {
367
+ CumulativeCount : proto .Uint64 (count ),
368
+ UpperBound : proto .Float64 (upperBound ),
369
+ })
370
+ }
371
+
372
+ if len (buckets ) > 0 {
373
+ sort .Sort (buckSort (buckets ))
374
+ }
375
+ his .Bucket = buckets
376
+
377
+ out .Histogram = his
378
+ out .Label = h .labelPairs
379
+
380
+ return nil
381
+ }
382
+
383
+ // NewConstHistogram returns a metric representing a Prometheus histogram with
384
+ // fixed values for the count, sum, and bucket counts. As those parameters
385
+ // cannot be changed, the returned value does not implement the Histogram
386
+ // interface (but only the Metric interface). Users of this package will not
387
+ // have much use for it in regular operations. However, when implementing custom
388
+ // Collectors, it is useful as a throw-away metric that is generated on the fly
389
+ // to send it to Prometheus in the Collect method.
390
+ //
391
+ // buckets is a map of upper bounds to cumulative counts, excluding the +Inf
392
+ // bucket.
393
+ //
394
+ // NewConstHistogram returns an error if the length of labelValues is not
395
+ // consistent with the variable labels in Desc.
396
+ func NewConstHistogram (
397
+ desc * Desc ,
398
+ count uint64 ,
399
+ sum float64 ,
400
+ buckets map [float64 ]uint64 ,
401
+ labelValues ... string ,
402
+ ) (Metric , error ) {
403
+ if len (desc .variableLabels ) != len (labelValues ) {
404
+ return nil , errInconsistentCardinality
405
+ }
406
+ return & constHistogram {
407
+ desc : desc ,
408
+ count : count ,
409
+ sum : sum ,
410
+ buckets : buckets ,
411
+ labelPairs : makeLabelPairs (desc , labelValues ),
412
+ }, nil
413
+ }
414
+
415
+ // MustNewConstHistogram is a version of NewConstHistogram that panics where
416
+ // NewConstMetric would have returned an error.
417
+ func MustNewConstHistogram (
418
+ desc * Desc ,
419
+ count uint64 ,
420
+ sum float64 ,
421
+ buckets map [float64 ]uint64 ,
422
+ labelValues ... string ,
423
+ ) Metric {
424
+ m , err := NewConstHistogram (desc , count , sum , buckets , labelValues ... )
425
+ if err != nil {
426
+ panic (err )
427
+ }
428
+ return m
429
+ }
430
+
431
+ type buckSort []* dto.Bucket
432
+
433
+ func (s buckSort ) Len () int {
434
+ return len (s )
435
+ }
436
+
437
+ func (s buckSort ) Swap (i , j int ) {
438
+ s [i ], s [j ] = s [j ], s [i ]
439
+ }
440
+
441
+ func (s buckSort ) Less (i , j int ) bool {
442
+ return s [i ].GetUpperBound () < s [j ].GetUpperBound ()
443
+ }
0 commit comments