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

Fix: Kafka metadata fixes #1261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type InputKafkaConfig struct {
Host string `json:"input-kafka-host"`
Topic string `json:"input-kafka-topic"`
UseJSON bool `json:"input-kafka-json-format"`
Offset string `json:"input-kafka-offset"`
Offset string `json:"input-kafka-offset"`
SASLConfig SASLKafkaConfig
}

Expand Down
18 changes: 11 additions & 7 deletions output_kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package goreplay

import (
"encoding/json"
"github.com/buger/goreplay/internal/byteutils"
"github.com/buger/goreplay/proto"
"log"
"strings"
"time"

"github.com/buger/goreplay/internal/byteutils"
"github.com/buger/goreplay/proto"

"github.com/Shopify/sarama"
"github.com/Shopify/sarama/mocks"
)
Expand Down Expand Up @@ -64,6 +65,8 @@ func (o *KafkaOutput) ErrorHandler() {
// PluginWrite writes a message to this plugin
func (o *KafkaOutput) PluginWrite(msg *Message) (n int, err error) {
var message sarama.StringEncoder
meta := payloadMeta(msg.Meta)
recordKey := byteutils.SliceToString(meta[1])

if !o.config.UseJSON {
message = sarama.StringEncoder(byteutils.SliceToString(msg.Meta) + byteutils.SliceToString(msg.Data))
Expand All @@ -73,10 +76,7 @@ func (o *KafkaOutput) PluginWrite(msg *Message) (n int, err error) {
for k, v := range mimeHeader {
header[k] = strings.Join(v, ", ")
}

meta := payloadMeta(msg.Meta)
req := msg.Data

kafkaMessage := KafkaMessage{
ReqURL: byteutils.SliceToString(proto.Path(req)),
ReqType: byteutils.SliceToString(meta[0]),
Expand All @@ -91,8 +91,12 @@ func (o *KafkaOutput) PluginWrite(msg *Message) (n int, err error) {
}

o.producer.Input() <- &sarama.ProducerMessage{
Topic: o.config.Topic,
Value: message,
Topic: o.config.Topic,
Value: message,
Timestamp: time.Now(),
//
// Making ReqID as Kafka record's key
Key: sarama.StringEncoder(recordKey),
}

return len(message), nil
Expand Down
12 changes: 12 additions & 0 deletions output_kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestOutputKafkaRAW(t *testing.T) {

resp := <-producer.Successes()

key, _ := resp.Key.Encode()

if string(key) != "2" {
t.Errorf("Key not properly encoded: %q", key)
}

data, _ := resp.Value.Encode()

if string(data) != "1 2 3\nGET / HTTP1.1\r\nHeader: 1\r\n\r\n" {
Expand All @@ -46,6 +52,12 @@ func TestOutputKafkaJSON(t *testing.T) {

resp := <-producer.Successes()

key, _ := resp.Key.Encode()

if string(key) != "2" {
t.Errorf("Key not properly encoded: %q", key)
}

data, _ := resp.Value.Encode()

if string(data) != `{"Req_URL":"","Req_Type":"1","Req_ID":"2","Req_Ts":"3","Req_Method":"GET"}` {
Expand Down
Loading