Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/querier/queryrange/detected_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"slices"
"strconv"
"strings"
"time"

"github.com/axiomhq/hyperloglog"
Expand Down Expand Up @@ -114,7 +115,13 @@ func parseDetectedFieldValues(limit uint32, streams []push.Stream, name string)
parsedLabels, _ := parseEntry(entry, entryLbls)
if vals, ok := parsedLabels[name]; ok {
for _, v := range vals {
values[v] = struct{}{}
// special case bytes values, so they can be directly inserted into a query
if bs, err := humanize.ParseBytes(v); err == nil {
bsString := strings.Replace(humanize.Bytes(bs), " ", "", 1)
values[bsString] = struct{}{}
} else {
values[v] = struct{}{}
}
}
}
}
Expand Down
66 changes: 65 additions & 1 deletion pkg/querier/queryrange/detected_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/grafana/loki/pkg/push"
)

func Test_parseDetectedFeilds(t *testing.T) {
func Test_parseDetectedFields(t *testing.T) {
now := time.Now()

t.Run("when no parsers are supplied", func(t *testing.T) {
Expand Down Expand Up @@ -1317,6 +1317,70 @@ func TestQuerier_DetectedFields(t *testing.T) {
}, secondValues)
},
)

t.Run("correctly formats bytes values for detected fields", func(t *testing.T) {
lbls := `{cluster="us-east-1", namespace="mimir-dev", pod="mimir-ruler-nfb37", service_name="mimir-ruler"}`
metric, err := parser.ParseMetric(lbls)
require.NoError(t, err)
now := time.Now()

infoDetectdFiledMetadata := []push.LabelAdapter{
{
Name: "detected_level",
Value: "info",
},
}

lines := []push.Entry{
{
Timestamp: now,
Line: "ts=2024-09-05T15:36:38.757788067Z caller=metrics.go:66 tenant=2419 level=info bytes=1,024",
StructuredMetadata: infoDetectdFiledMetadata,
},
{
Timestamp: now,
Line: `ts=2024-09-05T15:36:38.698375619Z caller=grpc_logging.go:66 tenant=29 level=info bytes="1024 MB"`,
StructuredMetadata: infoDetectdFiledMetadata,
},
{
Timestamp: now,
Line: "ts=2024-09-05T15:36:38.629424175Z caller=grpc_logging.go:66 tenant=2919 level=info bytes=1024KB",
StructuredMetadata: infoDetectdFiledMetadata,
},
}
stream := push.Stream{
Labels: lbls,
Entries: lines,
Hash: metric.Hash(),
}

handler := NewDetectedFieldsHandler(
limitedHandler(stream),
logHandler(stream),
limits,
)

request := DetectedFieldsRequest{
logproto.DetectedFieldsRequest{
Start: time.Now().Add(-1 * time.Minute),
End: time.Now(),
Query: `{cluster="us-east-1"} | logfmt`,
LineLimit: 1000,
Limit: 3,
Values: true,
Name: "bytes",
},
"/loki/api/v1/detected_field/bytes/values",
}

detectedFieldValues := handleRequest(handler, request).Values
slices.Sort(detectedFieldValues)
require.Equal(t, []string{
"1.0GB",
"1.0MB",
"1.0kB",
}, detectedFieldValues)
})
}

func BenchmarkQuerierDetectedFields(b *testing.B) {
Expand Down
Loading