forked from mathpl/kafka_httpcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
362 lines (315 loc) · 9.48 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"golang.org/x/net/context"
"github.com/Shopify/sarama"
cluster "github.com/bsm/sarama-cluster"
"github.com/codegangsta/cli"
"github.com/mathpl/go-tsdmetrics"
"github.com/rcrowley/go-metrics"
)
var version = "0.4"
var maxLag = 0
func generateConsumerLag(r tsdmetrics.TaggedRegistry) {
valsSent := make(map[string]int64, 0)
valsCommitted := make(map[string]int64, 0)
valsHWM := make(map[string]int64, 0)
fn := func(n string, tm tsdmetrics.TaggedMetric) {
switch n {
case "kafka_httpcat.consumer.sent":
if m, ok := tm.GetMetric().(metrics.Gauge); !ok {
log.Printf("Unexpect metric type.")
} else {
valsSent[tm.GetTags()["partition"]] = m.Value()
}
case "kafka_httpcat.consumer.committed":
if m, ok := tm.GetMetric().(metrics.Gauge); !ok {
log.Printf("Unexpect metric type.")
} else {
valsCommitted[tm.GetTags()["partition"]] = m.Value()
}
case "kafka_httpcat.consumer.high_water_mark":
if m, ok := tm.GetMetric().(metrics.Gauge); !ok {
log.Printf("Unexpect metric type.")
} else {
valsHWM[tm.GetTags()["partition"]] = m.Value()
}
}
}
r.Each(fn)
var maxLag int64
for partition, hwmOffset := range valsHWM {
if sentOffset, ok := valsSent[partition]; ok {
i := r.GetOrRegister("kafka_httpcat.consumer.sent.offset_lag", tsdmetrics.Tags{"partition": partition}, metrics.NewGauge())
if m, ok := i.(metrics.Gauge); ok {
offsetLag := hwmOffset - sentOffset
m.Update(offsetLag)
if offsetLag > maxLag {
maxLag = offsetLag
}
} else {
log.Print("Unexpected metric type")
}
}
if committedOffset, ok := valsCommitted[partition]; ok {
i := r.GetOrRegister("kafka_httpcat.consumer.committed.offset_lag", tsdmetrics.Tags{"partition": partition}, metrics.NewGauge())
if m, ok := i.(metrics.Gauge); ok {
offsetLag := hwmOffset - committedOffset
m.Update(offsetLag)
} else {
log.Print("Unexpected metric type")
}
}
}
}
func commaDelimitedToStringList(s string) []string {
list := strings.Split(s, ",")
cleanList := make([]string, 0)
for _, v := range list {
c := strings.TrimSpace(v)
if c != "" {
cleanList = append(cleanList, c)
}
}
return cleanList
}
func stringListToHeaderMap(l []string) (map[string][]string, error) {
headers := make(map[string][]string, len(l))
for _, h := range l {
idx := strings.Index(h, ":")
if idx == -1 {
return nil, fmt.Errorf("Unable to parse header %s", h)
}
headers[h[0:idx]] = append(headers[h[0:idx]], strings.TrimSpace(h[idx+1:]))
}
return headers, nil
}
func commaDelimitedToIntList(s string) ([]int, error) {
list := strings.Split(s, ",")
intList := make([]int, len(list))
for i, v := range list {
var err error
intList[i], err = strconv.Atoi(strings.TrimSpace(v))
if err != nil {
return nil, err
}
}
return intList, nil
}
func main() {
app := cli.NewApp()
app.Name = "kafka_httpcat"
app.Usage = "Forward kafka data to http endpoint"
app.Version = version
host, err := os.Hostname()
if err != nil {
log.Printf("Unable to get hostname: %s", err)
os.Exit(1)
}
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "verbosity",
Value: 2,
Usage: "verbosity (0-5)",
EnvVar: "VERBOSITY",
},
cli.StringFlag{
Name: "target-host-list, t",
Usage: "Comma delimited target hosts",
EnvVar: "TARGET_HOST_LIST",
},
cli.StringFlag{
Name: "target-path, p",
Usage: "HTTP path",
EnvVar: "TARGET_PATH",
},
cli.StringFlag{
Name: "headers, H",
Usage: "Comma delimited headers",
EnvVar: "HTTP_HEADERS",
},
cli.StringFlag{
Name: "method, m",
Value: "POST",
Usage: "HTTP method",
EnvVar: "HTTP_METHOD",
},
cli.StringFlag{
Name: "expected-statuses, e",
Value: "200",
Usage: "Comma delimited list of expected HTTP status",
EnvVar: "HTTP_EXPECTED_STATUSES",
},
cli.IntFlag{
Name: "discard-ratio",
Value: 0,
Usage: "Ratio of discarded messages. 0 => none, 1 => 1 in 2, 2 => 2 in 3",
EnvVar: "DISCARD_RATIO",
},
cli.StringFlag{
Name: "kafka-broker-list, b",
Usage: "Comma delimited kafka broker list.",
EnvVar: "KAFKA_BROKER_LIST",
},
cli.StringFlag{
Name: "kafka-topic, T",
Usage: "Kafka topic.",
EnvVar: "KAFKA_TOPIC",
},
cli.StringFlag{
Name: "kafka-consumer-id, i",
Value: fmt.Sprintf("%s-%d", host, os.Getpid()),
Usage: "Kafka consumer id.",
EnvVar: "KAFKA_CONSUMER_ID",
},
cli.StringFlag{
Name: "kafka-consumer-group, g",
Usage: "Kafka consumer group.",
EnvVar: "KAFKA_CONSUMER_GROUP",
},
cli.StringFlag{
Name: "kafka-start-offset, o",
Value: "newest",
Usage: "Kafka offset to start with (newest or oldest)",
EnvVar: "KAFKA_START_OFFSET",
},
cli.IntFlag{
Name: "max-offset-lag",
Value: 0,
Usage: "Max offset lag before aborting",
EnvVar: "MAX_OFFSET_LAG",
},
cli.IntFlag{
Name: "kafka-commit-batch, c",
Value: 1000,
Usage: "Commit consumed messages every X messages.",
EnvVar: "KAFKA_COMMIT_BATCH",
},
cli.StringFlag{
Name: "metrics-report-url, r",
Usage: "Where to send OpenTSDB metrics.",
EnvVar: "METRICS_REPORT_URL",
},
cli.StringFlag{
Name: "metrics-tags",
Usage: "Comma delimited list of default tags",
EnvVar: "METRICS_TAGS",
},
}
app.Action = func(c *cli.Context) error {
expectedStatuses, err := commaDelimitedToIntList(c.String("expected-statuses"))
if err != nil {
return fmt.Errorf("Expected http status must be an integer: %s", err)
}
httpHeaderList := commaDelimitedToStringList(c.String("headers"))
httpHeaders, err := stringListToHeaderMap(httpHeaderList)
if err != nil {
return fmt.Errorf("Unable to parse headers: %s", err)
}
targetHosts := commaDelimitedToStringList(c.String("target-host-list"))
defaultTags, err := tsdmetrics.TagsFromString(c.String("metrics-tags"))
if err != nil {
return err
}
defaultTags = defaultTags.AddTags(tsdmetrics.Tags{"topic": c.String("kafka-topic"), "consumergroup": c.String("kafka-consumer-group")})
rootRegistry := tsdmetrics.NewSegmentedTaggedRegistry("", defaultTags, nil)
tsdmetrics.RegisterTaggedRuntimeMemStats(rootRegistry)
metricsRegistry := tsdmetrics.NewSegmentedTaggedRegistry("kafka_httpcat", nil, rootRegistry)
metricsTsdb := tsdmetrics.TaggedOpenTSDB{Addr: c.String("metrics-report-url"), Registry: rootRegistry, FlushInterval: 15 * time.Second, DurationUnit: time.Millisecond, Format: tsdmetrics.Json}
log.Printf("Connecting to: %s", c.String("kafka-broker-list"))
discard := c.Int("discard-ratio")
if discard != 0 {
log.Printf("Disarding all but every %d messages", discard)
}
// Init config
config := cluster.NewConfig()
config.Consumer.Return.Errors = true
config.Group.Return.Notifications = true
switch c.String("kafka-start-offset") {
case "oldest":
config.Consumer.Offsets.Initial = sarama.OffsetOldest
case "newest":
config.Consumer.Offsets.Initial = sarama.OffsetNewest
// Only when starting with newest
maxLag = c.Int("max-lag")
default:
return fmt.Errorf("offset should be `oldest` or `newest`")
}
brokerList := strings.Split(c.String("kafka-broker-list"), ",")
topicList := strings.Split(c.String("kafka-topic"), ",")
// Init consumer, consume errors & messages
consumer, err := cluster.NewConsumer(brokerList, c.String("kafka-consumer-group"), topicList, config)
if err != nil {
log.Fatal(err)
}
wait := make(chan os.Signal, 1)
signal.Notify(wait, syscall.SIGINT, syscall.SIGTERM)
go func() {
for err := range consumer.Errors() {
fmt.Printf("Error: %s\n", err.Error())
// Abort all
wait <- syscall.SIGTERM
}
}()
go func() {
for note := range consumer.Notifications() {
fmt.Printf("Rebalanced: %+v\n", note)
}
}()
batchSize := int64(c.Int("kafka-commit-batch"))
hwmUpdate := func(r tsdmetrics.TaggedRegistry) {
hwms := consumer.HighWaterMarks()
for _, hwm := range hwms {
for partition, offset := range hwm {
tags := tsdmetrics.Tags{"partition": fmt.Sprintf("%d", partition)}
m := metricsRegistry.GetOrRegister("consumer.high_water_mark", tags, metrics.NewGauge())
m.(metrics.Gauge).Update(offset)
}
}
}
go func() {
sender := NewHTTPSender(targetHosts, c.String("target-path"), c.String("method"), httpHeaders, expectedStatuses)
discardCounter := 0
for msg := range consumer.Messages() {
discardCounter++
if discard != 0 && discardCounter%discard != 0 {
continue
}
if err := sender.RRSend(msg.Value); err != nil {
log.Printf("Error send data: %s\n", err)
}
tags := tsdmetrics.Tags{"partition": fmt.Sprintf("%d", msg.Partition)}
s := metricsRegistry.GetOrRegister("consumer.sent", tags, metrics.NewGauge())
s.(metrics.Gauge).Update(msg.Offset)
if msg.Offset%batchSize == 0 {
consumer.MarkOffset(msg, "")
c := metricsRegistry.GetOrRegister("consumer.committed", tags, metrics.NewGauge())
c.(metrics.Gauge).Update(msg.Offset)
log.Printf("Commited offset partition:%d offset:%d\n", msg.Partition, msg.Offset)
}
}
}()
collectFn := tsdmetrics.RuntimeCaptureFn
collectFn = append(collectFn, hwmUpdate)
collectFn = append(collectFn, generateConsumerLag)
go metricsTsdb.RunWithPreprocessing(context.Background(), collectFn)
<-wait
consumer.CommitOffsets()
if err := consumer.Close(); err != nil {
fmt.Printf("Failed to close consumer: %s\n", err)
}
return nil
}
err = app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}