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 2 commits
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
4 changes: 3 additions & 1 deletion cmd/logcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func listLabelValues(name string) (*logproto.LabelResponse, error) {

func doRequest(path string, out interface{}) error {
url := *addr + path
log.Print(url)
if !*quiet {
sh0rez marked this conversation as resolved.
Show resolved Hide resolved
log.Print(url)
}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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
18 changes: 13 additions & 5 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 @@ -48,11 +49,11 @@ func doQuery() {
common = common.MatchLabels(false, *showLabelsKey...)
}

if len(common) > 0 {
if len(common) > 0 && !*quiet {
log.Println("Common labels:", color.RedString(common.String()))
}

if len(*ignoreLabelsKey) > 0 {
if len(*ignoreLabelsKey) > 0 && !*quiet {
log.Println("Ignoring labels key:", color.RedString(strings.Join(*ignoreLabelsKey, ",")))
}

Expand All @@ -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