forked from akozlenkov/kafka-consumer-lag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (137 loc) · 3.54 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"github.com/Shopify/sarama"
"github.com/gookit/validate"
"github.com/jawher/mow.cli"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"time"
)
var (
results []Result
kafkaClient sarama.Client
)
type Result struct {
Cluster string
Group string
Topic string
Timestamp time.Time
Lag int64
}
type Config struct {
Cluster []struct{
Name string `yaml:"name"`
Cluster string `yaml:"cluster"`
Broker []string `yaml:"brokers",validate:"required|minLen:1"`
Auth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"auth"`
Check []struct{
Group string `yaml:"group",validate:"required"`
Topic []string `yaml:"topics",validate:"required|minLen:1"`
} `yaml:"checks",validate:"required|minLen:1"`
} `yaml:"clusters"`
}
func main() {
app := cli.App("kafka-consumer-lag", "Show kafka lag")
app.Spec = "--config"
var (
config = app.StringOpt("config", "kafka-consumer-lag.yml", "Path to kafka-consumer-lag config")
)
app.Action = func() {
if !fileExists(*config) {
exitWitError(fmt.Errorf("Config %s not found", *config))
}
var conf Config
file, err := ioutil.ReadFile(*config)
if err != nil {
exitWitError(err)
}
if err = yaml.Unmarshal(file, &conf); err != nil {
exitWitError(err)
}
v := validate.Struct(conf)
if !v.Validate() {
exitWitError(v.Errors)
}
results = make([]Result, 0)
for _, cluster := range conf.Cluster {
authConfig := sarama.NewConfig()
if len(cluster.Auth.Password) != 0 {
authConfig.Net.SASL.Mechanism = "PLAIN"
authConfig.Net.SASL.Enable = true
authConfig.Net.SASL.Handshake = true
authConfig.Net.SASL.User = cluster.Auth.Username
authConfig.Net.SASL.Password = cluster.Auth.Password
}
kafkaClient, err = sarama.NewClient(cluster.Broker, authConfig)
if err != nil {
exitWitError(err)
}
for _, c := range cluster.Check {
for _, t := range c.Topic {
processGroup(cluster.Name, c.Group, t)
}
}
}
for _, r := range results {
fmt.Printf("kafka_lag,consumer_cluster=%s,group=%s,topic=%s lag=%d %d\n", r.Cluster, r.Group, r.Topic, r.Lag, r.Timestamp.UnixNano())
}
}
app.Run(os.Args)
}
func processGroup(clusterName string, groupId string, topic string) {
partitions, err := kafkaClient.Partitions(topic)
if err != nil {
exitWitError(err)
}
offsetManager, err := sarama.NewOffsetManagerFromClient(groupId, kafkaClient)
if err != nil {
exitWitError(err)
}
var lag int64
var controlChannel = make(chan []int64)
for _, partition := range partitions {
go processPartition(topic, partition, controlChannel, offsetManager)
}
for range partitions {
response := <-controlChannel
lag += response[0]
}
results = append(results, Result{
Cluster: clusterName,
Group: groupId,
Topic: topic,
Timestamp: time.Now(),
Lag: lag,
})
}
func processPartition(topic string, partition int32, controlChannel chan []int64, offsetManager sarama.OffsetManager) {
pom, err := offsetManager.ManagePartition(topic, int32(partition))
if err != nil {
exitWitError(err)
}
consumerOffset, _ := pom.NextOffset()
offset, err := kafkaClient.GetOffset(topic, int32(partition), sarama.OffsetNewest)
if err != nil {
exitWitError(err)
}
var response = make([]int64, 0)
lag := offset - consumerOffset + 1
response = append(response, lag)
controlChannel <- response
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func exitWitError(err error) {
fmt.Println(err)
cli.Exit(1)
}