Skip to content

Commit

Permalink
Improve logging (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina authored Nov 6, 2023
1 parent e78565a commit dde69a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
29 changes: 24 additions & 5 deletions service/cachemdw/caching_middleware.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cachemdw

import (
"bytes"
"io"
"net/http"

"github.com/kava-labs/kava-proxy-service/decode"
"github.com/kava-labs/kava-proxy-service/logging"
)

// CachingMiddleware returns kava-proxy-service compatible middleware which works in the following way:
Expand Down Expand Up @@ -35,11 +38,7 @@ func (c *ServiceCache) CachingMiddleware(
req := r.Context().Value(c.decodedRequestContextKey)
decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope)
if !ok {
c.Logger.Error().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("host", r.Host).
Msg("can't cast request to *EVMRPCRequestEnvelope type")
LogCannotCastRequestError(c.ServiceLogger, r)

next.ServeHTTP(w, r)
return
Expand Down Expand Up @@ -67,6 +66,26 @@ func (c *ServiceCache) CachingMiddleware(
}
}

func LogCannotCastRequestError(logger *logging.ServiceLogger, r *http.Request) {
var bodyCopy bytes.Buffer
tee := io.TeeReader(r.Body, &bodyCopy)
// read all body from reader into bodyBytes, and copy into bodyCopy
bodyBytes, err := io.ReadAll(tee)
if err != nil {
logger.Error().Err(err).Msg("can't read lrw.body")
}

// replace empty body reader with fresh copy
r.Body = io.NopCloser(&bodyCopy)

logger.Error().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("host", r.Host).
Str("body", string(bodyBytes)).
Msg("can't cast request to *EVMRPCRequestEnvelope type")
}

// getHeadersToCache gets header map which has to be cached along with EVM JSON-RPC response
func getHeadersToCache(w http.ResponseWriter, whitelistedHeaders []string) map[string]string {
headersToCache := make(map[string]string, 0)
Expand Down
6 changes: 1 addition & 5 deletions service/cachemdw/is_cached_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func (c *ServiceCache) IsCachedMiddleware(
req := r.Context().Value(c.decodedRequestContextKey)
decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope)
if !ok {
c.Logger.Error().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("host", r.Host).
Msg("can't cast request to *EVMRPCRequestEnvelope type")
LogCannotCastRequestError(c.ServiceLogger, r)

next.ServeHTTP(w, r.WithContext(uncachedContext))
return
Expand Down
18 changes: 1 addition & 17 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,7 @@ func createProxyRequestMiddleware(next http.Handler, config config.Config, servi
req := r.Context().Value(DecodedRequestContextKey)
decodedReq, ok := (req).(*decode.EVMRPCRequestEnvelope)
if !ok {
var bodyCopy bytes.Buffer
tee := io.TeeReader(r.Body, &bodyCopy)
// read all body from reader into bodyBytes, and copy into bodyCopy
bodyBytes, err := io.ReadAll(tee)
if err != nil {
serviceLogger.Error().Err(err).Msg("can't read lrw.body")
}

// replace empty body reader with fresh copy
r.Body = io.NopCloser(&bodyCopy)

serviceLogger.Logger.Error().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("host", r.Host).
Str("body", string(bodyBytes)).
Msg("can't cast request to *EVMRPCRequestEnvelope type")
cachemdw.LogCannotCastRequestError(serviceLogger, r)

// if we can't get decoded request then assign it empty structure to avoid panics
decodedReq = new(decode.EVMRPCRequestEnvelope)
Expand Down

0 comments on commit dde69a5

Please sign in to comment.