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 MapCarrier #2334

Merged
merged 7 commits into from Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

## [1.1.0] - 2021-10-27

Expand Down
26 changes: 26 additions & 0 deletions propagation/propagation.go
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions propagation/propagation_test.go
Expand Up @@ -16,9 +16,11 @@ package propagation_test

import (
"context"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/propagation"
)

Expand Down Expand Up @@ -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)
}