Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions integration/query_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -970,15 +971,34 @@ var comparer = cmp.Comparer(func(x, y model.Value) bool {
return l == r || (compareFloats(float64(l.Count), float64(r.Count)) && compareFloats(float64(l.Sum), float64(r.Sum)) && compareHistogramBuckets(l.Buckets, r.Buckets))
}

fetchValuesFromNH := func(nhString string) []float64 {
// Regex to match float numbers
re := regexp.MustCompile(`-?\d+(\.\d+)?`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a regex here can we use parser.ParseSeriesDesc() to parse this into a real histogram?

See: https://github.com/prometheus/prometheus/blob/main/promql/parser/parse.go#L252

value := r.Metric.Get("value")
lbls, val, err := parser.ParseSeriesDesc(value)

This worked for me.

Copy link
Member Author

@SungJin1212 SungJin1212 May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but it doesn't seem to work for me..
The query result is like:

status": "success",
    "data": {
        "resultType": "matrix",
        "result": [
            {
                "metric": {
                    "value": "{count:102, sum:202.39999999999998, [-4,-2.82842712474619):11, [-2.82842712474619,-2):11, [-1.414213562373095,-1):12, [-1,-0.7071067811865475):11, [-0.001,0.001]:12, (0.7071067811865475,1]:11, (1,1.414213562373095]:12, (2,2.82842712474619]:11, (2.82842712474619,4]:11}"
                },
                "values": [
                    [
                        1747623681,
                        "1"
                    ]
                ]
            },

I got the error: err 1:7: parse error: unexpected character inside braces: ':' when the input of ParseSeriesDesc . is "{count:102, sum:202.39999999999998, [-4,-2.82842712474619):11, [-2.82842712474619,-2):11, [-1.414213562373095,-1):12, [-1,-0.7071067811865475):11, [-0.001,0.001]:12, (0.7071067811865475,1]:11, (1,1.414213562373095]:12, (2,2.82842712474619]:11, (2.82842712474619,4]:11}".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably parser.ParseSeriesDesc doesn't support NH format now. We can go ahead with the regex I think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I was mistaken earlier.


matches := re.FindAllString(nhString, -1)

var ret []float64
for _, match := range matches {
f, err := strconv.ParseFloat(match, 64)
if err != nil {
continue
}
ret = append(ret, f)
}
return ret
}

// count_values returns a metrics with one label {"value": "1.012321"}
// or {"value": "{count:114, sum:226.93333333333334, [-4,-2.82842712474619):12.333333333333332, [-2.82842712474619,-2):12.333333333333332, [-1.414213562373095,-1):13.333333333333334, [-1,-0.7071067811865475):12.333333333333332, [-0.001,0.001]:13.333333333333334, (0.7071067811865475,1]:12.333333333333332, (1,1.414213562373095]:13.333333333333334, (2,2.82842712474619]:12.333333333333332, (2.82842712474619,4]:12.333333333333332}"}}
compareValueMetrics := func(l, r model.Metric) (valueMetric bool, equals bool) {
lLabels := model.LabelSet(l).Clone()
rLabels := model.LabelSet(r).Clone()
var (
lVal, rVal model.LabelValue
lFloat, rFloat float64
ok bool
err error
lVal, rVal model.LabelValue
lFloats, rFloats []float64 // when NH, these contain float64 values in NH
lFloat, rFloat float64
ok bool
err error
)

if lVal, ok = lLabels["value"]; !ok {
Expand All @@ -989,6 +1009,24 @@ var comparer = cmp.Comparer(func(x, y model.Value) bool {
return false, false
}

if strings.Contains(string(lVal), "count") && strings.Contains(string(rVal), "count") {
// the values are histograms
lFloats = fetchValuesFromNH(string(lVal))
rFloats = fetchValuesFromNH(string(rVal))

if len(lFloats) != len(rFloats) {
return true, false
}

for i := 0; i < len(lFloats); i++ {
if !compareFloats(lFloats[i], rFloats[i]) {
return true, false
}
}

return true, true
}

if lFloat, err = strconv.ParseFloat(string(lVal), 64); err != nil {
return false, false
}
Expand Down
Loading