1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00

Move baggage and propagation to separate packages (#1325)

* Move propagation code to propagation package

* Move baggage code to baggage package

* Update changelog

* Make docs of baggage.Set more clear

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Krzesimir Nowak
2020-11-13 16:34:24 +01:00
committed by GitHub
parent f6df5df938
commit 63a11144cf
21 changed files with 116 additions and 119 deletions

View File

@@ -71,13 +71,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed ### Changed
- Set default propagator to no-op propagator. (#1184) - Set default propagator to no-op propagator. (#1184)
- The `HTTPSupplier`, `HTTPExtractor`, `HTTPInjector`, and `HTTPPropagator` from the `go.opentelemetry.io/otel/api/propagation` package were replaced with unified `TextMapCarrier` and `TextMapPropagator` in the `go.opentelemetry.io/otel` package. (#1212) - The `HTTPSupplier`, `HTTPExtractor`, `HTTPInjector`, and `HTTPPropagator` from the `go.opentelemetry.io/otel/api/propagation` package were replaced with unified `TextMapCarrier` and `TextMapPropagator` in the `go.opentelemetry.io/otel/propagation` package. (#1212) (#1325)
- The `New` function from the `go.opentelemetry.io/otel/api/propagation` package was replaced with `NewCompositeTextMapPropagator` in the `go.opentelemetry.io/otel` package. (#1212) - The `New` function from the `go.opentelemetry.io/otel/api/propagation` package was replaced with `NewCompositeTextMapPropagator` in the `go.opentelemetry.io/otel` package. (#1212)
- The status codes of the `go.opentelemetry.io/otel/codes` package have been updated to match the latest OpenTelemetry specification. - The status codes of the `go.opentelemetry.io/otel/codes` package have been updated to match the latest OpenTelemetry specification.
They now are `Unset`, `Error`, and `Ok`. They now are `Unset`, `Error`, and `Ok`.
They no longer track the gRPC codes. (#1214) They no longer track the gRPC codes. (#1214)
- The `StatusCode` field of the `SpanData` struct in the `go.opentelemetry.io/otel/sdk/export/trace` package now uses the codes package from this package instead of the gRPC project. (#1214) - The `StatusCode` field of the `SpanData` struct in the `go.opentelemetry.io/otel/sdk/export/trace` package now uses the codes package from this package instead of the gRPC project. (#1214)
- Move the `go.opentelemetry.io/otel/api/baggage` package into `go.opentelemetry.io/otel/propagators`. (#1217) - Move the `go.opentelemetry.io/otel/api/baggage` package into `go.opentelemetry.io/otel/baggage`. (#1217) (#1325)
- A `Shutdown` method of `SpanProcessor` and all its implementations receives a context and returns an error. (#1264) - A `Shutdown` method of `SpanProcessor` and all its implementations receives a context and returns an error. (#1264)
### Fixed ### Fixed

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package otel // import "go.opentelemetry.io/otel" package baggage // import "go.opentelemetry.io/otel/baggage"
import ( import (
"context" "context"
@@ -21,8 +21,8 @@ import (
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
) )
// Baggage returns a copy of the baggage in ctx. // Set returns a copy of the set of baggage key-values in ctx.
func Baggage(ctx context.Context) label.Set { func Set(ctx context.Context) label.Set {
// TODO (MrAlias, #1222): The underlying storage, the Map, shares many of // TODO (MrAlias, #1222): The underlying storage, the Map, shares many of
// the functional elements of the label.Set. These should be unified so // the functional elements of the label.Set. These should be unified so
// this conversion is unnecessary and there is no performance hit calling // this conversion is unnecessary and there is no performance hit calling
@@ -36,32 +36,32 @@ func Baggage(ctx context.Context) label.Set {
return label.NewSet(values...) return label.NewSet(values...)
} }
// BaggageValue returns the value related to key in the baggage of ctx. If no // Value returns the value related to key in the baggage of ctx. If no
// value is set, the returned label.Value will be an uninitialized zero-value // value is set, the returned label.Value will be an uninitialized zero-value
// with type INVALID. // with type INVALID.
func BaggageValue(ctx context.Context, key label.Key) label.Value { func Value(ctx context.Context, key label.Key) label.Value {
v, _ := baggage.MapFromContext(ctx).Value(key) v, _ := baggage.MapFromContext(ctx).Value(key)
return v return v
} }
// ContextWithBaggageValues returns a copy of parent with pairs updated in the baggage. // ContextWithValues returns a copy of parent with pairs updated in the baggage.
func ContextWithBaggageValues(parent context.Context, pairs ...label.KeyValue) context.Context { func ContextWithValues(parent context.Context, pairs ...label.KeyValue) context.Context {
m := baggage.MapFromContext(parent).Apply(baggage.MapUpdate{ m := baggage.MapFromContext(parent).Apply(baggage.MapUpdate{
MultiKV: pairs, MultiKV: pairs,
}) })
return baggage.ContextWithMap(parent, m) return baggage.ContextWithMap(parent, m)
} }
// ContextWithoutBaggageValues returns a copy of parent in which the values related // ContextWithoutValues returns a copy of parent in which the values related
// to keys have been removed from the baggage. // to keys have been removed from the baggage.
func ContextWithoutBaggageValues(parent context.Context, keys ...label.Key) context.Context { func ContextWithoutValues(parent context.Context, keys ...label.Key) context.Context {
m := baggage.MapFromContext(parent).Apply(baggage.MapUpdate{ m := baggage.MapFromContext(parent).Apply(baggage.MapUpdate{
DropMultiK: keys, DropMultiK: keys,
}) })
return baggage.ContextWithMap(parent, m) return baggage.ContextWithMap(parent, m)
} }
// ContextWithoutBaggage returns a copy of parent without baggage. // ContextWithEmpty returns a copy of parent without baggage.
func ContextWithoutBaggage(parent context.Context) context.Context { func ContextWithEmpty(parent context.Context) context.Context {
return baggage.ContextWithNoCorrelationData(parent) return baggage.ContextWithNoCorrelationData(parent)
} }

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package otel package baggage
import ( import (
"context" "context"
@@ -26,59 +26,59 @@ func TestBaggage(t *testing.T) {
ctx := context.Background() ctx := context.Background()
ctx = baggage.ContextWithMap(ctx, baggage.NewEmptyMap()) ctx = baggage.ContextWithMap(ctx, baggage.NewEmptyMap())
b := Baggage(ctx) b := Set(ctx)
if b.Len() != 0 { if b.Len() != 0 {
t.Fatalf("empty baggage returned a set with %d elements", b.Len()) t.Fatalf("empty baggage returned a set with %d elements", b.Len())
} }
first, second, third := label.Key("first"), label.Key("second"), label.Key("third") first, second, third := label.Key("first"), label.Key("second"), label.Key("third")
ctx = ContextWithBaggageValues(ctx, first.Bool(true), second.String("2")) ctx = ContextWithValues(ctx, first.Bool(true), second.String("2"))
m := baggage.MapFromContext(ctx) m := baggage.MapFromContext(ctx)
v, ok := m.Value(first) v, ok := m.Value(first)
if !ok { if !ok {
t.Fatal("WithBaggageValues failed to set first value") t.Fatal("WithValues failed to set first value")
} }
if !v.AsBool() { if !v.AsBool() {
t.Fatal("WithBaggageValues failed to set first correct value") t.Fatal("WithValues failed to set first correct value")
} }
v, ok = m.Value(second) v, ok = m.Value(second)
if !ok { if !ok {
t.Fatal("WithBaggageValues failed to set second value") t.Fatal("WithValues failed to set second value")
} }
if v.AsString() != "2" { if v.AsString() != "2" {
t.Fatal("WithBaggageValues failed to set second correct value") t.Fatal("WithValues failed to set second correct value")
} }
_, ok = m.Value(third) _, ok = m.Value(third)
if ok { if ok {
t.Fatal("WithBaggageValues set an unexpected third value") t.Fatal("WithValues set an unexpected third value")
} }
b = Baggage(ctx) b = Set(ctx)
if b.Len() != 2 { if b.Len() != 2 {
t.Fatalf("Baggage returned a set with %d elements, want 2", b.Len()) t.Fatalf("Baggage returned a set with %d elements, want 2", b.Len())
} }
v = BaggageValue(ctx, first) v = Value(ctx, first)
if v.Type() != label.BOOL || !v.AsBool() { if v.Type() != label.BOOL || !v.AsBool() {
t.Fatal("BaggageValue failed to get correct first value") t.Fatal("Value failed to get correct first value")
} }
v = BaggageValue(ctx, second) v = Value(ctx, second)
if v.Type() != label.STRING || v.AsString() != "2" { if v.Type() != label.STRING || v.AsString() != "2" {
t.Fatal("BaggageValue failed to get correct second value") t.Fatal("Value failed to get correct second value")
} }
ctx = ContextWithoutBaggageValues(ctx, first) ctx = ContextWithoutValues(ctx, first)
m = baggage.MapFromContext(ctx) m = baggage.MapFromContext(ctx)
_, ok = m.Value(first) _, ok = m.Value(first)
if ok { if ok {
t.Fatal("WithoutBaggageValues failed to remove a baggage value") t.Fatal("WithoutValues failed to remove a baggage value")
} }
_, ok = m.Value(second) _, ok = m.Value(second)
if !ok { if !ok {
t.Fatal("WithoutBaggageValues removed incorrect value") t.Fatal("WithoutValues removed incorrect value")
} }
ctx = ContextWithoutBaggage(ctx) ctx = ContextWithEmpty(ctx)
m = baggage.MapFromContext(ctx) m = baggage.MapFromContext(ctx)
if m.Len() != 0 { if m.Len() != 0 {
t.Fatal("WithoutBaggage failed to clear baggage") t.Fatal("WithoutBaggage failed to clear baggage")

View File

@@ -25,16 +25,15 @@ import (
otext "github.com/opentracing/opentracing-go/ext" otext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log" otlog "github.com/opentracing/opentracing-go/log"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/bridge/opentracing/migration"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
otelglobal "go.opentelemetry.io/otel/global" otelglobal "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/internal/baggage" "go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/internal/trace/noop" "go.opentelemetry.io/otel/internal/trace/noop"
otelparent "go.opentelemetry.io/otel/internal/trace/parent" otelparent "go.opentelemetry.io/otel/internal/trace/parent"
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/bridge/opentracing/migration"
) )
type bridgeSpanContext struct { type bridgeSpanContext struct {
@@ -294,7 +293,7 @@ type BridgeTracer struct {
warningHandler BridgeWarningHandler warningHandler BridgeWarningHandler
warnOnce sync.Once warnOnce sync.Once
propagator otel.TextMapPropagator propagator propagation.TextMapPropagator
} }
var _ ot.Tracer = &BridgeTracer{} var _ ot.Tracer = &BridgeTracer{}
@@ -329,7 +328,7 @@ func (t *BridgeTracer) SetOpenTelemetryTracer(tracer trace.Tracer) {
t.setTracer.isSet = true t.setTracer.isSet = true
} }
func (t *BridgeTracer) SetTextMapPropagator(propagator otel.TextMapPropagator) { func (t *BridgeTracer) SetTextMapPropagator(propagator propagation.TextMapPropagator) {
t.propagator = propagator t.propagator = propagator
} }
@@ -651,7 +650,7 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span
return bridgeSC, nil return bridgeSC, nil
} }
func (t *BridgeTracer) getPropagator() otel.TextMapPropagator { func (t *BridgeTracer) getPropagator() propagation.TextMapPropagator {
if t.propagator != nil { if t.propagator != nil {
return t.propagator return t.propagator
} }

View File

@@ -18,12 +18,12 @@ import (
"context" "context"
"log" "log"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/exporters/stdout" "go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/global" "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric/controller/push" "go.opentelemetry.io/otel/sdk/metric/controller/push"
"go.opentelemetry.io/otel/sdk/metric/processor/basic" "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple" "go.opentelemetry.io/otel/sdk/metric/selector/simple"
@@ -63,7 +63,7 @@ func main() {
global.SetMeterProvider(pusher.MeterProvider()) global.SetMeterProvider(pusher.MeterProvider())
// set global propagator to baggage (the default is no-op). // set global propagator to baggage (the default is no-op).
global.SetTextMapPropagator(propagators.Baggage{}) global.SetTextMapPropagator(propagation.Baggage{})
tracer := global.Tracer("ex.com/basic") tracer := global.Tracer("ex.com/basic")
meter := global.Meter("ex.com/basic") meter := global.Meter("ex.com/basic")
@@ -79,7 +79,7 @@ func main() {
valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two") valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two")
ctx := context.Background() ctx := context.Background()
ctx = otel.ContextWithBaggageValues(ctx, fooKey.String("foo1"), barKey.String("bar1")) ctx = baggage.ContextWithValues(ctx, fooKey.String("foo1"), barKey.String("bar1"))
valuerecorder := valuerecorderTwo.Bind(commonLabels...) valuerecorder := valuerecorderTwo.Bind(commonLabels...)
defer valuerecorder.Unbind() defer valuerecorder.Unbind()
@@ -94,7 +94,7 @@ func main() {
meter.RecordBatch( meter.RecordBatch(
// Note: call-site variables added as context Entries: // Note: call-site variables added as context Entries:
otel.ContextWithBaggageValues(ctx, anotherKey.String("xyz")), baggage.ContextWithValues(ctx, anotherKey.String("xyz")),
commonLabels, commonLabels,
valuerecorderTwo.Measurement(2.0), valuerecorderTwo.Measurement(2.0),

View File

@@ -18,7 +18,7 @@ import (
"context" "context"
"log" "log"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/example/namedtracer/foo" "go.opentelemetry.io/otel/example/namedtracer/foo"
"go.opentelemetry.io/otel/exporters/stdout" "go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/global" "go.opentelemetry.io/otel/global"
@@ -63,7 +63,7 @@ func main() {
tracer := tp.Tracer("example/namedtracer/main") tracer := tp.Tracer("example/namedtracer/main")
ctx := context.Background() ctx := context.Background()
defer func() { _ = tp.Shutdown(ctx) }() defer func() { _ = tp.Shutdown(ctx) }()
ctx = otel.ContextWithBaggageValues(ctx, fooKey.String("foo1"), barKey.String("bar1")) ctx = baggage.ContextWithValues(ctx, fooKey.String("foo1"), barKey.String("bar1"))
var span trace.Span var span trace.Span
ctx, span = tracer.Start(ctx, "operation") ctx, span = tracer.Start(ctx, "operation")

View File

@@ -29,7 +29,7 @@ import (
"go.opentelemetry.io/otel/global" "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric/controller/push" "go.opentelemetry.io/otel/sdk/metric/controller/push"
"go.opentelemetry.io/otel/sdk/metric/processor/basic" "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple" "go.opentelemetry.io/otel/sdk/metric/selector/simple"
@@ -81,7 +81,7 @@ func initProvider() func() {
) )
// set global propagator to tracecontext (the default is no-op). // set global propagator to tracecontext (the default is no-op).
global.SetTextMapPropagator(propagators.TraceContext{}) global.SetTextMapPropagator(propagation.TraceContext{})
global.SetTracerProvider(tracerProvider) global.SetTracerProvider(tracerProvider)
global.SetMeterProvider(pusher.MeterProvider()) global.SetMeterProvider(pusher.MeterProvider())
pusher.Start() pusher.Start()

View File

@@ -18,33 +18,33 @@ import (
"context" "context"
"sync" "sync"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation"
) )
// textMapPropagator is a default TextMapPropagator that delegates calls to a // textMapPropagator is a default TextMapPropagator that delegates calls to a
// registered delegate if one is set, otherwise it defaults to delegating the // registered delegate if one is set, otherwise it defaults to delegating the
// calls to a the default no-op otel.TextMapPropagator. // calls to a the default no-op propagation.TextMapPropagator.
type textMapPropagator struct { type textMapPropagator struct {
mtx sync.Mutex mtx sync.Mutex
once sync.Once once sync.Once
delegate otel.TextMapPropagator delegate propagation.TextMapPropagator
noop otel.TextMapPropagator noop propagation.TextMapPropagator
} }
// Compile-time guarantee that textMapPropagator implements the // Compile-time guarantee that textMapPropagator implements the
// otel.TextMapPropagator interface. // propagation.TextMapPropagator interface.
var _ otel.TextMapPropagator = (*textMapPropagator)(nil) var _ propagation.TextMapPropagator = (*textMapPropagator)(nil)
func newTextMapPropagator() *textMapPropagator { func newTextMapPropagator() *textMapPropagator {
return &textMapPropagator{ return &textMapPropagator{
noop: otel.NewCompositeTextMapPropagator(), noop: propagation.NewCompositeTextMapPropagator(),
} }
} }
// SetDelegate sets a delegate otel.TextMapPropagator that all calls are // SetDelegate sets a delegate propagation.TextMapPropagator that all calls are
// forwarded to. Delegation can only be performed once, all subsequent calls // forwarded to. Delegation can only be performed once, all subsequent calls
// perform no delegation. // perform no delegation.
func (p *textMapPropagator) SetDelegate(delegate otel.TextMapPropagator) { func (p *textMapPropagator) SetDelegate(delegate propagation.TextMapPropagator) {
if delegate == nil { if delegate == nil {
return return
} }
@@ -57,7 +57,7 @@ func (p *textMapPropagator) SetDelegate(delegate otel.TextMapPropagator) {
// effectiveDelegate returns the current delegate of p if one is set, // effectiveDelegate returns the current delegate of p if one is set,
// otherwise the default noop TextMapPropagator is returned. This method // otherwise the default noop TextMapPropagator is returned. This method
// can be called concurrently. // can be called concurrently.
func (p *textMapPropagator) effectiveDelegate() otel.TextMapPropagator { func (p *textMapPropagator) effectiveDelegate() propagation.TextMapPropagator {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()
if p.delegate != nil { if p.delegate != nil {
@@ -67,12 +67,12 @@ func (p *textMapPropagator) effectiveDelegate() otel.TextMapPropagator {
} }
// Inject set cross-cutting concerns from the Context into the carrier. // Inject set cross-cutting concerns from the Context into the carrier.
func (p *textMapPropagator) Inject(ctx context.Context, carrier otel.TextMapCarrier) { func (p *textMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
p.effectiveDelegate().Inject(ctx, carrier) p.effectiveDelegate().Inject(ctx, carrier)
} }
// Extract reads cross-cutting concerns from the carrier into a Context. // Extract reads cross-cutting concerns from the carrier into a Context.
func (p *textMapPropagator) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { func (p *textMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
return p.effectiveDelegate().Extract(ctx, carrier) return p.effectiveDelegate().Extract(ctx, carrier)
} }

View File

@@ -18,8 +18,8 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@@ -33,7 +33,7 @@ type (
} }
propagatorsHolder struct { propagatorsHolder struct {
tm otel.TextMapPropagator tm propagation.TextMapPropagator
} }
) )
@@ -92,12 +92,12 @@ func SetMeterProvider(mp metric.MeterProvider) {
} }
// TextMapPropagator is the internal implementation for global.TextMapPropagator. // TextMapPropagator is the internal implementation for global.TextMapPropagator.
func TextMapPropagator() otel.TextMapPropagator { func TextMapPropagator() propagation.TextMapPropagator {
return globalPropagators.Load().(propagatorsHolder).tm return globalPropagators.Load().(propagatorsHolder).tm
} }
// SetTextMapPropagator is the internal implementation for global.SetTextMapPropagator. // SetTextMapPropagator is the internal implementation for global.SetTextMapPropagator.
func SetTextMapPropagator(p otel.TextMapPropagator) { func SetTextMapPropagator(p propagation.TextMapPropagator) {
// For the textMapPropagator already returned by TextMapPropagator // For the textMapPropagator already returned by TextMapPropagator
// delegate to p. // delegate to p.
delegateTextMapPropagatorOnce.Do(func() { delegateTextMapPropagatorOnce.Do(func() {

View File

@@ -15,17 +15,17 @@
package global // import "go.opentelemetry.io/otel/global" package global // import "go.opentelemetry.io/otel/global"
import ( import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/global/internal" "go.opentelemetry.io/otel/global/internal"
"go.opentelemetry.io/otel/propagation"
) )
// TextMapPropagator returns the global TextMapPropagator. If none has been // TextMapPropagator returns the global TextMapPropagator. If none has been
// set, a No-Op TextMapPropagator is returned. // set, a No-Op TextMapPropagator is returned.
func TextMapPropagator() otel.TextMapPropagator { func TextMapPropagator() propagation.TextMapPropagator {
return internal.TextMapPropagator() return internal.TextMapPropagator()
} }
// SetTextMapPropagator sets propagator as the global TSetTextMapPropagator. // SetTextMapPropagator sets propagator as the global TSetTextMapPropagator.
func SetTextMapPropagator(propagator otel.TextMapPropagator) { func SetTextMapPropagator(propagator propagation.TextMapPropagator) {
internal.SetTextMapPropagator(propagator) internal.SetTextMapPropagator(propagator)
} }

View File

@@ -22,7 +22,7 @@ import (
"sync" "sync"
"testing" "testing"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation"
) )
type ctxKeyType string type ctxKeyType string
@@ -167,12 +167,12 @@ func (p *TextMapPropagator) stateFromContext(ctx context.Context) state {
return state{} return state{}
} }
func (p *TextMapPropagator) stateFromCarrier(carrier otel.TextMapCarrier) state { func (p *TextMapPropagator) stateFromCarrier(carrier propagation.TextMapCarrier) state {
return newState(carrier.Get(p.Name)) return newState(carrier.Get(p.Name))
} }
// Inject set cross-cutting concerns for p from the Context into the carrier. // Inject set cross-cutting concerns for p from the Context into the carrier.
func (p *TextMapPropagator) Inject(ctx context.Context, carrier otel.TextMapCarrier) { func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
s := p.stateFromContext(ctx) s := p.stateFromContext(ctx)
s.Injections++ s.Injections++
carrier.Set(p.Name, s.String()) carrier.Set(p.Name, s.String())
@@ -188,7 +188,7 @@ func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n i
} }
// Extract reads cross-cutting concerns for p from the carrier into a Context. // Extract reads cross-cutting concerns for p from the carrier into a Context.
func (p *TextMapPropagator) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { func (p *TextMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
s := p.stateFromCarrier(carrier) s := p.stateFromCarrier(carrier)
s.Extractions++ s.Extractions++
return context.WithValue(ctx, p.ctxKey, s) return context.WithValue(ctx, p.ctxKey, s)

View File

@@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators // import "go.opentelemetry.io/otel/propagators" package propagation // import "go.opentelemetry.io/otel/propagation"
import ( import (
"context" "context"
"net/url" "net/url"
"strings" "strings"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/baggage" "go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
) )
@@ -32,10 +31,10 @@ const baggageHeader = "baggage"
// specification is defined at https://w3c.github.io/baggage/. // specification is defined at https://w3c.github.io/baggage/.
type Baggage struct{} type Baggage struct{}
var _ otel.TextMapPropagator = Baggage{} var _ TextMapPropagator = Baggage{}
// Inject sets baggage key-values from ctx into the carrier. // Inject sets baggage key-values from ctx into the carrier.
func (b Baggage) Inject(ctx context.Context, carrier otel.TextMapCarrier) { func (b Baggage) Inject(ctx context.Context, carrier TextMapCarrier) {
baggageMap := baggage.MapFromContext(ctx) baggageMap := baggage.MapFromContext(ctx)
firstIter := true firstIter := true
var headerValueBuilder strings.Builder var headerValueBuilder strings.Builder
@@ -56,7 +55,7 @@ func (b Baggage) Inject(ctx context.Context, carrier otel.TextMapCarrier) {
} }
// Extract returns a copy of parent with the baggage from the carrier added. // Extract returns a copy of parent with the baggage from the carrier added.
func (b Baggage) Extract(parent context.Context, carrier otel.TextMapCarrier) context.Context { func (b Baggage) Extract(parent context.Context, carrier TextMapCarrier) context.Context {
bVal := carrier.Get(baggageHeader) bVal := carrier.Get(baggageHeader)
if bVal == "" { if bVal == "" {
return parent return parent

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators_test package propagation_test
import ( import (
"context" "context"
@@ -22,14 +22,13 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/baggage" "go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
) )
func TestExtractValidBaggageFromHTTPReq(t *testing.T) { func TestExtractValidBaggageFromHTTPReq(t *testing.T) {
prop := otel.TextMapPropagator(propagators.Baggage{}) prop := propagation.TextMapPropagator(propagation.Baggage{})
tests := []struct { tests := []struct {
name string name string
header string header string
@@ -118,7 +117,7 @@ func TestExtractValidBaggageFromHTTPReq(t *testing.T) {
} }
func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
prop := otel.TextMapPropagator(propagators.Baggage{}) prop := propagation.TextMapPropagator(propagation.Baggage{})
tests := []struct { tests := []struct {
name string name string
header string header string
@@ -176,7 +175,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
} }
func TestInjectBaggageToHTTPReq(t *testing.T) { func TestInjectBaggageToHTTPReq(t *testing.T) {
propagator := propagators.Baggage{} propagator := propagation.Baggage{}
tests := []struct { tests := []struct {
name string name string
kvs []label.KeyValue kvs []label.KeyValue
@@ -250,7 +249,7 @@ func TestInjectBaggageToHTTPReq(t *testing.T) {
} }
func TestBaggagePropagatorGetAllKeys(t *testing.T) { func TestBaggagePropagatorGetAllKeys(t *testing.T) {
var propagator propagators.Baggage var propagator propagation.Baggage
want := []string{"baggage"} want := []string{"baggage"}
got := propagator.Fields() got := propagator.Fields()
if diff := cmp.Diff(got, want); diff != "" { if diff := cmp.Diff(got, want); diff != "" {

View File

@@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
/* /*
Package propagators contains OpenTelemetry context propagators. Package propagation contains OpenTelemetry context propagators.
This package is currently in a pre-GA phase. Backwards incompatible changes This package is currently in a pre-GA phase. Backwards incompatible changes
may be introduced in subsequent minor version releases as we work to track the may be introduced in subsequent minor version releases as we work to track the
@@ -25,4 +25,4 @@ package is the W3C Trace Context encoding
(https://www.w3.org/TR/trace-context/), and W3C Baggage (https://www.w3.org/TR/trace-context/), and W3C Baggage
(https://w3c.github.io/baggage/). (https://w3c.github.io/baggage/).
*/ */
package propagators // import "go.opentelemetry.io/otel/propagators" package propagation // import "go.opentelemetry.io/otel/propagation"

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package otel // import "go.opentelemetry.io/otel" package propagation // import "go.opentelemetry.io/otel/propagation"
import "context" import "context"

View File

@@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package otel package propagation_test
import ( import (
"context" "context"
"strings" "strings"
"testing" "testing"
"go.opentelemetry.io/otel/propagation"
) )
type ctxKeyType uint type ctxKeyType uint
@@ -38,11 +40,11 @@ type propagator struct {
Name string Name string
} }
func (p propagator) Inject(ctx context.Context, carrier TextMapCarrier) { func (p propagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
carrier.Set(p.Name, "") carrier.Set(p.Name, "")
} }
func (p propagator) Extract(ctx context.Context, carrier TextMapCarrier) context.Context { func (p propagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
v := ctx.Value(ctxKey) v := ctx.Value(ctxKey)
if v == nil { if v == nil {
ctx = context.WithValue(ctx, ctxKey, []string{p.Name}) ctx = context.WithValue(ctx, ctxKey, []string{p.Name})
@@ -62,7 +64,7 @@ func TestCompositeTextMapPropagatorFields(t *testing.T) {
"a": {}, "a": {},
"b": {}, "b": {},
} }
got := NewCompositeTextMapPropagator(a, b1, b2).Fields() got := propagation.NewCompositeTextMapPropagator(a, b1, b2).Fields()
if len(got) != len(want) { if len(got) != len(want) {
t.Fatalf("invalid fields from composite: %v (want %v)", got, want) t.Fatalf("invalid fields from composite: %v (want %v)", got, want)
} }
@@ -77,7 +79,7 @@ func TestCompositeTextMapPropagatorInject(t *testing.T) {
a, b := propagator{"a"}, propagator{"b"} a, b := propagator{"a"}, propagator{"b"}
c := make(carrier, 0, 2) c := make(carrier, 0, 2)
NewCompositeTextMapPropagator(a, b).Inject(context.Background(), &c) propagation.NewCompositeTextMapPropagator(a, b).Inject(context.Background(), &c)
if got := strings.Join([]string(c), ","); got != "a,b" { if got := strings.Join([]string(c), ","); got != "a,b" {
t.Errorf("invalid inject order: %s", got) t.Errorf("invalid inject order: %s", got)
@@ -88,7 +90,7 @@ func TestCompositeTextMapPropagatorExtract(t *testing.T) {
a, b := propagator{"a"}, propagator{"b"} a, b := propagator{"a"}, propagator{"b"}
ctx := context.Background() ctx := context.Background()
ctx = NewCompositeTextMapPropagator(a, b).Extract(ctx, nil) ctx = propagation.NewCompositeTextMapPropagator(a, b).Extract(ctx, nil)
v := ctx.Value(ctxKey) v := ctx.Value(ctxKey)
if v == nil { if v == nil {

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators_test package propagation_test
import ( import (
"context" "context"
@@ -21,8 +21,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/propagators"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@@ -58,9 +57,9 @@ type outOfThinAirPropagator struct {
t *testing.T t *testing.T
} }
var _ otel.TextMapPropagator = outOfThinAirPropagator{} var _ propagation.TextMapPropagator = outOfThinAirPropagator{}
func (p outOfThinAirPropagator) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { func (p outOfThinAirPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
sc := trace.SpanContext{ sc := trace.SpanContext{
TraceID: traceID, TraceID: traceID,
SpanID: spanID, SpanID: spanID,
@@ -70,7 +69,7 @@ func (p outOfThinAirPropagator) Extract(ctx context.Context, carrier otel.TextMa
return trace.ContextWithRemoteSpanContext(ctx, sc) return trace.ContextWithRemoteSpanContext(ctx, sc)
} }
func (outOfThinAirPropagator) Inject(context.Context, otel.TextMapCarrier) {} func (outOfThinAirPropagator) Inject(context.Context, propagation.TextMapCarrier) {}
func (outOfThinAirPropagator) Fields() []string { func (outOfThinAirPropagator) Fields() []string {
return nil return nil
@@ -78,7 +77,7 @@ func (outOfThinAirPropagator) Fields() []string {
type nilCarrier struct{} type nilCarrier struct{}
var _ otel.TextMapCarrier = nilCarrier{} var _ propagation.TextMapCarrier = nilCarrier{}
func (nilCarrier) Get(key string) string { func (nilCarrier) Get(key string) string {
return "" return ""
@@ -89,8 +88,8 @@ func (nilCarrier) Set(key string, value string) {}
func TestMultiplePropagators(t *testing.T) { func TestMultiplePropagators(t *testing.T) {
ootaProp := outOfThinAirPropagator{t: t} ootaProp := outOfThinAirPropagator{t: t}
ns := nilCarrier{} ns := nilCarrier{}
testProps := []otel.TextMapPropagator{ testProps := []propagation.TextMapPropagator{
propagators.TraceContext{}, propagation.TraceContext{},
} }
bg := context.Background() bg := context.Background()
// sanity check of oota propagator, ensuring that it really // sanity check of oota propagator, ensuring that it really
@@ -109,7 +108,7 @@ func TestMultiplePropagators(t *testing.T) {
require.Falsef(t, sc.IsValid(), "%#v failed sanity check", prop) require.Falsef(t, sc.IsValid(), "%#v failed sanity check", prop)
} }
for _, prop := range testProps { for _, prop := range testProps {
props := otel.NewCompositeTextMapPropagator(ootaProp, prop) props := propagation.NewCompositeTextMapPropagator(ootaProp, prop)
ctx := props.Extract(bg, ns) ctx := props.Extract(bg, ns)
sc := trace.RemoteSpanContextFromContext(ctx) sc := trace.RemoteSpanContextFromContext(ctx)
assert.Truef(t, sc.IsValid(), "%#v clobbers span context", prop) assert.Truef(t, sc.IsValid(), "%#v clobbers span context", prop)

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators // import "go.opentelemetry.io/otel/propagators" package propagation // import "go.opentelemetry.io/otel/propagation"
import ( import (
"context" "context"
@@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@@ -47,11 +46,11 @@ const (
// their proprietary information. // their proprietary information.
type TraceContext struct{} type TraceContext struct{}
var _ otel.TextMapPropagator = TraceContext{} var _ TextMapPropagator = TraceContext{}
var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$") var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$")
// Inject set tracecontext from the Context into the carrier. // Inject set tracecontext from the Context into the carrier.
func (tc TraceContext) Inject(ctx context.Context, carrier otel.TextMapCarrier) { func (tc TraceContext) Inject(ctx context.Context, carrier TextMapCarrier) {
tracestate := ctx.Value(tracestateKey) tracestate := ctx.Value(tracestateKey)
if state, ok := tracestate.(string); tracestate != nil && ok { if state, ok := tracestate.(string); tracestate != nil && ok {
carrier.Set(tracestateHeader, state) carrier.Set(tracestateHeader, state)
@@ -70,7 +69,7 @@ func (tc TraceContext) Inject(ctx context.Context, carrier otel.TextMapCarrier)
} }
// Extract reads tracecontext from the carrier into a returned Context. // Extract reads tracecontext from the carrier into a returned Context.
func (tc TraceContext) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { func (tc TraceContext) Extract(ctx context.Context, carrier TextMapCarrier) context.Context {
state := carrier.Get(tracestateHeader) state := carrier.Get(tracestateHeader)
if state != "" { if state != "" {
ctx = context.WithValue(ctx, tracestateKey, state) ctx = context.WithValue(ctx, tracestateKey, state)
@@ -83,7 +82,7 @@ func (tc TraceContext) Extract(ctx context.Context, carrier otel.TextMapCarrier)
return trace.ContextWithRemoteSpanContext(ctx, sc) return trace.ContextWithRemoteSpanContext(ctx, sc)
} }
func (tc TraceContext) extract(carrier otel.TextMapCarrier) trace.SpanContext { func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
h := carrier.Get(traceparentHeader) h := carrier.Get(traceparentHeader)
if h == "" { if h == "" {
return trace.SpanContext{} return trace.SpanContext{}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators_test package propagation_test
import ( import (
"context" "context"
@@ -20,12 +20,12 @@ import (
"testing" "testing"
"go.opentelemetry.io/otel/oteltest" "go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
func BenchmarkInject(b *testing.B) { func BenchmarkInject(b *testing.B) {
var t propagators.TraceContext var t propagation.TraceContext
injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) { injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil) req, _ := http.NewRequest("GET", "http://example.com", nil)
@@ -62,7 +62,7 @@ func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
func BenchmarkExtract(b *testing.B) { func BenchmarkExtract(b *testing.B) {
extractSubBenchmarks(b, func(b *testing.B, req *http.Request) { extractSubBenchmarks(b, func(b *testing.B, req *http.Request) {
var propagator propagators.TraceContext var propagator propagation.TraceContext
ctx := context.Background() ctx := context.Background()
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

View File

@@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators_test package propagation_test
import ( import (
"go.opentelemetry.io/otel/global" "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
) )
func ExampleTraceContext() { func ExampleTraceContext() {
tc := propagators.TraceContext{} tc := propagation.TraceContext{}
// Register the TraceContext propagator globally. // Register the TraceContext propagator globally.
global.SetTextMapPropagator(tc) global.SetTextMapPropagator(tc)
} }

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package propagators_test package propagation_test
import ( import (
"context" "context"
@@ -22,12 +22,12 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/oteltest" "go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/propagators" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
func TestExtractValidTraceContextFromHTTPReq(t *testing.T) { func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
prop := propagators.TraceContext{} prop := propagation.TraceContext{}
tests := []struct { tests := []struct {
name string name string
header string header string
@@ -122,7 +122,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) { func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) {
wantSc := trace.SpanContext{} wantSc := trace.SpanContext{}
prop := propagators.TraceContext{} prop := propagation.TraceContext{}
tests := []struct { tests := []struct {
name string name string
header string header string
@@ -210,7 +210,7 @@ func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) {
func TestInjectTraceContextToHTTPReq(t *testing.T) { func TestInjectTraceContextToHTTPReq(t *testing.T) {
mockTracer := oteltest.DefaultTracer() mockTracer := oteltest.DefaultTracer()
prop := propagators.TraceContext{} prop := propagation.TraceContext{}
tests := []struct { tests := []struct {
name string name string
sc trace.SpanContext sc trace.SpanContext
@@ -267,7 +267,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) {
} }
func TestTraceContextPropagator_GetAllKeys(t *testing.T) { func TestTraceContextPropagator_GetAllKeys(t *testing.T) {
var propagator propagators.TraceContext var propagator propagation.TraceContext
want := []string{"traceparent", "tracestate"} want := []string{"traceparent", "tracestate"}
got := propagator.Fields() got := propagator.Fields()
if diff := cmp.Diff(got, want); diff != "" { if diff := cmp.Diff(got, want); diff != "" {
@@ -276,7 +276,7 @@ func TestTraceContextPropagator_GetAllKeys(t *testing.T) {
} }
func TestTraceStatePropagation(t *testing.T) { func TestTraceStatePropagation(t *testing.T) {
prop := propagators.TraceContext{} prop := propagation.TraceContext{}
want := "opaquevalue" want := "opaquevalue"
headerName := "tracestate" headerName := "tracestate"