forked from Telefonica/prometheus-kafka-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes Telefonica#75: allow labels to be hashed into topic partitions
- Loading branch information
1 parent
6a1dbf5
commit f52e2d2
Showing
5 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |