Skip to content

Commit

Permalink
feat(logcli): Include common labels (#15611)
Browse files Browse the repository at this point in the history
Adds a new `--include-common-labels` flag to logcli query, which causes the output to include all common labels.

This is mostly useful with `--quiet` and `--output=jsonl` as it allows users to get structured information from Loki that has all the information that Loki can provide.

---
Signed-off-by: Jonathan Lange <[email protected]>
Co-authored-by: Christian Haudum <[email protected]>
  • Loading branch information
jml authored Jan 8, 2025
1 parent 8ac0633 commit 639ac74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ func newQuery(instant bool, cmd *kingpin.CmdClause) *query.Query {
cmd.Flag("no-labels", "Do not print any labels").Default("false").BoolVar(&q.NoLabels)
cmd.Flag("exclude-label", "Exclude labels given the provided key during output.").StringsVar(&q.IgnoreLabelsKey)
cmd.Flag("include-label", "Include labels given the provided key during output.").StringsVar(&q.ShowLabelsKey)
cmd.Flag("include-common-labels", "Include common labels in output for each log line.").Default("false").BoolVar(&q.IncludeCommonLabels)
cmd.Flag("labels-length", "Set a fixed padding to labels").Default("0").IntVar(&q.FixedLabelsLen)
cmd.Flag("store-config", "Execute the current query using a configured storage from a given Loki configuration file.").Default("").StringVar(&q.LocalConfig)
cmd.Flag("remote-schema", "Execute the current query using a remote schema retrieved from the configured -schema-store.").Default("false").BoolVar(&q.FetchSchemaFromStorage)
Expand Down
2 changes: 1 addition & 1 deletion pkg/logcli/index/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetVolumeRange(q *volume.Query, c client.Client, out output.LogOutput, stat
func do(q *volume.Query, rangeQuery bool, c client.Client, out output.LogOutput, statistics bool) {
resp := getVolume(q, rangeQuery, c)

resultsPrinter := print.NewQueryResultPrinter(nil, nil, q.Quiet, 0, false)
resultsPrinter := print.NewQueryResultPrinter(nil, nil, q.Quiet, 0, false, false)

if statistics {
resultsPrinter.PrintStats(resp.Data.Statistics)
Expand Down
29 changes: 17 additions & 12 deletions pkg/logcli/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ import (
)

type QueryResultPrinter struct {
ShowLabelsKey []string
IgnoreLabelsKey []string
Quiet bool
FixedLabelsLen int
Forward bool
ShowLabelsKey []string
IgnoreLabelsKey []string
Quiet bool
FixedLabelsLen int
Forward bool
IncludeCommonLabels bool
}

func NewQueryResultPrinter(showLabelsKey []string, ignoreLabelsKey []string, quiet bool, fixedLabelsLen int, forward bool) *QueryResultPrinter {
func NewQueryResultPrinter(showLabelsKey []string, ignoreLabelsKey []string, quiet bool, fixedLabelsLen int, forward bool, includeCommonLabels bool) *QueryResultPrinter {
return &QueryResultPrinter{
ShowLabelsKey: showLabelsKey,
IgnoreLabelsKey: ignoreLabelsKey,
Quiet: quiet,
FixedLabelsLen: fixedLabelsLen,
Forward: forward,
ShowLabelsKey: showLabelsKey,
IgnoreLabelsKey: ignoreLabelsKey,
Quiet: quiet,
FixedLabelsLen: fixedLabelsLen,
Forward: forward,
IncludeCommonLabels: includeCommonLabels,
}
}

Expand Down Expand Up @@ -83,8 +85,11 @@ func (r *QueryResultPrinter) printStream(streams loghttp.Streams, out output.Log
// calculate the max labels length
maxLabelsLen := r.FixedLabelsLen
for i, s := range streams {
ls := s.Labels
// Remove common labels
ls := subtract(s.Labels, common)
if !r.IncludeCommonLabels {
ls = subtract(s.Labels, common)
}

if len(r.ShowLabelsKey) > 0 {
ls = matchLabels(true, ls, r.ShowLabelsKey)
Expand Down
5 changes: 3 additions & 2 deletions pkg/logcli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Query struct {
NoLabels bool
IgnoreLabelsKey []string
ShowLabelsKey []string
IncludeCommonLabels bool
FixedLabelsLen int
ColoredOutput bool
LocalConfig string
Expand Down Expand Up @@ -118,7 +119,7 @@ func (q *Query) DoQuery(c client.Client, out output.LogOutput, statistics bool)
out = out.WithWriter(partFile)
}

result := print.NewQueryResultPrinter(q.ShowLabelsKey, q.IgnoreLabelsKey, q.Quiet, q.FixedLabelsLen, q.Forward)
result := print.NewQueryResultPrinter(q.ShowLabelsKey, q.IgnoreLabelsKey, q.Quiet, q.FixedLabelsLen, q.Forward, q.IncludeCommonLabels)

if q.isInstant() {
resp, err = c.Query(q.QueryString, q.Limit, q.Start, d, q.Quiet)
Expand Down Expand Up @@ -523,7 +524,7 @@ func (q *Query) DoLocalQuery(out output.LogOutput, statistics bool, orgID string
return err
}

resPrinter := print.NewQueryResultPrinter(q.ShowLabelsKey, q.IgnoreLabelsKey, q.Quiet, q.FixedLabelsLen, q.Forward)
resPrinter := print.NewQueryResultPrinter(q.ShowLabelsKey, q.IgnoreLabelsKey, q.Quiet, q.FixedLabelsLen, q.Forward, q.IncludeCommonLabels)
if statistics {
resPrinter.PrintStats(result.Statistics)
}
Expand Down

0 comments on commit 639ac74

Please sign in to comment.