forked from TencentBlueKing/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
211 lines (177 loc) · 4.12 KB
/
query.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
package consumergroup
import (
"sync"
"github.com/Shopify/sarama"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/module/kafka"
)
type client interface {
ListGroups() ([]string, error)
DescribeGroups(group []string) (map[string]kafka.GroupDescription, error)
FetchGroupOffsets(group string) (*sarama.OffsetFetchResponse, error)
}
func fetchGroupInfo(
emit func(common.MapStr),
b client,
groupsFilter, topicsFilter func(string) bool,
) error {
type result struct {
err error
group string
off *sarama.OffsetFetchResponse
}
groups, err := listGroups(b, groupsFilter)
if err != nil {
logp.Err("failed to list known kafka groups: %v", err)
return err
}
if len(groups) == 0 {
return nil
}
debugf("known consumer groups: ", groups)
wg := sync.WaitGroup{}
results := make(chan result, len(groups))
for _, group := range groups {
group := group
wg.Add(1)
go func() {
defer wg.Done()
resp, err := fetchGroupOffset(b, group, topicsFilter)
if err != nil {
logp.Err("failed to fetch '%v' group offset: %v", group, err)
}
results <- result{err, group, resp}
}()
}
go func() {
wg.Wait()
close(results)
}()
assignments, err := fetchGroupAssignments(b, groups)
if err != nil {
// wait for workers to stop and drop results
for range results {
}
return err
}
for ret := range results {
if err := ret.err; err != nil {
// wait for workers to stop and drop results
for range results {
}
return err
}
asgnGroup := assignments[ret.group]
for topic, partitions := range ret.off.Blocks {
var asgnTopic map[int32]groupAssignment
if asgnGroup != nil {
asgnTopic = asgnGroup[topic]
}
for partition, info := range partitions {
event := common.MapStr{
"id": ret.group,
"topic": topic,
"partition": partition,
"offset": info.Offset,
"meta": info.Metadata,
"error": common.MapStr{
"code": info.Err,
},
}
if asgnTopic != nil {
if assignment, found := asgnTopic[partition]; found {
event["client"] = common.MapStr{
"id": assignment.clientID,
"host": assignment.clientHost,
"member_id": assignment.memberID,
}
}
}
emit(event)
}
}
}
return nil
}
func listGroups(b client, filter func(string) bool) ([]string, error) {
groups, err := b.ListGroups()
if err != nil {
return nil, err
}
if filter == nil {
return groups, nil
}
filtered := groups[:0]
for _, name := range groups {
if filter(name) {
filtered = append(filtered, name)
}
}
return filtered, nil
}
func fetchGroupAssignments(
b client,
groupIDs []string,
) (map[string]map[string]map[int32]groupAssignment, error) {
resp, err := b.DescribeGroups(groupIDs)
if err != nil {
return nil, err
}
groups := map[string]map[string]map[int32]groupAssignment{}
groupLoop:
for groupID, info := range resp {
G := groups[groupID]
if G == nil {
G = map[string]map[int32]groupAssignment{}
groups[groupID] = G
}
for memberID, memberDescr := range info.Members {
if memberDescr.Err != nil {
// group doesn't seem to use standardized member assignment encoding
// => try next group
continue groupLoop
}
clientID := memberDescr.ClientID
clientHost := memberDescr.ClientHost
if len(clientHost) > 1 && clientHost[0] == '/' {
clientHost = clientHost[1:]
}
meta := groupAssignment{
memberID: memberID,
clientID: clientID,
clientHost: clientHost,
}
for topic, partitions := range memberDescr.Topics {
T := G[topic]
if T == nil {
T = map[int32]groupAssignment{}
G[topic] = T
}
for _, partition := range partitions {
T[partition] = meta
}
}
}
}
return groups, nil
}
func fetchGroupOffset(
b client,
group string,
topics func(string) bool,
) (*sarama.OffsetFetchResponse, error) {
resp, err := b.FetchGroupOffsets(group)
if err != nil {
return nil, err
}
if topics == nil {
return resp, err
}
for topic := range resp.Blocks {
if !topics(topic) {
delete(resp.Blocks, topic)
}
}
return resp, nil
}