Skip to content

Commit

Permalink
feat: present DF bytes values in queryable format (backport release-3…
Browse files Browse the repository at this point in the history
….3.x) (#15320)

Co-authored-by: Trevor Whitney <[email protected]>
  • Loading branch information
loki-gh-app[bot] and trevorwhitney authored Dec 9, 2024
1 parent 635f04d commit 218ecbb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
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

0 comments on commit 218ecbb

Please sign in to comment.