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

Add BytesMapCarrier #2245

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions propagation/propagation.go
Expand Up @@ -40,6 +40,29 @@ type TextMapCarrier interface {
// must never be done outside of a new major release.
}

// BytesMapCarrier is a Carrier that stores propagated
jmacd marked this conversation as resolved.
Show resolved Hide resolved
// values in a map of bytes.
Comment on lines +43 to +44
Copy link
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

// 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
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
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
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
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
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, carrier.Keys(), []string{"baz", "foo"})
}

type outOfThinAirPropagator struct {
t *testing.T
}
Expand Down