-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathlog.go
114 lines (100 loc) Β· 3.04 KB
/
log.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
package middleware
import (
"context"
"crypto/rand"
"encoding/base64"
"io"
"time"
"github.com/golang/protobuf/proto"
"goa.design/goa/middleware"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// UnaryServerLog returns a middleware that logs incoming gRPC requests
// and outgoing responses. The middleware uses the request ID set by
// the RequestID middleware or creates a short unique request ID if
// missing for each incoming request and logs it with the request and
// corresponding response details.
//
// The middleware logs the incoming requests gRPC method. It also logs the
// response gRPC status code, message length (in bytes), and timing information.
func UnaryServerLog(l middleware.Logger) grpc.UnaryServerInterceptor {
return grpc.UnaryServerInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
var reqID string
{
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.MD{}
}
reqID = MetadataValue(md, RequestIDMetadataKey)
if reqID == "" {
reqID = shortID()
}
}
started := time.Now()
// before executing rpc
l.Log("id", reqID,
"method", info.FullMethod,
"bytes", messageLength(req))
// invoke rpc
resp, err = handler(ctx, req)
// after executing rpc
s, _ := status.FromError(err)
l.Log("id", reqID,
"status", s.Code(),
"bytes", messageLength(resp),
"time", time.Since(started).String())
return resp, err
})
}
// StreamServerLog returns a middleware that logs incoming streaming gRPC
// requests and responses. The middleware uses the request ID set by the
// RequestID middleware or creates a short unique request ID if missing for
// each incoming request and logs it with the request and corresponding
// response details.
func StreamServerLog(l middleware.Logger) grpc.StreamServerInterceptor {
return grpc.StreamServerInterceptor(func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
var reqID string
{
md, ok := metadata.FromIncomingContext(ss.Context())
if !ok {
md = metadata.MD{}
}
reqID = MetadataValue(md, RequestIDMetadataKey)
if reqID == "" {
reqID = shortID()
}
}
started := time.Now()
// before executing rpc
l.Log("id", reqID,
"method", info.FullMethod,
"msg", "started stream")
// invoke rpc
err := handler(srv, ss)
// after executing rpc
s, _ := status.FromError(err)
l.Log("id", reqID,
"status", s.Code(),
"msg", "completed stream",
"time", time.Since(started).String())
return err
})
}
// shortID produces a " unique" 6 bytes long string.
// Do not use as a reliable way to get unique IDs, instead use for things like logging.
func shortID() string {
b := make([]byte, 6)
io.ReadFull(rand.Reader, b)
return base64.RawURLEncoding.EncodeToString(b)
}
func messageLength(msg interface{}) int64 {
var length int64
{
if m, ok := msg.(proto.Message); ok {
length = int64(proto.Size(m))
}
}
return length
}