2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
2017-04-11 19:05:39 +00:00
|
|
|
"strings"
|
2015-06-27 03:56:14 +00:00
|
|
|
"testing"
|
|
|
|
|
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-11-16 20:12:45 +00:00
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
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
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
func newTestKafka() (*Kafka, chan *sarama.ConsumerMessage) {
|
|
|
|
in := make(chan *sarama.ConsumerMessage, 1000)
|
2015-11-16 20:12:45 +00:00
|
|
|
k := Kafka{
|
|
|
|
ConsumerGroup: "test",
|
|
|
|
Topics: []string{"telegraf"},
|
2017-06-08 01:22:28 +00:00
|
|
|
Brokers: []string{"localhost:9092"},
|
2015-11-16 20:12:45 +00:00
|
|
|
Offset: "oldest",
|
|
|
|
in: in,
|
|
|
|
doNotCommitMsgs: true,
|
2017-01-11 16:24:09 +00:00
|
|
|
errs: make(chan error, 1000),
|
2015-11-16 20:12:45 +00:00
|
|
|
done: make(chan struct{}),
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
return &k, in
|
|
|
|
}
|
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) {
|
2016-02-16 00:21:38 +00:00
|
|
|
k, in := newTestKafka()
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
2015-11-16 20:12:45 +00:00
|
|
|
defer close(k.done)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
|
|
|
go k.receiver()
|
2015-11-16 20:12:45 +00:00
|
|
|
in <- 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) {
|
2016-02-16 00:21:38 +00:00
|
|
|
k, in := newTestKafka()
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
2015-11-16 20:12:45 +00:00
|
|
|
defer close(k.done)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
|
|
|
go k.receiver()
|
2015-11-16 20:12:45 +00:00
|
|
|
in <- 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
|
|
|
|
k, in := newTestKafka()
|
|
|
|
k.MaxMessageLen = maxMessageLen
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
|
|
|
defer close(k.done)
|
|
|
|
overlongMsg := strings.Repeat("v", maxMessageLen+1)
|
|
|
|
|
|
|
|
go k.receiver()
|
|
|
|
in <- saramaMsg(overlongMsg)
|
|
|
|
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) {
|
2016-02-16 00:21:38 +00:00
|
|
|
k, in := newTestKafka()
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
2015-11-16 20:12:45 +00:00
|
|
|
defer close(k.done)
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
k.parser, _ = parsers.NewInfluxParser()
|
|
|
|
go k.receiver()
|
2015-11-16 20:12:45 +00:00
|
|
|
in <- 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) {
|
2016-02-16 00:21:38 +00:00
|
|
|
k, in := newTestKafka()
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
2016-02-06 00:36:35 +00:00
|
|
|
defer close(k.done)
|
|
|
|
|
|
|
|
k.parser, _ = parsers.NewGraphiteParser("_", []string{}, nil)
|
|
|
|
go k.receiver()
|
|
|
|
in <- 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) {
|
2016-02-16 00:21:38 +00:00
|
|
|
k, in := newTestKafka()
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
k.acc = &acc
|
2016-02-06 00:36:35 +00:00
|
|
|
defer close(k.done)
|
|
|
|
|
2018-06-27 22:53:52 +00:00
|
|
|
k.parser, _ = parsers.NewJSONParser("kafka_json_test", []string{}, nil, nil)
|
2016-02-06 00:36:35 +00:00
|
|
|
go k.receiver()
|
|
|
|
in <- 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,
|
|
|
|
}
|
|
|
|
}
|