TUN-4206: Better error message when user is only using one ingress rule

This commit is contained in:
Adam Chalmers 2021-04-09 16:30:14 -05:00
parent 1073f8db40
commit eed7d7bbc9
2 changed files with 23 additions and 6 deletions

View File

@ -138,6 +138,11 @@ func (ing Ingress) IsEmpty() bool {
return len(ing.Rules) == 0 return len(ing.Rules) == 0
} }
// IsSingleRule checks if the user only specified a single ingress rule.
func (ing Ingress) IsSingleRule() bool {
return len(ing.Rules) == 1
}
// StartOrigins will start any origin services managed by cloudflared, e.g. proxy servers or Hello World. // StartOrigins will start any origin services managed by cloudflared, e.g. proxy servers or Hello World.
func (ing Ingress) StartOrigins( func (ing Ingress) StartOrigins(
wg *sync.WaitGroup, wg *sync.WaitGroup,

View File

@ -19,6 +19,8 @@ import (
const ( const (
TagHeaderNamePrefix = "Cf-Warp-Tag-" TagHeaderNamePrefix = "Cf-Warp-Tag-"
LogFieldCFRay = "cfRay"
LogFieldRule = "ingressRule"
) )
type proxy struct { type proxy struct {
@ -84,7 +86,7 @@ func (p *proxy) Proxy(w connection.ResponseWriter, req *http.Request, sourceConn
if sourceConnectionType == connection.TypeHTTP { if sourceConnectionType == connection.TypeHTTP {
if err := p.proxyHTTPRequest(w, req, rule, logFields); err != nil { if err := p.proxyHTTPRequest(w, req, rule, logFields); err != nil {
p.logRequestError(err, cfRay, ruleNum) p.logRequestError(err, cfRay, ruleField(p.ingressRules, ruleNum))
return err return err
} }
return nil return nil
@ -97,12 +99,19 @@ func (p *proxy) Proxy(w connection.ResponseWriter, req *http.Request, sourceConn
} }
if err := p.proxyStreamRequest(serveCtx, w, req, connectionProxy, logFields); err != nil { if err := p.proxyStreamRequest(serveCtx, w, req, connectionProxy, logFields); err != nil {
p.logRequestError(err, cfRay, ruleNum) p.logRequestError(err, cfRay, ruleField(p.ingressRules, ruleNum))
return err return err
} }
return nil return nil
} }
func ruleField(ing ingress.Ingress, ruleNum int) string {
if ing.IsSingleRule() {
return ""
}
return fmt.Sprintf("%d", ruleNum)
}
func (p *proxy) proxyHTTPRequest(w connection.ResponseWriter, req *http.Request, rule *ingress.Rule, fields logFields) error { func (p *proxy) proxyHTTPRequest(w connection.ResponseWriter, req *http.Request, rule *ingress.Rule, fields logFields) error {
// Support for WSGI Servers by switching transfer encoding from chunked to gzip/deflate // Support for WSGI Servers by switching transfer encoding from chunked to gzip/deflate
if rule.Config.DisableChunkedEncoding { if rule.Config.DisableChunkedEncoding {
@ -257,13 +266,16 @@ func (p *proxy) logOriginResponse(resp *http.Response, fields logFields) {
} }
} }
func (p *proxy) logRequestError(err error, cfRay string, rule interface{}) { func (p *proxy) logRequestError(err error, cfRay string, rule string) {
requestErrors.Inc() requestErrors.Inc()
log := p.log.Error().Err(err)
if cfRay != "" { if cfRay != "" {
p.log.Error().Msgf("CF-RAY: %s Proxying to ingress %v error: %v", cfRay, rule, err) log = log.Str(LogFieldCFRay, cfRay)
} else {
p.log.Error().Msgf("Proxying to ingress %v error: %v", rule, err)
} }
if rule != "" {
log = log.Str(LogFieldRule, rule)
}
log.Msg("")
} }
func findCfRayHeader(req *http.Request) string { func findCfRayHeader(req *http.Request) string {