diff --git a/propagation/propagation.go b/propagation/propagation.go index 2f54532d9a7..815551c00c1 100644 --- a/propagation/propagation.go +++ b/propagation/propagation.go @@ -40,6 +40,32 @@ type TextMapCarrier interface { // must never be done outside of a new major release. } +// BytesMapCarrier is a Carrier that stores propagated +// values in a map of bytes. +type BytesMapCarrier map[string][]byte + +// Compile time check the BytesMapCarrier implements the TextMapCarrier. +var _ TextMapCarrier = BytesMapCarrier{} + +// Get returns the value associated with the passed key. +func (c BytesMapCarrier) Get(key string) string { + return string(c[key]) +} + +// Set stores the key-value pair. +func (c BytesMapCarrier) Set(key string, value string) { + c[key] = []byte(value) +} + +// Keys lists the keys stored in this carrier. +func (c BytesMapCarrier) Keys() []string { + keys := make([]string, 0, len(c)) + for k := range c { + keys = append(keys, k) + } + return keys +} + // HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface. type HeaderCarrier http.Header diff --git a/propagation/propagators_test.go b/propagation/propagators_test.go index 1ac8786bc2c..e5e0113f021 100644 --- a/propagation/propagators_test.go +++ b/propagation/propagators_test.go @@ -16,6 +16,7 @@ package propagation_test import ( "context" + "sort" "testing" "github.com/stretchr/testify/assert" @@ -53,6 +54,19 @@ func mustSpanIDFromHex(s string) (t trace.SpanID) { return } +func TestBytesMapCarrier(t *testing.T) { + carrier := make(propagation.BytesMapCarrier) + carrier.Set("foo", "bar") + carrier.Set("baz", "qux") + + assert.Equal(t, carrier.Get("foo"), "bar") + assert.Equal(t, carrier.Get("baz"), "qux") + + keys := carrier.Keys() + sort.Strings(keys) + assert.Equal(t, []string{"baz", "foo"}, keys) +} + type outOfThinAirPropagator struct { t *testing.T }