Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tracing_test.go « tracing « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 191261f6f4c932b44edf4d9a61e4c2456419d8ce (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package tracing

import (
	"testing"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/log"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/uber/jaeger-client-go"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
)

func TestCreateSpan(t *testing.T) {
	reporter := stubTracingReporter(t)
	var span opentracing.Span
	span, _ = StartSpan(testhelper.Context(t), "root", Tags{
		"tagRoot1": "value1",
		"tagRoot2": "value2",
		"tagRoot3": "value3",
	})
	span.Finish()

	require.Equal(t, []string{"root"}, reportedSpans(t, reporter))
	require.Equal(t, Tags{
		"tagRoot1": "value1",
		"tagRoot2": "value2",
		"tagRoot3": "value3",
	}, spanTags(span))
}

func TestCreateSpanIfHasParent_emptyContext(t *testing.T) {
	reporter := stubTracingReporter(t)
	ctx := testhelper.Context(t)

	var span, span2 opentracing.Span

	span, ctx = StartSpanIfHasParent(ctx, "should-not-report-root", nil)
	span.SetBaggageItem("baggage", "baggageValue")
	span.SetTag("tag", "tagValue")
	span.LogFields(log.String("log", "logValue"))
	span.LogKV("log2", "logValue")
	span.Finish()

	span2, _ = StartSpanIfHasParent(ctx, "should-not-report-child", nil)
	span2.Finish()

	require.Empty(t, reportedSpans(t, reporter))
}

func TestCreateSpanIfHasParent_hasParent(t *testing.T) {
	reporter := stubTracingReporter(t)
	ctx := testhelper.Context(t)

	var span1, span2 opentracing.Span
	span1, ctx = StartSpan(ctx, "root", nil)
	span2, _ = StartSpanIfHasParent(ctx, "child", nil)
	span2.Finish()
	span1.Finish()

	spans := reportedSpans(t, reporter)
	require.Equal(t, []string{"child", "root"}, spans)
}

func TestCreateSpanIfHasParent_hasParentWithTags(t *testing.T) {
	reporter := stubTracingReporter(t)
	ctx := testhelper.Context(t)

	var span1, span2 opentracing.Span
	span1, ctx = StartSpan(ctx, "root", Tags{
		"tagRoot1": "value1",
		"tagRoot2": "value2",
		"tagRoot3": "value3",
	})
	span2, _ = StartSpanIfHasParent(ctx, "child", Tags{
		"tagChild1": "value1",
		"tagChild2": "value2",
		"tagChild3": "value3",
	})
	span2.Finish()
	span1.Finish()

	spans := reportedSpans(t, reporter)
	require.Equal(t, []string{"child", "root"}, spans)
	require.Equal(t, Tags{
		"tagRoot1": "value1",
		"tagRoot2": "value2",
		"tagRoot3": "value3",
	}, spanTags(span1))
	require.Equal(t, Tags{
		"tagChild1": "value1",
		"tagChild2": "value2",
		"tagChild3": "value3",
	}, spanTags(span2))
}

func TestDiscardSpanInContext_emptyContext(t *testing.T) {
	ctx := DiscardSpanInContext(testhelper.Context(t))
	require.Nil(t, opentracing.SpanFromContext(ctx))
}

func TestDiscardSpanInContext_hasParent(t *testing.T) {
	reporter := stubTracingReporter(t)
	ctx := testhelper.Context(t)

	var span1, span2, span3 opentracing.Span
	span1, ctx = StartSpan(ctx, "root", nil)
	span2, ctx = StartSpanIfHasParent(ctx, "child", nil)
	ctx = DiscardSpanInContext(ctx)
	span3, _ = StartSpanIfHasParent(ctx, "discarded", nil)

	span3.Finish()
	span2.Finish()
	span1.Finish()

	spans := reportedSpans(t, reporter)
	require.Equal(t, []string{"child", "root"}, spans)
}

func stubTracingReporter(t *testing.T) *jaeger.InMemoryReporter {
	reporter := jaeger.NewInMemoryReporter()
	tracer, tracerCloser := jaeger.NewTracer("", jaeger.NewConstSampler(true), reporter)
	t.Cleanup(func() { testhelper.MustClose(t, tracerCloser) })

	old := opentracing.GlobalTracer()
	t.Cleanup(func() {
		opentracing.SetGlobalTracer(old)
	})
	opentracing.SetGlobalTracer(tracer)
	return reporter
}

func reportedSpans(t *testing.T, reporter *jaeger.InMemoryReporter) []string {
	var names []string
	for _, span := range reporter.GetSpans() {
		if !assert.IsType(t, span, &jaeger.Span{}) {
			continue
		}
		jaegerSpan := span.(*jaeger.Span)
		names = append(names, jaegerSpan.OperationName())
	}
	return names
}

func spanTags(span opentracing.Span) Tags {
	tags := Tags{}
	jaegerSpan := span.(*jaeger.Span)
	for key, value := range jaegerSpan.Tags() {
		tags[key] = value
	}
	return tags
}