Skip to content

Commit b8f78d2

Browse files
committed
hide context details, callers do not need to know how the low-level framework works
1 parent 7f9f309 commit b8f78d2

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

modules/gtprof/trace.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type contextKey struct {
1616
name string
1717
}
1818

19-
var ContextKeySpan = &contextKey{"span"}
19+
var contextKeySpan = &contextKey{"span"}
2020

2121
type traceStarter interface {
2222
start(ctx context.Context, traceSpan *TraceSpan, internalSpanIdx int) (context.Context, traceSpanInternal)
@@ -132,10 +132,29 @@ func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *
132132
ts.internalContexts = append(ts.internalContexts, ctx)
133133
ts.internalSpans = append(ts.internalSpans, internalSpan)
134134
}
135-
ctx = context.WithValue(ctx, ContextKeySpan, ts)
135+
ctx = context.WithValue(ctx, contextKeySpan, ts)
136136
return ctx, ts
137137
}
138138

139+
type mutableContext interface {
140+
context.Context
141+
SetContextValue(key, value any)
142+
GetContextValue(key any) any
143+
}
144+
145+
// StartInContext starts a trace span in Gitea's mutable context (usually the web request context).
146+
// Due to the design limitation of Gitea's web framework, it can't use `context.WithValue` to bind a new span into a new context.
147+
// So here we use our "reqctx" framework to achieve the same result: web request context could always see the latest "span".
148+
func (t *Tracer) StartInContext(ctx mutableContext, spanName string) (*TraceSpan, func()) {
149+
curTraceSpan := GetContextSpan(ctx)
150+
_, newTraceSpan := GetTracer().Start(ctx, spanName)
151+
ctx.SetContextValue(contextKeySpan, newTraceSpan)
152+
return newTraceSpan, func() {
153+
newTraceSpan.End()
154+
ctx.SetContextValue(contextKeySpan, curTraceSpan)
155+
}
156+
}
157+
139158
func (s *TraceSpan) End() {
140159
s.mu.Lock()
141160
s.endTime = time.Now()
@@ -151,6 +170,6 @@ func GetTracer() *Tracer {
151170
}
152171

153172
func GetContextSpan(ctx context.Context) *TraceSpan {
154-
ts, _ := ctx.Value(ContextKeySpan).(*TraceSpan)
173+
ts, _ := ctx.Value(contextKeySpan).(*TraceSpan)
155174
return ts
156175
}

modules/web/routing/context.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,12 @@ type contextKeyType struct{}
1515

1616
var contextKey contextKeyType
1717

18-
// StartContextSpan starts a trace span in Gitea's web request context
19-
// Due to the design limitation of Gitea's web framework, it can't use `context.WithValue` to bind a new span into a new context.
20-
// So here we use our "reqctx" framework to achieve the same result: web request context could always see the latest "span".
21-
func StartContextSpan(ctx reqctx.RequestContext, spanName string) (*gtprof.TraceSpan, func()) {
22-
curTraceSpan := gtprof.GetContextSpan(ctx)
23-
_, newTraceSpan := gtprof.GetTracer().Start(ctx, spanName)
24-
ctx.SetContextValue(gtprof.ContextKeySpan, newTraceSpan)
25-
return newTraceSpan, func() {
26-
newTraceSpan.End()
27-
ctx.SetContextValue(gtprof.ContextKeySpan, curTraceSpan)
28-
}
29-
}
30-
3118
// RecordFuncInfo records a func info into context
3219
func RecordFuncInfo(ctx context.Context, funcInfo *FuncInfo) (end func()) {
3320
end = func() {}
3421
if reqCtx := reqctx.FromContext(ctx); reqCtx != nil {
3522
var traceSpan *gtprof.TraceSpan
36-
traceSpan, end = StartContextSpan(reqCtx, "http.func")
23+
traceSpan, end = gtprof.GetTracer().StartInContext(reqCtx, "http.func")
3724
traceSpan.SetAttributeString("func", funcInfo.shortName)
3825
}
3926
if record, ok := ctx.Value(contextKey).(*requestRecord); ok {

0 commit comments

Comments
 (0)