-
Notifications
You must be signed in to change notification settings - Fork 32
/
tester.go
87 lines (74 loc) · 2.27 KB
/
tester.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
package metrics
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/config"
"go.opentelemetry.io/otel/attribute"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"testing"
)
type testHandler struct {
*baseHandler
exporter *tracetest.InMemoryExporter
}
// TestHandler is a handler that can be used for testing traces.
type TestHandler interface {
Handler
// GetSpansByName returns all spans with the given name.
GetSpansByName(name string) (spans []tracetest.SpanStub)
}
// NewTestTracer returns a new test tracer.
func NewTestTracer(ctx context.Context, tb testing.TB) TestHandler {
tb.Helper()
th := testHandler{}
th.exporter = tracetest.NewInMemoryExporter()
buildInfo := config.NewBuildInfo(config.DefaultVersion, config.DefaultCommit, "test", config.DefaultDate)
th.baseHandler = newBaseHandler(buildInfo, tracesdk.WithSyncer(th.exporter), tracesdk.WithSampler(tracesdk.AlwaysSample()))
err := th.baseHandler.Start(ctx)
assert.Nil(tb, err)
return &th
}
func (t *testHandler) Type() HandlerType {
return Null
}
// GetSpansByName returns all spans with the given name.
func (t *testHandler) GetSpansByName(name string) (spans []tracetest.SpanStub) {
allSpans := t.exporter.GetSpans()
for _, span := range allSpans {
if span.Name == name {
spans = append(spans, span)
}
}
return
}
// SpanEventByName returns the value of the first event with the given name.
// it is a helper function for tests.
func SpanEventByName(stub tracetest.SpanStub, name string) *attribute.Value {
for _, event := range stub.Events {
if event.Name == name {
return &event.Attributes[0].Value
}
}
return nil
}
// SpanAttributeByName returns the value of the first attribute with the given name.
// it is a helper function for tests.
func SpanAttributeByName(stub tracetest.SpanStub, name string) *attribute.Value {
for _, attr := range stub.Attributes {
if attr.Key == attribute.Key(name) {
return &attr.Value
}
}
return nil
}
// SpanHasException returns true if the span has an exception event.
// it is a helper function for tests.
func SpanHasException(stub tracetest.SpanStub) bool {
for _, event := range stub.Events {
if event.Name == "exception" {
return true
}
}
return false
}