Skip to content

Commit

Permalink
move inject/extract out of trace.
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia committed Aug 13, 2019
1 parent 5df48a5 commit 74ff726
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 82 deletions.
50 changes: 50 additions & 0 deletions api/propagation/propagator.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 0 additions & 29 deletions api/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions api/trace/noop_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
2 changes: 1 addition & 1 deletion example/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions experimental/streaming/sdk/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions plugin/httptrace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions propagation/http_textformat_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions propagation/propagator.go

This file was deleted.

4 changes: 0 additions & 4 deletions sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 74ff726

Please sign in to comment.