forked from twmb/franz-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (54 loc) · 1.53 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"
"github.com/twmb/franz-go/pkg/kgo"
)
var (
debugPort = flag.Int("debug-port", 9999, "localhost port that metrics can be curled from (path /metrics)")
seedBrokers = flag.String("brokers", "localhost:9092", "comma delimited list of seed brokers")
topic = flag.String("topic", "test", "topic to consume for metric incrementing")
produce = flag.Bool("produce", false, "if true, rather than consume, produce to the topic once per second (value \"foo\")")
)
func main() {
flag.Parse()
metrics := NewMetrics("kgo")
opts := []kgo.Opt{
kgo.SeedBrokers(strings.Split(*seedBrokers, ",")...),
kgo.WithHooks(metrics),
kgo.DefaultProduceTopic(*topic),
kgo.WithLogger(kgo.BasicLogger(os.Stderr, kgo.LogLevelInfo, func() string {
return time.Now().Format("[2006-01-02 15:04:05.999] ")
})),
}
if !*produce {
opts = append(opts, kgo.ConsumeTopics(*topic))
}
cl, err := kgo.NewClient(opts...)
if err != nil {
panic(fmt.Sprintf("unable to create client: %v", err))
}
defer cl.Close()
go func() {
http.Handle("/metrics", metrics.Handler())
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", *debugPort), nil))
}()
if *produce {
for range time.Tick(time.Second) {
if err := cl.ProduceSync(context.Background(), kgo.StringRecord("foo")).FirstErr(); err != nil {
panic(fmt.Sprintf("unable to produce: %v", err))
}
}
} else {
for {
cl.PollFetches(context.Background()) // busy work...
}
}
}