77 "context"
88 "fmt"
99 "strings"
10+ "sync/atomic"
1011 "time"
1112
1213 "code.gitea.io/gitea/modules/tailmsg"
@@ -29,16 +30,28 @@ func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) {
2930 if t .ts .endTime .IsZero () {
3031 out .WriteString (" duration: (not ended)" )
3132 } 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 ()))
3334 }
34- out .WriteString ("\n " )
3535 for _ , a := range t .ts .attributes {
36- out .WriteString (strings . Repeat ( " " , indent + 2 ) )
36+ out .WriteString (" " )
3737 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 .Contains (value , " " ) {
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 )
4153 }
54+ out .WriteString ("\n " )
4255 for _ , c := range t .ts .children {
4356 span := c .internalSpans [t .internalSpanIdx ].(* traceBuiltinSpan )
4457 span .toString (out , indent + 2 )
@@ -47,9 +60,10 @@ func (t *traceBuiltinSpan) toString(out *strings.Builder, indent int) {
4760
4861func (t * traceBuiltinSpan ) end () {
4962 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 {
5367 sb := & strings.Builder {}
5468 t .toString (sb , 0 )
5569 tailmsg .GetManager ().GetTraceRecorder ().Record (sb .String ())
@@ -64,3 +78,9 @@ func (t *traceBuiltinStarter) start(ctx context.Context, traceSpan *TraceSpan, i
6478func init () {
6579 globalTraceStarters = append (globalTraceStarters , & traceBuiltinStarter {})
6680}
81+
82+ var traceBuiltinThreshold atomic.Int64
83+
84+ func EnableBuiltinTracer (threshold time.Duration ) {
85+ traceBuiltinThreshold .Store (int64 (threshold ))
86+ }
0 commit comments