Skip to content

Commit 680df1d

Browse files
authored
Merge pull request #127 from leiyiz/logLimit
add option to limit grpc logging length
2 parents 1570ee5 + 43134c2 commit 680df1d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

connection/connection.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package connection
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"io/ioutil"
2324
"net"
2425
"strings"
@@ -40,6 +41,15 @@ const (
4041

4142
const terminationLogPath = "/dev/termination-log"
4243

44+
var maxLogChar int = -1
45+
46+
// SetMaxGRPCLogLength set the maximum character count for GRPC logging.
47+
// If characterCount is set to anything smaller than or equal to 0 then there's no limit on log length.
48+
// The default log length limit is unlimited.
49+
func SetMaxGRPCLogLength(characterCount int) {
50+
maxLogChar = characterCount
51+
}
52+
4353
// Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to UNIX domain socket
4454
// file or have format '<protocol>://', following gRPC name resolution mechanism at
4555
// https://github.com/grpc/grpc/blob/master/doc/naming.md.
@@ -183,7 +193,11 @@ func LogGRPC(ctx context.Context, method string, req, reply interface{}, cc *grp
183193
klog.V(5).Infof("GRPC call: %s", method)
184194
klog.V(5).Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
185195
err := invoker(ctx, method, req, reply, cc, opts...)
186-
klog.V(5).Infof("GRPC response: %s", protosanitizer.StripSecrets(reply))
196+
cappedStr := fmt.Sprintf("%s", protosanitizer.StripSecrets(reply))
197+
if maxLogChar > 0 && len(cappedStr) > maxLogChar {
198+
cappedStr = cappedStr[:maxLogChar] + fmt.Sprintf(" [response body too large, log capped to %d chars]", maxLogChar)
199+
}
200+
klog.V(5).Infof("GRPC response: %s", cappedStr)
187201
klog.V(5).Infof("GRPC error: %v", err)
188202
return err
189203
}

0 commit comments

Comments
 (0)