Skip to content

Commit

Permalink
Rename CurrentSpan to SpanFromContext & SetCurrentSpan to ContextWith…
Browse files Browse the repository at this point in the history
…Span (#379)

Signed-off-by: vineeth <[email protected]>
  • Loading branch information
VineethReddy02 authored and rghetia committed Dec 11, 2019
1 parent 658dd7d commit b863b8f
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion api/propagators/b3_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type B3 struct {
var _ TextFormat = B3{}

func (b3 B3) Inject(ctx context.Context, supplier Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext()
sc := trace.SpanFromContext(ctx).SpanContext()
if sc.IsValid() {
if b3.SingleHeader {
sampled := sc.TraceFlags & core.TraceFlagsSampled
Expand Down
2 changes: 1 addition & 1 deletion api/propagators/trace_context_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ TextFormat = TraceContext{}
var traceCtxRegExp = regexp.MustCompile("^[0-9a-f]{2}-[a-f0-9]{32}-[a-f0-9]{16}-[a-f0-9]{2}-?")

func (hp TraceContext) Inject(ctx context.Context, supplier Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext()
sc := trace.SpanFromContext(ctx).SpanContext()
if sc.IsValid() {
h := fmt.Sprintf("%.2x-%s-%.16x-%.2x",
supportedVersion,
Expand Down
12 changes: 6 additions & 6 deletions api/testharness/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {

e.Expect(span).NotToBeNil()
e.Expect(span.SpanContext()).NotToEqual(core.EmptySpanContext())
e.Expect(trace.CurrentSpan(ctx)).ToEqual(span)
e.Expect(trace.SpanFromContext(ctx)).ToEqual(span)
})

t.Run("starts spans with unique trace and span IDs", func(t *testing.T) {
Expand Down Expand Up @@ -219,7 +219,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
var span trace.Span

err := subject.WithSpan(context.Background(), "test", func(ctx context.Context) error {
span = trace.CurrentSpan(ctx)
span = trace.SpanFromContext(ctx)

return nil
})
Expand All @@ -242,15 +242,15 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
var span2 trace.Span

err := subject.WithSpan(context.Background(), "span1", func(ctx context.Context) error {
span1 = trace.CurrentSpan(ctx)
span1 = trace.SpanFromContext(ctx)

return nil
})

e.Expect(err).ToBeNil()

err = subject.WithSpan(context.Background(), "span2", func(ctx context.Context) error {
span2 = trace.CurrentSpan(ctx)
span2 = trace.SpanFromContext(ctx)

return nil
})
Expand All @@ -275,7 +275,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
var child trace.Span

err := subject.WithSpan(ctx, "child", func(ctx context.Context) error {
child = trace.CurrentSpan(ctx)
child = trace.SpanFromContext(ctx)

return nil
})
Expand Down Expand Up @@ -332,7 +332,7 @@ func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
return nil
})

return trace.CurrentSpan(actualCtx)
return trace.SpanFromContext(actualCtx)
},
}

Expand Down
4 changes: 2 additions & 2 deletions api/trace/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var (
currentSpanKey = &currentSpanKeyType{}
)

func SetCurrentSpan(ctx context.Context, span Span) context.Context {
func ContextWithSpan(ctx context.Context, span Span) context.Context {
return context.WithValue(ctx, currentSpanKey, span)
}

func CurrentSpan(ctx context.Context) Span {
func SpanFromContext(ctx context.Context) Span {
if span, has := ctx.Value(currentSpanKey).(Span); has {
return span
}
Expand Down
10 changes: 5 additions & 5 deletions api/trace/current_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestSetCurrentSpanOverridesPreviouslySetSpan(t *testing.T) {

ctx := context.Background()

ctx = trace.SetCurrentSpan(ctx, originalSpan)
ctx = trace.SetCurrentSpan(ctx, expectedSpan)
ctx = trace.ContextWithSpan(ctx, originalSpan)
ctx = trace.ContextWithSpan(ctx, expectedSpan)

if span := trace.CurrentSpan(ctx); span != expectedSpan {
if span := trace.SpanFromContext(ctx); span != expectedSpan {
t.Errorf("Want: %v, but have: %v", expectedSpan, span)
}
}
Expand All @@ -38,13 +38,13 @@ func TestCurrentSpan(t *testing.T) {
},
{
name: "CurrentSpan() returns current span if set",
ctx: trace.SetCurrentSpan(context.Background(), mockSpan{}),
ctx: trace.ContextWithSpan(context.Background(), mockSpan{}),
want: mockSpan{},
},
} {
t.Run(testcase.name, func(t *testing.T) {
// proto: CurrentSpan(ctx context.Context) trace.Span
have := trace.CurrentSpan(testcase.ctx)
have := trace.SpanFromContext(testcase.ctx)
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
Expand Down
2 changes: 1 addition & 1 deletion api/trace/noop_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func (t NoopTracer) WithSpan(ctx context.Context, name string, body func(context
// Start starts a noop span.
func (NoopTracer) Start(ctx context.Context, name string, opts ...StartOption) (context.Context, Span) {
span := NoopSpan{}
return SetCurrentSpan(ctx, span), span
return ContextWithSpan(ctx, span), span
}
4 changes: 2 additions & 2 deletions api/trace/testtrace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.StartOpti
if parentSpanContext := c.Relation.SpanContext; parentSpanContext.IsValid() {
traceID = parentSpanContext.TraceID
parentSpanID = parentSpanContext.SpanID
} else if parentSpanContext := trace.CurrentSpan(ctx).SpanContext(); parentSpanContext.IsValid() {
} else if parentSpanContext := trace.SpanFromContext(ctx).SpanContext(); parentSpanContext.IsValid() {
traceID = parentSpanContext.TraceID
parentSpanID = parentSpanContext.SpanID
} else {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.StartOpti

t.lock.Unlock()

return trace.SetCurrentSpan(ctx, span), span
return trace.ContextWithSpan(ctx, span), span
}

func (t *Tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {
Expand Down
2 changes: 1 addition & 1 deletion api/trace/testtrace/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestTracer(t *testing.T) {
var span trace.Span

err := tracer.WithSpan(context.Background(), name, func(ctx context.Context) error {
span = trace.CurrentSpan(ctx)
span = trace.SpanFromContext(ctx)

return nil
})
Expand Down
4 changes: 2 additions & 2 deletions bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S
SpanKind: oteltrace.ValidateSpanKind(spanOpts.SpanKind),
}
if !migration.SkipContextSetup(ctx) {
ctx = oteltrace.SetCurrentSpan(ctx, span)
ctx = oteltrace.ContextWithSpan(ctx, span)
ctx = t.addSpareContextValue(ctx)
}
return ctx, span
Expand Down Expand Up @@ -150,7 +150,7 @@ func (t *MockTracer) getParentSpanContext(ctx context.Context, spanOpts *oteltra
spanOpts.Relation.SpanContext.IsValid() {
return spanOpts.Relation.SpanContext
}
if parentSpanContext := oteltrace.CurrentSpan(ctx).SpanContext(); parentSpanContext.IsValid() {
if parentSpanContext := oteltrace.SpanFromContext(ctx).SpanContext(); parentSpanContext.IsValid() {
return parentSpanContext
}
return otelcore.EmptySpanContext()
Expand Down
2 changes: 1 addition & 1 deletion bridge/opentracing/migration/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type DeferredContextSetupTracerExtension interface {
// OpenTelemetry Span object and have WrapperTracer to alter the
// current OpenTelemetry span in the context so it points to the
// wrapped object, so the code in the tracer like
// `trace.CurrentSpan().(*realSpan)` would still work. Another
// `trace.SpanFromContent().(*realSpan)` would still work. Another
// argument for getting rid of this interface is that is only called
// by the WrapperTracer - WrapperTracer likely shouldn't require any
// changes in the underlying OpenTelemetry tracer to have things
Expand Down
4 changes: 2 additions & 2 deletions bridge/opentracing/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (cast *currentActiveSpanTest) runOTOtelOT(t *testing.T, ctx context.Context
}

func (cast *currentActiveSpanTest) recordSpans(t *testing.T, ctx context.Context) {
spanID := oteltrace.CurrentSpan(ctx).SpanContext().SpanID
spanID := oteltrace.SpanFromContext(ctx).SpanContext().SpanID
cast.recordedCurrentOtelSpanIDs = append(cast.recordedCurrentOtelSpanIDs, spanID)

spanID = otelcore.SpanID{}
Expand Down Expand Up @@ -458,7 +458,7 @@ func (tm *tracerMessTest) recordTracers(t *testing.T, ctx context.Context) {
tm.recordedOTSpanTracers = append(tm.recordedOTSpanTracers, otSpan.Tracer())
}

otelSpan := oteltrace.CurrentSpan(ctx)
otelSpan := oteltrace.SpanFromContext(ctx)
tm.recordedOtelSpanTracers = append(tm.recordedOtelSpanTracers, otelSpan.Tracer())
}

Expand Down
4 changes: 2 additions & 2 deletions bridge/opentracing/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (t *WrapperTracer) otelTracer() oteltrace.Tracer {
// calling the original callback.
func (t *WrapperTracer) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
return t.otelTracer().WithSpan(ctx, name, func(ctx context.Context) error {
span := oteltrace.CurrentSpan(ctx)
span := oteltrace.SpanFromContext(ctx)
if spanWithExtension, ok := span.(migration.OverrideTracerSpanExtension); ok {
spanWithExtension.OverrideTracer(t)
}
Expand Down Expand Up @@ -107,6 +107,6 @@ func (t *WrapperTracer) DeferredContextSetupHook(ctx context.Context, span otelt
if tracerWithExtension, ok := t.otelTracer().(migration.DeferredContextSetupTracerExtension); ok {
ctx = tracerWithExtension.DeferredContextSetupHook(ctx, span)
}
ctx = oteltrace.SetCurrentSpan(ctx, span)
ctx = oteltrace.ContextWithSpan(ctx, span)
return ctx
}
8 changes: 4 additions & 4 deletions example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func main() {

err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {

trace.CurrentSpan(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))
trace.SpanFromContext(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))

trace.CurrentSpan(ctx).SetAttributes(anotherKey.String("yes"))
trace.SpanFromContext(ctx).SetAttributes(anotherKey.String("yes"))

gauge.Set(ctx, 1)

Expand All @@ -126,9 +126,9 @@ func main() {
ctx,
"Sub operation...",
func(ctx context.Context) error {
trace.CurrentSpan(ctx).SetAttributes(lemonsKey.String("five"))
trace.SpanFromContext(ctx).SetAttributes(lemonsKey.String("five"))

trace.CurrentSpan(ctx).AddEvent(ctx, "Sub span event")
trace.SpanFromContext(ctx).AddEvent(ctx, "Sub span event")

measure.Record(ctx, 1.3)

Expand Down
4 changes: 2 additions & 2 deletions example/grpc/middleware/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func UnaryClientInterceptor(ctx context.Context, method string, req, reply inter
func setTraceStatus(ctx context.Context, err error) {
if err != nil {
s, _ := status.FromError(err)
trace.CurrentSpan(ctx).SetStatus(s.Code())
trace.SpanFromContext(ctx).SetStatus(s.Code())
} else {
trace.CurrentSpan(ctx).SetStatus(codes.OK)
trace.SpanFromContext(ctx).SetStatus(codes.OK)
}
}
2 changes: 1 addition & 1 deletion example/http-stackdriver/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
}
body, err = ioutil.ReadAll(res.Body)
_ = res.Body.Close()
trace.CurrentSpan(ctx).SetStatus(codes.OK)
trace.SpanFromContext(ctx).SetStatus(codes.OK)

return err
})
Expand Down
2 changes: 1 addition & 1 deletion example/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
}
body, err = ioutil.ReadAll(res.Body)
_ = res.Body.Close()
trace.CurrentSpan(ctx).SetStatus(codes.OK)
trace.SpanFromContext(ctx).SetStatus(codes.OK)

return err
})
Expand Down
4 changes: 2 additions & 2 deletions example/namedtracer/foo/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func SubOperation(ctx context.Context) error {
ctx,
"Sub operation...",
func(ctx context.Context) error {
trace.CurrentSpan(ctx).SetAttributes(lemonsKey.String("five"))
trace.SpanFromContext(ctx).SetAttributes(lemonsKey.String("five"))

trace.CurrentSpan(ctx).AddEvent(ctx, "Sub span event")
trace.SpanFromContext(ctx).AddEvent(ctx, "Sub span event")

return nil
},
Expand Down
4 changes: 2 additions & 2 deletions example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func main() {

err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {

trace.CurrentSpan(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))
trace.SpanFromContext(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))

trace.CurrentSpan(ctx).SetAttributes(anotherKey.String("yes"))
trace.SpanFromContext(ctx).SetAttributes(anotherKey.String("yes"))

return foo.SubOperation(ctx)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/trace/mock_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ func (mt *MockTracer) Start(ctx context.Context, name string, o ...apitrace.Star
tracer: mt,
}

return apitrace.SetCurrentSpan(ctx, span), span
return apitrace.ContextWithSpan(ctx, span), span
}
4 changes: 2 additions & 2 deletions plugin/othttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ func setAfterServeAttributes(span trace.Span, read, wrote, statusCode int64, rer
// RouteKey Tag.
func WithRouteTag(route string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := trace.CurrentSpan(r.Context())
span := trace.SpanFromContext(r.Context())
//TODO: Why doesn't tag.Upsert work?
span.SetAttributes(RouteKey.String(route))
h.ServeHTTP(w, r.WithContext(trace.SetCurrentSpan(r.Context(), span)))
h.ServeHTTP(w, r.WithContext(trace.ContextWithSpan(r.Context(), span)))
})
}
4 changes: 2 additions & 2 deletions plugin/othttp/handler_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ExampleNewHandler() {
case "":
err = fmt.Errorf("expected /hello/:name in %q", s)
default:
trace.CurrentSpan(ctx).SetAttributes(core.Key("name").String(pp[1]))
trace.SpanFromContext(ctx).SetAttributes(core.Key("name").String(pp[1]))
}
return pp[1], err
}
Expand All @@ -77,7 +77,7 @@ func ExampleNewHandler() {
ctx := r.Context()
var name string
// Wrap another function in it's own span
if err := trace.CurrentSpan(ctx).Tracer().WithSpan(ctx, "figureOutName",
if err := trace.SpanFromContext(ctx).Tracer().WithSpan(ctx, "figureOutName",
func(ctx context.Context) error {
var err error
name, err = figureOutName(ctx, r.URL.Path[1:])
Expand Down
4 changes: 2 additions & 2 deletions sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.StartOpt
// e.g., adding a `Link` instead of setting the `parent`
}
} else {
if p := apitrace.CurrentSpan(ctx); p != nil {
if p := apitrace.SpanFromContext(ctx); p != nil {
if sdkSpan, ok := p.(*span); ok {
sdkSpan.addChild()
parent = sdkSpan.spanContext
Expand All @@ -74,7 +74,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.StartOpt

ctx, end := startExecutionTracerTask(ctx, spanName)
span.executionTracerTaskEnd = end
return apitrace.SetCurrentSpan(ctx, span), span
return apitrace.ContextWithSpan(ctx, span), span
}

func (tr *tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {
Expand Down

0 comments on commit b863b8f

Please sign in to comment.