-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support performance trace #32973
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
Merged
Merged
Support performance trace #32973
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ee7a30e
fix
wxiaoguang a02dca8
Merge branch 'go-gitea:main' into support-trace
wxiaoguang 06866b1
Merge branch 'main' into support-trace
wxiaoguang 94816bf
trace web functions
wxiaoguang 5f29893
Merge branch 'main' into support-trace
wxiaoguang 5572bbe
improve
wxiaoguang 76c1e13
add tests
wxiaoguang 8e5a0ff
add comments
wxiaoguang 44aec40
move scrollbar to parent
wxiaoguang ea8dcfc
add comment for sql
wxiaoguang 392f349
improve shim layer, add event support
wxiaoguang 7f9f309
add SetName
wxiaoguang b8f78d2
hide context details, callers do not need to know how the low-level f…
wxiaoguang 9a57ac7
rename
wxiaoguang 8fdccb0
Update templates/admin/perftrace.tmpl
wxiaoguang 5647c41
Merge branch 'main' into support-trace
wxiaoguang 4ebb00a
Merge branch 'main' into support-trace
wxiaoguang f7ce9ee
Merge branch 'main' into support-trace
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gtprof | ||
|
||
type EventConfig struct { | ||
attributes []*TraceAttribute | ||
} | ||
|
||
type EventOption interface { | ||
applyEvent(*EventConfig) | ||
} | ||
|
||
type applyEventFunc func(*EventConfig) | ||
|
||
func (f applyEventFunc) applyEvent(cfg *EventConfig) { | ||
f(cfg) | ||
} | ||
|
||
func WithAttributes(attrs ...*TraceAttribute) EventOption { | ||
return applyEventFunc(func(cfg *EventConfig) { | ||
cfg.attributes = append(cfg.attributes, attrs...) | ||
}) | ||
} | ||
|
||
func eventConfigFromOptions(options ...EventOption) *EventConfig { | ||
cfg := &EventConfig{} | ||
for _, opt := range options { | ||
opt.applyEvent(cfg) | ||
} | ||
return cfg | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gtprof | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
type contextKey struct { | ||
name string | ||
} | ||
|
||
var contextKeySpan = &contextKey{"span"} | ||
|
||
type traceStarter interface { | ||
start(ctx context.Context, traceSpan *TraceSpan, internalSpanIdx int) (context.Context, traceSpanInternal) | ||
} | ||
|
||
type traceSpanInternal interface { | ||
addEvent(name string, cfg *EventConfig) | ||
recordError(err error, cfg *EventConfig) | ||
end() | ||
} | ||
|
||
type TraceSpan struct { | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// immutable | ||
parent *TraceSpan | ||
internalSpans []traceSpanInternal | ||
internalContexts []context.Context | ||
|
||
// mutable, must be protected by mutex | ||
mu sync.RWMutex | ||
name string | ||
statusCode uint32 | ||
statusDesc string | ||
startTime time.Time | ||
endTime time.Time | ||
attributes []*TraceAttribute | ||
children []*TraceSpan | ||
} | ||
|
||
type TraceAttribute struct { | ||
Key string | ||
Value TraceValue | ||
} | ||
|
||
type TraceValue struct { | ||
v any | ||
} | ||
|
||
func (t *TraceValue) AsString() string { | ||
return fmt.Sprint(t.v) | ||
} | ||
|
||
func (t *TraceValue) AsInt64() int64 { | ||
v, _ := util.ToInt64(t.v) | ||
return v | ||
} | ||
|
||
func (t *TraceValue) AsFloat64() float64 { | ||
v, _ := util.ToFloat64(t.v) | ||
return v | ||
} | ||
|
||
var globalTraceStarters []traceStarter | ||
|
||
type Tracer struct { | ||
starters []traceStarter | ||
} | ||
|
||
func (s *TraceSpan) SetName(name string) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.name = name | ||
} | ||
|
||
func (s *TraceSpan) SetStatus(code uint32, desc string) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.statusCode, s.statusDesc = code, desc | ||
} | ||
|
||
func (s *TraceSpan) AddEvent(name string, options ...EventOption) { | ||
cfg := eventConfigFromOptions(options...) | ||
for _, tsp := range s.internalSpans { | ||
tsp.addEvent(name, cfg) | ||
} | ||
} | ||
|
||
func (s *TraceSpan) RecordError(err error, options ...EventOption) { | ||
cfg := eventConfigFromOptions(options...) | ||
for _, tsp := range s.internalSpans { | ||
tsp.recordError(err, cfg) | ||
} | ||
} | ||
|
||
func (s *TraceSpan) SetAttributeString(key, value string) *TraceSpan { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
s.attributes = append(s.attributes, &TraceAttribute{Key: key, Value: TraceValue{v: value}}) | ||
return s | ||
} | ||
|
||
func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *TraceSpan) { | ||
starters := t.starters | ||
if starters == nil { | ||
starters = globalTraceStarters | ||
} | ||
ts := &TraceSpan{name: spanName, startTime: time.Now()} | ||
parentSpan := GetContextSpan(ctx) | ||
if parentSpan != nil { | ||
parentSpan.mu.Lock() | ||
parentSpan.children = append(parentSpan.children, ts) | ||
parentSpan.mu.Unlock() | ||
ts.parent = parentSpan | ||
} | ||
|
||
parentCtx := ctx | ||
for internalSpanIdx, tsp := range starters { | ||
var internalSpan traceSpanInternal | ||
if parentSpan != nil { | ||
parentCtx = parentSpan.internalContexts[internalSpanIdx] | ||
} | ||
ctx, internalSpan = tsp.start(parentCtx, ts, internalSpanIdx) | ||
ts.internalContexts = append(ts.internalContexts, ctx) | ||
ts.internalSpans = append(ts.internalSpans, internalSpan) | ||
} | ||
ctx = context.WithValue(ctx, contextKeySpan, ts) | ||
return ctx, ts | ||
} | ||
|
||
type mutableContext interface { | ||
context.Context | ||
SetContextValue(key, value any) | ||
GetContextValue(key any) any | ||
} | ||
|
||
// StartInContext starts a trace span in Gitea's mutable context (usually the web request context). | ||
// 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. | ||
// So here we use our "reqctx" framework to achieve the same result: web request context could always see the latest "span". | ||
func (t *Tracer) StartInContext(ctx mutableContext, spanName string) (*TraceSpan, func()) { | ||
curTraceSpan := GetContextSpan(ctx) | ||
_, newTraceSpan := GetTracer().Start(ctx, spanName) | ||
ctx.SetContextValue(contextKeySpan, newTraceSpan) | ||
return newTraceSpan, func() { | ||
newTraceSpan.End() | ||
ctx.SetContextValue(contextKeySpan, curTraceSpan) | ||
} | ||
} | ||
|
||
func (s *TraceSpan) End() { | ||
s.mu.Lock() | ||
s.endTime = time.Now() | ||
s.mu.Unlock() | ||
|
||
for _, tsp := range s.internalSpans { | ||
tsp.end() | ||
} | ||
} | ||
|
||
func GetTracer() *Tracer { | ||
return &Tracer{} | ||
} | ||
|
||
func GetContextSpan(ctx context.Context) *TraceSpan { | ||
ts, _ := ctx.Value(contextKeySpan).(*TraceSpan) | ||
return ts | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.