diff --git a/api/propagation/noop_propagator.go b/api/propagation/noop_propagator.go new file mode 100644 index 000000000000..802064b7ec48 --- /dev/null +++ b/api/propagation/noop_propagator.go @@ -0,0 +1,53 @@ +// Copyright 2019, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package propagation + +import ( + "go.opentelemetry.io/api/core" + "go.opentelemetry.io/api/tag" +) + +// NoopTextFormatPropagator implements TextFormatPropagator that does nothing. +type NoopTextFormatPropagator struct{} + +var _ TextFormatPropagator = NoopTextFormatPropagator{} + +// CarrierExtractor returns NoopExtractor +func (ntp NoopTextFormatPropagator) CarrierExtractor(carrier interface{}) Extractor { + return NoopExtractor{} +} + +// CarrierInjector returns NoopInjector +func (ntp NoopTextFormatPropagator) CarrierInjector(carrier interface{}) Injector { + return NoopInjector{} +} + +// NoopInjector implements Injector interface that does nothing. +type NoopInjector struct{} + +var _ Injector = NoopInjector{} + +func (ni NoopInjector) Inject(sc core.SpanContext, tm tag.Map) { +} + +// NoopExtractor implements Extractor interface that does nothing. +type NoopExtractor struct{} + +var _ Extractor = NoopExtractor{} + +// Extract method always returns Invalid SpanContext and empty tag.Map +func (ne NoopExtractor) Extract() (core.SpanContext, tag.Map) { + return core.EmptySpanContext(), tag.NewEmptyMap() +} diff --git a/propagation/http_trace_context_propagator.go b/propagation/http_trace_context_propagator.go index 4077edf6906c..cef014703610 100644 --- a/propagation/http_trace_context_propagator.go +++ b/propagation/http_trace_context_propagator.go @@ -40,27 +40,27 @@ var _ apipropagation.TextFormatPropagator = httpTraceContextPropagator{} // CarrierExtractor implements TextFormatPropagator interface. // // 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. Extract method on empty extractor does nothing. +// is expected to be *http.Request. If the carrier is nil or its type is not *http.Request +// then a NoopExtractor is returned. func (hp httpTraceContextPropagator) CarrierExtractor(carrier interface{}) apipropagation.Extractor { - req, ok := carrier.(*http.Request) - if ok { + req, _ := carrier.(*http.Request) + if req != nil { return traceContextExtractor{req: req} } - return traceContextExtractor{} + return apipropagation.NoopExtractor{} } // CarrierInjector implements TextFormatPropagator interface. // // 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. Inject method on empty injector does nothing. +// is expected to be of type *http.Request. If the carrier is nil or its type is not *http.Request +// then a NoopInjector is returned. func (hp httpTraceContextPropagator) CarrierInjector(carrier interface{}) apipropagation.Injector { - req, ok := carrier.(*http.Request) - if ok { + req, _ := carrier.(*http.Request) + if req != nil { return traceContextInjector{req: req} } - return traceContextInjector{} + return apipropagation.NoopInjector{} } // HttpTraceContextPropagator creates a new text format propagator that propagates SpanContext