Skip to content

Commit 94816bf

Browse files
committed
trace web functions
1 parent 06866b1 commit 94816bf

File tree

7 files changed

+43
-28
lines changed

7 files changed

+43
-28
lines changed

modules/gtprof/trace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *
8383
starters = globalTraceStarters
8484
}
8585
ts := &TraceSpan{name: spanName, startTime: time.Now()}
86-
existingCtxSpan, _ := ctx.Value(ContextKeySpan).(*TraceSpan)
86+
existingCtxSpan := GetContextSpan(ctx)
8787
if existingCtxSpan != nil {
8888
existingCtxSpan.mu.Lock()
8989
existingCtxSpan.children = append(existingCtxSpan.children, ts)

modules/web/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func wrapHandlerProvider[T http.Handler](hp func(next http.Handler) T, funcInfo
121121
return func(next http.Handler) http.Handler {
122122
h := hp(next) // this handle could be dynamically generated, so we can't use it for debug info
123123
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
124-
routing.UpdateFuncInfo(req.Context(), funcInfo)
124+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
125125
h.ServeHTTP(resp, req)
126126
})
127127
}
@@ -157,7 +157,7 @@ func toHandlerProvider(handler any) func(next http.Handler) http.Handler {
157157
return // it's doing pre-check, just return
158158
}
159159

160-
routing.UpdateFuncInfo(req.Context(), funcInfo)
160+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
161161
ret := fn.Call(argsIn)
162162

163163
// handle the return value (no-op at the moment)

modules/web/routing/context.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ package routing
66
import (
77
"context"
88
"net/http"
9+
10+
"code.gitea.io/gitea/modules/gtprof"
11+
"code.gitea.io/gitea/modules/reqctx"
912
)
1013

1114
type contextKeyType struct{}
1215

1316
var contextKey contextKeyType
1417

15-
// UpdateFuncInfo updates a context's func info
16-
func UpdateFuncInfo(ctx context.Context, funcInfo *FuncInfo) {
17-
record, ok := ctx.Value(contextKey).(*requestRecord)
18-
if !ok {
19-
return
18+
func StartContextSpan(ctx reqctx.RequestContext, spanName string) (*gtprof.TraceSpan, func()) {
19+
curTraceSpan := gtprof.GetContextSpan(ctx)
20+
_, newTraceSpan := gtprof.GetTracer().Start(ctx, spanName)
21+
ctx.SetContextValue(gtprof.ContextKeySpan, newTraceSpan)
22+
return newTraceSpan, func() {
23+
newTraceSpan.End()
24+
ctx.SetContextValue(gtprof.ContextKeySpan, curTraceSpan)
2025
}
26+
}
2127

22-
record.lock.Lock()
23-
record.funcInfo = funcInfo
24-
record.lock.Unlock()
28+
// RecordFuncInfo records a func info into context
29+
func RecordFuncInfo(ctx context.Context, funcInfo *FuncInfo) (end func()) {
30+
end = func() {}
31+
if reqCtx := reqctx.FromContext(ctx); reqCtx != nil {
32+
var traceSpan *gtprof.TraceSpan
33+
traceSpan, end = StartContextSpan(reqCtx, "http.func")
34+
traceSpan.SetAttributeString("func", funcInfo.shortName)
35+
}
36+
if record, ok := ctx.Value(contextKey).(*requestRecord); ok {
37+
record.lock.Lock()
38+
record.funcInfo = funcInfo
39+
record.lock.Unlock()
40+
}
41+
return end
2542
}
2643

2744
// MarkLongPolling marks the request is a long-polling request, and the logger may output different message for it

routers/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func NormalRoutes() *web.Router {
213213
}
214214

215215
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
216-
routing.UpdateFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound"))
216+
defer routing.RecordFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound"))()
217217
http.NotFound(w, req)
218218
})
219219
return r

routers/web/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
3434
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
3535
return
3636
}
37-
routing.UpdateFuncInfo(req.Context(), funcInfo)
37+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
3838

3939
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
4040
rPath = util.PathJoinRelX(rPath)
@@ -65,7 +65,7 @@ func storageHandler(storageSetting *setting.Storage, prefix string, objStore sto
6565
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
6666
return
6767
}
68-
routing.UpdateFuncInfo(req.Context(), funcInfo)
68+
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
6969

7070
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
7171
rPath = util.PathJoinRelX(rPath)

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ func registerRoutes(m *web.Router) {
16231623

16241624
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
16251625
ctx := context.GetWebContext(req)
1626-
routing.UpdateFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))
1626+
defer routing.RecordFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))()
16271627
ctx.NotFound("", nil)
16281628
})
16291629
}

services/context/base.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package context
55

66
import (
7-
"context"
87
"fmt"
98
"html/template"
109
"io"
@@ -25,8 +24,7 @@ type BaseContextKeyType struct{}
2524
var BaseContextKey BaseContextKeyType
2625

2726
type Base struct {
28-
context.Context
29-
reqctx.RequestDataStore
27+
reqctx.RequestContext
3028

3129
Resp ResponseWriter
3230
Req *http.Request
@@ -172,19 +170,19 @@ func (b *Base) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
172170
}
173171

174172
func NewBaseContext(resp http.ResponseWriter, req *http.Request) *Base {
175-
ds := reqctx.GetRequestDataStore(req.Context())
173+
reqCtx := reqctx.FromContext(req.Context())
176174
b := &Base{
177-
Context: req.Context(),
178-
RequestDataStore: ds,
179-
Req: req,
180-
Resp: WrapResponseWriter(resp),
181-
Locale: middleware.Locale(resp, req),
182-
Data: ds.GetData(),
175+
RequestContext: reqCtx,
176+
177+
Req: req,
178+
Resp: WrapResponseWriter(resp),
179+
Locale: middleware.Locale(resp, req),
180+
Data: reqCtx.GetData(),
183181
}
184182
b.Req = b.Req.WithContext(b)
185-
ds.SetContextValue(BaseContextKey, b)
186-
ds.SetContextValue(translation.ContextKey, b.Locale)
187-
ds.SetContextValue(httplib.RequestContextKey, b.Req)
183+
reqCtx.SetContextValue(BaseContextKey, b)
184+
reqCtx.SetContextValue(translation.ContextKey, b.Locale)
185+
reqCtx.SetContextValue(httplib.RequestContextKey, b.Req)
188186
return b
189187
}
190188

0 commit comments

Comments
 (0)