/
handler_server.go
214 lines (170 loc) · 5.82 KB
/
handler_server.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package grpc
import (
"context"
"fmt"
"runtime"
"time"
"github.com/Hyingerrr/mirco-esim/core/tracer"
"github.com/opentracing/opentracing-go/ext"
opentracinglog "github.com/opentracing/opentracing-go/log"
"github.com/Hyingerrr/mirco-esim/container"
tracerid "github.com/Hyingerrr/mirco-esim/pkg/tracer-id"
"github.com/Hyingerrr/mirco-esim/pkg/validate"
"github.com/opentracing/opentracing-go"
logx "github.com/Hyingerrr/mirco-esim/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/davecgh/go-spew/spew"
"google.golang.org/grpc/metadata"
"github.com/Hyingerrr/mirco-esim/core/meta"
"google.golang.org/grpc"
)
var checker = validate.NewValidateRepo()
func timeoutUnaryServerInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
var cancel func()
// get timeout from ctx, compare with the config
if dl, ok := ctx.Deadline(); ok {
out := time.Until(dl)
logx.Infoc(ctx, "Server UntilOut:%v", out)
if out-time.Millisecond*20 > 0 {
out = out - time.Millisecond*20 // 减去本进程中的消耗 略估计
}
if timeout > out {
timeout = out
}
}
// debug
logx.Infoc(ctx, "Server Deadline timeOut:%v", timeout)
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
dls, _ := ctx.Deadline()
logx.Infoc(ctx, "Server Deadline:%v", dls)
resp, err = handler(ctx, req)
return resp, err
}
}
func debugUnaryServerInterceptor(slowTime time.Duration) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
var (
start = time.Now()
)
// request logger
logx.Infoc(ctx, "Get_Request_Params: method[%v], server[%v], body:%+v",
info.FullMethod, info.Server, req)
resp, err = handler(ctx, req)
// response logger
logx.Infoc(ctx, "Send_Response_Params: method[%v], server[%v], cost[%v], resp_params:%+v",
info.FullMethod, info.Server, time.Since(start).String(), spew.Sdump(resp))
// check grpc slow
if sub := time.Now().Sub(start); sub > slowTime*time.Millisecond {
logx.Warnc(ctx, "Slow server %s, cost:%v", info.FullMethod, sub)
}
return resp, err
}
}
func metricUnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
var (
start = time.Now()
serviceName = container.AppName()
appId string
)
if mdCtx, ok := metadata.FromIncomingContext(ctx); ok {
if ai := mdCtx.Get(meta.AppID); len(ai) > 0 {
appId = ai[0]
} else {
logx.Infoc(ctx, "AppID_Is_Empty: method[%v], appId[%v]", info.FullMethod, appId)
}
}
// monitor
_serverGRPCReqQPS.Inc(serviceName, info.FullMethod, appId)
resp, err := handler(ctx, req)
// monitor
_serverGRPCReqDuration.Observe(float64(time.Since(start)/time.Millisecond),
serviceName, info.FullMethod, appId)
return resp, err
}
func traceUnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
esimTracer := opentracing.GlobalTracer()
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(nil)
}
spCtx, err := esimTracer.Extract(opentracing.TextMap, tracer.MetadataReaderWriter{MD: md})
if err != nil && err != opentracing.ErrSpanContextNotFound {
// todo something
logx.Errorc(ctx, "extract from metadata err:%v", err)
return handler(ctx, req)
}
span := esimTracer.StartSpan(
info.FullMethod,
ext.RPCServerOption(spCtx),
tracer.TagComponent("gRPC"),
ext.SpanKindRPCServer,
tracer.TagLocalIPV4(),
)
defer span.Finish()
newCtx := opentracing.ContextWithSpan(ctx, span)
resp, err := handler(newCtx, req)
if err != nil {
code := codes.Unknown
if s, ok := status.FromError(err); ok {
code = s.Code()
}
span.SetTag("gRPC_code", code)
ext.Error.Set(span, true)
span.LogFields(opentracinglog.String("event", "error"), opentracinglog.String("message", err.Error()))
}
span.SetTag("gRPC_code", codes.OK)
return resp, err
}
func recoverServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverFrom(r, info.FullMethod)
}
}()
resp, err = handler(ctx, req)
return
}
}
// tracerId If not found opentracing's tracer_id then generate a new tracer_id.
// Recommend to the end of the Interceptor.
func tracerIDServerInterceptor() grpc.UnaryServerInterceptor {
tracerID := tracerid.TracerID()
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
ctx = context.WithValue(ctx, tracerid.ActiveEsimKey, tracerID())
}
resp, err = handler(ctx, req)
return resp, err
}
}
func validateServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if err = checker.ValidateStruct(req); err != nil {
err = status.Error(codes.InvalidArgument, err.Error())
return
}
resp, err = handler(ctx, req)
return
}
}
func recoverFrom(r interface{}, fullMethod string) error {
var stacktrace string
for i := 1; i < 7; i++ {
_, f, l, got := runtime.Caller(i)
if !got {
break
}
stacktrace += fmt.Sprintf("%s:%d\n", f, l)
}
logx.Errorf("Handle_GRPC_PANIC. fullMethod[%v], panic: %v, stack: %v",
fullMethod, r, stacktrace)
return status.Errorf(codes.Unknown, "%s", r)
}
func extractSpanContext(ctx context.Context) (opentracing.SpanContext, error) {
return nil, nil
}