From b6425305f0eb2a14cf2ae7df8489829e708194f4 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:34:25 -0700 Subject: [PATCH 1/5] Add MapCarrier --- CHANGELOG.md | 1 + propagation/propagation.go | 26 ++++++++++++++++++++++++++ propagation/propagation_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a2e940c978..1cf5d937757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the `"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc".WithGRPCConn` option so the exporter can reuse an existing gRPC connection. (#2002) - Added a new `schema` module to help parse Schema Files in OTEP 0152 format. (#2267) +- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated coss-cutting concerns as a `map[string]string` held in memory. (TBD) ## [1.1.0] - 2021-10-27 diff --git a/propagation/propagation.go b/propagation/propagation.go index 2f54532d9a7..c94438f73a5 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. } +// MapCarrier is a TextMapCarrier that uses a map held in memory as a storage +// medium for propagated key-value pairs. +type MapCarrier map[string]string + +// Compile time check that MapCarrier implements the TextMapCarrier. +var _ TextMapCarrier = MapCarrier{} + +// Get returns the value associated with the passed key. +func (c MapCarrier) Get(key string) string { + return c[key] +} + +// Set stores the key-value pair. +func (c MapCarrier) Set(key, value string) { + c[key] = value +} + +// Keys lists the keys stored in this carrier. +func (c MapCarrier) 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/propagation_test.go b/propagation/propagation_test.go index 12d93cb9cf4..90bf16e780f 100644 --- a/propagation/propagation_test.go +++ b/propagation/propagation_test.go @@ -16,9 +16,11 @@ package propagation_test import ( "context" + "sort" "strings" "testing" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/propagation" ) @@ -102,3 +104,33 @@ func TestCompositeTextMapPropagatorExtract(t *testing.T) { t.Errorf("invalid extract order: %s", got) } } + +func TestMapCarrierGet(t *testing.T) { + carrier := propagation.MapCarrier{ + "foo": "bar", + "baz": "qux", + } + + assert.Equal(t, carrier.Get("foo"), "bar") + assert.Equal(t, carrier.Get("baz"), "qux") +} + +func TestMapCarrierSet(t *testing.T) { + carrier := make(propagation.MapCarrier) + carrier.Set("foo", "bar") + carrier.Set("baz", "qux") + + assert.Equal(t, carrier["foo"], "bar") + assert.Equal(t, carrier["baz"], "qux") +} + +func TestMapCarrierKeys(t *testing.T) { + carrier := propagation.MapCarrier{ + "foo": "bar", + "baz": "qux", + } + + keys := carrier.Keys() + sort.Strings(keys) + assert.Equal(t, []string{"baz", "foo"}, keys) +} From 5ac8a6dba1ee41a9e66c5ebd33e6b3b0d44efd7b Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:40:37 -0700 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cf5d937757..8a900dd0455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the `"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc".WithGRPCConn` option so the exporter can reuse an existing gRPC connection. (#2002) - Added a new `schema` module to help parse Schema Files in OTEP 0152 format. (#2267) -- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated coss-cutting concerns as a `map[string]string` held in memory. (TBD) +- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated coss-cutting concerns as a `map[string]string` held in memory. (#2334 ) ## [1.1.0] - 2021-10-27 From 803c7485d92db098338c3fe604f06a9e7e4fd02e Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:49:27 -0700 Subject: [PATCH 3/5] Lint propagation_test.go --- propagation/propagation_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/propagation/propagation_test.go b/propagation/propagation_test.go index 90bf16e780f..488d6551d5b 100644 --- a/propagation/propagation_test.go +++ b/propagation/propagation_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel/propagation" ) From cd5000e99c3f6a3c99861441e0c910ff7f1635a0 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:50:15 -0700 Subject: [PATCH 4/5] Remove trailing space in changelog entry --- CHANGELOG.md | 2 +- internal/tools/go.sum | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a900dd0455..6f35c83d76d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the `"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc".WithGRPCConn` option so the exporter can reuse an existing gRPC connection. (#2002) - Added a new `schema` module to help parse Schema Files in OTEP 0152 format. (#2267) -- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated coss-cutting concerns as a `map[string]string` held in memory. (#2334 ) +- Added a new `MapCarrier` to the `go.opentelemetry.io/otel/propagation` package to hold propagated coss-cutting concerns as a `map[string]string` held in memory. (#2334) ## [1.1.0] - 2021-10-27 diff --git a/internal/tools/go.sum b/internal/tools/go.sum index e7d6cc0cb4f..e919330a6ec 100644 --- a/internal/tools/go.sum +++ b/internal/tools/go.sum @@ -401,7 +401,6 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/itchyny/go-flags v1.5.0 h1:Z5q2ist2sfDjDlExVPBrMqlsEDxDR2h4zuOElB0OEYI= github.com/itchyny/go-flags v1.5.0/go.mod h1:lenkYuCobuxLBAd/HGFE4LRoW8D3B6iXRQfWYJ+MNbA= github.com/itchyny/gojq v0.12.5 h1:6SJ1BQ1VAwJAlIvLSIZmqHP/RUEq3qfVWvsRxrqhsD0= github.com/itchyny/gojq v0.12.5/go.mod h1:3e1hZXv+Kwvdp6V9HXpVrvddiHVApi5EDZwS+zLFeiE= From f029162ec48d129e579e9f88ae1423c6f4f249e6 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 28 Oct 2021 12:51:43 -0700 Subject: [PATCH 5/5] Revert change to internal/tools/go.sum --- internal/tools/go.sum | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/tools/go.sum b/internal/tools/go.sum index e919330a6ec..e7d6cc0cb4f 100644 --- a/internal/tools/go.sum +++ b/internal/tools/go.sum @@ -401,6 +401,7 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/itchyny/go-flags v1.5.0 h1:Z5q2ist2sfDjDlExVPBrMqlsEDxDR2h4zuOElB0OEYI= github.com/itchyny/go-flags v1.5.0/go.mod h1:lenkYuCobuxLBAd/HGFE4LRoW8D3B6iXRQfWYJ+MNbA= github.com/itchyny/gojq v0.12.5 h1:6SJ1BQ1VAwJAlIvLSIZmqHP/RUEq3qfVWvsRxrqhsD0= github.com/itchyny/gojq v0.12.5/go.mod h1:3e1hZXv+Kwvdp6V9HXpVrvddiHVApi5EDZwS+zLFeiE=