-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathreceiver_test.go
340 lines (285 loc) · 10.8 KB
/
receiver_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awsxrayreceiver
import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsxrayreceiver/internal/udppoller"
)
const (
segmentHeader = "{\"format\": \"json\", \"version\": 1}\n"
defaultRegionEnvName = "AWS_DEFAULT_REGION"
mockRegion = "us-west-2"
)
func TestProxyCreationFailed(t *testing.T) {
addr, err := findAvailableUDPAddress()
assert.NoError(t, err, "there should be address available")
sink := new(consumertest.TracesSink)
_, err = newReceiver(
&Config{
AddrConfig: confignet.AddrConfig{
Endpoint: addr,
Transport: udppoller.Transport,
},
ProxyServer: &proxy.Config{
TCPAddrConfig: confignet.TCPAddrConfig{
Endpoint: "invalidEndpoint",
},
},
},
sink,
receivertest.NewNopSettings(),
)
assert.Error(t, err, "receiver creation should fail due to failure to create TCP proxy")
}
func TestPollerCreationFailed(t *testing.T) {
sink := new(consumertest.TracesSink)
_, err := newReceiver(
&Config{
AddrConfig: confignet.AddrConfig{
Endpoint: "dontCare",
Transport: confignet.TransportTypeTCP,
},
},
sink,
receivertest.NewNopSettings(),
)
assert.Error(t, err, "receiver creation should fail due to failure to create UCP poller")
}
// TODO: Update this test to assert on the format of traces
// once the transformation from X-Ray segments -> OTLP is done.
func TestSegmentsPassedToConsumer(t *testing.T) {
if runtime.GOOS == "darwin" {
t.Skip("skipping test on darwin")
}
t.Skip("Flaky Test - See /~https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/10596")
receiverID := component.MustNewID("TestSegmentsPassedToConsumer")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
addr, rcvr, _ := createAndOptionallyStartReceiver(t, nil, true, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer func() {
assert.NoError(t, rcvr.Shutdown(context.Background()))
}()
content, err := os.ReadFile(filepath.Join("../../internal/aws/xray", "testdata", "ddbSample.txt"))
assert.NoError(t, err, "can not read raw segment")
err = writePacket(t, addr, segmentHeader+string(content))
assert.NoError(t, err, "can not write packet in the happy case")
sink := rcvr.(*xrayReceiver).consumer.(*consumertest.TracesSink)
assert.Eventuallyf(t, func() bool {
got := sink.AllTraces()
return len(got) == 1
}, 10*time.Second, 5*time.Millisecond, "consumer should eventually get the X-Ray span")
assert.NoError(t, tt.CheckReceiverTraces(udppoller.Transport, 18, 0))
}
func TestTranslatorErrorsOut(t *testing.T) {
receiverID := component.MustNewID("TestTranslatorErrorsOut")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
addr, rcvr, recordedLogs := createAndOptionallyStartReceiver(t, nil, true, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer func() {
assert.NoError(t, rcvr.Shutdown(context.Background()))
}()
err = writePacket(t, addr, segmentHeader+"invalidSegment")
assert.NoError(t, err, "can not write packet in the "+receiverID.String()+" case")
assert.Eventuallyf(t, func() bool {
logs := recordedLogs.All()
return len(logs) > 0 && strings.Contains(logs[len(logs)-1].Message,
"X-Ray segment to OT traces conversion failed")
}, 10*time.Second, 5*time.Millisecond, "poller should log warning because consumer errored out")
assert.NoError(t, tt.CheckReceiverTraces(udppoller.Transport, 1, 1))
}
func TestSegmentsConsumerErrorsOut(t *testing.T) {
receiverID := component.MustNewID("TestSegmentsConsumerErrorsOut")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
addr, rcvr, recordedLogs := createAndOptionallyStartReceiver(t, consumertest.NewErr(errors.New("can't consume traces")), true, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer func() {
assert.NoError(t, rcvr.Shutdown(context.Background()))
}()
content, err := os.ReadFile(filepath.Join("../../internal/aws/xray", "testdata", "serverSample.txt"))
assert.NoError(t, err, "can not read raw segment")
err = writePacket(t, addr, segmentHeader+string(content))
assert.NoError(t, err, "can not write packet")
assert.Eventuallyf(t, func() bool {
logs := recordedLogs.All()
return len(logs) > 0 && strings.Contains(logs[len(logs)-1].Message,
"Trace consumer errored out")
}, 10*time.Second, 5*time.Millisecond, "poller should log warning because consumer errored out")
assert.NoError(t, tt.CheckReceiverTraces(udppoller.Transport, 1, 1))
}
func TestPollerCloseError(t *testing.T) {
receiverID := component.MustNewID("TestPollerCloseError")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
_, rcvr, _ := createAndOptionallyStartReceiver(t, nil, false, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
mPoller := &mockPoller{closeErr: errors.New("mockPollerCloseErr")}
rcvr.(*xrayReceiver).poller = mPoller
rcvr.(*xrayReceiver).server = &mockProxy{}
err = rcvr.Shutdown(context.Background())
assert.ErrorIs(t, err, mPoller.closeErr, "expected error")
}
func TestProxyCloseError(t *testing.T) {
receiverID := component.MustNewID("TestPollerCloseError")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
_, rcvr, _ := createAndOptionallyStartReceiver(t, nil, false, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
mProxy := &mockProxy{closeErr: errors.New("mockProxyCloseErr")}
rcvr.(*xrayReceiver).poller = &mockPoller{}
rcvr.(*xrayReceiver).server = mProxy
err = rcvr.Shutdown(context.Background())
assert.ErrorIs(t, err, mProxy.closeErr, "expected error")
}
func TestBothPollerAndProxyCloseError(t *testing.T) {
receiverID := component.MustNewID("TestBothPollerAndProxyCloseError")
tt, err := componenttest.SetupTelemetry(receiverID)
assert.NoError(t, err, "SetupTelemetry should succeed")
defer func() {
assert.NoError(t, tt.Shutdown(context.Background()))
}()
t.Setenv(defaultRegionEnvName, mockRegion)
_, rcvr, _ := createAndOptionallyStartReceiver(t, nil, false, receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
mPoller := &mockPoller{closeErr: errors.New("mockPollerCloseErr")}
mProxy := &mockProxy{closeErr: errors.New("mockProxyCloseErr")}
rcvr.(*xrayReceiver).poller = mPoller
rcvr.(*xrayReceiver).server = mProxy
err = rcvr.Shutdown(context.Background())
assert.ErrorIs(t, err, mPoller.closeErr, "expected error")
assert.ErrorIs(t, err, mProxy.closeErr, "expected error")
}
type mockPoller struct {
closeErr error
}
func (m *mockPoller) SegmentsChan() <-chan udppoller.RawSegment {
return make(chan udppoller.RawSegment, 1)
}
func (m *mockPoller) Start(_ context.Context) {}
func (m *mockPoller) Close() error {
if m.closeErr != nil {
return m.closeErr
}
return nil
}
type mockProxy struct {
closeErr error
}
func (m *mockProxy) ListenAndServe() error {
return errors.New("returning from ListenAndServe() always errors out")
}
func (m *mockProxy) Shutdown(_ context.Context) error {
if m.closeErr != nil {
return m.closeErr
}
return nil
}
func createAndOptionallyStartReceiver(
t *testing.T,
csu consumer.Traces,
start bool,
set receiver.Settings,
) (string, receiver.Traces, *observer.ObservedLogs) {
addr, err := findAvailableUDPAddress()
assert.NoError(t, err, "there should be address available")
tcpAddr := testutil.GetAvailableLocalAddress(t)
var sink consumer.Traces
if csu == nil {
sink = new(consumertest.TracesSink)
} else {
sink = csu
}
logger, recorded := logSetup()
set.Logger = logger
rcvr, err := newReceiver(
&Config{
AddrConfig: confignet.AddrConfig{
Endpoint: addr,
Transport: udppoller.Transport,
},
ProxyServer: &proxy.Config{
TCPAddrConfig: confignet.TCPAddrConfig{
Endpoint: tcpAddr,
},
},
},
sink,
set,
)
assert.NoError(t, err, "receiver should be created")
if start {
err = rcvr.Start(context.Background(), componenttest.NewNopHost())
assert.NoError(t, err, "receiver should be started")
}
return addr, rcvr, recorded
}
// findAvailableUDPAddress finds an available local address+port and returns it.
// There might be race condition on the address returned by this function if
// there's some other code that grab the address before we can listen on it.
func findAvailableUDPAddress() (string, error) {
addr, err := net.ResolveUDPAddr(udppoller.Transport, "localhost:0")
if err != nil {
return "", err
}
sock, err := net.ListenUDP(udppoller.Transport, addr)
if err != nil {
return "", err
}
defer sock.Close()
return sock.LocalAddr().String(), nil
}
func writePacket(t *testing.T, addr, toWrite string) error {
conn, err := net.Dial(udppoller.Transport, addr)
if err != nil {
return err
}
defer conn.Close()
n, err := fmt.Fprint(conn, toWrite)
if err != nil {
return err
}
assert.Equal(t, len(toWrite), n, "unexpected number of bytes written")
return nil
}
func logSetup() (*zap.Logger, *observer.ObservedLogs) {
core, recorded := observer.New(zapcore.InfoLevel)
return zap.New(core), recorded
}