-
Notifications
You must be signed in to change notification settings - Fork 39
/
delayed_spans_test.go
163 lines (124 loc) · 3.64 KB
/
delayed_spans_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
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
153
154
155
156
157
158
159
160
161
162
163
// (c) Copyright IBM Corp. 2022
package instana
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"github.com/instana/go-sensor/acceptor"
"github.com/instana/go-sensor/autoprofile"
"github.com/stretchr/testify/assert"
)
func TestAppendALotDelayedSpans(t *testing.T) {
ds := &delayedSpans{
spans: make(chan *spanS, maxDelayedSpans),
}
i := 0
for i <= 2*maxDelayedSpans {
ds.append(&spanS{})
i++
}
assert.Len(t, ds.spans, maxDelayedSpans)
}
func resetDelayedSpans() {
delayed = &delayedSpans{
spans: make(chan *spanS, maxDelayedSpans),
}
}
func TestPartiallyFlushDelayedSpans(t *testing.T) {
defer resetDelayedSpans()
recorder := NewTestRecorder()
s := NewSensorWithTracer(NewTracerWithEverything(&Options{
Service: "go-sensor-test",
Tracer: TracerOptions{
Secrets: DefaultSecretsMatcher(),
},
}, recorder))
defer ShutdownSensor()
generateSomeTraffic(s, maxDelayedSpans)
assert.Len(t, delayed.spans, maxDelayedSpans)
notReadyAfter := maxDelayedSpans / 10
sensor.agent = &eventuallyNotReadyClient{
notReadyAfter: uint64(notReadyAfter),
}
delayed.flush()
assert.Len(t, delayed.spans, maxDelayedSpans-notReadyAfter)
}
func TestFlushDelayedSpans(t *testing.T) {
defer resetDelayedSpans()
recorder := NewTestRecorder()
s := NewSensorWithTracer(NewTracerWithEverything(&Options{
Service: "go-sensor-test",
Tracer: TracerOptions{
Secrets: DefaultSecretsMatcher(),
},
}, recorder))
defer ShutdownSensor()
generateSomeTraffic(s, maxDelayedSpans)
assert.Len(t, delayed.spans, maxDelayedSpans)
sensor.agent = alwaysReadyClient{}
delayed.flush()
assert.Len(t, delayed.spans, 0)
}
func TestParallelFlushDelayedSpans(t *testing.T) {
defer resetDelayedSpans()
m, _ := NamedMatcher(ContainsIgnoreCaseMatcher, []string{"q", "secret"})
recorder := NewTestRecorder()
s := NewSensorWithTracer(NewTracerWithEverything(&Options{
Service: "go-sensor-test",
Tracer: TracerOptions{
Secrets: m,
},
}, recorder))
defer ShutdownSensor()
generateSomeTraffic(s, maxDelayedSpans*2)
assert.Len(t, delayed.spans, maxDelayedSpans)
workers := 15
worker := 0
wg := sync.WaitGroup{}
wg.Add(workers)
sensor.agent = alwaysReadyClient{}
for worker < workers {
go func() {
delayed.flush()
wg.Done()
}()
worker++
}
wg.Wait()
recordedSpans := recorder.GetQueuedSpans()
assert.Equal(t, maxDelayedSpans, len(recordedSpans))
for _, v := range recordedSpans {
if v, ok := v.Data.(HTTPSpanData); ok {
assert.Equal(t, "q=%3Credacted%3E&secret=%3Credacted%3E", v.Tags.Params)
} else {
assert.Fail(t, "wrong span type")
}
}
}
type eventuallyNotReadyClient struct {
notReadyAfter uint64
ops uint64
}
func (e *eventuallyNotReadyClient) Ready() bool {
n := atomic.AddUint64(&e.ops, 1)
return n <= e.notReadyAfter
}
func (*eventuallyNotReadyClient) SendMetrics(data acceptor.Metrics) error { return nil }
func (*eventuallyNotReadyClient) SendEvent(event *EventData) error { return nil }
func (*eventuallyNotReadyClient) SendSpans(spans []Span) error { return nil }
func (*eventuallyNotReadyClient) SendProfiles(profiles []autoprofile.Profile) error { return nil }
func (*eventuallyNotReadyClient) Flush(context.Context) error { return nil }
func generateSomeTraffic(s TracerLogger, amount int) {
h := TracingNamedHandlerFunc(s, "action", "/{action}", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "Ok")
})
req := httptest.NewRequest(http.MethodGet, "/test?q=term&secret=mypassword", nil)
rec := httptest.NewRecorder()
for i := 0; i < amount; i++ {
h.ServeHTTP(rec, req)
}
}