@@ -6,22 +6,39 @@ package routing
66import (
77 "context"
88 "net/http"
9+
10+ "code.gitea.io/gitea/modules/gtprof"
11+ "code.gitea.io/gitea/modules/reqctx"
912)
1013
1114type contextKeyType struct {}
1215
1316var 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
0 commit comments