@@ -60,7 +60,7 @@ var consumerMaxWaitTime time.Duration
60
60
var consumerMaxProcessingTime time.Duration
61
61
var netMaxOpenRequests int
62
62
var offsetDuration time.Duration
63
- var kafkaStats map [ int32 ] * KafkaStats
63
+ var kafkaStats stats. Kafka
64
64
65
65
func ConfigSetup () {
66
66
inKafkaMdm := flag .NewFlagSet ("kafka-mdm-in" , flag .ExitOnError )
@@ -158,25 +158,13 @@ func ConfigProcess(instance string) {
158
158
cluster .Manager .SetPartitions (partitions )
159
159
}
160
160
161
- // initialize our offset metrics
162
- kafkaStats = make (map [int32 ]* KafkaStats )
163
- for _ , part := range partitions {
164
- ks := KafkaStats {}
165
- // metric input.kafka-mdm.partition.%d.offset is the current offset for the partition (%d) that we have consumed.
166
- stats .NewGauge64Existing (fmt .Sprintf ("input.kafka-mdm.partition.%d.offset" , part ), & ks .offset )
167
- // metric input.kafka-mdm.partition.%d.log_size is the current size of the kafka partition (%d), aka the newest available offset.
168
- stats .NewGauge64Existing (fmt .Sprintf ("input.kafka-mdm.partition.%d.log_size" , part ), & ks .logSize )
169
- // metric input.kafka-mdm.partition.%d.lag is how many messages (metrics) there are in the kafka partition (%d) that we have not yet consumed.
170
- stats .NewGauge64Existing (fmt .Sprintf ("input.kafka-mdm.partition.%d.lag" , part ), & ks .lag )
171
- kafkaStats [part ] = & ks
172
- }
173
- }
161
+ // the extra empty newlines are because metrics2docs doesn't recognize the comments properly otherwise
162
+ // metric input.kafka-mdm.partition.%d.offset is the current offset for the partition (%d) that we have consumed.
163
+
164
+ // metric input.kafka-mdm.partition.%d.log_size is the current size of the kafka partition (%d), aka the newest available offset.
174
165
175
- // KafkaStats is a set of per-partition stats to track the health of our consumer
176
- type KafkaStats struct {
177
- offset stats.Gauge64
178
- logSize stats.Gauge64
179
- lag stats.Gauge64
166
+ // metric input.kafka-mdm.partition.%d.lag is how many messages (metrics) there are in the kafka partition (%d) that we have not yet consumed.
167
+ kafkaStats = stats .NewKafka ("input.kafka-mdm" , partitions )
180
168
}
181
169
182
170
func New () * KafkaMdm {
@@ -284,9 +272,9 @@ func (k *KafkaMdm) consumePartition(topic string, partition int32, currentOffset
284
272
}
285
273
}
286
274
287
- kafkaStats .offset .Set (int (currentOffset ))
288
- kafkaStats .logSize .Set (int (newest ))
289
- kafkaStats .lag .Set (int (newest - currentOffset ))
275
+ kafkaStats .Offset .Set (int (currentOffset ))
276
+ kafkaStats .LogSize .Set (int (newest ))
277
+ kafkaStats .Lag .Set (int (newest - currentOffset ))
290
278
go k .trackStats (topic , partition )
291
279
292
280
log .Infof ("kafkamdm: consuming from %s:%d from offset %d" , topic , partition , currentOffset )
@@ -308,7 +296,7 @@ func (k *KafkaMdm) consumePartition(topic string, partition int32, currentOffset
308
296
}
309
297
log .Debugf ("kafkamdm: received message: Topic %s, Partition: %d, Offset: %d, Key: %x" , msg .Topic , msg .Partition , msg .Offset , msg .Key )
310
298
k .handleMsg (msg .Value , partition )
311
- kafkaStats .offset .Set (int (msg .Offset ))
299
+ kafkaStats .Offset .Set (int (msg .Offset ))
312
300
case <- k .shutdown :
313
301
pc .Close ()
314
302
log .Infof ("kafkamdm: consumer for %s:%d ended." , topic , partition )
@@ -359,16 +347,16 @@ func (k *KafkaMdm) trackStats(topic string, partition int32) {
359
347
ticker .Stop ()
360
348
return
361
349
case ts := <- ticker .C :
362
- currentOffset := int64 (kafkaStats .offset .Peek ())
350
+ currentOffset := int64 (kafkaStats .Offset .Peek ())
363
351
k .lagMonitor .StoreOffset (partition , currentOffset , ts )
364
352
newest , err := k .tryGetOffset (topic , partition , sarama .OffsetNewest , 1 , 0 )
365
353
if err != nil {
366
354
log .Errorf ("kafkamdm: %s" , err .Error ())
367
355
continue
368
356
}
369
- kafkaStats .logSize .Set (int (newest ))
357
+ kafkaStats .LogSize .Set (int (newest ))
370
358
lag := int (newest - currentOffset )
371
- kafkaStats .lag .Set (lag )
359
+ kafkaStats .Lag .Set (lag )
372
360
k .lagMonitor .StoreLag (partition , lag )
373
361
}
374
362
}
0 commit comments