-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
138 lines (121 loc) · 3.51 KB
/
main.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
package main
import (
"context"
"encoding/binary"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/IBM/sarama"
"github.com/eladleev/schema-registry-statistics/utils"
"github.com/fatih/color"
)
func main() {
cfg := parseFlags()
log.SetPrefix("[sr-stats] ")
log.Printf("Starting to consume from %v", cfg.topic)
version, err := sarama.ParseKafkaVersion(cfg.version)
if err != nil {
log.Panicf("Error parsing Kafka version: %v", err)
}
config := setConfig(version, cfg)
consumer := Consumer{
ready: make(chan bool),
stats: utils.ResultStats{
StatMap: map[string]int{"TOTAL": 0},
ResultStore: map[uint32][]int{},
},
config: cfg,
consumerLock: sync.RWMutex{},
}
ctx, cancel := context.WithCancel(context.Background())
client, err := sarama.NewConsumerGroup(strings.Split(cfg.bootstrapServers, ","), cfg.group, config)
if err != nil {
log.Panicf("Error creating consumer group: %v", err)
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for {
if err := client.Consume(ctx, strings.Split(cfg.topic, ","), &consumer); err != nil {
log.Panicf("Error from consumer: %v", err)
}
if ctx.Err() != nil {
return
}
consumer.ready = make(chan bool)
}
}()
<-consumer.ready
log.Println("Consumer up and running!...")
log.Println("Use SIGINT to stop consuming.")
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
log.Println("terminating: context cancelled")
case <-sigterm:
log.Println("terminating: via signal")
}
cancel()
wg.Wait()
if err = client.Close(); err != nil {
log.Panicf("Error closing client: %v", err)
}
}
// Setup is setting up a new CG session
func (consumer *Consumer) Setup(sarama.ConsumerGroupSession) error {
close(consumer.ready)
return nil
}
// Cleanup function to clean after the CG
func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
consumedMessages := consumer.stats.StatMap["TOTAL"]
log.Printf("Total messages consumed: %v\n", consumedMessages)
utils.BuildPercentileMap(consumer.stats.StatMap)
// Print results
for k, v := range utils.PercentileMap {
c := color.New(color.FgGreen)
c.Sprintf("Schema ID %v => %v%%\n", k, v)
}
// Dump stats to file
if consumer.config.store {
utils.DumpStats(consumer.stats, consumer.config.path)
}
// Build Charts
if consumer.config.chart {
utils.GenChart()
}
return nil
}
// ConsumeClaim processes Kafka messages from a given topic and partition within a consumer group.
func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for {
select {
case message := <-claim.Messages():
if len(message.Value) < 5 {
log.Printf("error encoding message offset: %v\n", message.Offset)
// append error value, using 4294967295 as a dummy value (end of uint32)
utils.CalcStat(consumer.stats, 4294967295, &consumer.consumerLock)
break
}
schemaID := binary.BigEndian.Uint32(message.Value[1:5])
utils.CalcStat(consumer.stats, schemaID, &consumer.consumerLock)
if consumer.config.store { // lock map, and build result for analysis
utils.AppendResult(consumer.stats, message.Offset, schemaID, &consumer.consumerLock)
}
consumer.consumerLock.RLock()
if consumer.stats.StatMap["TOTAL"]%100 == 0 { // I'm still alive :)
log.Printf("acked 100 messages\n")
}
consumer.consumerLock.RUnlock()
// ack
session.MarkMessage(message, "")
case <-session.Context().Done():
return nil
}
}
}