7
7
"context"
8
8
"fmt"
9
9
"strings"
10
+ "sync/atomic"
10
11
"time"
11
12
12
13
"code.gitea.io/gitea/modules/tailmsg"
@@ -29,16 +30,28 @@ func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) {
29
30
if t .ts .endTime .IsZero () {
30
31
out .WriteString (" duration: (not ended)" )
31
32
} else {
32
- out .WriteString (fmt .Sprintf (" duration: %.4fs" , t .ts .endTime .Sub (t .ts .startTime ).Seconds ()))
33
+ out .WriteString (fmt .Sprintf (" duration= %.4fs" , t .ts .endTime .Sub (t .ts .startTime ).Seconds ()))
33
34
}
34
- out .WriteString ("\n " )
35
35
for _ , a := range t .ts .attributes {
36
- out .WriteString (strings . Repeat ( " " , indent + 2 ) )
36
+ out .WriteString (" " )
37
37
out .WriteString (a .Key )
38
- out .WriteString (": " )
39
- out .WriteString (a .Value .AsString ())
40
- out .WriteString ("\n " )
38
+ out .WriteString ("=" )
39
+ value := a .Value .AsString ()
40
+ if strings .ContainsAny (value , " \t \r \n " ) {
41
+ quoted := false
42
+ for _ , c := range "\" '`" {
43
+ if quoted = ! strings .Contains (value , string (c )); quoted {
44
+ value = string (c ) + value + string (c )
45
+ break
46
+ }
47
+ }
48
+ if ! quoted {
49
+ value = fmt .Sprintf ("%q" , value )
50
+ }
51
+ }
52
+ out .WriteString (value )
41
53
}
54
+ out .WriteString ("\n " )
42
55
for _ , c := range t .ts .children {
43
56
span := c .internalSpans [t .internalSpanIdx ].(* traceBuiltinSpan )
44
57
span .toString (out , indent + 2 )
@@ -47,9 +60,10 @@ func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) {
47
60
48
61
func (t * traceBuiltinSpan ) end () {
49
62
if t .ts .parent == nil {
50
- // FIXME: debug purpose only
51
- // FIXME: it should distinguish between http response network lag and actual processing time
52
- if len (t .ts .children ) > 3 || t .ts .endTime .Sub (t .ts .startTime ) > 100 * time .Millisecond {
63
+ // TODO: debug purpose only
64
+ // TODO: it should distinguish between http response network lag and actual processing time
65
+ threshold := time .Duration (traceBuiltinThreshold .Load ())
66
+ if threshold != 0 && t .ts .endTime .Sub (t .ts .startTime ) > threshold {
53
67
sb := & strings.Builder {}
54
68
t .toString (sb , 0 )
55
69
tailmsg .GetManager ().GetTraceRecorder ().Record (sb .String ())
@@ -64,3 +78,9 @@ func (t *traceBuiltinStarter) start(ctx context.Context, traceSpan *TraceSpan, i
64
78
func init () {
65
79
globalTraceStarters = append (globalTraceStarters , & traceBuiltinStarter {})
66
80
}
81
+
82
+ var traceBuiltinThreshold atomic.Int64
83
+
84
+ func EnableBuiltinTracer (threshold time.Duration ) {
85
+ traceBuiltinThreshold .Store (int64 (threshold ))
86
+ }
0 commit comments