1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-16 02:47:20 +02:00

Rename CorrelationContext to Baggage (#1142)

* Rename 'correlation' to 'baggage'

* Rename CorrelationContext progator to Baggage

* Update CHANGELOG
This commit is contained in:
Anthony Mirabella 2020-09-09 14:13:37 -04:00 committed by GitHub
parent 9a4981cc67
commit 06689a01bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 89 additions and 87 deletions

View File

@ -30,6 +30,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Rename Jaeger tags used for instrumentation library information to reflect changes in OpenTelemetry specification. (#1119)
- Rename `ProbabilitySampler` to `TraceIDRatioBased` and change semantics to ignore parent span sampling status. (#1115)
- Move `tools` package under `internal`. (#1141)
- Move `go.opentelemetry.io/otel/api/correlation` package to `go.opentelemetry.io/otel/api/baggage`. (#1142)
The `correlation.CorrelationContext` propagator has been renamed `baggage.Baggage`. Other exported functions and types are unchanged.
## [0.11.0] - 2020-08-24

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package correlation
package baggage
import (
"context"
@ -25,27 +25,27 @@ import (
// Temporary header name until W3C finalizes format.
// https://github.com/open-telemetry/opentelemetry-specification/blob/18b2752ebe6c7f0cdd8c7b2bcbdceb0ae3f5ad95/specification/correlationcontext/api.md#header-name
const correlationContextHeader = "otcorrelations"
const baggageHeader = "otcorrelations"
// CorrelationContext propagates Key:Values in W3C CorrelationContext
// Baggage propagates Key:Values in W3C CorrelationContext
// format.
// nolint:golint
type CorrelationContext struct{}
type Baggage struct{}
var _ propagation.HTTPPropagator = CorrelationContext{}
var _ propagation.HTTPPropagator = Baggage{}
// DefaultHTTPPropagator returns the default context correlation HTTP
// propagator.
func DefaultHTTPPropagator() propagation.HTTPPropagator {
return CorrelationContext{}
return Baggage{}
}
// Inject implements HTTPInjector.
func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
correlationCtx := MapFromContext(ctx)
func (b Baggage) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
baggageMap := MapFromContext(ctx)
firstIter := true
var headerValueBuilder strings.Builder
correlationCtx.Foreach(func(kv label.KeyValue) bool {
baggageMap.Foreach(func(kv label.KeyValue) bool {
if !firstIter {
headerValueBuilder.WriteRune(',')
}
@ -57,21 +57,21 @@ func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPS
})
if headerValueBuilder.Len() > 0 {
headerString := headerValueBuilder.String()
supplier.Set(correlationContextHeader, headerString)
supplier.Set(baggageHeader, headerString)
}
}
// Extract implements HTTPExtractor.
func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
correlationContext := supplier.Get(correlationContextHeader)
if correlationContext == "" {
func (b Baggage) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
baggage := supplier.Get(baggageHeader)
if baggage == "" {
return ctx
}
contextValues := strings.Split(correlationContext, ",")
keyValues := make([]label.KeyValue, 0, len(contextValues))
for _, contextValue := range contextValues {
valueAndProps := strings.Split(contextValue, ";")
baggageValues := strings.Split(baggage, ",")
keyValues := make([]label.KeyValue, 0, len(baggageValues))
for _, baggageValue := range baggageValues {
valueAndProps := strings.Split(baggageValue, ";")
if len(valueAndProps) < 1 {
continue
}
@ -113,6 +113,6 @@ func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTP
}
// GetAllKeys implements HTTPPropagator.
func (CorrelationContext) GetAllKeys() []string {
return []string{correlationContextHeader}
func (b Baggage) GetAllKeys() []string {
return []string{baggageHeader}
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package correlation_test
package baggage_test
import (
"context"
@ -22,13 +22,13 @@ import (
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/baggage"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/label"
)
func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
props := propagation.New(propagation.WithExtractors(correlation.CorrelationContext{}))
func TestExtractValidBaggageFromHTTPReq(t *testing.T) {
props := propagation.New(propagation.WithExtractors(baggage.Baggage{}))
tests := []struct {
name string
header string
@ -91,18 +91,18 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
ctx := context.Background()
ctx = propagation.ExtractHTTP(ctx, props, req.Header)
gotCorCtx := correlation.MapFromContext(ctx)
wantCorCtx := correlation.NewMap(correlation.MapUpdate{MultiKV: tt.wantKVs})
if gotCorCtx.Len() != wantCorCtx.Len() {
gotBaggage := baggage.MapFromContext(ctx)
wantBaggage := baggage.NewMap(baggage.MapUpdate{MultiKV: tt.wantKVs})
if gotBaggage.Len() != wantBaggage.Len() {
t.Errorf(
"Got and Want CorCtx are not the same size %d != %d",
gotCorCtx.Len(),
wantCorCtx.Len(),
"Got and Want Baggage are not the same size %d != %d",
gotBaggage.Len(),
wantBaggage.Len(),
)
}
totalDiff := ""
wantCorCtx.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotCorCtx.Value(keyValue.Key)
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotBaggage.Value(keyValue.Key)
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
if diff != "" {
totalDiff += diff + "\n"
@ -117,7 +117,7 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
}
func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
props := propagation.New(propagation.WithExtractors(correlation.CorrelationContext{}))
props := propagation.New(propagation.WithExtractors(baggage.Baggage{}))
tests := []struct {
name string
header string
@ -150,20 +150,20 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("otcorrelations", tt.header)
ctx := correlation.NewContext(context.Background(), tt.hasKVs...)
wantCorCtx := correlation.MapFromContext(ctx)
ctx := baggage.NewContext(context.Background(), tt.hasKVs...)
wantBaggage := baggage.MapFromContext(ctx)
ctx = propagation.ExtractHTTP(ctx, props, req.Header)
gotCorCtx := correlation.MapFromContext(ctx)
if gotCorCtx.Len() != wantCorCtx.Len() {
gotBaggage := baggage.MapFromContext(ctx)
if gotBaggage.Len() != wantBaggage.Len() {
t.Errorf(
"Got and Want CorCtx are not the same size %d != %d",
gotCorCtx.Len(),
wantCorCtx.Len(),
"Got and Want Baggage are not the same size %d != %d",
gotBaggage.Len(),
wantBaggage.Len(),
)
}
totalDiff := ""
wantCorCtx.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotCorCtx.Value(keyValue.Key)
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotBaggage.Value(keyValue.Key)
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
if diff != "" {
totalDiff += diff + "\n"
@ -174,8 +174,8 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
}
}
func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
propagator := correlation.CorrelationContext{}
func TestInjectBaggageToHTTPReq(t *testing.T) {
propagator := baggage.Baggage{}
props := propagation.New(propagation.WithInjectors(propagator))
tests := []struct {
name string
@ -228,7 +228,7 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := correlation.ContextWithMap(context.Background(), correlation.NewMap(correlation.MapUpdate{MultiKV: tt.kvs}))
ctx := baggage.ContextWithMap(context.Background(), baggage.NewMap(baggage.MapUpdate{MultiKV: tt.kvs}))
propagation.InjectHTTP(ctx, props, req.Header)
gotHeader := req.Header.Get("otcorrelations")
@ -250,7 +250,7 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
}
func TestTraceContextPropagator_GetAllKeys(t *testing.T) {
var propagator correlation.CorrelationContext
var propagator baggage.Baggage
want := []string{"otcorrelations"}
got := propagator.GetAllKeys()
if diff := cmp.Diff(got, want); diff != "" {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package correlation
package baggage
import (
"context"

View File

@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Package correlation provides types and utilities for correlation features.
package correlation // import "go.opentelemetry.io/otel/api/correlation"
// Package baggage provides types and utilities for baggage features.
package baggage // import "go.opentelemetry.io/otel/api/baggage"

View File

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

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package correlation
package baggage
import (
"fmt"

View File

@ -18,7 +18,7 @@ import (
"sync"
"sync/atomic"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/baggage"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
@ -121,13 +121,13 @@ func defaultPropagatorsValue() *atomic.Value {
}
// getDefaultPropagators returns a default Propagators, configured
// with W3C trace and correlation context propagation.
// with W3C trace and baggage propagation.
func getDefaultPropagators() propagation.Propagators {
tcPropagator := propagators.TraceContext{}
ccPropagator := correlation.CorrelationContext{}
bagPropagator := baggage.Baggage{}
return propagation.New(
propagation.WithExtractors(tcPropagator, ccPropagator),
propagation.WithInjectors(tcPropagator, ccPropagator),
propagation.WithExtractors(tcPropagator, bagPropagator),
propagation.WithInjectors(tcPropagator, bagPropagator),
)
}

View File

@ -25,7 +25,7 @@ import (
otext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
otelcorrelation "go.opentelemetry.io/otel/api/correlation"
otelbaggage "go.opentelemetry.io/otel/api/baggage"
otelglobal "go.opentelemetry.io/otel/api/global"
otelpropagation "go.opentelemetry.io/otel/api/propagation"
oteltrace "go.opentelemetry.io/otel/api/trace"
@ -38,7 +38,7 @@ import (
)
type bridgeSpanContext struct {
baggageItems otelcorrelation.Map
baggageItems otelbaggage.Map
otelSpanContext oteltrace.SpanContext
}
@ -46,7 +46,7 @@ var _ ot.SpanContext = &bridgeSpanContext{}
func newBridgeSpanContext(otelSpanContext oteltrace.SpanContext, parentOtSpanContext ot.SpanContext) *bridgeSpanContext {
bCtx := &bridgeSpanContext{
baggageItems: otelcorrelation.NewEmptyMap(),
baggageItems: otelbaggage.NewEmptyMap(),
otelSpanContext: otelSpanContext,
}
if parentOtSpanContext != nil {
@ -66,7 +66,7 @@ func (c *bridgeSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {
func (c *bridgeSpanContext) setBaggageItem(restrictedKey, value string) {
crk := http.CanonicalHeaderKey(restrictedKey)
c.baggageItems = c.baggageItems.Apply(otelcorrelation.MapUpdate{SingleKV: label.String(crk, value)})
c.baggageItems = c.baggageItems.Apply(otelbaggage.MapUpdate{SingleKV: label.String(crk, value)})
}
func (c *bridgeSpanContext) baggageItem(restrictedKey string) string {
@ -327,12 +327,12 @@ func (t *BridgeTracer) SetPropagators(propagators otelpropagation.Propagators) {
}
func (t *BridgeTracer) NewHookedContext(ctx context.Context) context.Context {
ctx = otelcorrelation.ContextWithSetHook(ctx, t.correlationSetHook)
ctx = otelcorrelation.ContextWithGetHook(ctx, t.correlationGetHook)
ctx = otelbaggage.ContextWithSetHook(ctx, t.baggageSetHook)
ctx = otelbaggage.ContextWithGetHook(ctx, t.baggageGetHook)
return ctx
}
func (t *BridgeTracer) correlationSetHook(ctx context.Context) context.Context {
func (t *BridgeTracer) baggageSetHook(ctx context.Context) context.Context {
span := ot.SpanFromContext(ctx)
if span == nil {
t.warningHandler("No active OpenTracing span, can not propagate the baggage items from OpenTelemetry context\n")
@ -346,8 +346,8 @@ func (t *BridgeTracer) correlationSetHook(ctx context.Context) context.Context {
// we clear the context only to avoid calling a get hook
// during MapFromContext, but otherwise we don't change the
// context, so we don't care about the old hooks.
clearCtx, _, _ := otelcorrelation.ContextWithNoHooks(ctx)
m := otelcorrelation.MapFromContext(clearCtx)
clearCtx, _, _ := otelbaggage.ContextWithNoHooks(ctx)
m := otelbaggage.MapFromContext(clearCtx)
m.Foreach(func(kv label.KeyValue) bool {
bSpan.setBaggageItemOnly(string(kv.Key), kv.Value.Emit())
return true
@ -355,7 +355,7 @@ func (t *BridgeTracer) correlationSetHook(ctx context.Context) context.Context {
return ctx
}
func (t *BridgeTracer) correlationGetHook(ctx context.Context, m otelcorrelation.Map) otelcorrelation.Map {
func (t *BridgeTracer) baggageGetHook(ctx context.Context, m otelbaggage.Map) otelbaggage.Map {
span := ot.SpanFromContext(ctx)
if span == nil {
t.warningHandler("No active OpenTracing span, can not propagate the baggage items from OpenTracing span context\n")
@ -374,7 +374,7 @@ func (t *BridgeTracer) correlationGetHook(ctx context.Context, m otelcorrelation
for k, v := range items {
kv = append(kv, label.String(k, v))
}
return m.Apply(otelcorrelation.MapUpdate{MultiKV: kv})
return m.Apply(otelbaggage.MapUpdate{MultiKV: kv})
}
// StartSpan is a part of the implementation of the OpenTracing Tracer
@ -613,7 +613,7 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int
sc: bridgeSC.otelSpanContext,
}
ctx := oteltrace.ContextWithSpan(context.Background(), fs)
ctx = otelcorrelation.ContextWithMap(ctx, bridgeSC.baggageItems)
ctx = otelbaggage.ContextWithMap(ctx, bridgeSC.baggageItems)
otelpropagation.InjectHTTP(ctx, t.getPropagators(), header)
return nil
}
@ -632,7 +632,7 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span
}
header := http.Header(hhcarrier)
ctx := otelpropagation.ExtractHTTP(context.Background(), t.getPropagators(), header)
baggage := otelcorrelation.MapFromContext(ctx)
baggage := otelbaggage.MapFromContext(ctx)
otelSC, _, _ := otelparent.GetSpanContextAndLinks(ctx, false)
bridgeSC := &bridgeSpanContext{
baggageItems: baggage,

View File

@ -21,7 +21,7 @@ import (
"sync"
"time"
otelcorrelation "go.opentelemetry.io/otel/api/correlation"
otelbaggage "go.opentelemetry.io/otel/api/baggage"
oteltrace "go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/codes"
otelparent "go.opentelemetry.io/otel/internal/trace/parent"
@ -45,7 +45,7 @@ type MockContextKeyValue struct {
}
type MockTracer struct {
Resources otelcorrelation.Map
Resources otelbaggage.Map
FinishedSpans []*MockSpan
SpareTraceIDs []oteltrace.ID
SpareSpanIDs []oteltrace.SpanID
@ -60,7 +60,7 @@ var _ migration.DeferredContextSetupTracerExtension = &MockTracer{}
func NewMockTracer() *MockTracer {
return &MockTracer{
Resources: otelcorrelation.NewEmptyMap(),
Resources: otelbaggage.NewEmptyMap(),
FinishedSpans: nil,
SpareTraceIDs: nil,
SpareSpanIDs: nil,
@ -86,7 +86,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S
officialTracer: t,
spanContext: spanContext,
recording: config.Record,
Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{
Attributes: otelbaggage.NewMap(otelbaggage.MapUpdate{
MultiKV: config.Attributes,
}),
StartTime: startTime,
@ -179,10 +179,10 @@ func (t *MockTracer) DeferredContextSetupHook(ctx context.Context, span oteltrac
}
type MockEvent struct {
CtxAttributes otelcorrelation.Map
CtxAttributes otelbaggage.Map
Timestamp time.Time
Name string
Attributes otelcorrelation.Map
Attributes otelbaggage.Map
}
type MockSpan struct {
@ -192,7 +192,7 @@ type MockSpan struct {
SpanKind oteltrace.SpanKind
recording bool
Attributes otelcorrelation.Map
Attributes otelbaggage.Map
StartTime time.Time
EndTime time.Time
ParentSpanID oteltrace.SpanID
@ -223,7 +223,7 @@ func (s *MockSpan) SetError(v bool) {
}
func (s *MockSpan) SetAttributes(attributes ...label.KeyValue) {
s.applyUpdate(otelcorrelation.MapUpdate{
s.applyUpdate(otelbaggage.MapUpdate{
MultiKV: attributes,
})
}
@ -232,7 +232,7 @@ func (s *MockSpan) SetAttribute(k string, v interface{}) {
s.SetAttributes(label.Any(k, v))
}
func (s *MockSpan) applyUpdate(update otelcorrelation.MapUpdate) {
func (s *MockSpan) applyUpdate(update otelbaggage.MapUpdate) {
s.Attributes = s.Attributes.Apply(update)
}
@ -288,10 +288,10 @@ func (s *MockSpan) AddEvent(ctx context.Context, name string, attrs ...label.Key
func (s *MockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...label.KeyValue) {
s.Events = append(s.Events, MockEvent{
CtxAttributes: otelcorrelation.MapFromContext(ctx),
CtxAttributes: otelbaggage.MapFromContext(ctx),
Timestamp: timestamp,
Name: name,
Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{
Attributes: otelbaggage.NewMap(otelbaggage.MapUpdate{
MultiKV: attrs,
}),
})

View File

@ -21,7 +21,7 @@ import (
ot "github.com/opentracing/opentracing-go"
otelcorrelation "go.opentelemetry.io/otel/api/correlation"
otelbaggage "go.opentelemetry.io/otel/api/baggage"
otelglobal "go.opentelemetry.io/otel/api/global"
oteltrace "go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/label"
@ -589,7 +589,7 @@ func (bio *baggageInteroperationTest) addAndRecordBaggage(t *testing.T, ctx cont
value := bio.baggageItems[idx].value
otSpan.SetBaggageItem(otKey, value)
ctx = otelcorrelation.NewContext(ctx, label.String(otelKey, value))
ctx = otelbaggage.NewContext(ctx, label.String(otelKey, value))
otRecording := make(map[string]string)
otSpan.Context().ForeachBaggageItem(func(key, value string) bool {
@ -597,7 +597,7 @@ func (bio *baggageInteroperationTest) addAndRecordBaggage(t *testing.T, ctx cont
return true
})
otelRecording := make(map[string]string)
otelcorrelation.MapFromContext(ctx).Foreach(func(kv label.KeyValue) bool {
otelbaggage.MapFromContext(ctx).Foreach(func(kv label.KeyValue) bool {
otelRecording[string(kv.Key)] = kv.Value.Emit()
return true
})

View File

@ -18,7 +18,7 @@ import (
"context"
"log"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/baggage"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/api/trace"
@ -59,7 +59,7 @@ func main() {
ctx := context.Background()
ctx = correlation.NewContext(ctx,
ctx = baggage.NewContext(ctx,
fooKey.String("foo1"),
barKey.String("bar1"),
)
@ -77,7 +77,7 @@ func main() {
meter.RecordBatch(
// Note: call-site variables added as context Entries:
correlation.NewContext(ctx, anotherKey.String("xyz")),
baggage.NewContext(ctx, anotherKey.String("xyz")),
commonLabels,
valuerecorderTwo.Measurement(2.0),

View File

@ -18,7 +18,7 @@ import (
"context"
"log"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/baggage"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/example/namedtracer/foo"
@ -62,7 +62,7 @@ func main() {
tracer := tp.Tracer("example/namedtracer/main")
ctx := context.Background()
ctx = correlation.NewContext(ctx,
ctx = baggage.NewContext(ctx,
fooKey.String("foo1"),
barKey.String("bar1"),
)