forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_test.go
More file actions
99 lines (83 loc) · 2.83 KB
/
grpc_test.go
File metadata and controls
99 lines (83 loc) · 2.83 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelgrpc_test
import (
"context"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "google.golang.org/grpc/interop/grpc_testing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test"
)
var wantInstrumentationScope = instrumentation.Scope{
Name: "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc",
SchemaURL: semconv.SchemaURL,
Version: otelgrpc.Version(),
}
// newGrpcTest creates a grpc server, starts it, and returns the client, closes everything down during test cleanup.
func newGrpcTest(t testing.TB, listener net.Listener, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) pb.TestServiceClient {
grpcServer := grpc.NewServer(sOpt...)
pb.RegisterTestServiceServer(grpcServer, test.NewTestServer())
errCh := make(chan error)
go func() {
errCh <- grpcServer.Serve(listener)
}()
t.Cleanup(func() {
grpcServer.Stop()
assert.NoError(t, <-errCh)
})
cOpt = append(cOpt, grpc.WithTransportCredentials(insecure.NewCredentials()))
dialAddr := listener.Addr().String()
if l, ok := listener.(interface{ Dial() (net.Conn, error) }); ok {
dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
cOpt = append(cOpt, grpc.WithContextDialer(dial))
dialAddr = "passthrough:" + dialAddr
}
conn, err := grpc.NewClient(
dialAddr,
cOpt...,
)
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, conn.Close())
})
return pb.NewTestServiceClient(conn)
}
func doCalls(ctx context.Context, client pb.TestServiceClient) {
test.DoEmptyUnaryCall(ctx, client)
test.DoLargeUnaryCall(ctx, client)
test.DoClientStreaming(ctx, client)
test.DoServerStreaming(ctx, client)
test.DoPingPong(ctx, client)
}
func assertEvents(t *testing.T, expected, actual []trace.Event) bool { //nolint:unparam // ignore unparam lint
if !assert.Len(t, actual, len(expected)) {
return false
}
var failed bool
for i, e := range expected {
if !assert.Equal(t, e.Name, actual[i].Name, "names do not match") {
failed = true
}
if !assert.ElementsMatch(t, e.Attributes, actual[i].Attributes, "attributes do not match: %s", e.Name) {
failed = true
}
}
return !failed
}
func findAttribute(kvs []attribute.KeyValue, key attribute.Key) (attribute.KeyValue, bool) { //nolint:unparam // ignore unparam lint
for _, kv := range kvs {
if kv.Key == key {
return kv, true
}
}
return attribute.KeyValue{}, false
}