-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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 6 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,119 @@ | ||
// Copyright 2024 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 { | ||
end() | ||
} | ||
|
||
type TraceSpan struct { | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// immutable | ||
parent *TraceSpan | ||
internalSpans []traceSpanInternal | ||
|
||
// mutable, must be protected by mutex | ||
mu sync.RWMutex | ||
name 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) 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()} | ||
existingCtxSpan := GetContextSpan(ctx) | ||
if existingCtxSpan != nil { | ||
existingCtxSpan.mu.Lock() | ||
existingCtxSpan.children = append(existingCtxSpan.children, ts) | ||
existingCtxSpan.mu.Unlock() | ||
ts.parent = existingCtxSpan | ||
} | ||
for internalSpanIdx, tsp := range starters { | ||
var internalSpan traceSpanInternal | ||
ctx, internalSpan = tsp.start(ctx, ts, internalSpanIdx) | ||
ts.internalSpans = append(ts.internalSpans, internalSpan) | ||
} | ||
ctx = context.WithValue(ctx, ContextKeySpan, ts) | ||
return ctx, ts | ||
} | ||
|
||
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 | ||
} |
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,86 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gtprof | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync/atomic" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/tailmsg" | ||
) | ||
|
||
type traceBuiltinStarter struct{} | ||
|
||
type traceBuiltinSpan struct { | ||
ts *TraceSpan | ||
|
||
internalSpanIdx int | ||
} | ||
|
||
func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) { | ||
t.ts.mu.RLock() | ||
defer t.ts.mu.RUnlock() | ||
|
||
out.WriteString(strings.Repeat(" ", indent)) | ||
out.WriteString(t.ts.name) | ||
if t.ts.endTime.IsZero() { | ||
out.WriteString(" duration: (not ended)") | ||
} else { | ||
out.WriteString(fmt.Sprintf(" duration=%.4fs", t.ts.endTime.Sub(t.ts.startTime).Seconds())) | ||
} | ||
for _, a := range t.ts.attributes { | ||
out.WriteString(" ") | ||
out.WriteString(a.Key) | ||
out.WriteString("=") | ||
value := a.Value.AsString() | ||
if strings.ContainsAny(value, " \t\r\n") { | ||
quoted := false | ||
for _, c := range "\"'`" { | ||
if quoted = !strings.Contains(value, string(c)); quoted { | ||
value = string(c) + value + string(c) | ||
break | ||
} | ||
} | ||
if !quoted { | ||
value = fmt.Sprintf("%q", value) | ||
} | ||
} | ||
out.WriteString(value) | ||
} | ||
out.WriteString("\n") | ||
for _, c := range t.ts.children { | ||
span := c.internalSpans[t.internalSpanIdx].(*traceBuiltinSpan) | ||
span.toString(out, indent+2) | ||
} | ||
} | ||
|
||
func (t *traceBuiltinSpan) end() { | ||
if t.ts.parent == nil { | ||
// TODO: debug purpose only | ||
// TODO: it should distinguish between http response network lag and actual processing time | ||
threshold := time.Duration(traceBuiltinThreshold.Load()) | ||
if threshold != 0 && t.ts.endTime.Sub(t.ts.startTime) > threshold { | ||
sb := &strings.Builder{} | ||
t.toString(sb, 0) | ||
tailmsg.GetManager().GetTraceRecorder().Record(sb.String()) | ||
} | ||
} | ||
} | ||
|
||
func (t *traceBuiltinStarter) start(ctx context.Context, traceSpan *TraceSpan, internalSpanIdx int) (context.Context, traceSpanInternal) { | ||
return ctx, &traceBuiltinSpan{ts: traceSpan, internalSpanIdx: internalSpanIdx} | ||
} | ||
|
||
func init() { | ||
globalTraceStarters = append(globalTraceStarters, &traceBuiltinStarter{}) | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var traceBuiltinThreshold atomic.Int64 | ||
|
||
func EnableBuiltinTracer(threshold time.Duration) { | ||
traceBuiltinThreshold.Store(int64(threshold)) | ||
} |
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,19 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gtprof | ||
|
||
// Some interesting names could be found in https://github.com/open-telemetry/opentelemetry-go/tree/main/semconv | ||
|
||
const ( | ||
TraceSpanHTTP = "http" | ||
TraceSpanGitRun = "git-run" | ||
TraceSpanDatabase = "database" | ||
) | ||
|
||
const ( | ||
TraceAttrFuncCaller = "func.caller" | ||
TraceAttrDbSQL = "db.sql" | ||
TraceAttrGitCommand = "git.command" | ||
TraceAttrHTTPRoute = "http.route" | ||
) |
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.