-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoffset.go
55 lines (45 loc) · 1.37 KB
/
offset.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
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
type OffsetInfo struct {
Topic string
Partition int32
PartitionOffset int64
GroupOffset int64
}
func FetchOffsets(client sarama.Client, topic string, group string) ([]*OffsetInfo, error) {
var infos []*OffsetInfo
partitions, err := client.Partitions(topic)
if err != nil {
fmt.Printf("failed to get partitions for topic=%s, err=%v\n", topic, err)
return nil, err
}
offsetManager, err := sarama.NewOffsetManagerFromClient(group, client)
if err != nil {
fmt.Printf("failed to create offset manager for topic=%s, group=%s err=%v\n", topic, group, err)
return nil, err
}
for _, partition := range partitions {
partitionOffsetManager, err := offsetManager.ManagePartition(topic, partition)
if err != nil {
fmt.Printf("failed to get partition manager for topic=%s, partition=%d, err=%v\n", topic, partition, err)
continue
}
cgOffset, _ := partitionOffsetManager.NextOffset()
pOffset, err := client.GetOffset(topic, partition, sarama.OffsetNewest)
if err != nil {
fmt.Printf("failed to get partition offset for topic=%s, partition=%d, err=%v\n", topic, partition, err)
continue
}
info := &OffsetInfo{
Topic: topic,
Partition: partition,
PartitionOffset: pOffset,
GroupOffset: cgOffset,
}
infos = append(infos, info)
}
return infos, nil
}