Skip to content

Commit

Permalink
Move baggage and propagation to separate packages (open-telemetry#1325)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
krnowak and MrAlias authored Nov 13, 2020
1 parent f6df5df commit 63a1114
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 119 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions baggage.go → baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
}
34 changes: 17 additions & 17 deletions baggage_test.go → baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package otel
package baggage

import (
"context"
Expand All @@ -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")
Expand Down
11 changes: 5 additions & 6 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -294,7 +293,7 @@ type BridgeTracer struct {
warningHandler BridgeWarningHandler
warnOnce sync.Once

propagator otel.TextMapPropagator
propagator propagation.TextMapPropagator
}

var _ ot.Tracer = &BridgeTracer{}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand All @@ -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()
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 12 additions & 12 deletions global/internal/propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions global/internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -33,7 +33,7 @@ type (
}

propagatorsHolder struct {
tm otel.TextMapPropagator
tm propagation.TextMapPropagator
}
)

Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 63a1114

Please sign in to comment.