Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce logs for scaler-dlx #52

Merged
merged 6 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

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

- uses: actions/cache@v2
with:
Expand All @@ -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 All @@ -51,7 +51,7 @@ jobs:

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

- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:

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

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

"github.com/v3io/scaler/pkg/scalertypes"

"github.com/nuclio/errors"
"github.com/nuclio/logger"
"k8s.io/apimachinery/pkg/util/cache"
)

type Handler struct {
Expand All @@ -24,6 +26,9 @@ type Handler struct {
targetPathHeader string
targetPort int
multiTargetStrategy scalertypes.MultiTargetStrategy
targetURLCache *cache.LRUExpireCache
proxyLock sync.Locker
lastProxyErrorTime time.Time
}

func NewHandler(parentLogger logger.Logger,
Expand All @@ -41,6 +46,9 @@ func NewHandler(parentLogger logger.Logger,
targetPathHeader: targetPathHeader,
targetPort: targetPort,
multiTargetStrategy: multiTargetStrategy,
targetURLCache: cache.NewLRUExpireCache(100),
proxyLock: &sync.Mutex{},
lastProxyErrorTime: time.Now(),
}
h.HandleFunc = h.handleRequest
return h, nil
Expand Down Expand Up @@ -102,13 +110,40 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
return
}

h.logger.DebugWith("Creating reverse proxy", "targetURL", targetURL)
h.proxyLock.Lock()
targetURLCacheKey := targetURL.String()

// 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 {
h.logger.DebugWith("http: proxy error", "error", err)
}
Comment on lines +134 to +139
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment what are you trying to achieve here / why

rw.WriteHeader(http.StatusBadGateway)
}

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(scalertypes.Resource{Name: resourceName})
if err != nil {
h.logger.WarnWith("Failed resolving service name",
Expand Down