Skip to content

Commit

Permalink
Reduce logs for scaler-dlx [0.4.x] (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaelgen authored Jul 4, 2022
1 parent ddb627e commit 7d414a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: "^1.14.3"
go-version: "1.14"

- uses: actions/cache@v2
with:
Expand Down
24 changes: 18 additions & 6 deletions pkg/dlx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"

"github.com/v3io/scaler/pkg/common"
Expand All @@ -25,6 +26,7 @@ type Handler struct {
targetPathHeader string
targetPort int
targetURLCache *cache.LRUExpireCache
proxyLock sync.Locker
lastProxyErrorTime time.Time
}

Expand All @@ -42,6 +44,7 @@ func NewHandler(parentLogger logger.Logger,
targetPathHeader: targetPathHeader,
targetPort: targetPort,
targetURLCache: cache.NewLRUExpireCache(100),
proxyLock: &sync.Mutex{},
lastProxyErrorTime: time.Now(),
}
h.HandleFunc = h.handleRequest
Expand Down Expand Up @@ -98,31 +101,40 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
}

targetURL := h.selectTargetURL(resourceNames, resourceTargetURLMap)
h.proxyLock.Lock()
targetURLCacheKey := targetURL.String()

//if in cache, do not log
if _, found := h.targetURLCache.Get("targetURLCache"); !found {
// if in cache, do not log to avoid multiple identical log lines.
if _, found := h.targetURLCache.Get(targetURLCacheKey); !found {
h.logger.DebugWith("Creating reverse proxy", "targetURLCache", targetURL)

// store in cache
h.targetURLCache.Add(targetURLCacheKey, true, 5*time.Second)
}
h.proxyLock.Unlock()

proxy := httputil.NewSingleHostReverseProxy(targetURL)

// override the proxy's error handler in order to make the "context canceled" log appear once every hour at most,
// because it occurs frequently and spams the logs file, but we didn't want to remove it entirely.
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
if err == nil {
return
}
timeSinceLastCtxErr := time.Since(h.lastProxyErrorTime).Hours() > 1
if strings.Contains(err.Error(), "context canceled") && timeSinceLastCtxErr {
h.lastProxyErrorTime = time.Now()
}
if !strings.Contains(err.Error(), "context canceled") || timeSinceLastCtxErr {
proxy.ErrorLog.Printf("http: proxy error: %v", err)
h.logger.DebugWith("http: proxy error", "error", err)
}
rw.WriteHeader(http.StatusBadGateway)
}

// store in cache
h.targetURLCache.Add("targetURLCache", true, time.Second)
proxy.ServeHTTP(res, req)
}

func (h *Handler) parseTargetURL(resourceName, path string) (*url.URL, int) {
h.logger.DebugWith("Resolving service name", "resourceName", resourceName)
serviceName, err := h.resourceScaler.ResolveServiceName(scaler_types.Resource{Name: resourceName})
if err != nil {
h.logger.WarnWith("Failed resolving service name",
Expand Down

0 comments on commit 7d414a3

Please sign in to comment.