1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-17 01:12:45 +02:00

Rename CurrentSpan to SpanFromContext & SetCurrentSpan to ContextWithSpan (#379)

Signed-off-by: vineeth <vineethpothulapati@outlook.com>
This commit is contained in:
Vineeth Pothulapati
2019-12-11 22:21:32 +05:30
committed by rghetia
parent 658dd7d1b9
commit b863b8f6ab
22 changed files with 45 additions and 45 deletions

View File

@ -53,7 +53,7 @@ type B3 struct {
var _ TextFormat = B3{} var _ TextFormat = B3{}
func (b3 B3) Inject(ctx context.Context, supplier Supplier) { func (b3 B3) Inject(ctx context.Context, supplier Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext() sc := trace.SpanFromContext(ctx).SpanContext()
if sc.IsValid() { if sc.IsValid() {
if b3.SingleHeader { if b3.SingleHeader {
sampled := sc.TraceFlags & core.TraceFlagsSampled sampled := sc.TraceFlags & core.TraceFlagsSampled

View File

@ -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}-?") 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) { func (hp TraceContext) Inject(ctx context.Context, supplier Supplier) {
sc := trace.CurrentSpan(ctx).SpanContext() sc := trace.SpanFromContext(ctx).SpanContext()
if sc.IsValid() { if sc.IsValid() {
h := fmt.Sprintf("%.2x-%s-%.16x-%.2x", h := fmt.Sprintf("%.2x-%s-%.16x-%.2x",
supportedVersion, supportedVersion,

View File

@ -79,7 +79,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
e.Expect(span).NotToBeNil() e.Expect(span).NotToBeNil()
e.Expect(span.SpanContext()).NotToEqual(core.EmptySpanContext()) 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) { 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 var span trace.Span
err := subject.WithSpan(context.Background(), "test", func(ctx context.Context) error { err := subject.WithSpan(context.Background(), "test", func(ctx context.Context) error {
span = trace.CurrentSpan(ctx) span = trace.SpanFromContext(ctx)
return nil return nil
}) })
@ -242,7 +242,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
var span2 trace.Span var span2 trace.Span
err := subject.WithSpan(context.Background(), "span1", func(ctx context.Context) error { err := subject.WithSpan(context.Background(), "span1", func(ctx context.Context) error {
span1 = trace.CurrentSpan(ctx) span1 = trace.SpanFromContext(ctx)
return nil return nil
}) })
@ -250,7 +250,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
e.Expect(err).ToBeNil() e.Expect(err).ToBeNil()
err = subject.WithSpan(context.Background(), "span2", func(ctx context.Context) error { err = subject.WithSpan(context.Background(), "span2", func(ctx context.Context) error {
span2 = trace.CurrentSpan(ctx) span2 = trace.SpanFromContext(ctx)
return nil return nil
}) })
@ -275,7 +275,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
var child trace.Span var child trace.Span
err := subject.WithSpan(ctx, "child", func(ctx context.Context) error { err := subject.WithSpan(ctx, "child", func(ctx context.Context) error {
child = trace.CurrentSpan(ctx) child = trace.SpanFromContext(ctx)
return nil return nil
}) })
@ -332,7 +332,7 @@ func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
return nil return nil
}) })
return trace.CurrentSpan(actualCtx) return trace.SpanFromContext(actualCtx)
}, },
} }

View File

@ -24,11 +24,11 @@ var (
currentSpanKey = &currentSpanKeyType{} 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) 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 { if span, has := ctx.Value(currentSpanKey).(Span); has {
return span return span
} }

View File

@ -17,10 +17,10 @@ func TestSetCurrentSpanOverridesPreviouslySetSpan(t *testing.T) {
ctx := context.Background() ctx := context.Background()
ctx = trace.SetCurrentSpan(ctx, originalSpan) ctx = trace.ContextWithSpan(ctx, originalSpan)
ctx = trace.SetCurrentSpan(ctx, expectedSpan) 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) 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", name: "CurrentSpan() returns current span if set",
ctx: trace.SetCurrentSpan(context.Background(), mockSpan{}), ctx: trace.ContextWithSpan(context.Background(), mockSpan{}),
want: mockSpan{}, want: mockSpan{},
}, },
} { } {
t.Run(testcase.name, func(t *testing.T) { t.Run(testcase.name, func(t *testing.T) {
// proto: CurrentSpan(ctx context.Context) trace.Span // proto: CurrentSpan(ctx context.Context) trace.Span
have := trace.CurrentSpan(testcase.ctx) have := trace.SpanFromContext(testcase.ctx)
if have != testcase.want { if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have) t.Errorf("Want: %v, but have: %v", testcase.want, have)
} }

View File

@ -30,5 +30,5 @@ func (t NoopTracer) WithSpan(ctx context.Context, name string, body func(context
// Start starts a noop span. // Start starts a noop span.
func (NoopTracer) Start(ctx context.Context, name string, opts ...StartOption) (context.Context, Span) { func (NoopTracer) Start(ctx context.Context, name string, opts ...StartOption) (context.Context, Span) {
span := NoopSpan{} span := NoopSpan{}
return SetCurrentSpan(ctx, span), span return ContextWithSpan(ctx, span), span
} }

View File

@ -55,7 +55,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.StartOpti
if parentSpanContext := c.Relation.SpanContext; parentSpanContext.IsValid() { if parentSpanContext := c.Relation.SpanContext; parentSpanContext.IsValid() {
traceID = parentSpanContext.TraceID traceID = parentSpanContext.TraceID
parentSpanID = parentSpanContext.SpanID parentSpanID = parentSpanContext.SpanID
} else if parentSpanContext := trace.CurrentSpan(ctx).SpanContext(); parentSpanContext.IsValid() { } else if parentSpanContext := trace.SpanFromContext(ctx).SpanContext(); parentSpanContext.IsValid() {
traceID = parentSpanContext.TraceID traceID = parentSpanContext.TraceID
parentSpanID = parentSpanContext.SpanID parentSpanID = parentSpanContext.SpanID
} else { } else {
@ -96,7 +96,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.StartOpti
t.lock.Unlock() 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 { func (t *Tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {

View File

@ -199,7 +199,7 @@ func TestTracer(t *testing.T) {
var span trace.Span var span trace.Span
err := tracer.WithSpan(context.Background(), name, func(ctx context.Context) error { err := tracer.WithSpan(context.Background(), name, func(ctx context.Context) error {
span = trace.CurrentSpan(ctx) span = trace.SpanFromContext(ctx)
return nil return nil
}) })

View File

@ -104,7 +104,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S
SpanKind: oteltrace.ValidateSpanKind(spanOpts.SpanKind), SpanKind: oteltrace.ValidateSpanKind(spanOpts.SpanKind),
} }
if !migration.SkipContextSetup(ctx) { if !migration.SkipContextSetup(ctx) {
ctx = oteltrace.SetCurrentSpan(ctx, span) ctx = oteltrace.ContextWithSpan(ctx, span)
ctx = t.addSpareContextValue(ctx) ctx = t.addSpareContextValue(ctx)
} }
return ctx, span return ctx, span
@ -150,7 +150,7 @@ func (t *MockTracer) getParentSpanContext(ctx context.Context, spanOpts *oteltra
spanOpts.Relation.SpanContext.IsValid() { spanOpts.Relation.SpanContext.IsValid() {
return spanOpts.Relation.SpanContext return spanOpts.Relation.SpanContext
} }
if parentSpanContext := oteltrace.CurrentSpan(ctx).SpanContext(); parentSpanContext.IsValid() { if parentSpanContext := oteltrace.SpanFromContext(ctx).SpanContext(); parentSpanContext.IsValid() {
return parentSpanContext return parentSpanContext
} }
return otelcore.EmptySpanContext() return otelcore.EmptySpanContext()

View File

@ -56,7 +56,7 @@ type DeferredContextSetupTracerExtension interface {
// OpenTelemetry Span object and have WrapperTracer to alter the // OpenTelemetry Span object and have WrapperTracer to alter the
// current OpenTelemetry span in the context so it points to the // current OpenTelemetry span in the context so it points to the
// wrapped object, so the code in the tracer like // 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 // argument for getting rid of this interface is that is only called
// by the WrapperTracer - WrapperTracer likely shouldn't require any // by the WrapperTracer - WrapperTracer likely shouldn't require any
// changes in the underlying OpenTelemetry tracer to have things // changes in the underlying OpenTelemetry tracer to have things

View File

@ -215,7 +215,7 @@ func (cast *currentActiveSpanTest) runOTOtelOT(t *testing.T, ctx context.Context
} }
func (cast *currentActiveSpanTest) recordSpans(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) cast.recordedCurrentOtelSpanIDs = append(cast.recordedCurrentOtelSpanIDs, spanID)
spanID = otelcore.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()) tm.recordedOTSpanTracers = append(tm.recordedOTSpanTracers, otSpan.Tracer())
} }
otelSpan := oteltrace.CurrentSpan(ctx) otelSpan := oteltrace.SpanFromContext(ctx)
tm.recordedOtelSpanTracers = append(tm.recordedOtelSpanTracers, otelSpan.Tracer()) tm.recordedOtelSpanTracers = append(tm.recordedOtelSpanTracers, otelSpan.Tracer())
} }

View File

@ -76,7 +76,7 @@ func (t *WrapperTracer) otelTracer() oteltrace.Tracer {
// calling the original callback. // calling the original callback.
func (t *WrapperTracer) WithSpan(ctx context.Context, name string, body func(context.Context) error) error { 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 { 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 { if spanWithExtension, ok := span.(migration.OverrideTracerSpanExtension); ok {
spanWithExtension.OverrideTracer(t) spanWithExtension.OverrideTracer(t)
} }
@ -107,6 +107,6 @@ func (t *WrapperTracer) DeferredContextSetupHook(ctx context.Context, span otelt
if tracerWithExtension, ok := t.otelTracer().(migration.DeferredContextSetupTracerExtension); ok { if tracerWithExtension, ok := t.otelTracer().(migration.DeferredContextSetupTracerExtension); ok {
ctx = tracerWithExtension.DeferredContextSetupHook(ctx, span) ctx = tracerWithExtension.DeferredContextSetupHook(ctx, span)
} }
ctx = oteltrace.SetCurrentSpan(ctx, span) ctx = oteltrace.ContextWithSpan(ctx, span)
return ctx return ctx
} }

View File

@ -107,9 +107,9 @@ func main() {
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error { 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) gauge.Set(ctx, 1)
@ -126,9 +126,9 @@ func main() {
ctx, ctx,
"Sub operation...", "Sub operation...",
func(ctx context.Context) error { 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) measure.Record(ctx, 1.3)

View File

@ -82,8 +82,8 @@ func UnaryClientInterceptor(ctx context.Context, method string, req, reply inter
func setTraceStatus(ctx context.Context, err error) { func setTraceStatus(ctx context.Context, err error) {
if err != nil { if err != nil {
s, _ := status.FromError(err) s, _ := status.FromError(err)
trace.CurrentSpan(ctx).SetStatus(s.Code()) trace.SpanFromContext(ctx).SetStatus(s.Code())
} else { } else {
trace.CurrentSpan(ctx).SetStatus(codes.OK) trace.SpanFromContext(ctx).SetStatus(codes.OK)
} }
} }

View File

@ -82,7 +82,7 @@ func main() {
} }
body, err = ioutil.ReadAll(res.Body) body, err = ioutil.ReadAll(res.Body)
_ = res.Body.Close() _ = res.Body.Close()
trace.CurrentSpan(ctx).SetStatus(codes.OK) trace.SpanFromContext(ctx).SetStatus(codes.OK)
return err return err
}) })

View File

@ -77,7 +77,7 @@ func main() {
} }
body, err = ioutil.ReadAll(res.Body) body, err = ioutil.ReadAll(res.Body)
_ = res.Body.Close() _ = res.Body.Close()
trace.CurrentSpan(ctx).SetStatus(codes.OK) trace.SpanFromContext(ctx).SetStatus(codes.OK)
return err return err
}) })

View File

@ -37,9 +37,9 @@ func SubOperation(ctx context.Context) error {
ctx, ctx,
"Sub operation...", "Sub operation...",
func(ctx context.Context) error { 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 return nil
}, },

View File

@ -66,9 +66,9 @@ func main() {
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error { 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) return foo.SubOperation(ctx)
}) })

View File

@ -70,5 +70,5 @@ func (mt *MockTracer) Start(ctx context.Context, name string, o ...apitrace.Star
tracer: mt, tracer: mt,
} }
return apitrace.SetCurrentSpan(ctx, span), span return apitrace.ContextWithSpan(ctx, span), span
} }

View File

@ -220,9 +220,9 @@ func setAfterServeAttributes(span trace.Span, read, wrote, statusCode int64, rer
// RouteKey Tag. // RouteKey Tag.
func WithRouteTag(route string, h http.Handler) http.Handler { func WithRouteTag(route string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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? //TODO: Why doesn't tag.Upsert work?
span.SetAttributes(RouteKey.String(route)) 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)))
}) })
} }

View File

@ -65,7 +65,7 @@ func ExampleNewHandler() {
case "": case "":
err = fmt.Errorf("expected /hello/:name in %q", s) err = fmt.Errorf("expected /hello/:name in %q", s)
default: 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 return pp[1], err
} }
@ -77,7 +77,7 @@ func ExampleNewHandler() {
ctx := r.Context() ctx := r.Context()
var name string var name string
// Wrap another function in it's own span // 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 { func(ctx context.Context) error {
var err error var err error
name, err = figureOutName(ctx, r.URL.Path[1:]) name, err = figureOutName(ctx, r.URL.Path[1:])

View File

@ -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` // e.g., adding a `Link` instead of setting the `parent`
} }
} else { } else {
if p := apitrace.CurrentSpan(ctx); p != nil { if p := apitrace.SpanFromContext(ctx); p != nil {
if sdkSpan, ok := p.(*span); ok { if sdkSpan, ok := p.(*span); ok {
sdkSpan.addChild() sdkSpan.addChild()
parent = sdkSpan.spanContext parent = sdkSpan.spanContext
@ -74,7 +74,7 @@ func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.StartOpt
ctx, end := startExecutionTracerTask(ctx, spanName) ctx, end := startExecutionTracerTask(ctx, spanName)
span.executionTracerTaskEnd = end 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 { func (tr *tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {