forked from open-telemetry/opentelemetry-collector-contrib
/
opencensus_test.go
412 lines (349 loc) · 15.1 KB
/
opencensus_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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ocmetrics
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"testing"
"time"
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/opencensus"
)
var receiverID = component.MustNewID("opencensus")
func TestReceiver_endToEnd(t *testing.T) {
tt, err := componenttest.SetupTelemetry(receiverID)
require.NoError(t, err)
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()
metricSink := new(consumertest.MetricsSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink, receiver.CreateSettings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer doneFn()
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()
md := testdata.GenerateMetricsOneMetric()
node, resource, metrics := opencensus.ResourceMetricsToOC(md.ResourceMetrics().At(0))
assert.NoError(t, metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node, Resource: resource, Metrics: metrics}))
assert.Eventually(t, func() bool {
return len(metricSink.AllMetrics()) != 0
}, 10*time.Second, 5*time.Millisecond)
gotMetrics := metricSink.AllMetrics()
require.Len(t, gotMetrics, 1)
assert.Equal(t, md, gotMetrics[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 exportMetrics request, assume it is from the last received and non-nil node.
func TestExportMultiplexing(t *testing.T) {
tt, err := componenttest.SetupTelemetry(receiverID)
require.NoError(t, err)
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()
metricSink := new(consumertest.MetricsSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink, receiver.CreateSettings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer doneFn()
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()
// Step 1) The initiation.
initiatingNode := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{
Pid: 1,
HostName: "multiplexer",
},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: initiatingNode})
require.NoError(t, err, "Failed to send the initiating message: %v", err)
// Step 1a) Send some metrics without a node, they should be registered as coming from the initiating node.
mLi := []*metricspb.Metric{makeMetric(1)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLi})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)
// Step 2) Send a "proxied" metrics message from app1 with "node1"
node1 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 9489, HostName: "nodejs-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_NODE_JS},
}
mL1 := []*metricspb.Metric{makeMetric(2)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node1, Metrics: mL1})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)
// Step 3) Send a metrics message without a node but with metrics: this
// should be registered as belonging to the last used node i.e. "node1".
mLn1 := []*metricspb.Metric{makeMetric(3)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn1})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Step 4) Send a metrics 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},
}
mL2 := []*metricspb.Metric{makeMetric(4)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node2, Metrics: mL2})
require.NoError(t, err, "Failed to send the proxied message from app2: %v", err)
// Step 5a) Send a metrics message without a node but with metrics: this
// should be registered as belonging to the last used node i.e. "node2".
mLn2a := []*metricspb.Metric{makeMetric(5)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2a})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Step 5b)
mLn2b := []*metricspb.Metric{makeMetric(6)}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2b})
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][]*metricspb.Metric)
for _, md := range metricSink.AllMetrics() {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
node, _, metrics := opencensus.ResourceMetricsToOC(rms.At(i))
resultsMapping[nodeToKey(node)] = append(resultsMapping[nodeToKey(node)], metrics...)
}
}
// 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 metric counts
wantMetricCounts := map[string]int{
nodeToKey(initiatingNode): 1,
nodeToKey(node1): 2,
nodeToKey(node2): 3,
}
for key, wantMetricCounts := range wantMetricCounts {
gotMetricCounts := len(resultsMapping[key])
if gotMetricCounts != wantMetricCounts {
t.Errorf("Key=%q gotMetricCounts %d wantMetricCounts %d", key, gotMetricCounts, wantMetricCounts)
}
}
// Now ensure that the exported metrics match up exactly with
// the nodes and the last seen node expectation/behavior.
// (or at least their serialized equivalents match up)
wantContents := map[string][]*metricspb.Metric{
nodeToKey(initiatingNode): mLi,
nodeToKey(node1): append(mL1, mLn1...),
nodeToKey(node2): append(mL2, append(mLn2a, mLn2b...)...),
}
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)
}
}
// 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) {
tt, err := componenttest.SetupTelemetry(receiverID)
require.NoError(t, err)
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()
metricSink := new(consumertest.MetricsSink)
port, doneFn := ocReceiverOnGRPCServer(t, metricSink, receiver.CreateSettings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer doneFn()
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()
// Send a Nodeless first message
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil})
require.NoError(t, err, "Unexpectedly failed to send the first message: %v", err)
longDuration := 2 * time.Second
testDone := make(chan bool, 1)
var wg sync.WaitGroup
wg.Add(1)
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):
metricsClientDoneFn()
t.Errorf("Test took too long (%s) and is likely still hanging so this is a regression", longDuration)
}
wg.Done()
}()
// 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 := metricsClient.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 = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: n1}); err == nil {
t.Errorf("Iteration #%d: Unexpectedly succeeded in sending a message upstream. Connection must be in terminal state", i)
} else if !errors.Is(err, io.EOF) {
t.Errorf("Iteration #%d:\nGot error %q\nWant error %q", i, err, io.EOF)
}
}
close(testDone)
wg.Wait()
}
// If the first message is valid (has a non-nil Node) and has metrics, those
// metrics should be received and NEVER discarded.
// See https://github.com/census-instrumentation/opencensus-service/issues/51
func TestExportProtocolConformation_metricsInFirstMessage(t *testing.T) {
// This test used to be flaky on Windows. Skip if errors pop up again
tt, err := componenttest.SetupTelemetry(receiverID)
require.NoError(t, err)
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()
metricSink := new(consumertest.MetricsSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink, receiver.CreateSettings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()})
defer doneFn()
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()
mLi := []*metricspb.Metric{makeMetric(10), makeMetric(11)}
ni := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 1},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: ni, Metrics: mLi})
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][]*metricspb.Metric)
for _, md := range metricSink.AllMetrics() {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
node, _, metrics := opencensus.ResourceMetricsToOC(rms.At(i))
resultsMapping[nodeToKey(node)] = append(resultsMapping[nodeToKey(node)], metrics...)
}
}
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 metrics:: 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][]*metricspb.Metric{
nodeToKey(ni): mLi,
}
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 makeMetricsServiceClient(addr net.Addr) (agentmetricspb.MetricsService_ExportClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
svc := agentmetricspb.NewMetricsServiceClient(cc)
metricsClient, err := svc.Export(context.Background())
if err != nil {
_ = cc.Close()
return nil, nil, err
}
doneFn := func() { _ = cc.Close() }
return metricsClient, doneFn, nil
}
func nodeToKey(n *commonpb.Node) string {
blob, _ := proto.Marshal(n)
return string(blob)
}
func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics, set receiver.CreateSettings) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)
done := func() {
_ = ln.Close()
}
oci, err := New(sr, set)
require.NoError(t, err, "Failed to create the Receiver: %v", err)
// Now run it as a gRPC server
srv := grpc.NewServer()
agentmetricspb.RegisterMetricsServiceServer(srv, oci)
go func() {
_ = srv.Serve(ln)
}()
return ln.Addr(), done
}
func makeMetric(val int) *metricspb.Metric {
key := &metricspb.LabelKey{
Key: fmt.Sprintf("%s%d", "key", val),
}
value := &metricspb.LabelValue{
Value: fmt.Sprintf("%s%d", "value", val),
HasValue: true,
}
descriptor := &metricspb.MetricDescriptor{
Name: fmt.Sprintf("%s%d", "metric_descriptort_", val),
Description: "metric descriptor",
Unit: "1",
Type: metricspb.MetricDescriptor_GAUGE_INT64,
LabelKeys: []*metricspb.LabelKey{key},
}
now := time.Now().UTC()
point := &metricspb.Point{
Timestamp: timestamppb.New(now.Add(20 * time.Second)),
Value: &metricspb.Point_Int64Value{
Int64Value: int64(val),
},
}
ts := &metricspb.TimeSeries{
StartTimestamp: timestamppb.New(now.Add(-10 * time.Second)),
LabelValues: []*metricspb.LabelValue{value},
Points: []*metricspb.Point{point},
}
return &metricspb.Metric{
MetricDescriptor: descriptor,
Timeseries: []*metricspb.TimeSeries{ts},
}
}