This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 315
/
testtracer_test.go
89 lines (78 loc) · 2.62 KB
/
testtracer_test.go
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
package opentracing
import (
"strconv"
"strings"
)
const testHTTPHeaderPrefix = "testprefix-"
// testTracer is a most-noop Tracer implementation that makes it possible for
// unittests to verify whether certain methods were / were not called.
type testTracer struct{}
var fakeIDSource = 1
func nextFakeID() int {
fakeIDSource++
return fakeIDSource
}
type testSpan struct {
OperationName string
HasParent bool
FakeID int
}
// testSpan:
func (n testSpan) SetTag(key string, value interface{}) Span { return n }
func (n testSpan) Finish() {}
func (n testSpan) FinishWithOptions(opts FinishOptions) {}
func (n testSpan) SetBaggageItem(key, val string) Span { return n }
func (n testSpan) BaggageItem(key string) string { return "" }
func (n testSpan) LogEvent(event string) {}
func (n testSpan) LogEventWithPayload(event string, payload interface{}) {}
func (n testSpan) Log(data LogData) {}
func (n testSpan) SetOperationName(operationName string) Span { return n }
func (n testSpan) Tracer() Tracer { return testTracer{} }
// StartSpan belongs to the Tracer interface.
func (n testTracer) StartSpan(operationName string) Span {
return testSpan{
OperationName: operationName,
HasParent: false,
FakeID: nextFakeID(),
}
}
// StartSpanWithOptions belongs to the Tracer interface.
func (n testTracer) StartSpanWithOptions(opts StartSpanOptions) Span {
return testSpan{
OperationName: opts.OperationName,
HasParent: opts.Parent != nil,
FakeID: nextFakeID(),
}
}
// Inject belongs to the Tracer interface.
func (n testTracer) Inject(sp Span, format interface{}, carrier interface{}) error {
span := sp.(testSpan)
switch format {
case TextMap:
carrier.(TextMapWriter).Set(testHTTPHeaderPrefix+"fakeid", strconv.Itoa(span.FakeID))
return nil
}
return ErrUnsupportedFormat
}
// Join belongs to the Tracer interface.
func (n testTracer) Join(operationName string, format interface{}, carrier interface{}) (Span, error) {
switch format {
case TextMap:
// Just for testing purposes... generally not a worthwhile thing to
// propagate.
rval := testSpan{}
err := carrier.(TextMapReader).ForeachKey(func(key, val string) error {
switch strings.ToLower(key) {
case testHTTPHeaderPrefix + "fakeid":
i, err := strconv.Atoi(val)
if err != nil {
return err
}
rval.FakeID = i
}
return nil
})
return rval, err
}
return nil, ErrTraceNotFound
}