From ca3f3cedf7715716e6b6c39b02479232e809894f Mon Sep 17 00:00:00 2001 From: JBD Date: Thu, 16 Sep 2021 12:22:37 -0700 Subject: [PATCH 1/5] Add BytesMapCarrier BytesMapCarrier makes it easy for users to inject/extract propagated values from various sources. For example, if a trace context is propagated by an environment variable, users can create a new BytesMapCarrier with values from the environment and inject the trace context into a Go context. --- propagation/propagation.go | 23 +++++++++++++++++++++++ propagation/propagators_test.go | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/propagation/propagation.go b/propagation/propagation.go index 2f54532d9a7..0cd4df8fc65 100644 --- a/propagation/propagation.go +++ b/propagation/propagation.go @@ -40,6 +40,29 @@ 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 + +// 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..7d4f84c353d 100644 --- a/propagation/propagators_test.go +++ b/propagation/propagators_test.go @@ -53,6 +53,16 @@ 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") + assert.Equal(t, carrier.Keys(), []string{"foo", "baz"}) +} + type outOfThinAirPropagator struct { t *testing.T } From 5da702b8565060d87608b135dd4373b0898d479c Mon Sep 17 00:00:00 2001 From: JBD Date: Fri, 17 Sep 2021 10:09:16 -0700 Subject: [PATCH 2/5] Sort keys before asserting --- propagation/propagators_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/propagation/propagators_test.go b/propagation/propagators_test.go index 7d4f84c353d..0949efc1f7d 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" @@ -60,7 +61,10 @@ func TestBytesMapCarrier(t *testing.T) { assert.Equal(t, carrier.Get("foo"), "bar") assert.Equal(t, carrier.Get("baz"), "qux") - assert.Equal(t, carrier.Keys(), []string{"foo", "baz"}) + + keys := carrier.Keys() + sort.Strings(keys) + assert.Equal(t, carrier.Keys(), []string{"baz", "foo"}) } type outOfThinAirPropagator struct { From 617774c91c1af4462c575fc9649caef1db72de87 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 11:56:38 -0700 Subject: [PATCH 3/5] Fix TestBytesMapCarrier test The sort.Strings function sorts in increasing order. --- propagation/propagators_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propagation/propagators_test.go b/propagation/propagators_test.go index 0949efc1f7d..e2e6b58e87b 100644 --- a/propagation/propagators_test.go +++ b/propagation/propagators_test.go @@ -64,7 +64,7 @@ func TestBytesMapCarrier(t *testing.T) { keys := carrier.Keys() sort.Strings(keys) - assert.Equal(t, carrier.Keys(), []string{"baz", "foo"}) + assert.Equal(t, []string{"foo", "baz"}, carrier.Keys()) } type outOfThinAirPropagator struct { From 3733c1887c22c145d1fa4c17ab32de79f1986f0b Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:00:29 -0700 Subject: [PATCH 4/5] Add compile time type assertion for BytesMapCarrier --- propagation/propagation.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/propagation/propagation.go b/propagation/propagation.go index 0cd4df8fc65..815551c00c1 100644 --- a/propagation/propagation.go +++ b/propagation/propagation.go @@ -44,6 +44,9 @@ type TextMapCarrier interface { // 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]) From 878c1c7e0a0c18980c1253b412403073b17fb66f Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:02:10 -0700 Subject: [PATCH 5/5] Fix TestBytesMapCarrier test Evaluate the sorted keys. --- propagation/propagators_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propagation/propagators_test.go b/propagation/propagators_test.go index e2e6b58e87b..e5e0113f021 100644 --- a/propagation/propagators_test.go +++ b/propagation/propagators_test.go @@ -64,7 +64,7 @@ func TestBytesMapCarrier(t *testing.T) { keys := carrier.Keys() sort.Strings(keys) - assert.Equal(t, []string{"foo", "baz"}, carrier.Keys()) + assert.Equal(t, []string{"baz", "foo"}, keys) } type outOfThinAirPropagator struct {