Skip to content

Commit 76c1e13

Browse files
committed
add tests
1 parent 5572bbe commit 76c1e13

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

modules/gtprof/trace.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 The Gitea Authors. All rights reserved.
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package gtprof
@@ -28,8 +28,9 @@ type traceSpanInternal interface {
2828

2929
type TraceSpan struct {
3030
// immutable
31-
parent *TraceSpan
32-
internalSpans []traceSpanInternal
31+
parent *TraceSpan
32+
internalSpans []traceSpanInternal
33+
internalContexts []context.Context
3334

3435
// mutable, must be protected by mutex
3536
mu sync.RWMutex
@@ -83,16 +84,22 @@ func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *
8384
starters = globalTraceStarters
8485
}
8586
ts := &TraceSpan{name: spanName, startTime: time.Now()}
86-
existingCtxSpan := GetContextSpan(ctx)
87-
if existingCtxSpan != nil {
88-
existingCtxSpan.mu.Lock()
89-
existingCtxSpan.children = append(existingCtxSpan.children, ts)
90-
existingCtxSpan.mu.Unlock()
91-
ts.parent = existingCtxSpan
87+
parentSpan := GetContextSpan(ctx)
88+
if parentSpan != nil {
89+
parentSpan.mu.Lock()
90+
parentSpan.children = append(parentSpan.children, ts)
91+
parentSpan.mu.Unlock()
92+
ts.parent = parentSpan
9293
}
94+
95+
parentCtx := ctx
9396
for internalSpanIdx, tsp := range starters {
9497
var internalSpan traceSpanInternal
95-
ctx, internalSpan = tsp.start(ctx, ts, internalSpanIdx)
98+
if parentSpan != nil {
99+
parentCtx = parentSpan.internalContexts[internalSpanIdx]
100+
}
101+
ctx, internalSpan = tsp.start(parentCtx, ts, internalSpanIdx)
102+
ts.internalContexts = append(ts.internalContexts, ctx)
96103
ts.internalSpans = append(ts.internalSpans, internalSpan)
97104
}
98105
ctx = context.WithValue(ctx, ContextKeySpan, ts)

modules/gtprof/trace_builtin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 The Gitea Authors. All rights reserved.
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package gtprof

modules/gtprof/trace_const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 The Gitea Authors. All rights reserved.
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package gtprof

modules/gtprof/trace_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gtprof
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// "vendor span" is a simple demo for a span from a vendor library
14+
15+
var vendorContextKey any = "vendorContextKey"
16+
17+
type vendorSpan struct {
18+
name string
19+
children []*vendorSpan
20+
}
21+
22+
func vendorTraceStart(ctx context.Context, name string) (context.Context, *vendorSpan) {
23+
span := &vendorSpan{name: name}
24+
parentSpan, ok := ctx.Value(vendorContextKey).(*vendorSpan)
25+
if ok {
26+
parentSpan.children = append(parentSpan.children, span)
27+
}
28+
ctx = context.WithValue(ctx, vendorContextKey, span)
29+
return ctx, span
30+
}
31+
32+
// below "testTrace*" integrate the vendor span into our trace system
33+
34+
type testTraceSpan struct {
35+
vendorSpan *vendorSpan
36+
}
37+
38+
func (t *testTraceSpan) end() {}
39+
40+
type testTraceStarter struct{}
41+
42+
func (t *testTraceStarter) start(ctx context.Context, traceSpan *TraceSpan, internalSpanIdx int) (context.Context, traceSpanInternal) {
43+
ctx, span := vendorTraceStart(ctx, traceSpan.name)
44+
return ctx, &testTraceSpan{span}
45+
}
46+
47+
func TestTraceStarter(t *testing.T) {
48+
globalTraceStarters = []traceStarter{&testTraceStarter{}}
49+
50+
ctx := context.Background()
51+
ctx, span := GetTracer().Start(ctx, "root")
52+
defer span.End()
53+
54+
func(ctx context.Context) {
55+
ctx, span := GetTracer().Start(ctx, "span1")
56+
defer span.End()
57+
func(ctx context.Context) {
58+
ctx, span := GetTracer().Start(ctx, "spanA")
59+
defer span.End()
60+
}(ctx)
61+
func(ctx context.Context) {
62+
ctx, span := GetTracer().Start(ctx, "spanB")
63+
defer span.End()
64+
}(ctx)
65+
}(ctx)
66+
67+
func(ctx context.Context) {
68+
ctx, span := GetTracer().Start(ctx, "span2")
69+
defer span.End()
70+
}(ctx)
71+
72+
var spanFullNames []string
73+
var collectSpanNames func(parentFullName string, s *vendorSpan)
74+
collectSpanNames = func(parentFullName string, s *vendorSpan) {
75+
fullName := parentFullName + "/" + s.name
76+
spanFullNames = append(spanFullNames, fullName)
77+
for _, c := range s.children {
78+
collectSpanNames(fullName, c)
79+
}
80+
}
81+
collectSpanNames("", span.internalSpans[0].(*testTraceSpan).vendorSpan)
82+
assert.Equal(t, []string([]string{
83+
"/root",
84+
"/root/span1",
85+
"/root/span1/spanA",
86+
"/root/span1/spanB",
87+
"/root/span2",
88+
}), spanFullNames)
89+
}

0 commit comments

Comments
 (0)