Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colored labels output for logcli #2470

Merged
merged 2 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var (
statistics = app.Flag("stats", "Show query statistics").Default("false").Bool()
outputMode = app.Flag("output", "Specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp.").Default("default").Short('o').Enum("default", "raw", "jsonl")
timezone = app.Flag("timezone", "Specify the timezone to use when formatting output timestamps [Local, UTC]").Default("Local").Short('z').Enum("Local", "UTC")

cpuProfile = app.Flag("cpuprofile", "Specify the location for writing a CPU profile.").Default("").String()
memProfile = app.Flag("memprofile", "Specify the location for writing a memory profile.").Default("").String()

Expand Down Expand Up @@ -119,8 +118,9 @@ func main() {
}

outputOptions := &output.LogOutputOptions{
Timezone: location,
NoLabels: rangeQuery.NoLabels,
Timezone: location,
NoLabels: rangeQuery.NoLabels,
ColoredOutput: rangeQuery.ColoredOutput,
}

out, err := output.NewLogOutput(*outputMode, outputOptions)
Expand All @@ -140,8 +140,9 @@ func main() {
}

outputOptions := &output.LogOutputOptions{
Timezone: location,
NoLabels: instantQuery.NoLabels,
Timezone: location,
NoLabels: instantQuery.NoLabels,
ColoredOutput: instantQuery.ColoredOutput,
}

out, err := output.NewLogOutput(*outputMode, outputOptions)
Expand Down Expand Up @@ -280,6 +281,7 @@ func newQuery(instant bool, cmd *kingpin.CmdClause) *query.Query {
cmd.Flag("include-label", "Include labels given the provided key during output.").StringsVar(&q.ShowLabelsKey)
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("colored-output", "Show ouput with colored labels").Default("false").BoolVar(&q.ColoredOutput)

return q
}
Expand Down
1 change: 1 addition & 0 deletions docs/sources/getting-started/logcli.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Flags:
--store-config="" Execute the current query using a configured storage from a given Loki configuration file.
-t, --tail Tail the logs
--delay-for=0 Delay in tailing by number of seconds to accumulate logs for re-ordering
--colored-output Show ouput with colored labels
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add periods to all the descriptions in this list.


Args:
<query> eg '{foo="bar",baz=~".*blip"} |~ ".*error.*"'
Expand Down
5 changes: 5 additions & 0 deletions pkg/logcli/output/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (o *DefaultOutput) Format(ts time.Time, lbls loghttp.LabelSet, maxLabelsLen
return fmt.Sprintf("%s %s", color.BlueString(timestamp), line)
}

if o.options.ColoredOutput {
labelsColor := getColor(lbls.String()).SprintFunc()
return fmt.Sprintf("%s %s %s", color.BlueString(timestamp), labelsColor(padLabel(lbls, maxLabelsLen)), line)
}

return fmt.Sprintf("%s %s %s", color.BlueString(timestamp), color.RedString(padLabel(lbls, maxLabelsLen)), line)
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/logcli/output/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,49 @@ func TestDefaultOutput_FormatLabelsPadding(t *testing.T) {
}
}

func TestColorForLabels(t *testing.T) {
tests := map[string]struct {
labels loghttp.LabelSet
otherLabels loghttp.LabelSet
expected bool
}{

"different labels": {
loghttp.LabelSet(map[string]string{
"type": "test",
"app": "loki",
}),
loghttp.LabelSet(map[string]string{
"type": "test",
"app": "grafana-loki",
}),
false,
},
"same labels": {
loghttp.LabelSet(map[string]string{
"type": "test",
"app": "loki",
}),
loghttp.LabelSet(map[string]string{
"type": "test",
"app": "loki",
}),
true,
},
}

for testName, testData := range tests {
testData := testData

t.Run(testName, func(t *testing.T) {
t.Parallel()
labelsColor := getColor(testData.labels.String())
otherLablesColor := getColor(testData.otherLabels.String())
assert.Equal(t, testData.expected, labelsColor.Equals(otherLablesColor))
})
}
}

func findMaxLabelsLength(labelsList []loghttp.LabelSet) int {
maxLabelsLen := 0

Expand Down
31 changes: 29 additions & 2 deletions pkg/logcli/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@ package output

import (
"fmt"
"hash/fnv"
"time"

"github.com/fatih/color"

"github.com/grafana/loki/pkg/loghttp"
)

// Blue color is excluded since we are already printing timestamp
// in blue color
var colorList = []*color.Color{
color.New(color.FgHiCyan),
color.New(color.FgCyan),
color.New(color.FgHiGreen),
color.New(color.FgGreen),
color.New(color.FgHiMagenta),
color.New(color.FgMagenta),
color.New(color.FgHiYellow),
color.New(color.FgYellow),
color.New(color.FgHiRed),
color.New(color.FgRed),
}

// LogOutput is the interface any output mode must implement
type LogOutput interface {
Format(ts time.Time, lbls loghttp.LabelSet, maxLabelsLen int, line string) string
}

// LogOutputOptions defines options supported by LogOutput
type LogOutputOptions struct {
Timezone *time.Location
NoLabels bool
Timezone *time.Location
NoLabels bool
ColoredOutput bool
}

// NewLogOutput creates a log output based on the input mode and options
Expand All @@ -41,3 +60,11 @@ func NewLogOutput(mode string, options *LogOutputOptions) (LogOutput, error) {
return nil, fmt.Errorf("unknown log output mode '%s'", mode)
}
}

func getColor(labels string) *color.Color {
hash := fnv.New32()
hash.Write([]byte(labels))
id := hash.Sum32() % uint32(len(colorList))
color := colorList[id]
return color
}
2 changes: 1 addition & 1 deletion pkg/logcli/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestNewLogOutput(t *testing.T) {
options := &LogOutputOptions{time.UTC, false}
options := &LogOutputOptions{time.UTC, false, false}

out, err := NewLogOutput("default", options)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/logcli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type Query struct {
IgnoreLabelsKey []string
ShowLabelsKey []string
FixedLabelsLen int

LocalConfig string
ColoredOutput bool
LocalConfig string
}

// DoQuery executes the query and prints out the results
Expand Down