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 WithMessageKey to add the key to a producer message #549

Merged
merged 1 commit into from
Jun 30, 2020
Merged
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
11 changes: 11 additions & 0 deletions protocol/kafka_sarama/v2/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (s *Sender) Send(ctx context.Context, m binding.Message, transformers ...bi

kafkaMessage := sarama.ProducerMessage{Topic: s.topic}

if k := ctx.Value(withMessageKey{}); k != nil {
kafkaMessage.Key = k.(sarama.Encoder)
}

if err = WriteProducerMessage(ctx, m, &kafkaMessage, transformers...); err != nil {
return err
}
Expand All @@ -70,3 +74,10 @@ func (s *Sender) Close(ctx context.Context) error {
// otherwise it will close the whole client
return s.syncProducer.Close()
}

type withMessageKey struct{}

// WithMessageKey allows to set the key used when sending the producer message
func WithMessageKey(ctx context.Context, key sarama.Encoder) context.Context {
return context.WithValue(ctx, withMessageKey{}, key)
}
51 changes: 51 additions & 0 deletions protocol/kafka_sarama/v2/sender_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kafka_sarama

import (
"context"
"sync"
"testing"

"github.com/Shopify/sarama"
"github.com/stretchr/testify/require"

"github.com/cloudevents/sdk-go/v2/test"
)

type syncProducerMock struct {
lock sync.Mutex
sent []*sarama.ProducerMessage
}

func (s *syncProducerMock) SendMessage(msg *sarama.ProducerMessage) (partition int32, offset int64, err error) {
s.lock.Lock()
defer s.lock.Unlock()
s.sent = append(s.sent, msg)
return 0, int64(len(s.sent) - 1), err
}

func (s *syncProducerMock) SendMessages(msgs []*sarama.ProducerMessage) error {
s.lock.Lock()
defer s.lock.Unlock()
s.sent = append(s.sent, msgs...)
return nil
}

func (s *syncProducerMock) Close() error {
return nil
}

func TestSenderWithKey(t *testing.T) {
syncProducerMock := &syncProducerMock{}
topic := "aaa"

sender := &Sender{topic: topic, syncProducer: syncProducerMock}
require.NoError(t, sender.Send(
WithMessageKey(context.TODO(), sarama.StringEncoder("hello")),
test.FullMessage(),
))

require.Len(t, syncProducerMock.sent, 1)
kafkaMsg := syncProducerMock.sent[0]
require.Equal(t, kafkaMsg.Topic, topic)
require.Equal(t, kafkaMsg.Key, sarama.StringEncoder("hello"))
}
8 changes: 7 additions & 1 deletion samples/kafka/sender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/Shopify/sarama"
"github.com/google/uuid"

"github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2"
cloudevents "github.com/cloudevents/sdk-go/v2"
Expand Down Expand Up @@ -32,14 +33,19 @@ func main() {

for i := 0; i < count; i++ {
e := cloudevents.NewEvent()
e.SetID(uuid.New().String())
e.SetType("com.cloudevents.sample.sent")
e.SetSource("https://github.com/cloudevents/sdk-go/v2/samples/kafka/sender")
_ = e.SetData(cloudevents.ApplicationJSON, map[string]interface{}{
"id": i,
"message": "Hello, World!",
})

if result := c.Send(context.Background(), e); cloudevents.IsUndelivered(result) {
if result := c.Send(
// Set the producer message key
kafka_sarama.WithMessageKey(context.Background(), sarama.StringEncoder(e.ID())),
e,
); cloudevents.IsUndelivered(result) {
log.Printf("failed to send: %v", err)
} else {
log.Printf("sent: %d, accepted: %t", i, cloudevents.IsACK(result))
Expand Down
13 changes: 13 additions & 0 deletions v2/test/event_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"time"

"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/spec"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/types"
Expand Down Expand Up @@ -56,6 +57,18 @@ func MinEvent() event.Event {
}
}

// FullMessage returns the same event of FullEvent but wrapped as Message.
func FullMessage() binding.Message {
ev := FullEvent()
return binding.ToMessage(&ev)
}

// MinMessage returns the same event of MinEvent but wrapped as Message.
func MinMessage() binding.Message {
ev := MinEvent()
return binding.ToMessage(&ev)
}

// AllVersions returns all versions of each event in events.
// ID gets a -number suffix so IDs are unique.
func AllVersions(events []event.Event) []event.Event {
Expand Down