Skip to content

Commit b6d36c6

Browse files
authored
Improved gRPC debug logger (#1730)
Since many gRPC calls may happen together, to better understand what's happening now: - each call is identified by a sequence number (even if they are interleaved) - when a non-streaming calls ends a "CALL END" log is printed
1 parent ca8c48a commit b6d36c6

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

Diff for: cli/daemon/interceptors.go

+28-18
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@ import (
2121
"fmt"
2222
"os"
2323
"strings"
24+
"sync/atomic"
2425

2526
"google.golang.org/grpc"
2627
)
2728

2829
var debugStdOut = os.Stdout
30+
var debugSeq uint32
2931

30-
func log(isRequest bool, msg interface{}) {
31-
j, _ := json.MarshalIndent(msg, "| ", " ")
32-
inOut := map[bool]string{true: "REQ: ", false: "RESP: "}
33-
fmt.Fprintln(debugStdOut, "| "+inOut[isRequest]+string(j))
32+
func log(isRequest bool, seq uint32, msg interface{}) {
33+
prefix := fmt.Sprint(seq, " | ")
34+
j, _ := json.MarshalIndent(msg, prefix, " ")
35+
inOut := "RESP: "
36+
if isRequest {
37+
inOut = "REQ: "
38+
}
39+
fmt.Fprintln(debugStdOut, prefix+inOut+string(j))
3440
}
3541

36-
func logError(err error) {
42+
func logError(seq uint32, err error) {
3743
if err != nil {
38-
fmt.Fprintln(debugStdOut, "| ERROR: ", err)
44+
fmt.Fprintln(debugStdOut, seq, "| ERROR: ", err)
3945
}
4046
}
4147

@@ -55,11 +61,13 @@ func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
5561
if !logSelector(info.FullMethod) {
5662
return handler(ctx, req)
5763
}
58-
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod)
59-
log(true, req)
64+
seq := atomic.AddUint32(&debugSeq, 1)
65+
fmt.Fprintln(debugStdOut, seq, "CALLED:", info.FullMethod)
66+
log(true, seq, req)
6067
resp, err := handler(ctx, req)
61-
logError(err)
62-
log(false, resp)
68+
logError(seq, err)
69+
log(false, seq, resp)
70+
fmt.Fprintln(debugStdOut, seq, "CALL END")
6371
fmt.Fprintln(debugStdOut)
6472
return resp, err
6573
}
@@ -68,35 +76,37 @@ func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *gr
6876
if !logSelector(info.FullMethod) {
6977
return handler(srv, stream)
7078
}
79+
seq := atomic.AddUint32(&debugSeq, 1)
7180
streamReq := ""
7281
if info.IsClientStream {
7382
streamReq = "STREAM_REQ "
7483
}
7584
if info.IsServerStream {
7685
streamReq += "STREAM_RESP"
7786
}
78-
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod, streamReq)
79-
err := handler(srv, &loggingServerStream{ServerStream: stream})
80-
logError(err)
81-
fmt.Fprintln(debugStdOut, "STREAM CLOSED")
87+
fmt.Fprintln(debugStdOut, seq, "CALLED:", info.FullMethod, streamReq)
88+
err := handler(srv, &loggingServerStream{ServerStream: stream, seq: seq})
89+
logError(seq, err)
90+
fmt.Fprintln(debugStdOut, seq, "STREAM CLOSED")
8291
fmt.Fprintln(debugStdOut)
8392
return err
8493
}
8594

8695
type loggingServerStream struct {
8796
grpc.ServerStream
97+
seq uint32
8898
}
8999

90100
func (l *loggingServerStream) RecvMsg(m interface{}) error {
91101
err := l.ServerStream.RecvMsg(m)
92-
logError(err)
93-
log(true, m)
102+
logError(l.seq, err)
103+
log(true, l.seq, m)
94104
return err
95105
}
96106

97107
func (l *loggingServerStream) SendMsg(m interface{}) error {
98108
err := l.ServerStream.SendMsg(m)
99-
logError(err)
100-
log(false, m)
109+
logError(l.seq, err)
110+
log(false, l.seq, m)
101111
return err
102112
}

0 commit comments

Comments
 (0)