-
Notifications
You must be signed in to change notification settings - Fork 0
/
span_exporter_test.go
103 lines (85 loc) · 2.94 KB
/
span_exporter_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package bugsnagperformance
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func TestSpanExporter(t *testing.T) {
resetEnv()
testSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if header := r.Header.Get(samplingRequestHeader); header == "" {
t.Errorf("Expected %s header to be set", samplingRequestHeader)
}
}))
defer testSrv.Close()
Config.Endpoint = testSrv.URL
probMgr := &probabilityManager{probability: 1.0}
sampler := createSampler(probMgr)
commonSpanExporterTest(t, probMgr, sampler)
}
type testCustomSampler struct{}
func (s *testCustomSampler) ShouldSample(parameters sdktrace.SamplingParameters) sdktrace.SamplingResult {
return sdktrace.SamplingResult{
Decision: sdktrace.RecordAndSample,
}
}
func (s *testCustomSampler) Description() string {
return "testCustomSampler"
}
func TestSpanExporterWithCustomSampler(t *testing.T) {
resetEnv()
testSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if header := r.Header.Get(samplingRequestHeader); header != "" {
t.Errorf("Expected %s header not to be set", samplingRequestHeader)
}
}))
defer testSrv.Close()
Config.Endpoint = testSrv.URL
// custom sampler
Config.CustomSampler = &testCustomSampler{}
probMgr := &probabilityManager{probability: 1.0}
sampler := createSampler(probMgr)
commonSpanExporterTest(t, probMgr, sampler)
}
func TestSpanExporterCustomSpanIsOurs(t *testing.T) {
resetEnv()
testSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("REQUEST: %v\n", r)
if header := r.Header.Get(samplingRequestHeader); header == "" {
t.Errorf("Expected %s header to be set", samplingRequestHeader)
}
}))
defer testSrv.Close()
Config.Endpoint = testSrv.URL
probMgr := &probabilityManager{probability: 1.0}
sampler := createSampler(probMgr)
Config.CustomSampler = sampler
commonSpanExporterTest(t, probMgr, sampler)
}
func commonSpanExporterTest(t *testing.T, probMgr *probabilityManager, sampler *Sampler) {
testExporter := tracetest.NewInMemoryExporter()
options := []sdktrace.TracerProviderOption{
sdktrace.WithSpanProcessor(sdktrace.NewSimpleSpanProcessor(testExporter)),
}
tracerProvider := sdktrace.NewTracerProvider(options...)
makeSpan(tracerProvider, "test1", float64(0.1))
makeSpan(tracerProvider, "test2", float64(1.0))
makeSpan(tracerProvider, "test3", float64(0.5))
spanExporter := SpanExporter{
disabled: false,
loggedFirstBatchDestination: false,
probabilityManager: probMgr,
delivery: createDelivery(),
sampler: sampler,
unmanagedMode: false,
}
spans := testExporter.GetSpans().Snapshots()
err := spanExporter.ExportSpans(context.Background(), spans)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}