-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
52 lines (45 loc) · 1.69 KB
/
interceptor.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
package server
import (
"fmt"
"github.com/rs/zerolog"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
)
func serverUnaryServerInterceptor(s *Server) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = s.WithContext(ctx)
return handler(ctx, req)
}
}
func loggerUnaryServerInterceptor(s *Server) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// get information from peer
p, _ := peer.FromContext(ctx)
e := eidFromIncomingContext(ctx)
// sets new logger instance with eid
sublogger := s.logger.With().
Str("eid", e).
Str("method", info.FullMethod).Logger()
sublogger.Info().
Str("data", fmt.Sprintf("%v", req)).
Dict("peer", zerolog.Dict().
Str("addr", fmt.Sprintf("%v", p.Addr)).
Str("auth", fmt.Sprintf("%v", p.AuthInfo))).
Msg(fmt.Sprintf("request %s from %v", info.FullMethod, p.Addr))
// also nice to have a logger available in context
ctx = sublogger.WithContext(ctx)
h, err := handler(ctx, req)
sublogger.Info().
Str("data", fmt.Sprintf("%v", h)).
Msg(fmt.Sprintf("response %s to %v", info.FullMethod, p.Addr))
return h, err
}
}
// InterfaceUnaryServerInterceptor wraps any type to grpc unary server
func InterfaceUnaryServerInterceptor(name string, val interface{}) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = WithContextAny(ctx, name, val)
return handler(ctx, req)
}
}