Add support for static and random routing keys in kafka output (#4579)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
tlsint "github.com/influxdata/telegraf/internal/tls"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
"github.com/influxdata/telegraf/plugins/serializers"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"github.com/Shopify/sarama"
|
||||
)
|
||||
@@ -22,24 +23,16 @@ var ValidTopicSuffixMethods = []string{
|
||||
|
||||
type (
|
||||
Kafka struct {
|
||||
// Kafka brokers to send metrics to
|
||||
Brokers []string
|
||||
// Kafka topic
|
||||
Topic string
|
||||
// Kafka client id
|
||||
ClientID string `toml:"client_id"`
|
||||
// Kafka topic suffix option
|
||||
TopicSuffix TopicSuffix `toml:"topic_suffix"`
|
||||
// Routing Key Tag
|
||||
RoutingTag string `toml:"routing_tag"`
|
||||
// Compression Codec Tag
|
||||
Brokers []string
|
||||
Topic string
|
||||
ClientID string `toml:"client_id"`
|
||||
TopicSuffix TopicSuffix `toml:"topic_suffix"`
|
||||
RoutingTag string `toml:"routing_tag"`
|
||||
RoutingKey string `toml:"routing_key"`
|
||||
CompressionCodec int
|
||||
// RequiredAcks Tag
|
||||
RequiredAcks int
|
||||
// MaxRetry Tag
|
||||
MaxRetry int
|
||||
// Max Message Bytes
|
||||
MaxMessageBytes int `toml:"max_message_bytes"`
|
||||
RequiredAcks int
|
||||
MaxRetry int
|
||||
MaxMessageBytes int `toml:"max_message_bytes"`
|
||||
|
||||
Version string `toml:"version"`
|
||||
|
||||
@@ -116,6 +109,13 @@ var sampleConfig = `
|
||||
## ie, if this tag exists, its value will be used as the routing key
|
||||
routing_tag = "host"
|
||||
|
||||
## Static routing key. Used when no routing_tag is set or as a fallback
|
||||
## when the tag specified in routing tag is not found. If set to "random",
|
||||
## a random value will be generated for each message.
|
||||
## ex: routing_key = "random"
|
||||
## routing_key = "telegraf"
|
||||
# routing_key = ""
|
||||
|
||||
## CompressionCodec represents the various compression codecs recognized by
|
||||
## Kafka in messages.
|
||||
## 0 : No compression
|
||||
@@ -273,6 +273,22 @@ func (k *Kafka) Description() string {
|
||||
return "Configuration for the Kafka server to send metrics to"
|
||||
}
|
||||
|
||||
func (k *Kafka) routingKey(metric telegraf.Metric) string {
|
||||
if k.RoutingTag != "" {
|
||||
key, ok := metric.GetTag(k.RoutingTag)
|
||||
if ok {
|
||||
return key
|
||||
}
|
||||
}
|
||||
|
||||
if k.RoutingKey == "random" {
|
||||
u := uuid.NewV4()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
return k.RoutingKey
|
||||
}
|
||||
|
||||
func (k *Kafka) Write(metrics []telegraf.Metric) error {
|
||||
msgs := make([]*sarama.ProducerMessage, 0, len(metrics))
|
||||
for _, metric := range metrics {
|
||||
@@ -285,8 +301,9 @@ func (k *Kafka) Write(metrics []telegraf.Metric) error {
|
||||
Topic: k.GetTopicName(metric),
|
||||
Value: sarama.ByteEncoder(buf),
|
||||
}
|
||||
if h, ok := metric.GetTag(k.RoutingTag); ok {
|
||||
m.Key = sarama.StringEncoder(h)
|
||||
key := k.routingKey(metric)
|
||||
if key != "" {
|
||||
m.Key = sarama.StringEncoder(key)
|
||||
}
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user