Skip to content

Commit

Permalink
proxyd: Parameterize full RPC request logging (#3110)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mslipper and mergify[bot] committed Jul 27, 2022
1 parent d544f80 commit 915f3b2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-books-design.md
@@ -0,0 +1,5 @@
---
'@eth-optimism/proxyd': patch
---

Parameterize full RPC request logging
3 changes: 3 additions & 0 deletions proxyd/config.go
Expand Up @@ -18,6 +18,9 @@ type ServerConfig struct {
TimeoutSeconds int `toml:"timeout_seconds"`

MaxUpstreamBatchSize int `toml:"max_upstream_batch_size"`

EnableRequestLog bool `toml:"enable_request_log"`
MaxRequestBodyLogLen int `toml:"max_request_body_log_len"`
}

type CacheConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions proxyd/proxyd.go
Expand Up @@ -222,6 +222,8 @@ func Start(config *Config) (func(), error) {
secondsToDuration(config.Server.TimeoutSeconds),
config.Server.MaxUpstreamBatchSize,
rpcCache,
config.Server.EnableRequestLog,
config.Server.MaxRequestBodyLogLen,
)

if config.Metrics.Enabled {
Expand Down
30 changes: 21 additions & 9 deletions proxyd/server.go
Expand Up @@ -28,7 +28,7 @@ const (
MaxBatchRPCCalls = 100
cacheStatusHdr = "X-Proxyd-Cache-Status"
defaultServerTimeout = time.Second * 10
maxLogLength = 2000
maxRequestBodyLogLen = 2000
defaultMaxUpstreamBatchSize = 10
)

Expand All @@ -40,6 +40,8 @@ type Server struct {
wsMethodWhitelist *StringSet
rpcMethodMappings map[string]string
maxBodySize int64
enableRequestLog bool
maxRequestBodyLogLen int
authenticatedPaths map[string]string
timeout time.Duration
maxUpstreamBatchSize int
Expand All @@ -60,6 +62,8 @@ func NewServer(
timeout time.Duration,
maxUpstreamBatchSize int,
cache RPCCache,
enableRequestLog bool,
maxRequestBodyLogLen int,
) *Server {
if cache == nil {
cache = &NoopRPCCache{}
Expand Down Expand Up @@ -87,6 +91,8 @@ func NewServer(
timeout: timeout,
maxUpstreamBatchSize: maxUpstreamBatchSize,
cache: cache,
enableRequestLog: enableRequestLog,
maxRequestBodyLogLen: maxRequestBodyLogLen,
upgrader: &websocket.Upgrader{
HandshakeTimeout: 5 * time.Second,
},
Expand Down Expand Up @@ -169,11 +175,13 @@ func (s *Server) HandleRPC(w http.ResponseWriter, r *http.Request) {
}
RecordRequestPayloadSize(ctx, len(body))

log.Info("Raw RPC request",
"body", truncate(string(body)),
"req_id", GetReqID(ctx),
"auth", GetAuthCtx(ctx),
)
if s.enableRequestLog {
log.Info("Raw RPC request",
"body", truncate(string(body), s.maxRequestBodyLogLen),
"req_id", GetReqID(ctx),
"auth", GetAuthCtx(ctx),
)
}

if IsBatch(body) {
reqs, err := ParseBatchRPCReq(body)
Expand Down Expand Up @@ -527,9 +535,13 @@ func (n *NoopRPCCache) PutRPC(context.Context, *RPCReq, *RPCRes) error {
return nil
}

func truncate(str string) string {
if len(str) > maxLogLength {
return str[:maxLogLength] + "..."
func truncate(str string, maxLen int) string {
if maxLen == 0 {
maxLen = maxRequestBodyLogLen
}

if len(str) > maxLen {
return str[:maxLen] + "..."
} else {
return str
}
Expand Down

0 comments on commit 915f3b2

Please sign in to comment.