From 91bcbd8b7b8ad0b1bab3cd48d7398d01708141c4 Mon Sep 17 00:00:00 2001 From: rahulpa Date: Sun, 25 Aug 2019 23:31:52 -0700 Subject: [PATCH] rename structs and update doc comments.. --- api/propagation/propagator.go | 22 +++++--- ...or.go => http_trace_context_propagator.go} | 50 +++++++++---------- ... => http_trace_context_propagator_test.go} | 10 ++-- 3 files changed, 44 insertions(+), 38 deletions(-) rename propagation/{http_textformat_propagator.go => http_trace_context_propagator.go} (75%) rename propagation/{http_textformat_propagator_test.go => http_trace_context_propagator_test.go} (95%) diff --git a/api/propagation/propagator.go b/api/propagation/propagator.go index 07ac781050af..0dac763c695a 100644 --- a/api/propagation/propagator.go +++ b/api/propagation/propagator.go @@ -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 { diff --git a/propagation/http_textformat_propagator.go b/propagation/http_trace_context_propagator.go similarity index 75% rename from propagation/http_textformat_propagator.go rename to propagation/http_trace_context_propagator.go index fcd95206f4e9..24372ccf70d2 100644 --- a/propagation/http_textformat_propagator.go +++ b/propagation/http_trace_context_propagator.go @@ -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() } @@ -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() { @@ -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 } diff --git a/propagation/http_textformat_propagator_test.go b/propagation/http_trace_context_propagator_test.go similarity index 95% rename from propagation/http_textformat_propagator_test.go rename to propagation/http_trace_context_propagator_test.go index 943e9b850746..36653dccbc6a 100644 --- a/propagation/http_textformat_propagator_test.go +++ b/propagation/http_trace_context_propagator_test.go @@ -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 != "" { @@ -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) @@ -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") @@ -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, @@ -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()) }) }