Skip to content

Commit

Permalink
Merge propagation and api/propagation into api/propagators (#362)
Browse files Browse the repository at this point in the history
* Merge propagation

Rename and merge propagation and api/propagation
to api/propagators.

Drop propagator suffix in general such that
TextFormatPropagator becomes TextFormat since
usage is propagators.TextFormat

fixes #311

* Rebase and godoc updates

* Revert go mod changes

* Replace carrier with supplier in godoc
  • Loading branch information
ferhatelmas authored and rghetia committed Dec 3, 2019
1 parent d35ea75 commit ab164e6
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 250 deletions.
27 changes: 12 additions & 15 deletions propagation/b3_propagator.go → api/propagators/b3_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package propagation
package propagators

import (
"context"
"fmt"
"strings"

"go.opentelemetry.io/otel/api/trace"

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

const (
Expand All @@ -35,8 +33,7 @@ const (
B3ParentSpanIDHeader = "X-B3-ParentSpanId"
)

// B3Propagator that facilitates core.SpanContext
// propagation using B3 Headers.
// B3 propagator serializes core.SpanContext to/from B3 Headers.
// This propagator supports both version of B3 headers,
// 1. Single Header :
// X-B3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
Expand All @@ -49,13 +46,13 @@ const (
//
// If SingleHeader is set to true then X-B3 header is used to inject and extract. Otherwise,
// separate headers are used to inject and extract.
type B3Propagator struct {
type B3 struct {
SingleHeader bool
}

var _ apipropagation.TextFormatPropagator = B3Propagator{}
var _ TextFormat = B3{}

func (b3 B3Propagator) Inject(ctx context.Context, supplier apipropagation.Supplier) {
func (b3 B3) Inject(ctx context.Context, supplier Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext()
if sc.IsValid() {
if b3.SingleHeader {
Expand All @@ -79,21 +76,21 @@ func (b3 B3Propagator) Inject(ctx context.Context, supplier apipropagation.Suppl
}

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

func (b3 B3Propagator) GetAllKeys() []string {
func (b3 B3) GetAllKeys() []string {
if b3.SingleHeader {
return []string{B3SingleHeader}
}
return []string{B3TraceIDHeader, B3SpanIDHeader, B3SampledHeader}
}

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

func (b3 B3Propagator) extractSingleHeader(supplier apipropagation.Supplier) core.SpanContext {
func (b3 B3) extractSingleHeader(supplier Supplier) core.SpanContext {
h := supplier.Get(B3SingleHeader)
if h == "" || h == "0" {
core.EmptySpanContext()
Expand Down Expand Up @@ -177,7 +174,7 @@ func (b3 B3Propagator) extractSingleHeader(supplier apipropagation.Supplier) cor
}

// extractSampledState parses the value of the X-B3-Sampled b3Header.
func (b3 B3Propagator) extractSampledState(sampled string) (flag byte, ok bool) {
func (b3 B3) extractSampledState(sampled string) (flag byte, ok bool) {
switch sampled {
case "", "0":
return 0, true
Expand All @@ -196,7 +193,7 @@ func (b3 B3Propagator) extractSampledState(sampled string) (flag byte, ok bool)
}

// extracDebugFlag parses the value of the X-B3-Sampled b3Header.
func (b3 B3Propagator) extracDebugFlag(debug string) (flag byte, ok bool) {
func (b3 B3) extracDebugFlag(debug string) (flag byte, ok bool) {
switch debug {
case "", "0":
return 0, true
Expand Down
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 propagation_test
package propagators_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"
"go.opentelemetry.io/otel/propagation"
)

func BenchmarkExtractB3(b *testing.B) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func BenchmarkExtractB3(b *testing.B) {
}

for _, tg := range testGroup {
propagator := propagation.B3Propagator{tg.singleHeader}
propagator := propagators.B3{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 +97,7 @@ func BenchmarkInjectB3(b *testing.B) {

for _, tg := range testGroup {
id = 0
propagator := propagation.B3Propagator{tg.singleHeader}
propagator := propagators.B3{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 ab164e6

Please sign in to comment.