Skip to content

Commit 8e260fe

Browse files
fix: bytes unit detection in detected fields (#15525)
1 parent 10194f7 commit 8e260fe

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

‎pkg/querier/queryrange/detected_fields.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,45 @@ func NewDetectedFieldsHandler(
9191
})
9292
}
9393

94+
type bytesUnit []string
95+
96+
func (b bytesUnit) Contains(s string) bool {
97+
for _, u := range b {
98+
if strings.HasSuffix(s, u) {
99+
return true
100+
}
101+
}
102+
return false
103+
}
104+
105+
var allowedBytesUnits = bytesUnit{
106+
"b",
107+
"kib",
108+
"kb",
109+
"mib",
110+
"mb",
111+
"gib",
112+
"gb",
113+
"tib",
114+
"tb",
115+
"pib",
116+
"pb",
117+
"eib",
118+
"eb",
119+
"ki",
120+
"k",
121+
"mi",
122+
"m",
123+
"gi",
124+
"g",
125+
"ti",
126+
"t",
127+
"pi",
128+
"p",
129+
"ei",
130+
"e",
131+
}
132+
94133
func parseDetectedFieldValues(limit uint32, streams []push.Stream, name string) []string {
95134
values := map[string]struct{}{}
96135
for _, stream := range streams {
@@ -116,7 +155,7 @@ func parseDetectedFieldValues(limit uint32, streams []push.Stream, name string)
116155
if vals, ok := parsedLabels[name]; ok {
117156
for _, v := range vals {
118157
// special case bytes values, so they can be directly inserted into a query
119-
if bs, err := humanize.ParseBytes(v); err == nil {
158+
if bs, err := humanize.ParseBytes(v); err == nil && allowedBytesUnits.Contains(strings.ToLower(v)) {
120159
bsString := strings.Replace(humanize.Bytes(bs), " ", "", 1)
121160
values[bsString] = struct{}{}
122161
} else {

‎pkg/querier/queryrange/detected_fields_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ func TestQuerier_DetectedFields(t *testing.T) {
13341334
lines := []push.Entry{
13351335
{
13361336
Timestamp: now,
1337-
Line: "ts=2024-09-05T15:36:38.757788067Z caller=metrics.go:66 tenant=2419 level=info bytes=1,024",
1337+
Line: "ts=2024-09-05T15:36:38.757788067Z caller=metrics.go:66 tenant=2419 level=info bytes=1024",
13381338
StructuredMetadata: infoDetectdFiledMetadata,
13391339
},
13401340
{
@@ -1378,7 +1378,29 @@ func TestQuerier_DetectedFields(t *testing.T) {
13781378
require.Equal(t, []string{
13791379
"1.0GB",
13801380
"1.0MB",
1381-
"1.0kB",
1381+
"1024",
1382+
}, detectedFieldValues)
1383+
1384+
// does not affect other numeric values
1385+
request = DetectedFieldsRequest{
1386+
logproto.DetectedFieldsRequest{
1387+
Start: time.Now().Add(-1 * time.Minute),
1388+
End: time.Now(),
1389+
Query: `{cluster="us-east-1"} | logfmt`,
1390+
LineLimit: 1000,
1391+
Limit: 3,
1392+
Values: true,
1393+
Name: "tenant",
1394+
},
1395+
"/loki/api/v1/detected_field/tenant/values",
1396+
}
1397+
1398+
detectedFieldValues = handleRequest(handler, request).Values
1399+
slices.Sort(detectedFieldValues)
1400+
require.Equal(t, []string{
1401+
"2419",
1402+
"29",
1403+
"2919",
13821404
}, detectedFieldValues)
13831405
})
13841406
}

0 commit comments

Comments
 (0)