MCPcopy Create free account
hub / github.com/cortexproject/cortex / marshalHistogram

Function marshalHistogram

pkg/querier/tripperware/query.go:715–741  ·  view source on GitHub ↗

MarshalHistogram marshals a histogram value using the passed jsoniter stream. It writes something like: { "count": "42", "sum": "34593.34", "buckets": [ [ 3, "-0.25", "0.25", "3"], [ 0, "0.25", "0.5", "12"], [ 0, "0.5", "1", "21"], [ 0, "2", "4", "6"]

(h SampleHistogram, stream *jsoniter.Stream)

Source from the content-addressed store, hash-verified

713// the bucket count.
714// Adapted from https://github.com/prometheus/client_golang/blob/4b158abea9470f75b6f07460cdc2189b91914562/api/prometheus/v1/api.go#L329
715func marshalHistogram(h SampleHistogram, stream *jsoniter.Stream) {
716 stream.WriteObjectStart()
717 stream.WriteObjectField(`count`)
718 jsonutil.MarshalFloat(h.Count, stream)
719 stream.WriteMore()
720 stream.WriteObjectField(`sum`)
721 jsonutil.MarshalFloat(h.Sum, stream)
722
723 bucketFound := false
724 for _, bucket := range h.Buckets {
725 if bucket.Count == 0 {
726 continue // No need to expose empty buckets in JSON.
727 }
728 stream.WriteMore()
729 if !bucketFound {
730 stream.WriteObjectField(`buckets`)
731 stream.WriteArrayStart()
732 }
733 bucketFound = true
734 marshalHistogramBucket(*bucket, stream)
735 }
736
737 if bucketFound {
738 stream.WriteArrayEnd()
739 }
740 stream.WriteObjectEnd()
741}
742
743// marshalHistogramBucket writes something like: [ 3, "-0.25", "0.25", "3"]
744// See marshalHistogram to understand what the numbers mean.

Callers 1

Calls 1

marshalHistogramBucketFunction · 0.85

Tested by

no test coverage detected