forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencensus_test.go
More file actions
388 lines (334 loc) · 14.5 KB
/
opencensus_test.go
File metadata and controls
388 lines (334 loc) · 14.5 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package octrace
import (
"bytes"
"context"
"encoding/json"
"io"
"net"
"strings"
"testing"
"time"
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opencensus.io/plugin/ocgrpc"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/exporter/opencensusexporter"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/translator/internaldata"
)
func TestReceiver_endToEnd(t *testing.T) {
spanSink := new(consumertest.TracesSink)
addr, doneFn := ocReceiverOnGRPCServer(t, spanSink)
defer doneFn()
expFactory := opencensusexporter.NewFactory()
expCfg := expFactory.CreateDefaultConfig().(*opencensusexporter.Config)
expCfg.GRPCClientSettings.TLSSetting.Insecure = true
expCfg.Endpoint = addr.String()
expCfg.WaitForReady = true
oce, err := expFactory.CreateTracesExporter(context.Background(), component.ExporterCreateSettings{Logger: zap.NewNop()}, expCfg)
require.NoError(t, err)
err = oce.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
defer func() {
require.NoError(t, oce.Shutdown(context.Background()))
}()
td := testdata.GenerateTracesOneSpan()
assert.NoError(t, oce.ConsumeTraces(context.Background(), td))
assert.Eventually(t, func() bool {
return len(spanSink.AllTraces()) != 0
}, 10*time.Second, 5*time.Millisecond)
gotTraces := spanSink.AllTraces()
require.Len(t, gotTraces, 1)
assert.Equal(t, td, gotTraces[0])
}
// Issue #43. Export should support node multiplexing.
// The goal is to ensure that Receiver can always support
// a passthrough mode where it initiates Export normally by firstly
// receiving the initiator node. However ti should still be able to
// accept nodes from downstream sources, but if a node isn't specified in
// an exportTrace request, assume it is from the last received and non-nil node.
func TestExportMultiplexing(t *testing.T) {
spanSink := new(consumertest.TracesSink)
port, doneFn := ocReceiverOnGRPCServer(t, spanSink)
defer doneFn()
traceClient, traceClientDoneFn, err := makeTraceServiceClient(port)
require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err)
defer traceClientDoneFn()
// Step 1) The initiation.
initiatingNode := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{
Pid: 1,
HostName: "multiplexer",
},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: initiatingNode})
require.NoError(t, err, "Failed to send the initiating message: %v", err)
// Step 1a) Send some spans without a node, they should be registered as coming from the initiating node.
sLi := []*tracepb.Span{{TraceId: []byte("1234567890abcdef"), Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLi})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)
// Step 2) Send a "proxied" trace message from app1 with "node1"
node1 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 9489, HostName: "nodejs-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_NODE_JS},
}
sL1 := []*tracepb.Span{{TraceId: []byte("abcdefghijklmnop"), Name: &tracepb.TruncatableString{Value: "test"}, Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node1, Spans: sL1})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)
// Step 3) Send a trace message without a node but with spans: this
// should be registered as belonging to the last used node i.e. "node1".
sLn1 := []*tracepb.Span{{TraceId: []byte("ABCDEFGHIJKLMNOP"), Status: &tracepb.Status{}}, {TraceId: []byte("1234567890abcdef"), Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn1})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Step 4) Send a trace message from a differently proxied node "node2" from app2
node2 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 7752, HostName: "golang-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_GO_LANG},
}
sL2 := []*tracepb.Span{{TraceId: []byte("_B_D_F_H_J_L_N_O"), Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node2, Spans: sL2})
require.NoError(t, err, "Failed to send the proxied message from app2: %v", err)
// Step 5a) Send a trace message without a node but with spans: this
// should be registered as belonging to the last used node i.e. "node2".
sLn2a := []*tracepb.Span{{TraceId: []byte("_BCDEFGHIJKLMNO_"), Status: &tracepb.Status{}}, {TraceId: []byte("_234567890abcde_"), Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2a})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Step 5b)
sLn2b := []*tracepb.Span{{TraceId: []byte("_xxxxxxxxxxxxxx_"), Status: &tracepb.Status{}}, {TraceId: []byte("B234567890abcdAB"), Status: &tracepb.Status{}}}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2b})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Give the process sometime to send data over the wire and perform batching
<-time.After(150 * time.Millisecond)
// Examination time!
resultsMapping := make(map[string][]*tracepb.Span)
for _, td := range spanSink.AllTraces() {
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
node, _, spans := internaldata.ResourceSpansToOC(rss.At(i))
resultsMapping[nodeToKey(node)] = append(resultsMapping[nodeToKey(node)], spans...)
}
}
// First things first, we expect exactly 3 unique keys
// 1. Initiating Node
// 2. Node 1
// 3. Node 2
if g, w := len(resultsMapping), 3; g != w {
t.Errorf("Got %d keys in the results map; Wanted exactly %d\n\nResultsMapping: %+v\n", g, w, resultsMapping)
}
// Want span counts
wantSpanCounts := map[string]int{
nodeToKey(initiatingNode): 1,
nodeToKey(node1): 3,
nodeToKey(node2): 5,
}
for key, wantSpanCounts := range wantSpanCounts {
gotSpanCounts := len(resultsMapping[key])
if gotSpanCounts != wantSpanCounts {
t.Errorf("Key=%q gotSpanCounts %d wantSpanCounts %d", key, gotSpanCounts, wantSpanCounts)
}
}
// Now ensure that the exported spans match up exactly with
// the nodes and the last seen node expectation/behavior.
// (or at least their serialized equivalents match up)
wantContents := map[string][]*tracepb.Span{
nodeToKey(initiatingNode): sLi,
nodeToKey(node1): append(sL1, sLn1...),
nodeToKey(node2): append(sL2, append(sLn2a, sLn2b...)...),
}
for nodeKey, wantSpans := range wantContents {
gotSpans, ok := resultsMapping[nodeKey]
if !ok {
t.Errorf("Wanted to find a node that was not found for key: %s", nodeKey)
}
if len(gotSpans) != len(wantSpans) {
t.Errorf("Unequal number of spans for nodeKey: %s", nodeKey)
}
for _, wantSpan := range wantSpans {
found := false
for _, gotSpan := range gotSpans {
wantStr, _ := json.Marshal(wantSpan)
gotStr, _ := json.Marshal(gotSpan)
if bytes.Equal(wantStr, gotStr) {
found = true
}
}
if !found {
t.Errorf("Unequal span serialization\nGot:\n\t%s\nWant:\n\t%s\n", gotSpans, wantSpans)
}
}
}
}
// The first message without a Node MUST be rejected and teardown the connection.
// See https://github.com/census-instrumentation/opencensus-service/issues/53
func TestExportProtocolViolations_nodelessFirstMessage(t *testing.T) {
spanSink := new(consumertest.TracesSink)
port, doneFn := ocReceiverOnGRPCServer(t, spanSink)
defer doneFn()
traceClient, traceClientDoneFn, err := makeTraceServiceClient(port)
require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err)
defer traceClientDoneFn()
// Send a Nodeless first message
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil})
require.NoError(t, err, "Unexpectedly failed to send the first message: %v", err)
longDuration := 2 * time.Second
testDone := make(chan struct{})
goroutineDone := make(chan struct{})
go func() {
// Our insurance policy to ensure that this test doesn't hang
// forever and should quickly report if/when we regress.
select {
case <-testDone:
t.Log("Test ended early enough")
case <-time.After(longDuration):
traceClientDoneFn()
t.Errorf("Test took too long (%s) and is likely still hanging so this is a regression", longDuration)
}
close(goroutineDone)
}()
// Now the response should return an error and should have been torn down
// regardless of the number of times after invocation below, or any attempt
// to send the proper/corrective data should be rejected.
for i := 0; i < 10; i++ {
recv, err := traceClient.Recv()
if recv != nil {
t.Errorf("Iteration #%d: Unexpectedly got back a response: %#v", i, recv)
}
if err == nil {
t.Errorf("Iteration #%d: Unexpectedly got back a nil error", i)
continue
}
wantSubStr := "protocol violation: Export's first message must have a Node"
if g := err.Error(); !strings.Contains(g, wantSubStr) {
t.Errorf("Iteration #%d: Got error:\n\t%s\nWant substring:\n\t%s\n", i, g, wantSubStr)
}
// The connection should be invalid at this point and
// no attempt to send corrections should succeed.
n1 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 9489, HostName: "nodejs-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_NODE_JS},
}
if err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: n1}); err == nil {
t.Errorf("Iteration #%d: Unexpectedly succeeded in sending a message upstream. Connection must be in terminal state", i)
} else if g, w := err, io.EOF; g != w {
t.Errorf("Iteration #%d:\nGot error %q\nWant error %q", i, g, w)
}
}
close(testDone)
<-goroutineDone
}
// If the first message is valid (has a non-nil Node) and has spans, those
// spans should be received and NEVER discarded.
// See https://github.com/census-instrumentation/opencensus-service/issues/51
func TestExportProtocolConformation_spansInFirstMessage(t *testing.T) {
spanSink := new(consumertest.TracesSink)
port, doneFn := ocReceiverOnGRPCServer(t, spanSink)
defer doneFn()
traceClient, traceClientDoneFn, err := makeTraceServiceClient(port)
require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err)
defer traceClientDoneFn()
sLi := []*tracepb.Span{
{TraceId: []byte("1234567890abcdef"), Status: &tracepb.Status{}},
{TraceId: []byte("XXXXXXXXXXabcdef"), Status: &tracepb.Status{}},
}
ni := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 1},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}
err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: ni, Spans: sLi})
require.NoError(t, err, "Failed to send the first message: %v", err)
// Give it time to be sent over the wire, then exported.
<-time.After(100 * time.Millisecond)
// Examination time!
resultsMapping := make(map[string][]*tracepb.Span)
for _, td := range spanSink.AllTraces() {
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
node, _, spans := internaldata.ResourceSpansToOC(rss.At(i))
resultsMapping[nodeToKey(node)] = append(resultsMapping[nodeToKey(node)], spans...)
}
}
if g, w := len(resultsMapping), 1; g != w {
t.Errorf("Results mapping: Got len(keys) %d Want %d", g, w)
}
// Check for the keys
wantLengths := map[string]int{
nodeToKey(ni): 2,
}
for key, wantLength := range wantLengths {
gotLength := len(resultsMapping[key])
if gotLength != wantLength {
t.Errorf("Exported spans:: Key: %s\nGot length %d\nWant length %d", key, gotLength, wantLength)
}
}
// And finally ensure that the protos' serializations are equivalent to the expected
wantContents := map[string][]*tracepb.Span{
nodeToKey(ni): sLi,
}
gotBlob, _ := json.Marshal(resultsMapping)
wantBlob, _ := json.Marshal(wantContents)
if !bytes.Equal(gotBlob, wantBlob) {
t.Errorf("Unequal serialization results\nGot:\n\t%s\nWant:\n\t%s\n", gotBlob, wantBlob)
}
}
// Helper functions from here on below
func makeTraceServiceClient(addr net.Addr) (agenttracepb.TraceService_ExportClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
svc := agenttracepb.NewTraceServiceClient(cc)
traceClient, err := svc.Export(context.Background())
if err != nil {
_ = cc.Close()
return nil, nil, err
}
doneFn := func() { _ = cc.Close() }
return traceClient, doneFn, nil
}
func nodeToKey(n *commonpb.Node) string {
blob, _ := proto.Marshal(n)
return string(blob)
}
func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Traces) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)
doneFnList := []func(){func() { require.NoError(t, ln.Close()) }}
done := func() {
for _, doneFn := range doneFnList {
doneFn()
}
}
oci, err := New(config.NewID("opencensus"), sr)
require.NoError(t, err, "Failed to create the Receiver: %v", err)
// Now run it as a gRPC server
srv := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
agenttracepb.RegisterTraceServiceServer(srv, oci)
go func() {
_ = srv.Serve(ln)
}()
return ln.Addr(), done
}