-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
280 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package internal | ||
|
||
import "github.com/segmentio/kafka-go" | ||
|
||
type Message struct { | ||
*kafka.Message | ||
} | ||
|
||
func NewMessage(msg *kafka.Message) *Message { | ||
return &Message{Message: msg} | ||
} | ||
|
||
func (m *Message) GetHeader(key string) string { | ||
for _, h := range m.Headers { | ||
if h.Key == key { | ||
return string(h.Value) | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (m *Message) SetHeader(key, val string) { | ||
// Ensure uniqueness of keys | ||
for i := 0; i < len(m.Headers); i++ { | ||
if m.Headers[i].Key == key { | ||
m.Headers = append(m.Headers[:i], m.Headers[i+1:]...) | ||
i-- | ||
} | ||
} | ||
m.Headers = append(m.Headers, kafka.Header{ | ||
Key: key, | ||
Value: []byte(val), | ||
}) | ||
} |
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,57 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/segmentio/kafka-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMessageGetHeader(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
msg *Message | ||
key string | ||
expected string | ||
}{ | ||
{ | ||
name: "exists", | ||
msg: &Message{ | ||
Message: &kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}, | ||
}}}, | ||
key: "foo", | ||
expected: "bar", | ||
}, | ||
{ | ||
name: "not exists", | ||
msg: &Message{Message: &kafka.Message{Headers: []kafka.Header{}}}, | ||
key: "foo", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result := tc.msg.GetHeader(tc.key) | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestMessageSetHeader(t *testing.T) { | ||
msg := &Message{Message: &kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}}, | ||
}} | ||
|
||
msg.SetHeader("foo", "bar2") | ||
msg.SetHeader("foo2", "bar2") | ||
msg.SetHeader("foo2", "bar3") | ||
msg.SetHeader("foo3", "bar4") | ||
|
||
assert.ElementsMatch(t, msg.Headers, []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar2")}, | ||
{Key: "foo2", Value: []byte("bar3")}, | ||
{Key: "foo3", Value: []byte("bar4")}, | ||
}) | ||
} |
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,35 @@ | ||
package internal | ||
|
||
import "go.opentelemetry.io/otel/propagation" | ||
|
||
var _ propagation.TextMapCarrier = (*MessageCarrier)(nil) | ||
|
||
// MessageCarrier injects and extracts traces from a types.Message. | ||
type MessageCarrier struct { | ||
msg *Message | ||
} | ||
|
||
// NewMessageCarrier returns a new MessageCarrier. | ||
func NewMessageCarrier(msg *Message) MessageCarrier { | ||
return MessageCarrier{msg: msg} | ||
} | ||
|
||
// Get returns the value associated with the passed key. | ||
func (m MessageCarrier) Get(key string) string { | ||
return m.msg.GetHeader(key) | ||
} | ||
|
||
// Set stores the key-value pair. | ||
func (m MessageCarrier) Set(key string, value string) { | ||
m.msg.SetHeader(key, value) | ||
} | ||
|
||
// Keys lists the keys stored in this carrier. | ||
func (m MessageCarrier) Keys() []string { | ||
out := make([]string, len(m.msg.Headers)) | ||
for i, h := range m.msg.Headers { | ||
out[i] = h.Key | ||
} | ||
|
||
return out | ||
} |
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,93 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/segmentio/kafka-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMessageCarrierGet(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
carrier MessageCarrier | ||
key string | ||
expected string | ||
}{ | ||
{ | ||
name: "exists", | ||
carrier: NewMessageCarrier(&Message{&kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}, | ||
}}}), | ||
key: "foo", | ||
expected: "bar", | ||
}, | ||
{ | ||
name: "not exists", | ||
carrier: NewMessageCarrier(&Message{&kafka.Message{Headers: []kafka.Header{}}}), | ||
key: "foo", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result := tc.carrier.Get(tc.key) | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestMessageCarrierSet(t *testing.T) { | ||
msg := Message{&kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}, | ||
}}} | ||
carrier := MessageCarrier{msg: &msg} | ||
|
||
carrier.Set("foo", "bar2") | ||
carrier.Set("foo2", "bar2") | ||
carrier.Set("foo2", "bar3") | ||
carrier.Set("foo3", "bar4") | ||
|
||
assert.ElementsMatch(t, carrier.msg.Headers, []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar2")}, | ||
{Key: "foo2", Value: []byte("bar3")}, | ||
{Key: "foo3", Value: []byte("bar4")}, | ||
}) | ||
} | ||
|
||
func TestMessageCarrierKeys(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
carrier MessageCarrier | ||
expected []string | ||
}{ | ||
{ | ||
name: "one", | ||
carrier: MessageCarrier{msg: &Message{&kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}, | ||
}}}}, | ||
expected: []string{"foo"}, | ||
}, | ||
{ | ||
name: "none", | ||
carrier: MessageCarrier{msg: &Message{&kafka.Message{Headers: []kafka.Header{}}}}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "many", | ||
carrier: MessageCarrier{msg: &Message{&kafka.Message{Headers: []kafka.Header{ | ||
{Key: "foo", Value: []byte("bar")}, | ||
{Key: "baz", Value: []byte("quux")}, | ||
}}}}, | ||
expected: []string{"foo", "baz"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result := tc.carrier.Keys() | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.