|
| 1 | +// Copyright 2024 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 | + end() |
| 27 | +} |
| 28 | + |
| 29 | +type TraceSpan struct { |
| 30 | + // immutable |
| 31 | + parent *TraceSpan |
| 32 | + internalSpans []traceSpanInternal |
| 33 | + |
| 34 | + // mutable, must be protected by mutex |
| 35 | + mu sync.RWMutex |
| 36 | + name string |
| 37 | + startTime time.Time |
| 38 | + endTime time.Time |
| 39 | + attributes []TraceAttribute |
| 40 | + children []*TraceSpan |
| 41 | +} |
| 42 | + |
| 43 | +type TraceAttribute struct { |
| 44 | + Key string |
| 45 | + Value TraceValue |
| 46 | +} |
| 47 | + |
| 48 | +type TraceValue struct { |
| 49 | + v any |
| 50 | +} |
| 51 | + |
| 52 | +func (t *TraceValue) AsString() string { |
| 53 | + return fmt.Sprint(t.v) |
| 54 | +} |
| 55 | + |
| 56 | +func (t *TraceValue) AsInt64() int64 { |
| 57 | + v, _ := util.ToInt64(t.v) |
| 58 | + return v |
| 59 | +} |
| 60 | + |
| 61 | +func (t *TraceValue) AsFloat64() float64 { |
| 62 | + v, _ := util.ToFloat64(t.v) |
| 63 | + return v |
| 64 | +} |
| 65 | + |
| 66 | +var globalTraceStarters []traceStarter |
| 67 | + |
| 68 | +type Tracer struct { |
| 69 | + starters []traceStarter |
| 70 | +} |
| 71 | + |
| 72 | +func (s *TraceSpan) SetAttributeString(key, value string) *TraceSpan { |
| 73 | + s.mu.Lock() |
| 74 | + defer s.mu.Unlock() |
| 75 | + |
| 76 | + s.attributes = append(s.attributes, TraceAttribute{Key: key, Value: TraceValue{v: value}}) |
| 77 | + return s |
| 78 | +} |
| 79 | + |
| 80 | +func (t *Tracer) Start(ctx context.Context, spanName string) (context.Context, *TraceSpan) { |
| 81 | + starters := t.starters |
| 82 | + if starters == nil { |
| 83 | + starters = globalTraceStarters |
| 84 | + } |
| 85 | + ts := &TraceSpan{name: spanName, startTime: time.Now()} |
| 86 | + ts.parent, _ = ctx.Value(ContextKeySpan).(*TraceSpan) |
| 87 | + if ts.parent != nil { |
| 88 | + ts.parent.children = append(ts.parent.children, ts) |
| 89 | + } |
| 90 | + for i, tsp := range starters { |
| 91 | + var internalSpan traceSpanInternal |
| 92 | + ctx, internalSpan = tsp.start(ctx, ts, i) |
| 93 | + ts.internalSpans = append(ts.internalSpans, internalSpan) |
| 94 | + } |
| 95 | + ctx = context.WithValue(ctx, ContextKeySpan, ts) |
| 96 | + return ctx, ts |
| 97 | +} |
| 98 | + |
| 99 | +func (s *TraceSpan) End() { |
| 100 | + s.mu.Lock() |
| 101 | + s.endTime = time.Now() |
| 102 | + s.mu.Unlock() |
| 103 | + |
| 104 | + for _, tsp := range s.internalSpans { |
| 105 | + tsp.end() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func GetTracer() *Tracer { |
| 110 | + return &Tracer{} |
| 111 | +} |
| 112 | + |
| 113 | +func GetContextSpan(ctx context.Context) *TraceSpan { |
| 114 | + ts, _ := ctx.Value(ContextKeySpan).(*TraceSpan) |
| 115 | + return ts |
| 116 | +} |
0 commit comments