2025-10-10 09:42:30 -04:00
// Copyright The Prometheus Authors
2025-09-17 06:02:16 -04:00
// Licensed under the Apache License, Version 2.0 (the "License");
2025-10-10 09:42:30 -04:00
// you may not use this file except in compliance with the License.
2025-09-17 06:02:16 -04:00
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
2025-10-10 09:42:30 -04:00
// Unless required by applicable law or agreed to in writing, software
2025-09-17 06:02:16 -04:00
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package histogram
import (
"testing"
2025-09-24 18:12:23 -04:00
"github.com/stretchr/testify/require"
2025-09-25 05:58:25 -04:00
"github.com/prometheus/prometheus/model/labels"
2025-09-17 06:02:16 -04:00
)
2025-10-06 13:26:45 -04:00
type sample struct {
lset labels . Labels
val float64
2025-09-24 18:12:23 -04:00
}
2025-09-17 06:02:16 -04:00
func TestConvertNHCBToClassicHistogram ( t * testing . T ) {
tests := [ ] struct {
2025-09-24 18:12:23 -04:00
name string
nhcb any
labels labels . Labels
expectErr bool
2025-10-06 13:26:45 -04:00
expected [ ] sample
2025-09-17 06:02:16 -04:00
} {
{
2025-10-06 13:26:45 -04:00
name : "valid histogram" ,
2025-09-17 06:02:16 -04:00
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 , 2 , 3 } ,
2025-09-25 08:10:10 -04:00
PositiveBuckets : [ ] int64 { 10 , 20 , 30 } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span {
{ Offset : 0 , Length : 3 } ,
} ,
Count : 100 ,
Sum : 100.0 ,
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
2025-10-06 13:26:45 -04:00
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 10 } ,
2025-10-10 09:42:30 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 40 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "3.0" ) , val : 100 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 100 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 100 } ,
2025-10-06 13:26:45 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 100 } ,
2025-09-17 06:02:16 -04:00
} ,
} ,
{
2025-10-06 13:26:45 -04:00
name : "valid floatHistogram" ,
2025-09-17 06:02:16 -04:00
nhcb : & FloatHistogram {
CustomValues : [ ] float64 { 1 , 2 , 3 } ,
2025-10-10 09:42:30 -04:00
PositiveBuckets : [ ] float64 { 20.0 , 40.0 , 60.0 } , // 20 -> 60 ->120
PositiveSpans : [ ] Span {
{ Offset : 0 , Length : 3 } ,
} ,
Count : 120.0 ,
Sum : 100.0 ,
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
2025-10-06 13:26:45 -04:00
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 20 } ,
2025-10-10 09:42:30 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 60 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "3.0" ) , val : 120 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 120 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 120 } ,
2025-10-06 13:26:45 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 100 } ,
2025-09-17 06:02:16 -04:00
} ,
} ,
{
2025-10-06 13:26:45 -04:00
name : "empty histogram" ,
2025-09-17 06:02:16 -04:00
nhcb : & Histogram {
CustomValues : [ ] float64 { } ,
PositiveBuckets : [ ] int64 { } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span { } ,
2025-09-17 06:02:16 -04:00
Count : 0 ,
Sum : 0.0 ,
2025-10-10 09:42:30 -04:00
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
2025-10-06 13:26:45 -04:00
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 0 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 0 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 0 } ,
2025-09-17 06:02:16 -04:00
} ,
} ,
{
2025-10-06 13:26:45 -04:00
name : "missing __name__ label" ,
2025-09-17 06:02:16 -04:00
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 , 2 , 3 } ,
2025-09-25 08:10:10 -04:00
PositiveBuckets : [ ] int64 { 10 , 20 , 30 } ,
2025-10-10 09:42:30 -04:00
Count : 100 ,
2025-09-17 06:02:16 -04:00
Sum : 100.0 ,
2025-10-10 09:42:30 -04:00
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "job" , "test_job" ) ,
expectErr : true ,
} ,
{
2025-10-06 13:26:45 -04:00
name : "unsupported histogram type" ,
2025-09-17 06:02:16 -04:00
nhcb : nil ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
expectErr : true ,
} ,
{
2025-10-06 13:26:45 -04:00
name : "histogram with zero bucket counts" ,
2025-09-17 06:02:16 -04:00
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 , 2 , 3 } ,
2025-09-25 08:10:10 -04:00
PositiveBuckets : [ ] int64 { 0 , 10 , 0 } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span {
{ Offset : 0 , Length : 3 } ,
} ,
Count : 20 ,
Sum : 50.0 ,
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
2025-10-06 13:26:45 -04:00
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 0 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 10 } ,
2025-10-10 09:42:30 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "3.0" ) , val : 20 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 20 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 20 } ,
2025-10-06 13:26:45 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 50 } ,
2025-09-17 06:02:16 -04:00
} ,
} ,
{
2025-10-28 10:59:44 -04:00
name : "extra bucket counts than custom values" ,
2025-09-17 06:02:16 -04:00
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 , 2 } ,
2025-09-25 08:10:10 -04:00
PositiveBuckets : [ ] int64 { 10 , 20 , 30 } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span { { Offset : 0 , Length : 3 } } ,
Count : 100 ,
2025-09-17 06:02:16 -04:00
Sum : 100.0 ,
2025-10-10 09:42:30 -04:00
Schema : CustomBucketsSchema ,
} ,
2025-10-28 10:59:44 -04:00
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 40 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 100 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 100 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 100 } ,
} ,
2025-10-10 09:42:30 -04:00
} ,
{
name : "mismatched bucket lengths with less filled bucket count" ,
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 , 2 } ,
PositiveBuckets : [ ] int64 { 10 } ,
PositiveSpans : [ ] Span { { Offset : 0 , Length : 2 } } ,
Count : 100 ,
Sum : 100.0 ,
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
2025-10-06 13:26:45 -04:00
labels : labels . FromStrings ( "__name__" , "test_metric_bucket" ) ,
2025-09-17 06:02:16 -04:00
expectErr : true ,
} ,
{
name : "single series Histogram" ,
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 } ,
2025-09-25 08:10:10 -04:00
PositiveBuckets : [ ] int64 { 10 } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span {
{ Offset : 0 , Length : 1 } ,
} ,
Count : 10 ,
Sum : 20.0 ,
Schema : CustomBucketsSchema ,
2025-09-17 06:02:16 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
2025-10-06 13:26:45 -04:00
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 20 } ,
} ,
} ,
{
name : "multiset label histogram" ,
nhcb : & Histogram {
CustomValues : [ ] float64 { 1 } ,
PositiveBuckets : [ ] int64 { 10 } ,
2025-10-10 09:42:30 -04:00
PositiveSpans : [ ] Span {
{ Offset : 0 , Length : 1 } ,
} ,
Count : 10 ,
Sum : 20.0 ,
Schema : CustomBucketsSchema ,
2025-10-06 13:26:45 -04:00
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" , "job" , "test_job" , "instance" , "localhost:9090" ) ,
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "job" , "test_job" , "instance" , "localhost:9090" , "le" , "1.0" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "job" , "test_job" , "instance" , "localhost:9090" , "le" , "+Inf" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" , "job" , "test_job" , "instance" , "localhost:9090" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" , "job" , "test_job" , "instance" , "localhost:9090" ) , val : 20 } ,
} ,
} ,
{
name : "exponential histogram" ,
nhcb : & FloatHistogram {
Schema : 1 ,
ZeroThreshold : 0.01 ,
ZeroCount : 5.5 ,
Count : 3493.3 ,
Sum : 2349209.324 ,
PositiveSpans : [ ] Span {
{ - 2 , 1 } ,
{ 2 , 3 } ,
} ,
PositiveBuckets : [ ] float64 { 1 , 3.3 , 4.2 , 0.1 } ,
NegativeSpans : [ ] Span {
{ 3 , 2 } ,
{ 3 , 2 } ,
2025-09-24 18:12:23 -04:00
} ,
2025-10-06 13:26:45 -04:00
NegativeBuckets : [ ] float64 { 3.1 , 3 , 1.234e5 , 1000 } ,
2025-09-17 06:02:16 -04:00
} ,
2025-10-06 13:26:45 -04:00
labels : labels . FromStrings ( "__name__" , "test_metric_bucket" ) ,
expectErr : true ,
2025-09-17 06:02:16 -04:00
} ,
2025-10-10 09:42:30 -04:00
{
name : "sparse histogram" ,
nhcb : & Histogram {
Schema : CustomBucketsSchema ,
CustomValues : [ ] float64 { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ,
PositiveSpans : [ ] Span {
{ 0 , 2 } ,
{ 4 , 1 } ,
{ 1 , 2 } ,
} ,
PositiveBuckets : [ ] int64 { 1 , 2 , 3 , 4 , 5 } , // 1 -> 3 -> 3 -> 3 -> 3 -> 3 -> 6 ->6 ->10 -> 15
2025-10-28 10:59:44 -04:00
Count : 35 , // 1 -> 4 -> 7 -> 10 -> 13 -> 16 -> 22 -> 28 -> 38 -> 53
2025-10-10 09:42:30 -04:00
Sum : 123 ,
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 1 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 4 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "3.0" ) , val : 7 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "4.0" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "5.0" ) , val : 13 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "6.0" ) , val : 16 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "7.0" ) , val : 22 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "8.0" ) , val : 28 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "9.0" ) , val : 38 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "10.0" ) , val : 53 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 53 } ,
2025-10-28 10:59:44 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 35 } ,
2025-10-10 09:42:30 -04:00
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 123 } ,
} ,
} ,
{
name : "sparse float histogram" ,
nhcb : & FloatHistogram {
Schema : CustomBucketsSchema ,
CustomValues : [ ] float64 { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ,
PositiveSpans : [ ] Span {
{ 0 , 2 } ,
{ 4 , 1 } ,
{ 1 , 2 } ,
} ,
PositiveBuckets : [ ] float64 { 1 , 2 , 3 , 4 , 5 } , // 1 -> 2 -> 0 -> 0 -> 0 -> 0 -> 3 -> 0 -> 4 -> 5
Count : 15 , // 1 -> 3 -> 3 -> 3 -> 3 -> 3 -> 6 -> 6 -> 10 -> 15
Sum : 123 ,
} ,
labels : labels . FromStrings ( "__name__" , "test_metric" ) ,
expected : [ ] sample {
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "1.0" ) , val : 1 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "2.0" ) , val : 3 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "3.0" ) , val : 3 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "4.0" ) , val : 3 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "5.0" ) , val : 3 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "6.0" ) , val : 3 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "7.0" ) , val : 6 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "8.0" ) , val : 6 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "9.0" ) , val : 10 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "10.0" ) , val : 15 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_bucket" , "le" , "+Inf" ) , val : 15 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_count" ) , val : 15 } ,
{ lset : labels . FromStrings ( "__name__" , "test_metric_sum" ) , val : 123 } ,
} ,
} ,
2025-09-17 06:02:16 -04:00
}
2025-09-24 18:12:23 -04:00
labelBuilder := labels . NewBuilder ( labels . EmptyLabels ( ) )
2025-09-17 06:02:16 -04:00
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2025-10-06 13:26:45 -04:00
var emittedSamples [ ] sample
err := ConvertNHCBToClassic ( tt . nhcb , tt . labels , labelBuilder , func ( lbls labels . Labels , val float64 ) error {
emittedSamples = append ( emittedSamples , sample { lset : lbls , val : val } )
2025-09-17 06:02:16 -04:00
return nil
} )
2025-09-24 18:12:23 -04:00
require . Equal ( t , tt . expectErr , err != nil , "unexpected error: %v" , err )
2025-09-17 06:02:16 -04:00
if ! tt . expectErr {
2025-10-06 13:26:45 -04:00
require . Len ( t , emittedSamples , len ( tt . expected ) )
for i , expSample := range tt . expected {
2025-10-06 14:52:25 -04:00
require . True ( t , labels . Equal ( expSample . lset , emittedSamples [ i ] . lset ) , "labels mismatch at index %d: expected %v, got %v" , i , expSample . lset , emittedSamples [ i ] . lset )
require . Equal ( t , expSample . val , emittedSamples [ i ] . val , "value mismatch at index %d" , i )
2025-09-17 06:02:16 -04:00
}
}
} )
}
}