Skip to content

Commit

Permalink
Add MapCarrier (#2334)
Browse files Browse the repository at this point in the history
* Add MapCarrier

* Update CHANGELOG.md

* Lint propagation_test.go

* Remove trailing space in changelog entry

* Revert change to internal/tools/go.sum
  • Loading branch information
MrAlias committed Oct 29, 2021
1 parent 4ba964b commit ef0cdeb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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. (#2334)

## [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
33 changes: 33 additions & 0 deletions propagation/propagation_test.go
Expand Up @@ -16,9 +16,12 @@ package propagation_test

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

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/propagation"
)

Expand Down Expand Up @@ -102,3 +105,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)
}

0 comments on commit ef0cdeb

Please sign in to comment.