generated from calmato/project-template
/
grpc.go
200 lines (163 loc) · 4.67 KB
/
grpc.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
package cmd
import (
"context"
"encoding/json"
"fmt"
"net"
"strings"
"github.com/calmato/gran-book/api/proto/book"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_ctxzap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"go.uber.org/zap"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type grpcServer struct {
s *grpc.Server
lis net.Listener
}
func newGRPCServer(port string, logger *zap.Logger, reg *registry) (*grpcServer, error) {
opts := grpcServerOptions(logger)
s := grpc.NewServer(opts...)
book.RegisterBookServiceServer(s, reg.book)
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
return nil, fmt.Errorf("config: failed to listen: %w", err)
}
gs := &grpcServer{
s: s,
lis: lis,
}
return gs, nil
}
func (s *grpcServer) Serve() error {
return s.s.Serve(s.lis)
}
func (s *grpcServer) Stop() {
s.s.GracefulStop()
}
/*
* ServerOptions
*/
func grpcServerOptions(logger *zap.Logger) []grpc.ServerOption {
streamInterceptors := grpcStreamServerInterceptors(logger)
unaryInterceptors := grpcUnaryServerInterceptors(logger)
opts := []grpc.ServerOption{
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptors...)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
}
return opts
}
/*
* ServerOptions - StremServerInterceptor
*/
func grpcStreamServerInterceptors(logger *zap.Logger) []grpc.StreamServerInterceptor {
opts := []grpc_zap.Option{}
interceptors := []grpc.StreamServerInterceptor{
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(logger, opts...),
grpc_recovery.StreamServerInterceptor(),
}
return interceptors
}
/*
* ServerOptions - UnaryServerInterceptor
*/
func grpcUnaryServerInterceptors(logger *zap.Logger) []grpc.UnaryServerInterceptor {
opts := []grpc_zap.Option{}
interceptors := []grpc.UnaryServerInterceptor{
grpc_ctxtags.UnaryServerInterceptor(),
grpc_zap.UnaryServerInterceptor(logger, opts...),
accessLogUnaryServerInterceptor(),
grpc_recovery.UnaryServerInterceptor(),
}
return interceptors
}
func accessLogUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler,
) (interface{}, error) {
clientIP := ""
if p, ok := peer.FromContext(ctx); ok {
clientIP = p.Addr.String()
}
requestID := ""
if md, ok := metadata.FromIncomingContext(ctx); ok {
if id, ok := md["x-request-id"]; ok {
requestID = strings.Join(id, ",")
}
}
userAgent := ""
if md, ok := metadata.FromIncomingContext(ctx); ok {
if u, ok := md["user-agent"]; ok {
userAgent = strings.Join(u, ",")
}
}
res, err := handler(ctx, req)
// Request / Response Messages
reqParams := map[string]interface{}{}
if p, ok := req.(proto.Message); ok {
reqParams, _ = filterParams(p)
}
resParams := map[string]interface{}{}
if p, ok := res.(proto.Message); ok {
resParams, _ = filterParams(p)
}
ds := getErrorDetails(err)
grpc_ctxzap.AddFields(
ctx,
zap.String("request.client_ip", clientIP),
zap.String("request.request_id", requestID),
zap.String("request.user_agent", userAgent),
zap.Reflect("request.content", reqParams),
zap.Reflect("response.content", resParams),
zap.Reflect("error.details", ds),
)
return res, err
}
}
func filterParams(pb proto.Message) (map[string]interface{}, error) {
bs, err := protojson.Marshal(pb)
if err != nil {
return nil, fmt.Errorf("jsonpb serializer failed: %v", err)
}
bj := make(map[string]interface{})
_ = json.Unmarshal(bs, &bj) // ignore error here.
var toFilter []string
for k := range bj {
if strings.Contains(strings.ToLower(k), "password") {
toFilter = append(toFilter, k)
}
}
for _, k := range toFilter {
bj[k] = "<FILTERED>"
}
return bj, nil
}
func getErrorDetails(err error) interface{} {
if err == nil {
return ""
}
s, ok := status.FromError(err)
if !ok {
return ""
}
// TODO: 配列に1つしか値入れないようにしてるからいったんこれで
for _, detail := range s.Details() {
switch v := detail.(type) {
case *errdetails.LocalizedMessage:
return v.Message
case *errdetails.BadRequest:
return v.FieldViolations
}
}
return ""
}