-
Notifications
You must be signed in to change notification settings - Fork 6
/
prometheus_test.go
executable file
·154 lines (134 loc) · 4.5 KB
/
prometheus_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
package promgrpc_test
import (
"net"
"testing"
"time"
"github.com/piotrkowalczuk/promgrpc"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/stats"
)
func ExampleInterceptor_Dialer() {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
var opts []grpc.DialOption
opts = append(opts, grpc.WithDialer(interceptor.Dialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
})))
}
func TestIntercepror_Collector(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
req := prometheus.NewRegistry()
if err := req.Register(interceptor); err != nil {
t.Fatal(err)
}
_, err := req.Gather()
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_Dialer(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
fn := interceptor.Dialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return nil, nil
})
_, err := fn("X", 1*time.Second)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_UnaryServer(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{TrackPeers: true})
_, err := interceptor.UnaryServer()(context.Background(), nil, &grpc.UnaryServerInfo{}, func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, nil
})
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_StreamServer(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{TrackPeers: true})
err := interceptor.StreamServer()(context.Background(), nil, &grpc.StreamServerInfo{}, func(srv interface{}, stream grpc.ServerStream) error {
return nil
})
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_UnaryClient(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
err := interceptor.UnaryClient()(context.Background(), "method", nil, nil, nil, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
return nil
})
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_StreamClient(t *testing.T) {
interceptor := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
_, err := interceptor.StreamClient()(context.Background(), &grpc.StreamDesc{}, nil, "method", func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, nil
})
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
}
func TestInterceptor_HandleConn(t *testing.T) {
handler := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
ctx := handler.TagConn(context.Background(), &stats.ConnTagInfo{
LocalAddr: &net.TCPAddr{},
RemoteAddr: &net.TCPAddr{},
})
handler.HandleConn(ctx, &stats.ConnBegin{})
handler.HandleConn(ctx, &stats.ConnBegin{Client: true})
handler.HandleConn(ctx, &stats.ConnEnd{})
handler.HandleConn(ctx, &stats.ConnEnd{Client: true})
}
func TestInterceptor_HandleRPC(t *testing.T) {
handler := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
ctx := handler.TagRPC(context.Background(), &stats.RPCTagInfo{
FullMethodName: "method",
FailFast: true,
})
handler.HandleRPC(ctx, &stats.Begin{})
handler.HandleRPC(ctx, &stats.Begin{Client: true})
handler.HandleRPC(ctx, &stats.End{})
handler.HandleRPC(ctx, &stats.End{Client: true})
}
func TestRegisterInterceptor(t *testing.T) {
ms := mockServer{
"test": grpc.ServiceInfo{
Methods: []grpc.MethodInfo{
{
Name: "regular",
},
{
Name: "client-stream",
IsClientStream: true,
},
{
Name: "server-stream",
IsServerStream: true,
},
{
Name: "bidirectional-stream",
IsClientStream: true,
IsServerStream: true,
},
},
},
}
interceptor1 := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{})
if err := promgrpc.RegisterInterceptor(ms, interceptor1); err != nil {
t.Fatal(err)
}
interceptor2 := promgrpc.NewInterceptor(promgrpc.InterceptorOpts{TrackPeers: true})
if err := promgrpc.RegisterInterceptor(ms, interceptor2); err != nil {
t.Fatal(err)
}
}
type mockServer map[string]grpc.ServiceInfo
// GetServiceInfo implements ServiceInfoProvider interface.
func (ms mockServer) GetServiceInfo() map[string]grpc.ServiceInfo {
return ms
}