/
display.go
149 lines (115 loc) · 3.58 KB
/
display.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package kafka
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/batchcorp/plumber-schemas/build/go/protos/opts"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/batchcorp/plumber-schemas/build/go/protos/records"
"github.com/batchcorp/plumber/printer"
"github.com/batchcorp/plumber/types"
)
func (k *Kafka) DisplayMessage(cliOpts *opts.CLIOptions, msg *records.ReadRecord) error {
if err := validateReadRecord(msg); err != nil {
return errors.Wrap(err, "unable to validate read record")
}
var err error
// We either display consumer group lag stats or display the parsed record
if _, ok := msg.Metadata["lag"]; ok {
err = k.displayLag(msg)
} else {
err = k.displayRecord(cliOpts, msg)
}
return err
}
func validateReadRecord(msg *records.ReadRecord) error {
if msg == nil {
return errors.New("msg cannot be nil")
}
return nil
}
func (k *Kafka) DisplayError(msg *records.ErrorRecord) error {
printer.DefaultDisplayError(msg)
return nil
}
func (k *Kafka) displayRecord(cliOpts *opts.CLIOptions, msg *records.ReadRecord) error {
record := msg.GetKafka()
if record == nil {
return errors.New("BUG: record in message is nil")
}
key := aurora.Gray(12, "NONE").String()
if len(record.Key) != 0 {
key = string(record.Key)
}
properties := [][]string{
{"Key", key},
{"topic", record.Topic},
{"Offset", fmt.Sprintf("%d", record.Offset)},
{"Partition", fmt.Sprintf("%d", record.Partition)},
}
// Display offset info if it exists
if lastOffset, ok := msg.Metadata["last_offset"]; ok {
lastOffsetInt, err := strconv.ParseInt(lastOffset, 10, 64)
if err != nil {
k.log.Errorf("unable to parse last_offset '%s': %s", lastOffset, err)
} else {
lastOffStr := strconv.FormatUint(uint64(lastOffsetInt), 10)
properties = append(properties, []string{"LastOffset", lastOffStr})
}
}
properties = append(properties, generateHeaders(record.Headers)...)
receivedAt := time.Unix(msg.ReceivedAtUnixTsUtc, 0)
printer.PrintTable(cliOpts, msg.Num, receivedAt, msg.Payload, properties)
return nil
}
func generateHeaders(headers []*records.KafkaHeader) [][]string {
if len(headers) == 0 {
return [][]string{
{"Header(s)", aurora.Gray(12, "NONE").String()},
}
}
result := make([][]string, len(headers))
result[0] = []string{
"Header(s)", fmt.Sprintf("KEY: %s / VALUE: %s", headers[0].Key, headers[0].Value),
}
for i := 1; i != len(headers); i++ {
result[i] = []string{
"", fmt.Sprintf("KEY: %s / VALUE: %s", headers[i].Key, headers[i].Value),
}
}
return result
}
func (k *Kafka) displayLag(msg *records.ReadRecord) error {
if _, ok := msg.Metadata["lag"]; !ok {
return errors.New("record doesn't appear to be a lag record (missing metadata)")
}
topicStats := make([]*types.TopicStats, 0)
if err := json.Unmarshal(msg.XRaw, &topicStats); err != nil {
return errors.Wrap(err, "unable to unmarshal raw lag data to topic stats")
}
properties := make([][]string, 0)
for _, v := range topicStats {
if len(properties) != 0 {
// Add a separator between diff topics
properties = append(properties, []string{
"---------------------------------------------------------------",
})
}
properties = append(properties, []string{
"Topic", v.TopicName,
}, []string{
"Group ID", v.GroupID,
})
for partitionID, pstats := range v.Partitions {
properties = append(properties, []string{
"Partition ID", fmt.Sprint(partitionID),
}, []string{
" └─ Messages Behind", fmt.Sprint(pstats.MessagesBehind),
})
}
}
printer.PrintTableProperties(properties, time.Unix(msg.ReceivedAtUnixTsUtc, 0))
return nil
}