mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-21 09:46:40 +00:00
Improve logging requests
This commit is contained in:
@@ -15,6 +15,33 @@ const (
|
||||
capturedDataKey requestContextKey = "capturedData"
|
||||
)
|
||||
|
||||
// ResponseOrigin indicates where a response was generated.
|
||||
type ResponseOrigin int
|
||||
|
||||
const (
|
||||
// OriginBackend means the response came from the backend service.
|
||||
OriginBackend ResponseOrigin = iota
|
||||
// OriginNoRoute means the proxy had no matching host or path.
|
||||
OriginNoRoute
|
||||
// OriginProxyError means the proxy failed to reach the backend.
|
||||
OriginProxyError
|
||||
// OriginAuth means the proxy intercepted the request for authentication.
|
||||
OriginAuth
|
||||
)
|
||||
|
||||
func (o ResponseOrigin) String() string {
|
||||
switch o {
|
||||
case OriginNoRoute:
|
||||
return "no_route"
|
||||
case OriginProxyError:
|
||||
return "proxy_error"
|
||||
case OriginAuth:
|
||||
return "auth"
|
||||
default:
|
||||
return "backend"
|
||||
}
|
||||
}
|
||||
|
||||
// CapturedData is a mutable struct that allows downstream handlers
|
||||
// to pass data back up the middleware chain.
|
||||
type CapturedData struct {
|
||||
@@ -22,6 +49,7 @@ type CapturedData struct {
|
||||
RequestID string
|
||||
ServiceId string
|
||||
AccountId types.AccountID
|
||||
Origin ResponseOrigin
|
||||
}
|
||||
|
||||
// GetRequestID safely gets the request ID
|
||||
@@ -59,6 +87,20 @@ func (c *CapturedData) GetAccountId() types.AccountID {
|
||||
return c.AccountId
|
||||
}
|
||||
|
||||
// SetOrigin safely sets the response origin
|
||||
func (c *CapturedData) SetOrigin(origin ResponseOrigin) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.Origin = origin
|
||||
}
|
||||
|
||||
// GetOrigin safely gets the response origin
|
||||
func (c *CapturedData) GetOrigin() ResponseOrigin {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.Origin
|
||||
}
|
||||
|
||||
// WithCapturedData adds a CapturedData struct to the context
|
||||
func WithCapturedData(ctx context.Context, data *CapturedData) context.Context {
|
||||
return context.WithValue(ctx, capturedDataKey, data)
|
||||
|
||||
@@ -48,6 +48,9 @@ func NewReverseProxy(transport http.RoundTripper, forwardedProto string, logger
|
||||
func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
result, exists := p.findTargetForRequest(r)
|
||||
if !exists {
|
||||
if cd := CapturedDataFromContext(r.Context()); cd != nil {
|
||||
cd.SetOrigin(OriginNoRoute)
|
||||
}
|
||||
requestID := getRequestID(r)
|
||||
web.ServeErrorPage(w, r, http.StatusNotFound, "Service Not Found",
|
||||
"The requested service could not be found. Please check the URL, try refreshing, or check if the peer is running. If that doesn't work, see our documentation for help.",
|
||||
@@ -156,8 +159,15 @@ func extractForwardedPort(host, resolvedProto string) string {
|
||||
// proxyErrorHandler handles errors from the reverse proxy and serves
|
||||
// user-friendly error pages instead of raw error responses.
|
||||
func proxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if cd := CapturedDataFromContext(r.Context()); cd != nil {
|
||||
cd.SetOrigin(OriginProxyError)
|
||||
}
|
||||
requestID := getRequestID(r)
|
||||
title, message, code, status := classifyProxyError(err)
|
||||
|
||||
log.Warnf("proxy error: request_id=%s method=%s host=%s path=%s status=%d title=%q err=%v",
|
||||
requestID, r.Method, r.Host, r.URL.Path, code, title, err)
|
||||
|
||||
web.ServeErrorPage(w, r, code, title, message, requestID, status)
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ func (p *ReverseProxy) findTargetForRequest(req *http.Request) (targetResult, bo
|
||||
host = h
|
||||
}
|
||||
|
||||
p.logger.Debugf("looking for mapping for host: %s, path: %s", host, req.URL.Path)
|
||||
m, exists := p.mappings[host]
|
||||
if !exists {
|
||||
p.logger.Debugf("no mapping found for host: %s", host)
|
||||
|
||||
Reference in New Issue
Block a user