Skip to content
Closed
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
26 changes: 26 additions & 0 deletions propagation/propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ type TextMapCarrier interface {
// must never be done outside of a new major release.
}

// BytesMapCarrier is a Carrier that stores propagated
Comment thread
jmacd marked this conversation as resolved.
// values in a map of bytes.
Comment on lines +43 to +44
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BytesMapCarrier is a Carrier that stores propagated
// values in a map of bytes.
// BytesMapCarrier adapts map of bytes to satisfy the TextMapCarrier interface.

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why input the value as a string and store it as a []byte?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmacd So that it implements the TextMapCarrier interface.

@rakyll Maybe it would be worth adding var _ TextMapCarrier = (*BytesMapCarrier)(nil) ?

Copy link
Copy Markdown
Contributor

@jmacd jmacd Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not store the value as a string, vs. storing as a []byte?

Not blocking, just curiousity. It seems to mean that every call to Get() copies the []byte into a new string because the compiler can't tell that the []byte is never modified.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rakyll would you mind following up, here?

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

Expand Down
14 changes: 14 additions & 0 deletions propagation/propagators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package propagation_test

import (
"context"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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
}
Expand Down