You've already forked opentelemetry-go
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:
committed by
rghetia
parent
658dd7d1b9
commit
b863b8f6ab
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -24,11 +24,11 @@ var (
|
||||
currentSpanKey = ¤tSpanKeyType{}
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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())
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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
|
||||
},
|
||||
|
@ -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)
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)))
|
||||
})
|
||||
}
|
||||
|
@ -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:])
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user