2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
2018-11-05 21:34:28 +00:00
|
|
|
"context"
|
2017-04-11 19:05:39 +00:00
|
|
|
"strings"
|
2015-06-27 03:56:14 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-06-27 03:56:14 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
const (
|
2016-11-28 18:19:35 +00:00
|
|
|
testMsg = "cpu_load_short,host=server01 value=23422.0 1422568543702900257\n"
|
2016-02-06 00:36:35 +00:00
|
|
|
testMsgGraphite = "cpu.load.short.graphite 23422 1454780029"
|
|
|
|
testMsgJSON = "{\"a\": 5, \"b\": {\"c\": 6}}\n"
|
2016-11-28 18:19:35 +00:00
|
|
|
invalidMsg = "cpu_load_short,host=server01 1422568543702900257\n"
|
2015-11-16 20:12:45 +00:00
|
|
|
)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
type TestConsumer struct {
|
|
|
|
errors chan error
|
|
|
|
messages chan *sarama.ConsumerMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestConsumer) Errors() <-chan error {
|
|
|
|
return c.errors
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestConsumer) Messages() <-chan *sarama.ConsumerMessage {
|
|
|
|
return c.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestConsumer) MarkOffset(msg *sarama.ConsumerMessage, metadata string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestConsumer) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestConsumer) Inject(msg *sarama.ConsumerMessage) {
|
|
|
|
c.messages <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestKafka() (*Kafka, *TestConsumer) {
|
|
|
|
consumer := &TestConsumer{
|
|
|
|
errors: make(chan error),
|
|
|
|
messages: make(chan *sarama.ConsumerMessage, 1000),
|
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
k := Kafka{
|
2018-11-05 21:34:28 +00:00
|
|
|
cluster: consumer,
|
|
|
|
ConsumerGroup: "test",
|
|
|
|
Topics: []string{"telegraf"},
|
|
|
|
Brokers: []string{"localhost:9092"},
|
|
|
|
Offset: "oldest",
|
|
|
|
MaxUndeliveredMessages: defaultMaxUndeliveredMessages,
|
|
|
|
doNotCommitMsgs: true,
|
|
|
|
messages: make(map[telegraf.TrackingID]*sarama.ConsumerMessage),
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
return &k, consumer
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Test that the parser parses kafka messages into points
|
|
|
|
func TestRunParser(t *testing.T) {
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(testMsg))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
assert.Equal(t, acc.NFields(), 1)
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Test that the parser ignores invalid messages
|
|
|
|
func TestRunParserInvalidMsg(t *testing.T) {
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(invalidMsg))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.WaitError(1)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
assert.Equal(t, acc.NFields(), 0)
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 19:05:39 +00:00
|
|
|
// Test that overlong messages are dropped
|
|
|
|
func TestDropOverlongMsg(t *testing.T) {
|
|
|
|
const maxMessageLen = 64 * 1024
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2017-04-11 19:05:39 +00:00
|
|
|
k.MaxMessageLen = maxMessageLen
|
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2017-04-11 19:05:39 +00:00
|
|
|
overlongMsg := strings.Repeat("v", maxMessageLen+1)
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(overlongMsg))
|
2017-04-11 19:05:39 +00:00
|
|
|
acc.WaitError(1)
|
|
|
|
|
|
|
|
assert.Equal(t, acc.NFields(), 0)
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Test that the parser parses kafka messages into points
|
|
|
|
func TestRunParserAndGather(t *testing.T) {
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2015-11-16 20:12:45 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(testMsg))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(k.Gather)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
assert.Equal(t, acc.NFields(), 1)
|
2016-01-06 23:55:28 +00:00
|
|
|
acc.AssertContainsFields(t, "cpu_load_short",
|
|
|
|
map[string]interface{}{"value": float64(23422)})
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
// Test that the parser parses kafka messages into points
|
|
|
|
func TestRunParserAndGatherGraphite(t *testing.T) {
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2016-02-06 00:36:35 +00:00
|
|
|
|
|
|
|
k.parser, _ = parsers.NewGraphiteParser("_", []string{}, nil)
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(testMsgGraphite))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(k.Gather)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
assert.Equal(t, acc.NFields(), 1)
|
2016-02-06 00:36:35 +00:00
|
|
|
acc.AssertContainsFields(t, "cpu_load_short_graphite",
|
|
|
|
map[string]interface{}{"value": float64(23422)})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the parser parses kafka messages into points
|
|
|
|
func TestRunParserAndGatherJSON(t *testing.T) {
|
2018-11-05 21:34:28 +00:00
|
|
|
k, consumer := newTestKafka()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx := context.Background()
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2018-08-23 02:26:48 +00:00
|
|
|
k.parser, _ = parsers.NewParser(&parsers.Config{
|
|
|
|
DataFormat: "json",
|
|
|
|
MetricName: "kafka_json_test",
|
|
|
|
})
|
2018-11-05 21:34:28 +00:00
|
|
|
go k.receiver(ctx, &acc)
|
|
|
|
consumer.Inject(saramaMsg(testMsgJSON))
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.GatherError(k.Gather)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
assert.Equal(t, acc.NFields(), 2)
|
2016-02-06 00:36:35 +00:00
|
|
|
acc.AssertContainsFields(t, "kafka_json_test",
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": float64(5),
|
|
|
|
"b_c": float64(6),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:56:14 +00:00
|
|
|
func saramaMsg(val string) *sarama.ConsumerMessage {
|
|
|
|
return &sarama.ConsumerMessage{
|
|
|
|
Key: nil,
|
|
|
|
Value: []byte(val),
|
|
|
|
Offset: 0,
|
|
|
|
Partition: 0,
|
|
|
|
}
|
|
|
|
}
|