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

Added async producer mode for kafka #4150

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/outputs/kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This plugin writes to a [Kafka Broker](http://kafka.apache.org/07/quickstart.htm
## Kafka topic for producer messages
topic = "telegraf"

## Producer mode, either sync or async
## With async producer required_acks setting doesn't matter
# mode = "async"

## Optional topic suffix configuration.
## If the section is omitted, no suffix is used.
## Following topic suffix methods are supported:
Expand Down
47 changes: 37 additions & 10 deletions plugins/outputs/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type (
Brokers []string
// Kafka topic
Topic string
// Producer mode
Mode string
// Kafka topic suffix option
TopicSuffix TopicSuffix `toml:"topic_suffix"`
// Routing Key Tag
Expand Down Expand Up @@ -52,7 +54,8 @@ type (
SASLPassword string `toml:"sasl_password"`

tlsConfig tls.Config
producer sarama.SyncProducer
syncProducer sarama.SyncProducer
asyncProducer sarama.AsyncProducer

serializer serializers.Serializer
}
Expand All @@ -69,6 +72,10 @@ var sampleConfig = `
## Kafka topic for producer messages
topic = "telegraf"

## Producer mode, either sync or async
## With async producer required_acks setting doesn't matter
# mode = "async"

## Optional topic suffix configuration.
## If the section is omitted, no suffix is used.
## Following topic suffix methods are supported:
Expand Down Expand Up @@ -214,16 +221,32 @@ func (k *Kafka) Connect() error {
config.Net.SASL.Enable = true
}

producer, err := sarama.NewSyncProducer(k.Brokers, config)
if err != nil {
return err
var syncProducer sarama.SyncProducer
var asyncProducer sarama.AsyncProducer
err = nil
if k.Mode == "sync" {
config.Producer.Return.Successes = true
syncProducer, err = sarama.NewSyncProducer(k.Brokers, config)
k.syncProducer = syncProducer
if err != nil {
return err
}
} else if k.Mode == "async" {
config.Producer.Return.Successes = false
asyncProducer, err = sarama.NewAsyncProducer(k.Brokers, config)
k.asyncProducer = asyncProducer
}
k.producer = producer

return nil
}

func (k *Kafka) Close() error {
return k.producer.Close()
if k.Mode == "sync" {
return k.syncProducer.Close()
} else if k.Mode == "async" {
return k.asyncProducer.Close()
}
return nil
}

func (k *Kafka) SampleConfig() string {
Expand Down Expand Up @@ -255,10 +278,13 @@ func (k *Kafka) Write(metrics []telegraf.Metric) error {
m.Key = sarama.StringEncoder(h)
}

_, _, err = k.producer.SendMessage(m)

if err != nil {
return fmt.Errorf("FAILED to send kafka message: %s\n", err)
if k.Mode == "sync" {
_, _, err = k.syncProducer.SendMessage(m)
if err != nil {
return fmt.Errorf("FAILED to send kafka message: %s\n", err)
}
} else if k.Mode == "async" {
k.asyncProducer.Input() <- m
}
}
return nil
Expand All @@ -269,6 +295,7 @@ func init() {
return &Kafka{
MaxRetry: 3,
RequiredAcks: -1,
Mode: "async",
}
})
}
1 change: 1 addition & 0 deletions plugins/outputs/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestConnectAndWrite(t *testing.T) {
k := &Kafka{
Brokers: brokers,
Topic: "Test",
Mode: "async",
serializer: s,
}

Expand Down