Repository: open-telemetry/opentelemetry-go
Pull request #379 (2019-12-10 02:30:11)

Rename CurrentSpan to SpanFromContext & SetCurrentSpan to ContextWith…
#185 

Signed-off-by: vineeth <vineethpothulapati@outlook.com>


Patch:
diff --git a/api/propagators/b3_propagator.go b/api/propagators/b3_propagator.go
index 4753603f..b608be9e 100644
--- a/api/propagators/b3_propagator.go
+++ b/api/propagators/b3_propagator.go
@@ -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
diff --git a/api/propagators/trace_context_propagator.go b/api/propagators/trace_context_propagator.go
index 91440bcc..75e88b51 100644
--- a/api/propagators/trace_context_propagator.go
+++ b/api/propagators/trace_context_propagator.go
@@ -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,
diff --git a/api/trace/current.go b/api/trace/current.go
index abadaa4f..c1ad829d 100644
--- a/api/trace/current.go
+++ b/api/trace/current.go
@@ -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
 	}
diff --git a/api/trace/noop_trace.go b/api/trace/noop_trace.go
index aecc1da0..d8af3235 100644
--- a/api/trace/noop_trace.go
+++ b/api/trace/noop_trace.go
@@ -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
 }
diff --git a/bridge/opentracing/internal/mock.go b/bridge/opentracing/internal/mock.go
index 741b1d4c..5392c4c3 100644
--- a/bridge/opentracing/internal/mock.go
+++ b/bridge/opentracing/internal/mock.go
@@ -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
@@ -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()
diff --git a/bridge/opentracing/migration/api.go b/bridge/opentracing/migration/api.go
index f71669f2..5b0bcad9 100644
--- a/bridge/opentracing/migration/api.go
+++ b/bridge/opentracing/migration/api.go
@@ -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
diff --git a/bridge/opentracing/wrapper.go b/bridge/opentracing/wrapper.go
index 21799307..085065cc 100644
--- a/bridge/opentracing/wrapper.go
+++ b/bridge/opentracing/wrapper.go
@@ -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)
 		}
@@ -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
 }
diff --git a/example/basic/main.go b/example/basic/main.go
index 4d1c57de..7950e802 100644
--- a/example/basic/main.go
+++ b/example/basic/main.go
@@ -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)
 
@@ -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)
 
diff --git a/example/grpc/middleware/tracing/tracing.go b/example/grpc/middleware/tracing/tracing.go
index 055e9191..b737773e 100644
--- a/example/grpc/middleware/tracing/tracing.go
+++ b/example/grpc/middleware/tracing/tracing.go
@@ -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)
 	}
 }
diff --git a/example/http-stackdriver/client/client.go b/example/http-stackdriver/client/client.go
index 98672330..1cb7d13f 100644
--- a/example/http-stackdriver/client/client.go
+++ b/example/http-stackdriver/client/client.go
@@ -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
 		})
diff --git a/example/http/client/client.go b/example/http/client/client.go
index 63aaaf24..726ae860 100644
--- a/example/http/client/client.go
+++ b/example/http/client/client.go
@@ -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
 		})
diff --git a/example/namedtracer/foo/foo.go b/example/namedtracer/foo/foo.go
index 0d9b5afe..9e360b04 100644
--- a/example/namedtracer/foo/foo.go
+++ b/example/namedtracer/foo/foo.go
@@ -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
 		},
diff --git a/example/namedtracer/main.go b/example/namedtracer/main.go
index 038f6ee5..a2c1f2c4 100644
--- a/example/namedtracer/main.go
+++ b/example/namedtracer/main.go
@@ -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)
 	})
diff --git a/internal/trace/mock_tracer.go b/internal/trace/mock_tracer.go
index 2dd8c696..d6d033d0 100644
--- a/internal/trace/mock_tracer.go
+++ b/internal/trace/mock_tracer.go
@@ -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
 }
diff --git a/plugin/othttp/handler.go b/plugin/othttp/handler.go
index 14325e9b..c3583abe 100644
--- a/plugin/othttp/handler.go
+++ b/plugin/othttp/handler.go
@@ -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)))
 	})
 }
diff --git a/sdk/trace/tracer.go b/sdk/trace/tracer.go
index e47b1160..0832d392 100644
--- a/sdk/trace/tracer.go
+++ b/sdk/trace/tracer.go
@@ -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
@@ -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 {


Test patch:
diff --git a/api/testharness/harness.go b/api/testharness/harness.go
index d943d0aa..e82c1cc1 100644
--- a/api/testharness/harness.go
+++ b/api/testharness/harness.go
@@ -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) {
@@ -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
 			})
@@ -242,7 +242,7 @@ 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
 			})
@@ -250,7 +250,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
 			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
 			})
@@ -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
 			})
@@ -332,7 +332,7 @@ func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
 				return nil
 			})
 
-			return trace.CurrentSpan(actualCtx)
+			return trace.SpanFromContext(actualCtx)
 		},
 	}
 
diff --git a/api/trace/current_test.go b/api/trace/current_test.go
index 76be78b0..0bf2f772 100644
--- a/api/trace/current_test.go
+++ b/api/trace/current_test.go
@@ -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)
 	}
 }
@@ -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)
 			}
diff --git a/api/trace/testtrace/tracer.go b/api/trace/testtrace/tracer.go
index f9eca61b..deb3221b 100644
--- a/api/trace/testtrace/tracer.go
+++ b/api/trace/testtrace/tracer.go
@@ -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 {
@@ -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 {
diff --git a/api/trace/testtrace/tracer_test.go b/api/trace/testtrace/tracer_test.go
index ad2c110b..3c7907fd 100644
--- a/api/trace/testtrace/tracer_test.go
+++ b/api/trace/testtrace/tracer_test.go
@@ -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
 			})
diff --git a/bridge/opentracing/mix_test.go b/bridge/opentracing/mix_test.go
index 35fae1be..ec1ec9fa 100644
--- a/bridge/opentracing/mix_test.go
+++ b/bridge/opentracing/mix_test.go
@@ -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{}
@@ -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())
 }
 
diff --git a/plugin/othttp/handler_example_test.go b/plugin/othttp/handler_example_test.go
index a6846e26..c41300bb 100644
--- a/plugin/othttp/handler_example_test.go
+++ b/plugin/othttp/handler_example_test.go
@@ -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
 	}
@@ -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:])