File: tracing.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (72 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package catfile

import (
	"context"
	"sync"

	"github.com/opentracing/opentracing-go"
	"github.com/prometheus/client_golang/prometheus"
	"gitlab.com/gitlab-org/gitaly/v16/internal/tracing"
)

type trace struct {
	span    opentracing.Span
	counter *prometheus.CounterVec

	requestsLock sync.Mutex
	requests     map[string]int
}

// startTrace starts a new tracing span and updates metrics according to how many requests have been
// done during that trace. The caller must call `finish()` on the resulting after it's deemed to be
// done such that metrics get recorded correctly.
func startTrace(
	ctx context.Context,
	counter *prometheus.CounterVec,
	methodName string,
) *trace {
	var span opentracing.Span
	if methodName == "" {
		span = &tracing.NoopSpan{}
	} else {
		span, _ = tracing.StartSpanIfHasParent(ctx, methodName, nil)
	}

	trace := &trace{
		span:    span,
		counter: counter,
		requests: map[string]int{
			"blob":   0,
			"commit": 0,
			"tree":   0,
			"tag":    0,
			"info":   0,
		},
	}

	return trace
}

func (t *trace) recordRequest(requestType string) {
	t.requestsLock.Lock()
	defer t.requestsLock.Unlock()
	t.requests[requestType]++
}

func (t *trace) finish() {
	t.requestsLock.Lock()
	defer t.requestsLock.Unlock()

	for requestType, requestCount := range t.requests {
		if requestCount == 0 {
			continue
		}

		t.span.SetTag(requestType, requestCount)
		if t.counter != nil {
			t.counter.WithLabelValues(requestType).Add(float64(requestCount))
		}
	}

	t.span.Finish()
}