You've already forked opentelemetry-go
							
							
				mirror of
				https://github.com/open-telemetry/opentelemetry-go.git
				synced 2025-10-31 00:07:40 +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:
		| @@ -71,13 +71,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm | ||||
| ### Changed | ||||
|  | ||||
| - 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 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 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) | ||||
| - 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) | ||||
|  | ||||
| ### Fixed | ||||
|   | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package otel // import "go.opentelemetry.io/otel" | ||||
| package baggage // import "go.opentelemetry.io/otel/baggage" | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -21,8 +21,8 @@ import ( | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| ) | ||||
| 
 | ||||
| // Baggage returns a copy of the baggage in ctx. | ||||
| func Baggage(ctx context.Context) label.Set { | ||||
| // Set returns a copy of the set of baggage key-values in ctx. | ||||
| func Set(ctx context.Context) label.Set { | ||||
| 	// TODO (MrAlias, #1222): The underlying storage, the Map, shares many of | ||||
| 	// the functional elements of the label.Set. These should be unified so | ||||
| 	// 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...) | ||||
| } | ||||
| 
 | ||||
| // 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 | ||||
| // 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) | ||||
| 	return v | ||||
| } | ||||
| 
 | ||||
| // ContextWithBaggageValues returns a copy of parent with pairs updated in the baggage. | ||||
| func ContextWithBaggageValues(parent context.Context, pairs ...label.KeyValue) context.Context { | ||||
| // ContextWithValues returns a copy of parent with pairs updated in the baggage. | ||||
| func ContextWithValues(parent context.Context, pairs ...label.KeyValue) context.Context { | ||||
| 	m := baggage.MapFromContext(parent).Apply(baggage.MapUpdate{ | ||||
| 		MultiKV: pairs, | ||||
| 	}) | ||||
| 	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. | ||||
| 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{ | ||||
| 		DropMultiK: keys, | ||||
| 	}) | ||||
| 	return baggage.ContextWithMap(parent, m) | ||||
| } | ||||
| 
 | ||||
| // ContextWithoutBaggage returns a copy of parent without baggage. | ||||
| func ContextWithoutBaggage(parent context.Context) context.Context { | ||||
| // ContextWithEmpty returns a copy of parent without baggage. | ||||
| func ContextWithEmpty(parent context.Context) context.Context { | ||||
| 	return baggage.ContextWithNoCorrelationData(parent) | ||||
| } | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package otel | ||||
| package baggage | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -26,59 +26,59 @@ func TestBaggage(t *testing.T) { | ||||
| 	ctx := context.Background() | ||||
| 	ctx = baggage.ContextWithMap(ctx, baggage.NewEmptyMap()) | ||||
| 
 | ||||
| 	b := Baggage(ctx) | ||||
| 	b := Set(ctx) | ||||
| 	if b.Len() != 0 { | ||||
| 		t.Fatalf("empty baggage returned a set with %d elements", b.Len()) | ||||
| 	} | ||||
| 
 | ||||
| 	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) | ||||
| 	v, ok := m.Value(first) | ||||
| 	if !ok { | ||||
| 		t.Fatal("WithBaggageValues failed to set first value") | ||||
| 		t.Fatal("WithValues failed to set first value") | ||||
| 	} | ||||
| 	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) | ||||
| 	if !ok { | ||||
| 		t.Fatal("WithBaggageValues failed to set second value") | ||||
| 		t.Fatal("WithValues failed to set second value") | ||||
| 	} | ||||
| 	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) | ||||
| 	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 { | ||||
| 		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() { | ||||
| 		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" { | ||||
| 		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) | ||||
| 	_, ok = m.Value(first) | ||||
| 	if ok { | ||||
| 		t.Fatal("WithoutBaggageValues failed to remove a baggage value") | ||||
| 		t.Fatal("WithoutValues failed to remove a baggage value") | ||||
| 	} | ||||
| 	_, ok = m.Value(second) | ||||
| 	if !ok { | ||||
| 		t.Fatal("WithoutBaggageValues removed incorrect value") | ||||
| 		t.Fatal("WithoutValues removed incorrect value") | ||||
| 	} | ||||
| 
 | ||||
| 	ctx = ContextWithoutBaggage(ctx) | ||||
| 	ctx = ContextWithEmpty(ctx) | ||||
| 	m = baggage.MapFromContext(ctx) | ||||
| 	if m.Len() != 0 { | ||||
| 		t.Fatal("WithoutBaggage failed to clear baggage") | ||||
| @@ -25,16 +25,15 @@ import ( | ||||
| 	otext "github.com/opentracing/opentracing-go/ext" | ||||
| 	otlog "github.com/opentracing/opentracing-go/log" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/bridge/opentracing/migration" | ||||
| 	"go.opentelemetry.io/otel/codes" | ||||
| 	otelglobal "go.opentelemetry.io/otel/global" | ||||
| 	"go.opentelemetry.io/otel/internal/baggage" | ||||
| 	"go.opentelemetry.io/otel/internal/trace/noop" | ||||
| 	otelparent "go.opentelemetry.io/otel/internal/trace/parent" | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel/bridge/opentracing/migration" | ||||
| ) | ||||
|  | ||||
| type bridgeSpanContext struct { | ||||
| @@ -294,7 +293,7 @@ type BridgeTracer struct { | ||||
| 	warningHandler BridgeWarningHandler | ||||
| 	warnOnce       sync.Once | ||||
|  | ||||
| 	propagator otel.TextMapPropagator | ||||
| 	propagator propagation.TextMapPropagator | ||||
| } | ||||
|  | ||||
| var _ ot.Tracer = &BridgeTracer{} | ||||
| @@ -329,7 +328,7 @@ func (t *BridgeTracer) SetOpenTelemetryTracer(tracer trace.Tracer) { | ||||
| 	t.setTracer.isSet = true | ||||
| } | ||||
|  | ||||
| func (t *BridgeTracer) SetTextMapPropagator(propagator otel.TextMapPropagator) { | ||||
| func (t *BridgeTracer) SetTextMapPropagator(propagator propagation.TextMapPropagator) { | ||||
| 	t.propagator = propagator | ||||
| } | ||||
|  | ||||
| @@ -651,7 +650,7 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span | ||||
| 	return bridgeSC, nil | ||||
| } | ||||
|  | ||||
| func (t *BridgeTracer) getPropagator() otel.TextMapPropagator { | ||||
| func (t *BridgeTracer) getPropagator() propagation.TextMapPropagator { | ||||
| 	if t.propagator != nil { | ||||
| 		return t.propagator | ||||
| 	} | ||||
|   | ||||
| @@ -18,12 +18,12 @@ import ( | ||||
| 	"context" | ||||
| 	"log" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/baggage" | ||||
| 	"go.opentelemetry.io/otel/exporters/stdout" | ||||
| 	"go.opentelemetry.io/otel/global" | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| 	"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/processor/basic" | ||||
| 	"go.opentelemetry.io/otel/sdk/metric/selector/simple" | ||||
| @@ -63,7 +63,7 @@ func main() { | ||||
| 	global.SetMeterProvider(pusher.MeterProvider()) | ||||
|  | ||||
| 	// set global propagator to baggage (the default is no-op). | ||||
| 	global.SetTextMapPropagator(propagators.Baggage{}) | ||||
| 	global.SetTextMapPropagator(propagation.Baggage{}) | ||||
| 	tracer := global.Tracer("ex.com/basic") | ||||
| 	meter := global.Meter("ex.com/basic") | ||||
|  | ||||
| @@ -79,7 +79,7 @@ func main() { | ||||
| 	valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two") | ||||
|  | ||||
| 	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...) | ||||
| 	defer valuerecorder.Unbind() | ||||
| @@ -94,7 +94,7 @@ func main() { | ||||
|  | ||||
| 		meter.RecordBatch( | ||||
| 			// Note: call-site variables added as context Entries: | ||||
| 			otel.ContextWithBaggageValues(ctx, anotherKey.String("xyz")), | ||||
| 			baggage.ContextWithValues(ctx, anotherKey.String("xyz")), | ||||
| 			commonLabels, | ||||
|  | ||||
| 			valuerecorderTwo.Measurement(2.0), | ||||
|   | ||||
| @@ -18,7 +18,7 @@ import ( | ||||
| 	"context" | ||||
| 	"log" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/baggage" | ||||
| 	"go.opentelemetry.io/otel/example/namedtracer/foo" | ||||
| 	"go.opentelemetry.io/otel/exporters/stdout" | ||||
| 	"go.opentelemetry.io/otel/global" | ||||
| @@ -63,7 +63,7 @@ func main() { | ||||
| 	tracer := tp.Tracer("example/namedtracer/main") | ||||
| 	ctx := context.Background() | ||||
| 	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 | ||||
| 	ctx, span = tracer.Start(ctx, "operation") | ||||
|   | ||||
| @@ -29,7 +29,7 @@ import ( | ||||
| 	"go.opentelemetry.io/otel/global" | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| 	"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/processor/basic" | ||||
| 	"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). | ||||
| 	global.SetTextMapPropagator(propagators.TraceContext{}) | ||||
| 	global.SetTextMapPropagator(propagation.TraceContext{}) | ||||
| 	global.SetTracerProvider(tracerProvider) | ||||
| 	global.SetMeterProvider(pusher.MeterProvider()) | ||||
| 	pusher.Start() | ||||
|   | ||||
| @@ -18,33 +18,33 @@ import ( | ||||
| 	"context" | ||||
| 	"sync" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
|  | ||||
| // textMapPropagator is a default TextMapPropagator that delegates calls to a | ||||
| // 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 { | ||||
| 	mtx      sync.Mutex | ||||
| 	once     sync.Once | ||||
| 	delegate otel.TextMapPropagator | ||||
| 	noop     otel.TextMapPropagator | ||||
| 	delegate propagation.TextMapPropagator | ||||
| 	noop     propagation.TextMapPropagator | ||||
| } | ||||
|  | ||||
| // Compile-time guarantee that textMapPropagator implements the | ||||
| // otel.TextMapPropagator interface. | ||||
| var _ otel.TextMapPropagator = (*textMapPropagator)(nil) | ||||
| // propagation.TextMapPropagator interface. | ||||
| var _ propagation.TextMapPropagator = (*textMapPropagator)(nil) | ||||
|  | ||||
| func newTextMapPropagator() *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 | ||||
| // perform no delegation. | ||||
| func (p *textMapPropagator) SetDelegate(delegate otel.TextMapPropagator) { | ||||
| func (p *textMapPropagator) SetDelegate(delegate propagation.TextMapPropagator) { | ||||
| 	if delegate == nil { | ||||
| 		return | ||||
| 	} | ||||
| @@ -57,7 +57,7 @@ func (p *textMapPropagator) SetDelegate(delegate otel.TextMapPropagator) { | ||||
| // effectiveDelegate returns the current delegate of p if one is set, | ||||
| // otherwise the default noop TextMapPropagator is returned. This method | ||||
| // can be called concurrently. | ||||
| func (p *textMapPropagator) effectiveDelegate() otel.TextMapPropagator { | ||||
| func (p *textMapPropagator) effectiveDelegate() propagation.TextMapPropagator { | ||||
| 	p.mtx.Lock() | ||||
| 	defer p.mtx.Unlock() | ||||
| 	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. | ||||
| 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) | ||||
| } | ||||
|  | ||||
| // 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) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -18,8 +18,8 @@ import ( | ||||
| 	"sync" | ||||
| 	"sync/atomic" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/metric" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
| ) | ||||
|  | ||||
| @@ -33,7 +33,7 @@ type ( | ||||
| 	} | ||||
|  | ||||
| 	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. | ||||
| func TextMapPropagator() otel.TextMapPropagator { | ||||
| func TextMapPropagator() propagation.TextMapPropagator { | ||||
| 	return globalPropagators.Load().(propagatorsHolder).tm | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| 	// delegate to p. | ||||
| 	delegateTextMapPropagatorOnce.Do(func() { | ||||
|   | ||||
| @@ -15,17 +15,17 @@ | ||||
| package global // import "go.opentelemetry.io/otel/global" | ||||
|  | ||||
| import ( | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/global/internal" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
|  | ||||
| // TextMapPropagator returns the global TextMapPropagator. If none has been | ||||
| // set, a No-Op TextMapPropagator is returned. | ||||
| func TextMapPropagator() otel.TextMapPropagator { | ||||
| func TextMapPropagator() propagation.TextMapPropagator { | ||||
| 	return internal.TextMapPropagator() | ||||
| } | ||||
|  | ||||
| // SetTextMapPropagator sets propagator as the global TSetTextMapPropagator. | ||||
| func SetTextMapPropagator(propagator otel.TextMapPropagator) { | ||||
| func SetTextMapPropagator(propagator propagation.TextMapPropagator) { | ||||
| 	internal.SetTextMapPropagator(propagator) | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,7 @@ import ( | ||||
| 	"sync" | ||||
| 	"testing" | ||||
|  | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
|  | ||||
| type ctxKeyType string | ||||
| @@ -167,12 +167,12 @@ func (p *TextMapPropagator) stateFromContext(ctx context.Context) 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)) | ||||
| } | ||||
|  | ||||
| // 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.Injections++ | ||||
| 	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. | ||||
| 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.Extractions++ | ||||
| 	return context.WithValue(ctx, p.ctxKey, s) | ||||
|   | ||||
| @@ -12,14 +12,13 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators // import "go.opentelemetry.io/otel/propagators" | ||||
| package propagation // import "go.opentelemetry.io/otel/propagation" | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"net/url" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/internal/baggage" | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| ) | ||||
| @@ -32,10 +31,10 @@ const baggageHeader = "baggage" | ||||
| // specification is defined at https://w3c.github.io/baggage/. | ||||
| type Baggage struct{} | ||||
| 
 | ||||
| var _ otel.TextMapPropagator = Baggage{} | ||||
| var _ TextMapPropagator = Baggage{} | ||||
| 
 | ||||
| // 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) | ||||
| 	firstIter := true | ||||
| 	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. | ||||
| 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) | ||||
| 	if bVal == "" { | ||||
| 		return parent | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators_test | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -22,14 +22,13 @@ import ( | ||||
| 
 | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/internal/baggage" | ||||
| 	"go.opentelemetry.io/otel/label" | ||||
| 	"go.opentelemetry.io/otel/propagators" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
| 
 | ||||
| func TestExtractValidBaggageFromHTTPReq(t *testing.T) { | ||||
| 	prop := otel.TextMapPropagator(propagators.Baggage{}) | ||||
| 	prop := propagation.TextMapPropagator(propagation.Baggage{}) | ||||
| 	tests := []struct { | ||||
| 		name    string | ||||
| 		header  string | ||||
| @@ -118,7 +117,7 @@ func TestExtractValidBaggageFromHTTPReq(t *testing.T) { | ||||
| } | ||||
| 
 | ||||
| func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { | ||||
| 	prop := otel.TextMapPropagator(propagators.Baggage{}) | ||||
| 	prop := propagation.TextMapPropagator(propagation.Baggage{}) | ||||
| 	tests := []struct { | ||||
| 		name   string | ||||
| 		header string | ||||
| @@ -176,7 +175,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) { | ||||
| } | ||||
| 
 | ||||
| func TestInjectBaggageToHTTPReq(t *testing.T) { | ||||
| 	propagator := propagators.Baggage{} | ||||
| 	propagator := propagation.Baggage{} | ||||
| 	tests := []struct { | ||||
| 		name         string | ||||
| 		kvs          []label.KeyValue | ||||
| @@ -250,7 +249,7 @@ func TestInjectBaggageToHTTPReq(t *testing.T) { | ||||
| } | ||||
| 
 | ||||
| func TestBaggagePropagatorGetAllKeys(t *testing.T) { | ||||
| 	var propagator propagators.Baggage | ||||
| 	var propagator propagation.Baggage | ||||
| 	want := []string{"baggage"} | ||||
| 	got := propagator.Fields() | ||||
| 	if diff := cmp.Diff(got, want); diff != "" { | ||||
| @@ -13,7 +13,7 @@ | ||||
| // 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 | ||||
| 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://w3c.github.io/baggage/). | ||||
| */ | ||||
| package propagators // import "go.opentelemetry.io/otel/propagators" | ||||
| package propagation // import "go.opentelemetry.io/otel/propagation" | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package otel // import "go.opentelemetry.io/otel" | ||||
| package propagation // import "go.opentelemetry.io/otel/propagation" | ||||
| 
 | ||||
| import "context" | ||||
| 
 | ||||
| @@ -12,12 +12,14 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package otel | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
| 
 | ||||
| type ctxKeyType uint | ||||
| @@ -38,11 +40,11 @@ type propagator struct { | ||||
| 	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, "") | ||||
| } | ||||
| 
 | ||||
| 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) | ||||
| 	if v == nil { | ||||
| 		ctx = context.WithValue(ctx, ctxKey, []string{p.Name}) | ||||
| @@ -62,7 +64,7 @@ func TestCompositeTextMapPropagatorFields(t *testing.T) { | ||||
| 		"a": {}, | ||||
| 		"b": {}, | ||||
| 	} | ||||
| 	got := NewCompositeTextMapPropagator(a, b1, b2).Fields() | ||||
| 	got := propagation.NewCompositeTextMapPropagator(a, b1, b2).Fields() | ||||
| 	if len(got) != len(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"} | ||||
| 
 | ||||
| 	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" { | ||||
| 		t.Errorf("invalid inject order: %s", got) | ||||
| @@ -88,7 +90,7 @@ func TestCompositeTextMapPropagatorExtract(t *testing.T) { | ||||
| 	a, b := propagator{"a"}, propagator{"b"} | ||||
| 
 | ||||
| 	ctx := context.Background() | ||||
| 	ctx = NewCompositeTextMapPropagator(a, b).Extract(ctx, nil) | ||||
| 	ctx = propagation.NewCompositeTextMapPropagator(a, b).Extract(ctx, nil) | ||||
| 
 | ||||
| 	v := ctx.Value(ctxKey) | ||||
| 	if v == nil { | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators_test | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -21,8 +21,7 @@ import ( | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"github.com/stretchr/testify/require" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/propagators" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
| ) | ||||
| 
 | ||||
| @@ -58,9 +57,9 @@ type outOfThinAirPropagator struct { | ||||
| 	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{ | ||||
| 		TraceID:    traceID, | ||||
| 		SpanID:     spanID, | ||||
| @@ -70,7 +69,7 @@ func (p outOfThinAirPropagator) Extract(ctx context.Context, carrier otel.TextMa | ||||
| 	return trace.ContextWithRemoteSpanContext(ctx, sc) | ||||
| } | ||||
| 
 | ||||
| func (outOfThinAirPropagator) Inject(context.Context, otel.TextMapCarrier) {} | ||||
| func (outOfThinAirPropagator) Inject(context.Context, propagation.TextMapCarrier) {} | ||||
| 
 | ||||
| func (outOfThinAirPropagator) Fields() []string { | ||||
| 	return nil | ||||
| @@ -78,7 +77,7 @@ func (outOfThinAirPropagator) Fields() []string { | ||||
| 
 | ||||
| type nilCarrier struct{} | ||||
| 
 | ||||
| var _ otel.TextMapCarrier = nilCarrier{} | ||||
| var _ propagation.TextMapCarrier = nilCarrier{} | ||||
| 
 | ||||
| func (nilCarrier) Get(key string) string { | ||||
| 	return "" | ||||
| @@ -89,8 +88,8 @@ func (nilCarrier) Set(key string, value string) {} | ||||
| func TestMultiplePropagators(t *testing.T) { | ||||
| 	ootaProp := outOfThinAirPropagator{t: t} | ||||
| 	ns := nilCarrier{} | ||||
| 	testProps := []otel.TextMapPropagator{ | ||||
| 		propagators.TraceContext{}, | ||||
| 	testProps := []propagation.TextMapPropagator{ | ||||
| 		propagation.TraceContext{}, | ||||
| 	} | ||||
| 	bg := context.Background() | ||||
| 	// 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) | ||||
| 	} | ||||
| 	for _, prop := range testProps { | ||||
| 		props := otel.NewCompositeTextMapPropagator(ootaProp, prop) | ||||
| 		props := propagation.NewCompositeTextMapPropagator(ootaProp, prop) | ||||
| 		ctx := props.Extract(bg, ns) | ||||
| 		sc := trace.RemoteSpanContextFromContext(ctx) | ||||
| 		assert.Truef(t, sc.IsValid(), "%#v clobbers span context", prop) | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators // import "go.opentelemetry.io/otel/propagators" | ||||
| package propagation // import "go.opentelemetry.io/otel/propagation" | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -20,7 +20,6 @@ import ( | ||||
| 	"fmt" | ||||
| 	"regexp" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
| ) | ||||
| 
 | ||||
| @@ -47,11 +46,11 @@ const ( | ||||
| // their proprietary information. | ||||
| 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})(?:-.*)?$") | ||||
| 
 | ||||
| // 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) | ||||
| 	if state, ok := tracestate.(string); tracestate != nil && ok { | ||||
| 		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. | ||||
| 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) | ||||
| 	if 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) | ||||
| } | ||||
| 
 | ||||
| func (tc TraceContext) extract(carrier otel.TextMapCarrier) trace.SpanContext { | ||||
| func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext { | ||||
| 	h := carrier.Get(traceparentHeader) | ||||
| 	if h == "" { | ||||
| 		return trace.SpanContext{} | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators_test | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -20,12 +20,12 @@ import ( | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel/oteltest" | ||||
| 	"go.opentelemetry.io/otel/propagators" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
| ) | ||||
| 
 | ||||
| func BenchmarkInject(b *testing.B) { | ||||
| 	var t propagators.TraceContext | ||||
| 	var t propagation.TraceContext | ||||
| 
 | ||||
| 	injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) { | ||||
| 		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) { | ||||
| 	extractSubBenchmarks(b, func(b *testing.B, req *http.Request) { | ||||
| 		var propagator propagators.TraceContext | ||||
| 		var propagator propagation.TraceContext | ||||
| 		ctx := context.Background() | ||||
| 		b.ResetTimer() | ||||
| 		for i := 0; i < b.N; i++ { | ||||
| @@ -12,15 +12,15 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators_test | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"go.opentelemetry.io/otel/global" | ||||
| 	"go.opentelemetry.io/otel/propagators" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| ) | ||||
| 
 | ||||
| func ExampleTraceContext() { | ||||
| 	tc := propagators.TraceContext{} | ||||
| 	tc := propagation.TraceContext{} | ||||
| 	// Register the TraceContext propagator globally. | ||||
| 	global.SetTextMapPropagator(tc) | ||||
| } | ||||
| @@ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
| 
 | ||||
| package propagators_test | ||||
| package propagation_test | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| @@ -22,12 +22,12 @@ import ( | ||||
| 	"github.com/google/go-cmp/cmp" | ||||
| 
 | ||||
| 	"go.opentelemetry.io/otel/oteltest" | ||||
| 	"go.opentelemetry.io/otel/propagators" | ||||
| 	"go.opentelemetry.io/otel/propagation" | ||||
| 	"go.opentelemetry.io/otel/trace" | ||||
| ) | ||||
| 
 | ||||
| func TestExtractValidTraceContextFromHTTPReq(t *testing.T) { | ||||
| 	prop := propagators.TraceContext{} | ||||
| 	prop := propagation.TraceContext{} | ||||
| 	tests := []struct { | ||||
| 		name   string | ||||
| 		header string | ||||
| @@ -122,7 +122,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) { | ||||
| 
 | ||||
| func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) { | ||||
| 	wantSc := trace.SpanContext{} | ||||
| 	prop := propagators.TraceContext{} | ||||
| 	prop := propagation.TraceContext{} | ||||
| 	tests := []struct { | ||||
| 		name   string | ||||
| 		header string | ||||
| @@ -210,7 +210,7 @@ func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) { | ||||
| 
 | ||||
| func TestInjectTraceContextToHTTPReq(t *testing.T) { | ||||
| 	mockTracer := oteltest.DefaultTracer() | ||||
| 	prop := propagators.TraceContext{} | ||||
| 	prop := propagation.TraceContext{} | ||||
| 	tests := []struct { | ||||
| 		name       string | ||||
| 		sc         trace.SpanContext | ||||
| @@ -267,7 +267,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { | ||||
| } | ||||
| 
 | ||||
| func TestTraceContextPropagator_GetAllKeys(t *testing.T) { | ||||
| 	var propagator propagators.TraceContext | ||||
| 	var propagator propagation.TraceContext | ||||
| 	want := []string{"traceparent", "tracestate"} | ||||
| 	got := propagator.Fields() | ||||
| 	if diff := cmp.Diff(got, want); diff != "" { | ||||
| @@ -276,7 +276,7 @@ func TestTraceContextPropagator_GetAllKeys(t *testing.T) { | ||||
| } | ||||
| 
 | ||||
| func TestTraceStatePropagation(t *testing.T) { | ||||
| 	prop := propagators.TraceContext{} | ||||
| 	prop := propagation.TraceContext{} | ||||
| 	want := "opaquevalue" | ||||
| 	headerName := "tracestate" | ||||
| 
 | ||||
		Reference in New Issue
	
	Block a user