-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathlogging.go
94 lines (84 loc) · 2.89 KB
/
logging.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
package grpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/golang-jwt/jwt/v4"
grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/logging"
ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func logRequest(entry *logrus.Entry, info string, pbMsg interface{}, ctx context.Context, logClaims bool) {
if logClaims {
claims := ctx.Value("claims")
mapClaims, ok := claims.(jwt.MapClaims)
if ok {
copy := make(map[string]interface{})
for k, v := range mapClaims {
if k != "groups" || entry.Logger.IsLevelEnabled(logrus.DebugLevel) {
copy[k] = v
}
}
if data, err := json.Marshal(copy); err == nil {
entry = entry.WithField("grpc.request.claims", string(data))
}
}
}
if p, ok := pbMsg.(proto.Message); ok {
entry = entry.WithField("grpc.request.content", &jsonpbMarshalleble{p})
}
entry.Info(info)
}
type jsonpbMarshalleble struct {
proto.Message
}
func (j *jsonpbMarshalleble) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
m := &jsonpb.Marshaler{}
err := m.Marshal(&b, j.Message)
if err != nil {
return nil, fmt.Errorf("jsonpb serializer failed: %v", err)
}
return b.Bytes(), nil
}
type loggingServerStream struct {
grpc.ServerStream
entry *logrus.Entry
logClaims bool
info string
}
func (l *loggingServerStream) SendMsg(m interface{}) error {
return l.ServerStream.SendMsg(m)
}
func (l *loggingServerStream) RecvMsg(m interface{}) error {
err := l.ServerStream.RecvMsg(m)
if err == nil {
logRequest(l.entry, l.info, m, l.ServerStream.Context(), l.logClaims)
}
return err
}
func PayloadStreamServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if !decider(stream.Context(), info.FullMethod, srv) {
return handler(srv, stream)
}
logEntry := entry.WithFields(ctx_logrus.Extract(stream.Context()).Data)
newStream := &loggingServerStream{ServerStream: stream, entry: logEntry, logClaims: logClaims, info: fmt.Sprintf("received streaming call %s", info.FullMethod)}
return handler(srv, newStream)
}
}
func PayloadUnaryServerInterceptor(entry *logrus.Entry, logClaims bool, decider grpc_logging.ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !decider(ctx, info.FullMethod, info.Server) {
return handler(ctx, req)
}
logEntry := entry.WithFields(ctx_logrus.Extract(ctx).Data)
logRequest(logEntry, fmt.Sprintf("received unary call %s", info.FullMethod), req, ctx, logClaims)
resp, err := handler(ctx, req)
return resp, err
}
}