diff --git a/cmd/logcli/main.go b/cmd/logcli/main.go index 835216c2bb83c..c65896228f444 100644 --- a/cmd/logcli/main.go +++ b/cmd/logcli/main.go @@ -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) diff --git a/pkg/logcli/index/volume.go b/pkg/logcli/index/volume.go index b6a3205706912..902c9a33ee4f3 100644 --- a/pkg/logcli/index/volume.go +++ b/pkg/logcli/index/volume.go @@ -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) diff --git a/pkg/logcli/print/print.go b/pkg/logcli/print/print.go index 0f7d5d131151e..decb5ffa55844 100644 --- a/pkg/logcli/print/print.go +++ b/pkg/logcli/print/print.go @@ -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, } } @@ -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) diff --git a/pkg/logcli/query/query.go b/pkg/logcli/query/query.go index cb693a0125139..0551e076f742e 100644 --- a/pkg/logcli/query/query.go +++ b/pkg/logcli/query/query.go @@ -50,6 +50,7 @@ type Query struct { NoLabels bool IgnoreLabelsKey []string ShowLabelsKey []string + IncludeCommonLabels bool FixedLabelsLen int ColoredOutput bool LocalConfig string @@ -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) @@ -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) }