-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65e45fd
commit 4b7547b
Showing
19 changed files
with
378 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 { | ||
// 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, _ := ctx.Value(ContextKeySpan).(*TraceSpan) | ||
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 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,66 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package gtprof | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"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())) | ||
} | ||
out.WriteString("\n") | ||
for _, a := range t.ts.attributes { | ||
out.WriteString(strings.Repeat(" ", indent+2)) | ||
out.WriteString(a.Key) | ||
out.WriteString(": ") | ||
out.WriteString(a.Value.AsString()) | ||
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 { | ||
// FIXME: debug purpose only | ||
// FIXME: it should distinguish between http response network lag and actual processing time | ||
if len(t.ts.children) > 3 || t.ts.endTime.Sub(t.ts.startTime) > 100*time.Millisecond { | ||
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{}) | ||
} |
This file contains 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" | ||
) |
This file contains 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,73 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package tailmsg | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type MsgRecord struct { | ||
Time time.Time | ||
Content string | ||
} | ||
|
||
type MsgRecorder interface { | ||
Record(content string) | ||
GetRecords() []*MsgRecord | ||
} | ||
|
||
type memoryMsgRecorder struct { | ||
mu sync.RWMutex | ||
msgs []*MsgRecord | ||
limit int | ||
} | ||
|
||
// TODO: use redis for a clustered environment | ||
|
||
func (m *memoryMsgRecorder) Record(content string) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.msgs = append(m.msgs, &MsgRecord{ | ||
Time: time.Now(), | ||
Content: content, | ||
}) | ||
if len(m.msgs) > m.limit { | ||
m.msgs = m.msgs[len(m.msgs)-m.limit:] | ||
} | ||
} | ||
|
||
func (m *memoryMsgRecorder) GetRecords() []*MsgRecord { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
ret := make([]*MsgRecord, len(m.msgs)) | ||
copy(ret, m.msgs) | ||
return ret | ||
} | ||
|
||
func NewMsgRecorder(limit int) MsgRecorder { | ||
return &memoryMsgRecorder{ | ||
limit: limit, | ||
} | ||
} | ||
|
||
type Manager struct { | ||
traceRecorder MsgRecorder | ||
logRecorder MsgRecorder | ||
} | ||
|
||
func (m *Manager) GetTraceRecorder() MsgRecorder { | ||
return m.traceRecorder | ||
} | ||
|
||
func (m *Manager) GetLogRecorder() MsgRecorder { | ||
return m.logRecorder | ||
} | ||
|
||
var GetManager = sync.OnceValue(func() *Manager { | ||
return &Manager{ | ||
traceRecorder: NewMsgRecorder(100), | ||
logRecorder: NewMsgRecorder(1000), | ||
} | ||
}) |
This file contains 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
Oops, something went wrong.