From eed7d7bbc90ccaca129bc6b6c3fd18b19e6bf856 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Fri, 9 Apr 2021 16:30:14 -0500 Subject: [PATCH] TUN-4206: Better error message when user is only using one ingress rule --- ingress/ingress.go | 5 +++++ origin/proxy.go | 24 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ingress/ingress.go b/ingress/ingress.go index db951216..f8b22df4 100644 --- a/ingress/ingress.go +++ b/ingress/ingress.go @@ -138,6 +138,11 @@ func (ing Ingress) IsEmpty() bool { 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. func (ing Ingress) StartOrigins( wg *sync.WaitGroup, diff --git a/origin/proxy.go b/origin/proxy.go index 87f77b3a..95ffa0d9 100644 --- a/origin/proxy.go +++ b/origin/proxy.go @@ -19,6 +19,8 @@ import ( const ( TagHeaderNamePrefix = "Cf-Warp-Tag-" + LogFieldCFRay = "cfRay" + LogFieldRule = "ingressRule" ) type proxy struct { @@ -84,7 +86,7 @@ func (p *proxy) Proxy(w connection.ResponseWriter, req *http.Request, sourceConn if sourceConnectionType == connection.TypeHTTP { 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 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 { - p.logRequestError(err, cfRay, ruleNum) + p.logRequestError(err, cfRay, ruleField(p.ingressRules, ruleNum)) return err } 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 { // Support for WSGI Servers by switching transfer encoding from chunked to gzip/deflate 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() + log := p.log.Error().Err(err) if cfRay != "" { - p.log.Error().Msgf("CF-RAY: %s Proxying to ingress %v error: %v", cfRay, rule, err) - } else { - p.log.Error().Msgf("Proxying to ingress %v error: %v", rule, err) + log = log.Str(LogFieldCFRay, cfRay) } + if rule != "" { + log = log.Str(LogFieldRule, rule) + } + log.Msg("") } func findCfRayHeader(req *http.Request) string {