Skip to content

Commit

Permalink
rename structs and update doc comments..
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia committed Aug 26, 2019
1 parent a579ac4 commit 91bcbd8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
22 changes: 14 additions & 8 deletions api/propagation/propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ import (
"go.opentelemetry.io/api/tag"
)

// TextFormatPropagator is an interface that specifies methods to create Injector
// and Extractor objects. Injector object implements Inject method to inject
// SpanContext and tag.Map as a text format into carrier like HTTP request. Similarly,
// Extractor object implements Extract method to extract SpanContext encoded in text
// format from a carrier like HTTP request.
// TextFormatPropagator is an interface that specifies methods to create CarrierInjector
// and CarrierExtractor objects. These methods bind given carrier to the created object.
// CarrierInjector object implements Inject method to inject
// SpanContext and tag.Map as a text format into carrier like HTTP request.
// Similarly, CarrierExtractor object implements Extract method to extract SpanContext
// encoded in text format from a carrier like HTTP request.
// Typically, a plugin for transport like HTTP uses this interface to allow user
// to configure appropriate propagators.
// to configure appropriate text format propagators.
type TextFormatPropagator interface {
Extractor(carrier interface{}) Extractor
Injector(carrier interface{}) Injector
// CarrierExtractor creates an object that implements Extractor interface.
// It binds provided carrier to the object.
CarrierExtractor(carrier interface{}) Extractor

// CarrierInjector creates an object that implements Injector interface.
// It binds provided carrier to the object.
CarrierInjector(carrier interface{}) Injector
}

type Injector interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,57 @@ type httpTraceContextPropagator struct{}

var _ apipropagation.TextFormatPropagator = httpTraceContextPropagator{}

// Extractor implements Extractor method of TextFormatPropagator interface.
// CarrierExtractor implements CarrierExtractor method of TextFormatPropagator interface.
//
// It creates Extractor object and binds carrier to the object. The carrier
// It creates CarrierExtractor object and binds carrier to the object. The carrier
// is expected to be *http.Request. If the carrier type is not *http.Request
// then an empty extractor is returned which will extract nothing.
func (t httpTraceContextPropagator) Extractor(carrier interface{}) apipropagation.Extractor {
// then an empty extractor. Extract method on empty extractor does nothing.
func (hp httpTraceContextPropagator) CarrierExtractor(carrier interface{}) apipropagation.Extractor {
req, ok := carrier.(*http.Request)
if ok {
return textFormatExtractor{req: req}
return traceContextExtractor{req: req}
}
return textFormatExtractor{}
return traceContextExtractor{}
}

// Injector implements Injector method of TextFormatPropagator interface.
// CarrierInjector implements CarrierInjector method of TextFormatPropagator interface.
//
// It creates Injector object and binds carrier to the object. The carrier
// It creates CarrierInjector object and binds carrier to the object. The carrier
// is expected to be of type *http.Request. If the carrier type is not *http.Request
// then an empty injector is returned which will inject nothing.
func (t httpTraceContextPropagator) Injector(carrier interface{}) apipropagation.Injector {
// then an empty injector is returned. Inject method on empty injector does nothing.
func (hp httpTraceContextPropagator) CarrierInjector(carrier interface{}) apipropagation.Injector {
req, ok := carrier.(*http.Request)
if ok {
return textFormatInjector{req: req}
return traceContextInjector{req: req}
}
return textFormatInjector{}
return traceContextInjector{}
}

// HttpTraceContextPropagator creates a new propagator that propagates SpanContext
// in W3C TraceContext format.
//
// The propagator is then used to create Injector and Extractor associated with a
// The propagator is then used to create CarrierInjector and CarrierExtractor associated with a
// specific request. Injectors and Extractors respectively provides method to
// inject and extract SpanContext into/from the http request. Inject method encodes
// SpanContext into W3C TraceContext Header and injects the header in the request.
// Extract extracts the header and decodes SpanContext.
// Extract method extracts the header and decodes SpanContext.
func HttpTraceContextPropagator() httpTraceContextPropagator {
return httpTraceContextPropagator{}
}

type textFormatExtractor struct {
type traceContextExtractor struct {
req *http.Request
}

var _ apipropagation.Extractor = textFormatExtractor{}
var _ apipropagation.Extractor = traceContextExtractor{}

// Extract implements Extract method of trace.Extractor interface. It extracts
// Extract implements Extract method of propagation.Extractor interface. It extracts
// W3C TraceContext Header and decodes SpanContext from the Header.
func (tfe textFormatExtractor) Extract() (sc core.SpanContext, tm tag.Map) {
if tfe.req == nil {
func (tce traceContextExtractor) Extract() (sc core.SpanContext, tm tag.Map) {
if tce.req == nil {
return core.EmptySpanContext(), tag.NewEmptyMap()
}
h, ok := getRequestHeader(tfe.req, traceparentHeader, false)
h, ok := getRequestHeader(tce.req, traceparentHeader, false)
if !ok {
return core.EmptySpanContext(), tag.NewEmptyMap()
}
Expand Down Expand Up @@ -153,17 +153,17 @@ func (tfe textFormatExtractor) Extract() (sc core.SpanContext, tm tag.Map) {
return sc, tag.NewEmptyMap()
}

type textFormatInjector struct {
type traceContextInjector struct {
req *http.Request
}

var _ apipropagation.Injector = textFormatInjector{}
var _ apipropagation.Injector = traceContextInjector{}

// Inject implements Inject method of propagation.Injector interface. It encodes
// SpanContext into W3C TraceContext Header and injects the header into
// the associated request.
func (tfi textFormatInjector) Inject(sc core.SpanContext, tm tag.Map) {
if tfi.req == nil {
func (tci traceContextInjector) Inject(sc core.SpanContext, tm tag.Map) {
if tci.req == nil {
return
}
if sc.IsValid() {
Expand All @@ -173,7 +173,7 @@ func (tfi textFormatInjector) Inject(sc core.SpanContext, tm tag.Map) {
sc.TraceID.Low,
sc.SpanID,
sc.TraceOptions)
tfi.req.Header.Set(traceparentHeader, h)
tci.req.Header.Set(traceparentHeader, h)
}
// TODO: [rghetia] add tag.Map (distributed context) injection
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestExtractTraceContextFromHTTPReq(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("traceparent", tt.header)
e := propagator.Extractor(req)
e := propagator.CarrierExtractor(req)

gotSc, _ := e.Extract()
if diff := cmp.Diff(gotSc, tt.wantSc); diff != "" {
Expand All @@ -102,7 +102,7 @@ func TestExtractTraceContextFromInvalidCarrier(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := propagator.Extractor(tt.header)
e := propagator.CarrierExtractor(tt.header)
gotSc, _ := e.Extract()
if diff := cmp.Diff(gotSc, tt.wantSc); diff != "" {
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, diff)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
i := propagator.Injector(req)
i := propagator.CarrierInjector(req)
i.Inject(tt.sc, tag.NewEmptyMap())

gotHeader := req.Header.Get("traceparent")
Expand All @@ -162,7 +162,7 @@ func TestInjectTraceContextToInvalidCarrier(t *testing.T) {
sc core.SpanContext
}{
{
name: "valid spancontext to invalid carrier",
name: "valid spancontext to invalid carrier does nothing.",
sc: core.SpanContext{
TraceID: traceID,
SpanID: spanID,
Expand All @@ -172,7 +172,7 @@ func TestInjectTraceContextToInvalidCarrier(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := propagator.Injector("")
i := propagator.CarrierInjector("")
i.Inject(tt.sc, tag.NewEmptyMap())
})
}
Expand Down

0 comments on commit 91bcbd8

Please sign in to comment.