Skip to content

Commit 392f349

Browse files
committed
improve shim layer, add event support
1 parent ea8dcfc commit 392f349

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

modules/gtprof/event.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gtprof
5+
6+
type EventConfig struct {
7+
attributes []*TraceAttribute
8+
}
9+
10+
type EventOption interface {
11+
applyEvent(*EventConfig)
12+
}
13+
14+
type applyEventFunc func(*EventConfig)
15+
16+
func (f applyEventFunc) applyEvent(cfg *EventConfig) {
17+
f(cfg)
18+
}
19+
20+
func WithAttributes(attrs ...*TraceAttribute) EventOption {
21+
return applyEventFunc(func(cfg *EventConfig) {
22+
cfg.attributes = append(cfg.attributes, attrs...)
23+
})
24+
}
25+
26+
func eventConfigFromOptions(options ...EventOption) *EventConfig {
27+
cfg := &EventConfig{}
28+
for _, opt := range options {
29+
opt.applyEvent(cfg)
30+
}
31+
return cfg
32+
}

modules/gtprof/trace.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type traceStarter interface {
2323
}
2424

2525
type traceSpanInternal interface {
26+
addEvent(name string, cfg *EventConfig)
27+
recordError(err error, cfg *EventConfig)
2628
end()
2729
}
2830

@@ -35,9 +37,11 @@ type TraceSpan struct {
3537
// mutable, must be protected by mutex
3638
mu sync.RWMutex
3739
name string
40+
statusCode uint32
41+
statusDesc string
3842
startTime time.Time
3943
endTime time.Time
40-
attributes []TraceAttribute
44+
attributes []*TraceAttribute
4145
children []*TraceSpan
4246
}
4347

@@ -70,11 +74,31 @@ type Tracer struct {
7074
starters []traceStarter
7175
}
7276

77+
func (s *TraceSpan) SetStatus(code uint32, desc string) {
78+
s.mu.Lock()
79+
defer s.mu.Unlock()
80+
s.statusCode, s.statusDesc = code, desc
81+
}
82+
83+
func (s *TraceSpan) AddEvent(name string, options ...EventOption) {
84+
cfg := eventConfigFromOptions(options...)
85+
for _, tsp := range s.internalSpans {
86+
tsp.addEvent(name, cfg)
87+
}
88+
}
89+
90+
func (s *TraceSpan) RecordError(err error, options ...EventOption) {
91+
cfg := eventConfigFromOptions(options...)
92+
for _, tsp := range s.internalSpans {
93+
tsp.recordError(err, cfg)
94+
}
95+
}
96+
7397
func (s *TraceSpan) SetAttributeString(key, value string) *TraceSpan {
7498
s.mu.Lock()
7599
defer s.mu.Unlock()
76100

77-
s.attributes = append(s.attributes, TraceAttribute{Key: key, Value: TraceValue{v: value}})
101+
s.attributes = append(s.attributes, &TraceAttribute{Key: key, Value: TraceValue{v: value}})
78102
return s
79103
}
80104

modules/gtprof/trace_builtin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ type traceBuiltinSpan struct {
2121
internalSpanIdx int
2222
}
2323

24+
func (t *traceBuiltinSpan) addEvent(name string, cfg *EventConfig) {
25+
// No-op because builtin tracer doesn't need it.
26+
// In the future we might use it to mark the time point between backend logic and network response.
27+
}
28+
29+
func (t *traceBuiltinSpan) recordError(err error, cfg *EventConfig) {
30+
// No-op because builtin tracer doesn't need it.
31+
// Actually Gitea doesn't handle err this way in most cases
32+
}
33+
2434
func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) {
2535
t.ts.mu.RLock()
2636
defer t.ts.mu.RUnlock()

modules/gtprof/trace_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ type testTraceSpan struct {
3535
vendorSpan *vendorSpan
3636
}
3737

38+
func (t *testTraceSpan) addEvent(name string, cfg *EventConfig) {}
39+
40+
func (t *testTraceSpan) recordError(err error, cfg *EventConfig) {}
41+
3842
func (t *testTraceSpan) end() {}
3943

4044
type testTraceStarter struct{}

0 commit comments

Comments
 (0)