forked from go-admin-team/go-admin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_interceptors.go
93 lines (79 loc) · 3.16 KB
/
server_interceptors.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
/*
* @Author: lwnmengjing
* @Date: 2021/5/18 9:13 下午
* @Last Modified by: lwnmengjing
* @Last Modified time: 2021/5/18 9:13 下午
*/
package logging
import (
"context"
"path"
"time"
"github.com/xuanlingzi/go-admin-core/server/grpc/interceptors/logging/ctxlog"
"github.com/xuanlingzi/go-admin-core/tools/utils"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc"
)
var (
// SystemField is used in every log statement made through grpc_zap. Can be overwritten before any initialization code.
SystemField = ctxlog.NewFields("system", "grpc")
// ServerField is used in every server-side log statement made through grpc_zap.Can be overwritten before initialization.
ServerField = ctxlog.NewFields("span.kind", "server")
)
// UnaryServerInterceptor returns a new unary server interceptors that adds zap.Logger to the context.
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
o := evaluateServerOpt(opts)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()
newCtx := newLoggerForCall(ctx, info.FullMethod, startTime, o.timestampFormat)
resp, err := handler(newCtx, req)
if !o.shouldLog(info.FullMethod, err) {
return resp, err
}
code := o.codeFunc(err)
level := o.levelFunc(code)
duration := o.durationFunc(time.Since(startTime))
o.messageFunc(newCtx, "finished unary call with code "+code.String(), level, code, err, duration)
return resp, err
}
}
// StreamServerInterceptor returns a new streaming server interceptor that adds zap.Logger to the context.
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
o := evaluateServerOpt(opts)
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
startTime := time.Now()
newCtx := newLoggerForCall(stream.Context(), info.FullMethod, startTime, o.timestampFormat)
wrapped := middleware.WrapServerStream(stream)
wrapped.WrappedContext = newCtx
err := handler(srv, wrapped)
if !o.shouldLog(info.FullMethod, err) {
return err
}
code := o.codeFunc(err)
level := o.levelFunc(code)
duration := o.durationFunc(time.Since(startTime))
o.messageFunc(newCtx, "finished streaming call with code "+code.String(), level, code, err, duration)
return err
}
}
func serverCallFields(fullMethodString string) ctxlog.Fields {
service := path.Dir(fullMethodString)[1:]
method := path.Base(fullMethodString)
f := *SystemField
f.Merge(ServerField)
f.Set("grpc.service", service)
f.Set("grpc.method", method)
return f
}
func newLoggerForCall(ctx context.Context, fullMethodString string, start time.Time, timestampFormat string) context.Context {
f := serverCallFields(fullMethodString)
f.Set("grpc.start_time", start.Format(timestampFormat))
if d, ok := ctx.Deadline(); ok {
f.Set("grpc.request.deadline", d.Format(timestampFormat))
}
requestID := utils.GetRequestID(ctx)
f.Set(utils.RequestIDKey, requestID)
callLog := ctxlog.Extract(ctx).WithFields(f.Values())
ctx = context.WithValue(ctx, utils.RequestIDKey, requestID)
return ctxlog.ToContext(ctx, callLog)
}