Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for static and random routing keys in kafka output #4579

Merged
merged 2 commits into from Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/outputs/kafka/README.md
Expand Up @@ -50,6 +50,11 @@ This plugin writes to a [Kafka Broker](http://kafka.apache.org/07/quickstart.htm
## 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.
routing_key = "random"

## CompressionCodec represents the various compression codecs recognized by
## Kafka in messages.
## 0 : No compression
Expand Down
53 changes: 34 additions & 19 deletions plugins/outputs/kafka/kafka.go
Expand Up @@ -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"
)
Expand All @@ -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"`

Expand Down Expand Up @@ -116,6 +109,11 @@ 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.
routing_key = "random"

## CompressionCodec represents the various compression codecs recognized by
## Kafka in messages.
## 0 : No compression
Expand Down Expand Up @@ -273,6 +271,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 {
Expand All @@ -285,8 +299,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)
}
Expand Down
43 changes: 43 additions & 0 deletions plugins/outputs/kafka/kafka_test.go
Expand Up @@ -2,7 +2,9 @@ package kafka

import (
"testing"
"time"

"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -96,3 +98,44 @@ func TestValidateTopicSuffixMethod(t *testing.T) {
require.NoError(t, err, "Topic suffix method used should be valid.")
}
}

func TestRoutingKeyStatic(t *testing.T) {
k := &Kafka{
RoutingKey: "static",
}

metric, err := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
)
require.NoError(t, err)

expected := "static"
actual := k.routingKey(metric)
require.Equal(t, expected, actual)
require.NoError(t, err)
}

func TestRoutingKeyRandom(t *testing.T) {
k := &Kafka{
RoutingKey: "random",
}

metric, err := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
)
require.NoError(t, err)

actual := k.routingKey(metric)
require.Equal(t, 36, len(actual))
require.NoError(t, err)
}