forked from Telefonica/prometheus-kafka-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtopic_metadata.go
47 lines (39 loc) · 996 Bytes
/
topic_metadata.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
package main
import (
"context"
"math"
"sync"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/sirupsen/logrus"
)
var topicPartitionCount sync.Map
type metaDataFetcher interface {
GetMetadata(topic *string, allTopics bool, timeoutMs int) (*kafka.Metadata, error)
}
func syncTopicMetadata(ctx context.Context, producer metaDataFetcher) error {
if err := processMetadata(producer); err != nil {
return err
}
go func() {
select {
case <-ctx.Done():
return
case <-time.After(kafkaMetadataInterval):
if err := processMetadata(producer); err != nil {
logrus.WithError(err).Error("could not fetch topic metadata")
}
}
}()
return nil
}
func processMetadata(producer metaDataFetcher) error {
metadata, err := producer.GetMetadata(nil, true, int(math.Ceil(kafkaMetadataTimeout.Seconds())))
if err != nil {
return err
}
for name, topic := range metadata.Topics {
topicPartitionCount.Store(name, len(topic.Partitions))
}
return nil
}