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

feat(logcli): output modes #731

Merged
merged 6 commits into from
Jul 15, 2019
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
Prev Previous commit
Next Next commit
feat(logcli): output modes
Adds two alternative output modes (-o / --output)

- raw: emits the line as parsed
- jsonl: emits the line plus all known metadata as JSONL (JSON Line)

Usage: -o [default, raw, jsonl]
  • Loading branch information
sh0rez committed Jul 9, 2019
commit e26beda350cde300cdd812059dd8a25106670437
3 changes: 2 additions & 1 deletion cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

var (
app = kingpin.New("logcli", "A command-line for loki.")
app = kingpin.New("logcli", "A command-line for loki.")
quiet = app.Flag("quiet", "suppress everything but log lines").Default("false").Short('q').Bool()
outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]").Default("default").Short('o').Enum("default", "raw", "jsonl")

addr = app.Flag("addr", "Server address.").Default("https://logs-us-west1.grafana.net").Envar("GRAFANA_ADDR").String()
username = app.Flag("username", "Username for HTTP basic auth.").Default("").Envar("GRAFANA_USERNAME").String()
Expand Down
14 changes: 11 additions & 3 deletions cmd/logcli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -72,8 +73,8 @@ func doQuery() {
i = iter.NewQueryResponseIterator(resp, d)

for i.Next() {
ls := labelsCache(i.Labels())
ls = subtract(ls, common)
fullls := labelsCache(i.Labels())
sh0rez marked this conversation as resolved.
Show resolved Hide resolved
ls := subtract(fullls, common)
if len(*ignoreLabelsKey) > 0 {
ls = ls.MatchLabels(false, *ignoreLabelsKey...)
}
Expand All @@ -83,7 +84,14 @@ func doQuery() {
labels = padLabel(ls, maxLabelsLen)
}

printLogEntry(i.Entry().Timestamp, labels, i.Entry().Line)
switch *outputMode {
sh0rez marked this conversation as resolved.
Show resolved Hide resolved
case "jsonl":
printLogEntryJSONL(i.Entry().Timestamp, fullls, i.Entry().Line)
case "raw":
fmt.Println(i.Entry().Line)
default:
printLogEntry(i.Entry().Timestamp, labels, i.Entry().Line)
}
}

if err := i.Error(); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions cmd/logcli/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"log"
"sort"
Expand All @@ -22,6 +23,20 @@ func printLogEntry(ts time.Time, lbls string, line string) {
)
}

// print a log entry as json line
func printLogEntryJSONL(ts time.Time, lbls labels.Labels, line string) {
entry := map[string]interface{}{
"timestamp": ts,
"labels": lbls,
"line": line,
}
out, err := json.Marshal(entry)
if err != nil {
log.Fatalf("error marshalling entry: %s", err)
}
fmt.Println(string(out))
}

// add some padding after labels
func padLabel(ls labels.Labels, maxLabelsLen int) string {
labels := ls.String()
Expand Down