Skip to content

Commit

Permalink
Reorganize propagation code (shrink PR 381) (#444)
Browse files Browse the repository at this point in the history
* Rename distributedcontext package to correlation

Correlation is the name we agreed upon.

* Move trace propagators to api/trace

The trace propagators tests had to be moved to a testtrace subpackage
to avoid import cycles between api/trace and internal/trace.

Needed to shut up golint about stutter in trace.TraceContext -
TraceContext is a name of a W3C spec, so this stutter is
expected. It's certainly still better than golint's suggestion of
having trace.Context.

* Rename api/propagators to api/propagation

This package will not contain any propagators in the long run, just
the interface definitions.

Co-authored-by: Joshua MacDonald <[email protected]>
  • Loading branch information
2 people authored and rghetia committed Jan 28, 2020
1 parent 437d9af commit 6b4acf4
Show file tree
Hide file tree
Showing 30 changed files with 271 additions and 312 deletions.
31 changes: 31 additions & 0 deletions api/correlation/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package correlation

import (
"context"

"go.opentelemetry.io/otel/api/core"
)

type correlationsType struct{}

var correlationsKey = &correlationsType{}

// WithMap enters a Map into a new Context.
func WithMap(ctx context.Context, m Map) context.Context {
return context.WithValue(ctx, correlationsKey, m)
}

// WithMap enters a key:value set into a new Context.
func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context {
return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{
MultiKV: keyvalues,
}))
}

// FromContext gets the current Map from a Context.
func FromContext(ctx context.Context) Map {
if m, ok := ctx.Value(correlationsKey).(Map); ok {
return m
}
return NewEmptyMap()
}
4 changes: 3 additions & 1 deletion api/distributedcontext/map.go → api/correlation/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package distributedcontext
package correlation

import (
"go.opentelemetry.io/otel/api/core"
)

// TODO Comments needed! This was formerly known as distributedcontext.Map

type entry struct {
value core.Value
}
Expand Down
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 distributedcontext
package correlation

import (
"fmt"
Expand Down
56 changes: 0 additions & 56 deletions api/distributedcontext/context.go

This file was deleted.

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 propagators
package propagation

import (
"go.opentelemetry.io/otel/api/core"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagators_test
package propagation_test

import (
"testing"

"github.com/google/go-cmp/cmp"

"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/propagators"
"go.opentelemetry.io/otel/api/propagation"
)

func TestExtractSpanContextFromBytes(t *testing.T) {
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")

propagator := propagators.Binary()
propagator := propagation.Binary()
tests := []struct {
name string
bytes []byte
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestConvertSpanContextToBytes(t *testing.T) {
traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7")

propagator := propagators.Binary()
propagator := propagation.Binary()
tests := []struct {
name string
sc core.SpanContext
Expand Down
4 changes: 3 additions & 1 deletion api/distributedcontext/doc.go → api/propagation/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package distributedcontext // import "go.opentelemetry.io/otel/api/distributedcontext"
// Package propagation contains interface definition for BinaryFormat and
// TextFormat propagators.
package propagation // import "go.opentelemetry.io/otel/api/propagation"
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagators
package propagation

import (
"context"

"go.opentelemetry.io/otel/api/core"
dctx "go.opentelemetry.io/otel/api/distributedcontext"
"go.opentelemetry.io/otel/api/correlation"
)

// NoopTextFormat implements TextFormat that does nothing.
Expand All @@ -31,8 +31,8 @@ func (np NoopTextFormat) Inject(ctx context.Context, supplier Supplier) {
}

// Extract does nothing and returns an empty SpanContext
func (np NoopTextFormat) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map) {
return core.EmptySpanContext(), dctx.NewEmptyMap()
func (np NoopTextFormat) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, correlation.Map) {
return core.EmptySpanContext(), correlation.NewEmptyMap()
}

// GetAllKeys returns empty list of strings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagators
package propagation

import (
"context"

"go.opentelemetry.io/otel/api/core"
dctx "go.opentelemetry.io/otel/api/distributedcontext"
"go.opentelemetry.io/otel/api/correlation"
)

// TextFormat is an interface that specifies methods to inject and extract SpanContext
Expand All @@ -33,10 +33,10 @@ type TextFormat interface {
Inject(ctx context.Context, supplier Supplier)

// Extract method retrieves encoded SpanContext using supplier from the associated carrier.
// It decodes the SpanContext and returns it and a dctx of correlated context.
// It decodes the SpanContext and returns it and a baggage of correlated context.
// If no SpanContext was retrieved OR if the retrieved SpanContext is invalid then
// an empty SpanContext is returned.
Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map)
Extract(ctx context.Context, supplier Supplier) (core.SpanContext, correlation.Map)

// GetAllKeys returns all the keys that this propagator injects/extracts into/from a
// carrier. The use cases for this are
Expand Down
18 changes: 0 additions & 18 deletions api/propagators/doc.go

This file was deleted.

22 changes: 11 additions & 11 deletions api/propagators/b3_propagator.go → api/trace/b3_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagators
package trace

import (
"context"
"fmt"
"strings"

"go.opentelemetry.io/otel/api/core"
dctx "go.opentelemetry.io/otel/api/distributedcontext"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/propagation"
)

const (
Expand Down Expand Up @@ -50,10 +50,10 @@ type B3 struct {
SingleHeader bool
}

var _ TextFormat = B3{}
var _ propagation.TextFormat = B3{}

func (b3 B3) Inject(ctx context.Context, supplier Supplier) {
sc := trace.SpanFromContext(ctx).SpanContext()
func (b3 B3) Inject(ctx context.Context, supplier propagation.Supplier) {
sc := SpanFromContext(ctx).SpanContext()
if sc.IsValid() {
if b3.SingleHeader {
sampled := sc.TraceFlags & core.TraceFlagsSampled
Expand All @@ -76,11 +76,11 @@ func (b3 B3) Inject(ctx context.Context, supplier Supplier) {
}

// Extract retrieves B3 Headers from the supplier
func (b3 B3) Extract(ctx context.Context, supplier Supplier) (core.SpanContext, dctx.Map) {
func (b3 B3) Extract(ctx context.Context, supplier propagation.Supplier) (core.SpanContext, correlation.Map) {
if b3.SingleHeader {
return b3.extractSingleHeader(supplier), dctx.NewEmptyMap()
return b3.extractSingleHeader(supplier), correlation.NewEmptyMap()
}
return b3.extract(supplier), dctx.NewEmptyMap()
return b3.extract(supplier), correlation.NewEmptyMap()
}

func (b3 B3) GetAllKeys() []string {
Expand All @@ -90,7 +90,7 @@ func (b3 B3) GetAllKeys() []string {
return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader}
}

func (b3 B3) extract(supplier Supplier) core.SpanContext {
func (b3 B3) extract(supplier propagation.Supplier) core.SpanContext {
tid, err := core.TraceIDFromHex(supplier.Get(B3TraceIDHeader))
if err != nil {
return core.EmptySpanContext()
Expand Down Expand Up @@ -125,7 +125,7 @@ func (b3 B3) extract(supplier Supplier) core.SpanContext {
return sc
}

func (b3 B3) extractSingleHeader(supplier Supplier) core.SpanContext {
func (b3 B3) extractSingleHeader(supplier propagation.Supplier) core.SpanContext {
h := supplier.Get(B3SingleHeader)
if h == "" || h == "0" {
core.EmptySpanContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagators_test
package testtrace_test

import (
"context"
"net/http"
"testing"

"go.opentelemetry.io/otel/api/propagators"
"go.opentelemetry.io/otel/api/trace"
mocktrace "go.opentelemetry.io/otel/internal/trace"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func BenchmarkExtractB3(b *testing.B) {
}

for _, tg := range testGroup {
propagator := propagators.B3{SingleHeader: tg.singleHeader}
propagator := trace.B3{SingleHeader: tg.singleHeader}
for _, tt := range tg.tests {
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
ctx := context.Background()
Expand Down Expand Up @@ -97,7 +96,7 @@ func BenchmarkInjectB3(b *testing.B) {

for _, tg := range testGroup {
id = 0
propagator := propagators.B3{SingleHeader: tg.singleHeader}
propagator := trace.B3{SingleHeader: tg.singleHeader}
for _, tt := range tg.tests {
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
Expand Down
Loading

0 comments on commit 6b4acf4

Please sign in to comment.