diff --git a/net/ghttp/ghttp_request_param_ctx.go b/net/ghttp/ghttp_request_param_ctx.go index 685cfa909..4d42205c7 100644 --- a/net/ghttp/ghttp_request_param_ctx.go +++ b/net/ghttp/ghttp_request_param_ctx.go @@ -30,8 +30,6 @@ func (r *Request) Context() context.Context { if RequestFromCtx(ctx) == nil { // Inject Request object into context. ctx = context.WithValue(ctx, ctxKeyForRequest, r) - // Add default tracing info if using default tracing provider. - ctx = gctx.WithCtx(ctx) // Update the values of the original HTTP request. *r.Request = *r.Request.WithContext(ctx) } diff --git a/os/gctx/gctx.go b/os/gctx/gctx.go index e0bcb6667..a3117fd78 100644 --- a/os/gctx/gctx.go +++ b/os/gctx/gctx.go @@ -44,15 +44,15 @@ func init() { context.Background(), propagation.MapCarrier(m), ) - initCtx = WithCtx(initCtx) } // New creates and returns a context which contains context id. func New() context.Context { - return WithCtx(context.Background()) + return WithSpan(context.Background(), "gctx.New") } // WithCtx creates and returns a context containing context id upon given parent context `ctx`. +// Deprecated: use WithSpan instead. func WithCtx(ctx context.Context) context.Context { if CtxId(ctx) != "" { return ctx @@ -63,6 +63,20 @@ func WithCtx(ctx context.Context) context.Context { return ctx } +// WithSpan creates and returns a context containing span upon given parent context `ctx`. +func WithSpan(ctx context.Context, spanName string) context.Context { + if CtxId(ctx) != "" { + return ctx + } + if spanName == "" { + spanName = "gctx.WithSpan" + } + var span *gtrace.Span + ctx, span = gtrace.NewSpan(ctx, spanName) + defer span.End() + return ctx +} + // CtxId retrieves and returns the context id from context. func CtxId(ctx context.Context) string { return gtrace.GetTraceID(ctx) diff --git a/os/gctx/gctx_z_unit_test.go b/os/gctx/gctx_z_unit_test.go index aa09b92ff..25d63b573 100644 --- a/os/gctx/gctx_z_unit_test.go +++ b/os/gctx/gctx_z_unit_test.go @@ -25,7 +25,7 @@ func Test_New(t *testing.T) { func Test_WithCtx(t *testing.T) { gtest.C(t, func(t *gtest.T) { ctx := context.WithValue(context.TODO(), "TEST", 1) - ctx = gctx.WithCtx(ctx) + ctx = gctx.WithSpan(ctx, "test") t.AssertNE(gctx.CtxId(ctx), "") t.Assert(ctx.Value("TEST"), 1) })