forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
197 lines (163 loc) · 4.37 KB
/
partition.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
package partition
import (
"crypto/tls"
"errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/kafka"
"github.com/Shopify/sarama"
)
// init registers the partition MetricSet with the central registry.
func init() {
if err := mb.Registry.AddMetricSet("kafka", "partition", New, parse.PassThruHostParser); err != nil {
panic(err)
}
}
// MetricSet type defines all fields of the partition MetricSet
type MetricSet struct {
mb.BaseMetricSet
broker *kafka.Broker
topics []string
}
const noID int32 = -1
var errFailQueryOffset = errors.New("operation failed")
var debugf = logp.MakeDebug("kafka")
// New creates a new instance of the partition MetricSet.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("BETA: The kafka partition metricset is beta")
config := defaultConfig
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
var tls *tls.Config
tlsCfg, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil, err
}
if tlsCfg != nil {
tls = tlsCfg.BuildModuleConfig("")
}
timeout := base.Module().Config().Timeout
cfg := kafka.BrokerSettings{
MatchID: true,
DialTimeout: timeout,
ReadTimeout: timeout,
ClientID: config.ClientID,
Retries: config.Retries,
Backoff: config.Backoff,
TLS: tls,
Username: config.Username,
Password: config.Password,
}
return &MetricSet{
BaseMetricSet: base,
broker: kafka.NewBroker(base.Host(), cfg),
topics: config.Topics,
}, nil
}
func (m *MetricSet) connect() (*kafka.Broker, error) {
err := m.broker.Connect()
return m.broker, err
}
// Fetch partition stats list from kafka
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
b, err := m.connect()
if err != nil {
return nil, err
}
defer b.Close()
topics, err := b.GetTopicsMetadata(m.topics...)
if err != nil {
return nil, err
}
events := []common.MapStr{}
evtBroker := common.MapStr{
"id": b.ID(),
"address": b.Addr(),
}
for _, topic := range topics {
debugf("fetch events for topic: ", topic.Name)
evtTopic := common.MapStr{
"name": topic.Name,
}
if topic.Err != 0 {
evtTopic["error"] = common.MapStr{
"code": topic.Err,
}
}
for _, partition := range topic.Partitions {
// partition offsets can be queried from leader only
if b.ID() != partition.Leader {
debugf("broker is not leader (broker=%v, leader=%v)", b.ID(), partition.Leader)
continue
}
// collect offsets for all replicas
for _, id := range partition.Replicas {
// Get oldest and newest available offsets
offOldest, offNewest, offOK, err := queryOffsetRange(b, id, topic.Name, partition.ID)
if !offOK {
if err == nil {
err = errFailQueryOffset
}
logp.Err("Failed to query kafka partition (%v:%v) offsets: %v",
topic.Name, partition.ID, err)
continue
}
partitionEvent := common.MapStr{
"id": partition.ID,
"leader": partition.Leader,
"replica": id,
"insync_replica": hasID(id, partition.Isr),
}
if partition.Err != 0 {
partitionEvent["error"] = common.MapStr{
"code": partition.Err,
}
}
// create event
event := common.MapStr{
"topic": evtTopic,
"broker": evtBroker,
"partition": partitionEvent,
"offset": common.MapStr{
"newest": offNewest,
"oldest": offOldest,
},
}
events = append(events, event)
}
}
}
return events, nil
}
// queryOffsetRange queries the broker for the oldest and the newest offsets in
// a kafka topics partition for a given replica.
func queryOffsetRange(
b *kafka.Broker,
replicaID int32,
topic string,
partition int32,
) (int64, int64, bool, error) {
oldest, err := b.PartitionOffset(replicaID, topic, partition, sarama.OffsetOldest)
if err != nil {
return -1, -1, false, err
}
newest, err := b.PartitionOffset(replicaID, topic, partition, sarama.OffsetNewest)
if err != nil {
return -1, -1, false, err
}
okOld := oldest != -1
okNew := newest != -1
return oldest, newest, okOld && okNew, nil
}
func hasID(id int32, lst []int32) bool {
for _, other := range lst {
if id == other {
return true
}
}
return false
}