Skip to content

Commit ef0cdeb

Browse files
authored
Add MapCarrier (#2334)
* Add MapCarrier * Update CHANGELOG.md * Lint propagation_test.go * Remove trailing space in changelog entry * Revert change to internal/tools/go.sum
1 parent 4ba964b commit ef0cdeb

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121

2222
- Add the `"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc".WithGRPCConn` option so the exporter can reuse an existing gRPC connection. (#2002)
2323
- Added a new `schema` module to help parse Schema Files in OTEP 0152 format. (#2267)
24+
- 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)
2425

2526
## [1.1.0] - 2021-10-27
2627

propagation/propagation.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ type TextMapCarrier interface {
4040
// must never be done outside of a new major release.
4141
}
4242

43+
// MapCarrier is a TextMapCarrier that uses a map held in memory as a storage
44+
// medium for propagated key-value pairs.
45+
type MapCarrier map[string]string
46+
47+
// Compile time check that MapCarrier implements the TextMapCarrier.
48+
var _ TextMapCarrier = MapCarrier{}
49+
50+
// Get returns the value associated with the passed key.
51+
func (c MapCarrier) Get(key string) string {
52+
return c[key]
53+
}
54+
55+
// Set stores the key-value pair.
56+
func (c MapCarrier) Set(key, value string) {
57+
c[key] = value
58+
}
59+
60+
// Keys lists the keys stored in this carrier.
61+
func (c MapCarrier) Keys() []string {
62+
keys := make([]string, 0, len(c))
63+
for k := range c {
64+
keys = append(keys, k)
65+
}
66+
return keys
67+
}
68+
4369
// HeaderCarrier adapts http.Header to satisfy the TextMapCarrier interface.
4470
type HeaderCarrier http.Header
4571

propagation/propagation_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ package propagation_test
1616

1717
import (
1818
"context"
19+
"sort"
1920
"strings"
2021
"testing"
2122

23+
"github.com/stretchr/testify/assert"
24+
2225
"go.opentelemetry.io/otel/propagation"
2326
)
2427

@@ -102,3 +105,33 @@ func TestCompositeTextMapPropagatorExtract(t *testing.T) {
102105
t.Errorf("invalid extract order: %s", got)
103106
}
104107
}
108+
109+
func TestMapCarrierGet(t *testing.T) {
110+
carrier := propagation.MapCarrier{
111+
"foo": "bar",
112+
"baz": "qux",
113+
}
114+
115+
assert.Equal(t, carrier.Get("foo"), "bar")
116+
assert.Equal(t, carrier.Get("baz"), "qux")
117+
}
118+
119+
func TestMapCarrierSet(t *testing.T) {
120+
carrier := make(propagation.MapCarrier)
121+
carrier.Set("foo", "bar")
122+
carrier.Set("baz", "qux")
123+
124+
assert.Equal(t, carrier["foo"], "bar")
125+
assert.Equal(t, carrier["baz"], "qux")
126+
}
127+
128+
func TestMapCarrierKeys(t *testing.T) {
129+
carrier := propagation.MapCarrier{
130+
"foo": "bar",
131+
"baz": "qux",
132+
}
133+
134+
keys := carrier.Keys()
135+
sort.Strings(keys)
136+
assert.Equal(t, []string{"baz", "foo"}, keys)
137+
}

0 commit comments

Comments
 (0)