| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internaltest // import "go.opentelemetry.io/otel/internal/internaltest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/propagation" |
| 14 |
) |
| 15 |
|
| 16 |
type ctxKeyType string |
| 17 |
|
| 18 |
type state struct { |
| 19 |
Injections uint64 |
| 20 |
Extractions uint64 |
| 21 |
} |
| 22 |
|
| 23 |
func newState(encoded string) state { |
| 24 |
if encoded == "" { |
| 25 |
return state{} |
| 26 |
} |
| 27 |
s0, s1, _ := strings.Cut(encoded, ",") |
| 28 |
injects, _ := strconv.ParseUint(s0, 10, 64) |
| 29 |
extracts, _ := strconv.ParseUint(s1, 10, 64) |
| 30 |
return state{ |
| 31 |
Injections: injects, |
| 32 |
Extractions: extracts, |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func (s state) String() string { |
| 37 |
return fmt.Sprintf("%d,%d", s.Injections, s.Extractions) |
| 38 |
} |
| 39 |
|
| 40 |
// TextMapPropagator is a propagation.TextMapPropagator used for testing. |
| 41 |
type TextMapPropagator struct { |
| 42 |
name string |
| 43 |
ctxKey ctxKeyType |
| 44 |
} |
| 45 |
|
| 46 |
var _ propagation.TextMapPropagator = (*TextMapPropagator)(nil) |
| 47 |
|
| 48 |
// NewTextMapPropagator returns a new TextMapPropagator for testing. It will |
| 49 |
// use name as the key it injects into a TextMapCarrier when Inject is called. |
| 50 |
func NewTextMapPropagator(name string) *TextMapPropagator { |
| 51 |
return &TextMapPropagator{name: name, ctxKey: ctxKeyType(name)} |
| 52 |
} |
| 53 |
|
| 54 |
func (p *TextMapPropagator) stateFromContext(ctx context.Context) state { |
| 55 |
if v := ctx.Value(p.ctxKey); v != nil { |
| 56 |
if s, ok := v.(state); ok { |
| 57 |
return s |
| 58 |
} |
| 59 |
} |
| 60 |
return state{} |
| 61 |
} |
| 62 |
|
| 63 |
func (p *TextMapPropagator) stateFromCarrier(carrier propagation.TextMapCarrier) state { |
| 64 |
return newState(carrier.Get(p.name)) |
| 65 |
} |
| 66 |
|
| 67 |
// Inject sets cross-cutting concerns for p from ctx into carrier. |
| 68 |
func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { |
| 69 |
s := p.stateFromContext(ctx) |
| 70 |
s.Injections++ |
| 71 |
carrier.Set(p.name, s.String()) |
| 72 |
} |
| 73 |
|
| 74 |
// InjectedN tests if p has made n injections to carrier. |
| 75 |
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n uint64) bool { |
| 76 |
if actual := p.stateFromCarrier(carrier).Injections; actual != n { |
| 77 |
t.Errorf("TextMapPropagator{%q} injected %d times, not %d", p.name, actual, n) |
| 78 |
return false |
| 79 |
} |
| 80 |
return true |
| 81 |
} |
| 82 |
|
| 83 |
// Extract reads cross-cutting concerns for p from carrier into ctx. |
| 84 |
func (p *TextMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { |
| 85 |
s := p.stateFromCarrier(carrier) |
| 86 |
s.Extractions++ |
| 87 |
return context.WithValue(ctx, p.ctxKey, s) |
| 88 |
} |
| 89 |
|
| 90 |
// ExtractedN tests if p has made n extractions from the lineage of ctx. |
| 91 |
// nolint (context is not first arg) |
| 92 |
func (p *TextMapPropagator) ExtractedN(t *testing.T, ctx context.Context, n int) bool { |
| 93 |
if actual := p.stateFromContext(ctx).Extractions; actual != uint64(n) { |
| 94 |
t.Errorf("TextMapPropagator{%q} extracted %d time, not %d", p.name, actual, n) |
| 95 |
return false |
| 96 |
} |
| 97 |
return true |
| 98 |
} |
| 99 |
|
| 100 |
// Fields returns the name of p as the key who's value is set with Inject. |
| 101 |
func (p *TextMapPropagator) Fields() []string { return []string{p.name} } |