/
helper.go
78 lines (69 loc) · 1.94 KB
/
helper.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
package grpchelper
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
log "gopkg.in/src-d/go-log.v1"
"gopkg.in/src-d/lookout-sdk.v0/pb"
)
// LogAsDebug allows to log gRPC messages as debug level instead of info level
var LogAsDebug = false
var logFn = func(fields pb.Fields, format string, args ...interface{}) {
l := log.With(log.Fields(fields))
if LogAsDebug {
l.Debugf(format, args...)
} else {
l.Infof(format, args...)
}
}
// NewServer creates new grpc.Server with custom options and log interceptors
func NewServer(opts ...grpc.ServerOption) *grpc.Server {
return pb.NewServerWithInterceptors(
[]grpc.StreamServerInterceptor{
pb.LogStreamServerInterceptor(logFn),
CtxlogStreamServerInterceptor,
},
[]grpc.UnaryServerInterceptor{
pb.LogUnaryServerInterceptor(logFn),
CtxlogUnaryServerInterceptor,
},
opts...,
)
}
// DialContext creates a client connection to the given target with custom
// options and log interceptors
func DialContext(ctx context.Context, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return pb.DialContextWithInterceptors(
ctx, target,
[]grpc.StreamClientInterceptor{
CtxlogStreamClientInterceptor,
pb.LogStreamClientInterceptor(logFn),
},
[]grpc.UnaryClientInterceptor{
CtxlogUnaryClientInterceptor,
pb.LogUnaryClientInterceptor(logFn),
},
opts...,
)
}
func logConnStatus(l log.Logger, state connectivity.State) {
if state == connectivity.TransientFailure {
l.Warningf("connection failed")
} else {
l.Infof("connection state changed to '%s'", state)
}
}
// LogConnStatusChanges logs gRPC connection status changes
func LogConnStatusChanges(ctx context.Context, l log.Logger, conn *grpc.ClientConn) {
state := conn.GetState()
logConnStatus(l, state)
for {
if conn.WaitForStateChange(ctx, state) {
state = conn.GetState()
logConnStatus(l, state)
} else {
// ctx expired / canceled, stop listing
return
}
}
}