/
tail.go
72 lines (54 loc) · 1.45 KB
/
tail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"log"
"strings"
"github.com/grafana/loki/pkg/querier"
"github.com/fatih/color"
)
func tailQuery() {
conn, err := liveTailQueryConn()
if err != nil {
log.Fatalf("Tailing logs failed: %+v", err)
}
tailReponse := new(querier.TailResponse)
if len(*ignoreLabelsKey) > 0 {
log.Println("Ingoring labels key:", color.RedString(strings.Join(*ignoreLabelsKey, ",")))
}
if len(*showLabelsKey) > 0 {
log.Println("Print only labels key:", color.RedString(strings.Join(*showLabelsKey, ",")))
}
for {
err := conn.ReadJSON(tailReponse)
if err != nil {
log.Println("Error reading stream:", err)
return
}
labels := ""
for _, stream := range tailReponse.Streams {
if !*noLabels {
if len(*ignoreLabelsKey) > 0 || len(*showLabelsKey) > 0 {
ls := mustParseLabels(stream.GetLabels())
if len(*showLabelsKey) > 0 {
ls = ls.MatchLabels(true, *showLabelsKey...)
}
if len(*ignoreLabelsKey) > 0 {
ls = ls.MatchLabels(false, *ignoreLabelsKey...)
}
labels = ls.String()
} else {
labels = stream.Labels
}
}
for _, entry := range stream.Entries {
lbls := mustParseLabels(labels)
Outputs[*outputMode].Print(entry.Timestamp, &lbls, entry.Line)
}
}
if len(tailReponse.DroppedEntries) != 0 {
log.Println("Server dropped following entries due to slow client")
for _, d := range tailReponse.DroppedEntries {
log.Println(d.Timestamp, d.Labels)
}
}
}
}