Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gctx): rename and remove gctx functions to prevent ambiguity #3892

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions net/ghttp/ghttp_request_param_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 16 additions & 2 deletions os/gctx/gctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) != "" {
mingzaily marked this conversation as resolved.
Show resolved Hide resolved
return ctx
}
if spanName == "" {
spanName = "gctx.WithSpan"
}
var span *gtrace.Span
ctx, span = gtrace.NewSpan(ctx, spanName)
defer span.End()
houseme marked this conversation as resolved.
Show resolved Hide resolved
return ctx
}

// CtxId retrieves and returns the context id from context.
func CtxId(ctx context.Context) string {
return gtrace.GetTraceID(ctx)
Expand Down
2 changes: 1 addition & 1 deletion os/gctx/gctx_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Loading