|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package gtprof |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/util" |
| 13 | +) |
| 14 | + |
| 15 | +type contextKey struct { |
| 16 | + name string |
| 17 | +} |
| 18 | + |
| 19 | +var contextKeySpan = &contextKey{"span"} |
| 20 | + |
| 21 | +type traceStarter interface { |
| 22 | + start(ctx context.Context, traceSpan *TraceSpan, internalSpanIdx int) (context.Context, traceSpanInternal) |
| 23 | +} |
| 24 | + |
| 25 | +type traceSpanInternal interface { |
| 26 | + addEvent(name string, cfg *EventConfig) |
| 27 | + recordError(err error, cfg *EventConfig) |
| 28 | + end() |
| 29 | +} |
| 30 | + |
| 31 | +type TraceSpan struct { |
| 32 | + // immutable |
| 33 | + parent *TraceSpan |
| 34 | + internalSpans []traceSpanInternal |
| 35 | + internalContexts []context.Context |
| 36 | + |
| 37 | + // mutable, must be protected by mutex |
| 38 | + mu sync.RWMutex |
| 39 | + name string |
| 40 | + statusCode uint32 |
| 41 | + statusDesc string |
| 42 | + startTime time.Time |
| 43 | + endTime time.Time |
| 44 | + attributes []*TraceAttribute |
| 45 | + children []*TraceSpan |
| 46 | +} |
| 47 | + |
| 48 | +type TraceAttribute struct { |
| 49 | + Key string |
| 50 | + Value TraceValue |
| 51 | +} |
| 52 | + |
| 53 | +type TraceValue struct { |
| 54 | + v any |
| 55 | +} |
| 56 | + |
| 57 | +func (t *TraceValue) AsString() string { |
| 58 | + return fmt.Sprint(t.v) |
| 59 | +} |
| 60 | + |
| 61 | +func (t *TraceValue) AsInt64() int64 { |
| 62 | + v, _ := util.ToInt64(t.v) |
| 63 | + return v |
| 64 | +} |
| 65 | + |
| 66 | +func (t *TraceValue) AsFloat64() float64 { |
| 67 | + v, _ := util.ToFloat64(t.v) |
| 68 | + return v |
| 69 | +} |
| 70 | + |
| 71 | +var globalTraceStarters []traceStarter |
| 72 | + |
| 73 | +type Tracer struct { |
| 74 | + starters []traceStarter |
| 75 | +} |
| 76 | + |
| 77 | +func (s *TraceSpan) SetName(name string) { |
| 78 | + s.mu.Lock() |
| 79 | + defer s.mu.Unlock() |
| 80 | + s.name = name |
| 81 | +} |
| 82 | + |
| 83 | +func (s *TraceSpan) SetStatus(code uint32, desc string) { |
| 84 | + s.mu.Lock() |
| 85 | + defer s.mu.Unlock() |
| 86 | + s.statusCode, s.statusDesc = code, desc |
| 87 | +} |
| 88 | + |
| 89 | +func (s *TraceSpan) AddEvent(name string, options ...EventOption) { |
| 90 | + cfg := eventConfigFromOptions(options...) |
| 91 | + for _, tsp := range s.internalSpans { |
| 92 | + tsp.addEvent(name, cfg) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (s *TraceSpan) RecordError(err error, options ...EventOption) { |
| 97 | + cfg := eventConfigFromOptions(options...) |
| 98 | + for _, tsp := range s.internalSpans { |
| 99 | + tsp.recordError(err, cfg) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (s *TraceSpan) SetAttributeString(key, value string) *TraceSpan { |
| 104 | + s.mu.Lock() |
| 105 | + defer s.mu.Unlock() |
| 106 | + |
| 107 | + s.attributes = append(s.attributes, &TraceAttribute{Key: key, Value: TraceValue{v: value}}) |
| 108 | + return s |
| 109 | +} |
| 110 | + |
| 111 | +func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *TraceSpan) { |
| 112 | + starters := t.starters |
| 113 | + if starters == nil { |
| 114 | + starters = globalTraceStarters |
| 115 | + } |
| 116 | + ts := &TraceSpan{name: spanName, startTime: time.Now()} |
| 117 | + parentSpan := GetContextSpan(ctx) |
| 118 | + if parentSpan != nil { |
| 119 | + parentSpan.mu.Lock() |
| 120 | + parentSpan.children = append(parentSpan.children, ts) |
| 121 | + parentSpan.mu.Unlock() |
| 122 | + ts.parent = parentSpan |
| 123 | + } |
| 124 | + |
| 125 | + parentCtx := ctx |
| 126 | + for internalSpanIdx, tsp := range starters { |
| 127 | + var internalSpan traceSpanInternal |
| 128 | + if parentSpan != nil { |
| 129 | + parentCtx = parentSpan.internalContexts[internalSpanIdx] |
| 130 | + } |
| 131 | + ctx, internalSpan = tsp.start(parentCtx, ts, internalSpanIdx) |
| 132 | + ts.internalContexts = append(ts.internalContexts, ctx) |
| 133 | + ts.internalSpans = append(ts.internalSpans, internalSpan) |
| 134 | + } |
| 135 | + ctx = context.WithValue(ctx, contextKeySpan, ts) |
| 136 | + return ctx, ts |
| 137 | +} |
| 138 | + |
| 139 | +type mutableContext interface { |
| 140 | + context.Context |
| 141 | + SetContextValue(key, value any) |
| 142 | + GetContextValue(key any) any |
| 143 | +} |
| 144 | + |
| 145 | +// StartInContext starts a trace span in Gitea's mutable context (usually the web request context). |
| 146 | +// Due to the design limitation of Gitea's web framework, it can't use `context.WithValue` to bind a new span into a new context. |
| 147 | +// So here we use our "reqctx" framework to achieve the same result: web request context could always see the latest "span". |
| 148 | +func (t *Tracer) StartInContext(ctx mutableContext, spanName string) (*TraceSpan, func()) { |
| 149 | + curTraceSpan := GetContextSpan(ctx) |
| 150 | + _, newTraceSpan := GetTracer().Start(ctx, spanName) |
| 151 | + ctx.SetContextValue(contextKeySpan, newTraceSpan) |
| 152 | + return newTraceSpan, func() { |
| 153 | + newTraceSpan.End() |
| 154 | + ctx.SetContextValue(contextKeySpan, curTraceSpan) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func (s *TraceSpan) End() { |
| 159 | + s.mu.Lock() |
| 160 | + s.endTime = time.Now() |
| 161 | + s.mu.Unlock() |
| 162 | + |
| 163 | + for _, tsp := range s.internalSpans { |
| 164 | + tsp.end() |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func GetTracer() *Tracer { |
| 169 | + return &Tracer{} |
| 170 | +} |
| 171 | + |
| 172 | +func GetContextSpan(ctx context.Context) *TraceSpan { |
| 173 | + ts, _ := ctx.Value(contextKeySpan).(*TraceSpan) |
| 174 | + return ts |
| 175 | +} |
0 commit comments