| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package propagation_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"slices" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/propagation" |
| 15 |
) |
| 16 |
|
| 17 |
type ctxKeyType uint |
| 18 |
|
| 19 |
var ctxKey ctxKeyType |
| 20 |
|
| 21 |
type carrier []string |
| 22 |
|
| 23 |
func (*carrier) Keys() []string { return nil } |
| 24 |
|
| 25 |
func (*carrier) Get(string) string { return "" } |
| 26 |
|
| 27 |
func (c *carrier) Set(setter, _ string) { |
| 28 |
*c = append(*c, setter) |
| 29 |
} |
| 30 |
|
| 31 |
type propagator struct { |
| 32 |
Name string |
| 33 |
} |
| 34 |
|
| 35 |
func (p propagator) Inject(_ context.Context, carrier propagation.TextMapCarrier) { |
| 36 |
carrier.Set(p.Name, "") |
| 37 |
} |
| 38 |
|
| 39 |
func (p propagator) Extract(ctx context.Context, _ propagation.TextMapCarrier) context.Context { |
| 40 |
v := ctx.Value(ctxKey) |
| 41 |
if v == nil { |
| 42 |
ctx = context.WithValue(ctx, ctxKey, []string{p.Name}) |
| 43 |
} else { |
| 44 |
orig := v.([]string) |
| 45 |
ctx = context.WithValue(ctx, ctxKey, append(orig, p.Name)) |
| 46 |
} |
| 47 |
return ctx |
| 48 |
} |
| 49 |
|
| 50 |
func (p propagator) Fields() []string { return []string{p.Name} } |
| 51 |
|
| 52 |
func TestCompositeTextMapPropagatorFields(t *testing.T) { |
| 53 |
a, b1, b2 := propagator{"a"}, propagator{"b"}, propagator{"b"} |
| 54 |
|
| 55 |
want := map[string]struct{}{ |
| 56 |
"a": {}, |
| 57 |
"b": {}, |
| 58 |
} |
| 59 |
got := propagation.NewCompositeTextMapPropagator(a, b1, b2).Fields() |
| 60 |
if len(got) != len(want) { |
| 61 |
t.Fatalf("invalid fields from composite: %v (want %v)", got, want) |
| 62 |
} |
| 63 |
for _, v := range got { |
| 64 |
if _, ok := want[v]; !ok { |
| 65 |
t.Errorf("invalid field returned from composite: %q", v) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestCompositeTextMapPropagatorInject(t *testing.T) { |
| 71 |
a, b := propagator{"a"}, propagator{"b"} |
| 72 |
|
| 73 |
c := make(carrier, 0, 2) |
| 74 |
propagation.NewCompositeTextMapPropagator(a, b).Inject(t.Context(), &c) |
| 75 |
|
| 76 |
if got := strings.Join([]string(c), ","); got != "a,b" { |
| 77 |
t.Errorf("invalid inject order: %s", got) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestCompositeTextMapPropagatorExtract(t *testing.T) { |
| 82 |
a, b := propagator{"a"}, propagator{"b"} |
| 83 |
|
| 84 |
ctx := t.Context() |
| 85 |
ctx = propagation.NewCompositeTextMapPropagator(a, b).Extract(ctx, nil) |
| 86 |
|
| 87 |
v := ctx.Value(ctxKey) |
| 88 |
if v == nil { |
| 89 |
t.Fatal("no composite extraction") |
| 90 |
} |
| 91 |
if got := strings.Join(v.([]string), ","); got != "a,b" { |
| 92 |
t.Errorf("invalid extract order: %s", got) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestMapCarrierGet(t *testing.T) { |
| 97 |
carrier := propagation.MapCarrier{ |
| 98 |
"foo": "bar", |
| 99 |
"baz": "qux", |
| 100 |
} |
| 101 |
|
| 102 |
assert.Equal(t, "bar", carrier.Get("foo")) |
| 103 |
assert.Equal(t, "qux", carrier.Get("baz")) |
| 104 |
} |
| 105 |
|
| 106 |
func TestMapCarrierSet(t *testing.T) { |
| 107 |
carrier := make(propagation.MapCarrier) |
| 108 |
carrier.Set("foo", "bar") |
| 109 |
carrier.Set("baz", "qux") |
| 110 |
|
| 111 |
assert.Equal(t, "bar", carrier["foo"]) |
| 112 |
assert.Equal(t, "qux", carrier["baz"]) |
| 113 |
} |
| 114 |
|
| 115 |
func TestMapCarrierKeys(t *testing.T) { |
| 116 |
carrier := propagation.MapCarrier{ |
| 117 |
"foo": "bar", |
| 118 |
"baz": "qux", |
| 119 |
} |
| 120 |
|
| 121 |
keys := carrier.Keys() |
| 122 |
slices.Sort(keys) |
| 123 |
assert.Equal(t, []string{"baz", "foo"}, keys) |
| 124 |
} |