From 94c9d5d568832e0d276b3f52d76e21228bbdaf35 Mon Sep 17 00:00:00 2001 From: rahulpa Date: Tue, 13 Aug 2019 10:28:40 -0700 Subject: [PATCH] move inject/extract out of trace. --- api/propagation/propagator.go | 50 +++++++++++++++++++++++ api/trace/api.go | 29 ------------- api/trace/noop_trace.go | 4 -- example/http/client/client.go | 2 +- experimental/streaming/sdk/trace.go | 5 --- plugin/httptrace/api.go | 4 +- propagation/http_textformat_propagator.go | 12 +++--- propagation/propagator.go | 31 -------------- sdk/trace/tracer.go | 4 -- 9 files changed, 59 insertions(+), 82 deletions(-) create mode 100644 api/propagation/propagator.go delete mode 100644 propagation/propagator.go diff --git a/api/propagation/propagator.go b/api/propagation/propagator.go new file mode 100644 index 000000000000..a29c240e7ed7 --- /dev/null +++ b/api/propagation/propagator.go @@ -0,0 +1,50 @@ +// 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 tracecontext contains HTTP propagator for TraceContext standard. +// See https://github.com/w3c/distributed-tracing for more information. +package propagation // import "go.opentelemetry.io/api/propagation" + +import ( + "net/http" + + "go.opentelemetry.io/api/core" + "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. +// Typically, a plugin for transport like HTTP uses this interface to allow user +// to configure appropriate propagators. +type TextFormatPropagator interface { + Extractor(req *http.Request) Extractor + Injector(req *http.Request) Injector +} + +type Injector interface { + // Inject serializes span context and tag.Map and inserts them in to + // carrier associated with the injector. For example in case of http request, + // span context could be added to the request (carrier) as W3C Trace context header. + Inject(core.SpanContext, tag.Map) +} + +type Extractor interface { + // Extract de-serializes span context and tag.Map from a carrier associated with the + // extractor. For example in case of http request, span context could be extracted + // from the W3C Trace context header. + Extract() (core.SpanContext, tag.Map) +} diff --git a/api/trace/api.go b/api/trace/api.go index 4d831bd948d1..7adfd4a9ba07 100644 --- a/api/trace/api.go +++ b/api/trace/api.go @@ -45,9 +45,6 @@ type Tracer interface { // WithResources attaches resource attributes to the Tracer. WithResources(res ...core.KeyValue) Tracer - - // Note: see https://github.com/opentracing/opentracing-go/issues/127 - Inject(context.Context, Span, Injector) } type Span interface { @@ -83,20 +80,6 @@ type Span interface { ModifyAttributes(...tag.Mutator) } -type Injector interface { - // Inject serializes span context and tag.Map and inserts them in to - // carrier associated with the injector. For example in case of http request, - // span context could added to the request (carrier) as W3C Trace context header. - Inject(core.SpanContext, tag.Map) -} - -type Extractor interface { - // Extract deserializes span context and tag.Map from a carrier associated with the - // extractor. For example in case of http request, span context could be extracted - // from the W3C Trace context header. - Extract() (core.SpanContext, tag.Map) -} - // SpanOption apply changes to SpanOptions. type SpanOption func(*SpanOptions) @@ -129,18 +112,6 @@ func Start(ctx context.Context, name string, opts ...SpanOption) (context.Contex return GlobalTracer().Start(ctx, name, opts...) } -// Inject is convenient function to inject current span context using injector. -// Injector is expected to serialize span context and inject it in to a carrier. -// An example of a carrier is http request. -func Inject(ctx context.Context, injector Injector) { - span := CurrentSpan(ctx) - if span == nil { - return - } - - span.Tracer().Inject(ctx, span, injector) -} - // WithStartTime sets the start time of the span to provided time t, when it is started. // In absensce of this option, wall clock time is used as start time. // This option is typically used when starting of the span is delayed. diff --git a/api/trace/noop_trace.go b/api/trace/noop_trace.go index 8a5902323001..b99c0773d45e 100644 --- a/api/trace/noop_trace.go +++ b/api/trace/noop_trace.go @@ -49,7 +49,3 @@ func (NoopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (c span := NoopSpan{} return SetCurrentSpan(ctx, span), span } - -// Inject does nothing. -func (NoopTracer) Inject(ctx context.Context, span Span, injector Injector) { -} diff --git a/example/http/client/client.go b/example/http/client/client.go index 0dc8e0ea8b9a..5b61a815f959 100644 --- a/example/http/client/client.go +++ b/example/http/client/client.go @@ -52,7 +52,7 @@ func main() { ctx, req, inj := httptrace.W3C(ctx, req) - trace.Inject(ctx, inj) + inj.Inject(trace.CurrentSpan(ctx).SpanContext(), nil) res, err := client.Do(req) if err != nil { diff --git a/experimental/streaming/sdk/trace.go b/experimental/streaming/sdk/trace.go index c765e7c2f937..7ac95d38715d 100644 --- a/experimental/streaming/sdk/trace.go +++ b/experimental/streaming/sdk/trace.go @@ -20,7 +20,6 @@ import ( "go.opentelemetry.io/api/core" "go.opentelemetry.io/api/key" - "go.opentelemetry.io/api/tag" "go.opentelemetry.io/api/trace" apitrace "go.opentelemetry.io/api/trace" "go.opentelemetry.io/experimental/streaming/exporter/observer" @@ -127,7 +126,3 @@ func (t *tracer) Start(ctx context.Context, name string, opts ...apitrace.SpanOp } return trace.SetCurrentSpan(ctx, span), span } - -func (t *tracer) Inject(ctx context.Context, span apitrace.Span, injector apitrace.Injector) { - injector.Inject(span.SpanContext(), tag.FromContext(ctx)) -} diff --git a/plugin/httptrace/api.go b/plugin/httptrace/api.go index da3a0d40131d..f1e0a8f4178f 100644 --- a/plugin/httptrace/api.go +++ b/plugin/httptrace/api.go @@ -19,11 +19,11 @@ import ( "net/http" "net/http/httptrace" - "go.opentelemetry.io/api/trace" + "go.opentelemetry.io/api/propagation" ) // Client -func W3C(ctx context.Context, req *http.Request) (context.Context, *http.Request, trace.Injector) { +func W3C(ctx context.Context, req *http.Request) (context.Context, *http.Request, propagation.Injector) { t := newClientTracer(ctx) t.GetConn = t.getConn diff --git a/propagation/http_textformat_propagator.go b/propagation/http_textformat_propagator.go index 234bd1ced4d7..9a92bc6306db 100644 --- a/propagation/http_textformat_propagator.go +++ b/propagation/http_textformat_propagator.go @@ -25,8 +25,8 @@ import ( "strings" "go.opentelemetry.io/api/core" + apipropagation "go.opentelemetry.io/api/propagation" "go.opentelemetry.io/api/tag" - "go.opentelemetry.io/api/trace" ) const ( @@ -37,13 +37,13 @@ const ( type textFormatPropagator struct{} -var _ HTTPPropagator = textFormatPropagator{} +var _ apipropagation.TextFormatPropagator = textFormatPropagator{} -func (t textFormatPropagator) Extractor(req *http.Request) trace.Extractor { +func (t textFormatPropagator) Extractor(req *http.Request) apipropagation.Extractor { return textFormatExtractor{req: req} } -func (t textFormatPropagator) Injector(req *http.Request) trace.Injector { +func (t textFormatPropagator) Injector(req *http.Request) apipropagation.Injector { return textFormatInjector{req: req} } @@ -61,7 +61,7 @@ type textFormatExtractor struct { req *http.Request } -var _ trace.Extractor = textFormatExtractor{} +var _ apipropagation.Extractor = textFormatExtractor{} // Extract implements Extract method of trace.Extractor interface. It extracts // W3C TraceContext Header and decodes SpanContext from the Header. @@ -134,7 +134,7 @@ type textFormatInjector struct { req *http.Request } -var _ trace.Injector = textFormatInjector{} +var _ apipropagation.Injector = textFormatInjector{} // Inject implements Inject method of trace.Injector interface. It encodes // SpanContext into W3C TraceContext Header and injects the header into diff --git a/propagation/propagator.go b/propagation/propagator.go deleted file mode 100644 index 122870a8b27b..000000000000 --- a/propagation/propagator.go +++ /dev/null @@ -1,31 +0,0 @@ -// 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 tracecontext contains HTTP propagator for TraceContext standard. -// See https://github.com/w3c/distributed-tracing for more information. -package propagation // import "go.opentelemetry.io/propagation" - -import ( - "net/http" - - "go.opentelemetry.io/api/trace" -) - -// HTTPPropagator is an interface that specifies methods to create Extractor -// and Injector objects for an http request. Typically, an http plugin uses -// this interface to allow user to configure appropriate propagators. -type HTTPPropagator interface { - Extractor(req *http.Request) trace.Extractor - Injector(req *http.Request) trace.Injector -} diff --git a/sdk/trace/tracer.go b/sdk/trace/tracer.go index 33aa0012967e..5c302abc5fc4 100644 --- a/sdk/trace/tracer.go +++ b/sdk/trace/tracer.go @@ -92,7 +92,3 @@ func (tr *tracer) WithComponent(component string) apitrace.Tracer { tr.component = component return tr } - -func (tr *tracer) Inject(ctx context.Context, span apitrace.Span, injector apitrace.Injector) { - injector.Inject(span.SpanContext(), nil) -}