-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka output producer, send telegraf metrics to Kafka brokers
Closes #38
- Loading branch information
Showing
10 changed files
with
167 additions
and
33 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
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,91 @@ | ||
package kafka | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/influxdb/influxdb/client" | ||
"github.com/influxdb/telegraf/outputs" | ||
) | ||
|
||
type Kafka struct { | ||
// Kafka brokers to send metrics to | ||
Brokers []string | ||
// Kafka topic | ||
Topic string | ||
|
||
producer sarama.SyncProducer | ||
} | ||
|
||
var sampleConfig = ` | ||
# URLs of kafka brokers | ||
brokers = ["localhost:9092"] | ||
# Kafka topic for producer messages | ||
topic = "telegraf" | ||
` | ||
|
||
func (k *Kafka) Connect() error { | ||
producer, err := sarama.NewSyncProducer(k.Brokers, nil) | ||
if err != nil { | ||
return err | ||
} | ||
k.producer = producer | ||
return nil | ||
} | ||
|
||
func (k *Kafka) Close() error { | ||
return k.producer.Close() | ||
} | ||
|
||
func (k *Kafka) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (k *Kafka) Description() string { | ||
return "Configuration for the Kafka server to send metrics to" | ||
} | ||
|
||
func (k *Kafka) Write(bp client.BatchPoints) error { | ||
if len(bp.Points) == 0 { | ||
return nil | ||
} | ||
|
||
for _, p := range bp.Points { | ||
// Combine tags from Point and BatchPoints and grab the resulting | ||
// line-protocol output string to write to Kafka | ||
var value string | ||
if p.Raw != "" { | ||
value = p.Raw | ||
} else { | ||
for k, v := range bp.Tags { | ||
if p.Tags == nil { | ||
p.Tags = make(map[string]string, len(bp.Tags)) | ||
} | ||
p.Tags[k] = v | ||
} | ||
value = p.MarshalString() | ||
} | ||
|
||
m := &sarama.ProducerMessage{ | ||
Topic: k.Topic, | ||
Value: sarama.StringEncoder(value), | ||
} | ||
if h, ok := p.Tags["host"]; ok { | ||
m.Key = sarama.StringEncoder(h) | ||
} | ||
|
||
_, _, err := k.producer.SendMessage(m) | ||
if err != nil { | ||
return errors.New(fmt.Sprintf("FAILED to send kafka message: %s\n", | ||
err)) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
outputs.Add("kafka", func() outputs.Output { | ||
return &Kafka{} | ||
}) | ||
} |
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,28 @@ | ||
package kafka | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdb/telegraf/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConnectAndWrite(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping integration test in short mode") | ||
} | ||
|
||
brokers := []string{testutil.GetLocalHost() + ":9092"} | ||
k := &Kafka{ | ||
Brokers: brokers, | ||
Topic: "Test", | ||
} | ||
|
||
// Verify that we can connect to the Kafka broker | ||
err := k.Connect() | ||
require.NoError(t, err) | ||
|
||
// Verify that we can successfully write data to the kafka broker | ||
err = k.Write(testutil.MockBatchPoints()) | ||
require.NoError(t, err) | ||
} |
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