aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/reverseproxy/upstreams.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp/reverseproxy/upstreams.go')
-rw-r--r--modules/caddyhttp/reverseproxy/upstreams.go62
1 files changed, 41 insertions, 21 deletions
diff --git a/modules/caddyhttp/reverseproxy/upstreams.go b/modules/caddyhttp/reverseproxy/upstreams.go
index c8ba930d2..aa59dc41b 100644
--- a/modules/caddyhttp/reverseproxy/upstreams.go
+++ b/modules/caddyhttp/reverseproxy/upstreams.go
@@ -12,6 +12,7 @@ import (
"time"
"go.uber.org/zap"
+ "go.uber.org/zap/zapcore"
"github.com/caddyserver/caddy/v2"
)
@@ -136,10 +137,13 @@ func (su SRVUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
return allNew(cached.upstreams), nil
}
- su.logger.Debug("refreshing SRV upstreams",
- zap.String("service", service),
- zap.String("proto", proto),
- zap.String("name", name))
+ if c := su.logger.Check(zapcore.DebugLevel, "refreshing SRV upstreams"); c != nil {
+ c.Write(
+ zap.String("service", service),
+ zap.String("proto", proto),
+ zap.String("name", name),
+ )
+ }
_, records, err := su.resolver.LookupSRV(r.Context(), service, proto, name)
if err != nil {
@@ -148,23 +152,30 @@ func (su SRVUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
// only return an error if no records were also returned.
if len(records) == 0 {
if su.GracePeriod > 0 {
- su.logger.Error("SRV lookup failed; using previously cached", zap.Error(err))
+ if c := su.logger.Check(zapcore.ErrorLevel, "SRV lookup failed; using previously cached"); c != nil {
+ c.Write(zap.Error(err))
+ }
cached.freshness = time.Now().Add(time.Duration(su.GracePeriod) - time.Duration(su.Refresh))
srvs[suAddr] = cached
return allNew(cached.upstreams), nil
}
return nil, err
}
- su.logger.Warn("SRV records filtered", zap.Error(err))
+ if c := su.logger.Check(zapcore.WarnLevel, "SRV records filtered"); c != nil {
+ c.Write(zap.Error(err))
+ }
}
upstreams := make([]Upstream, len(records))
for i, rec := range records {
- su.logger.Debug("discovered SRV record",
- zap.String("target", rec.Target),
- zap.Uint16("port", rec.Port),
- zap.Uint16("priority", rec.Priority),
- zap.Uint16("weight", rec.Weight))
+ if c := su.logger.Check(zapcore.DebugLevel, "discovered SRV record"); c != nil {
+ c.Write(
+ zap.String("target", rec.Target),
+ zap.Uint16("port", rec.Port),
+ zap.Uint16("priority", rec.Priority),
+ zap.Uint16("weight", rec.Weight),
+ )
+ }
addr := net.JoinHostPort(rec.Target, strconv.Itoa(int(rec.Port)))
upstreams[i] = Upstream{Dial: addr}
}
@@ -361,10 +372,13 @@ func (au AUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
name := repl.ReplaceAll(au.Name, "")
port := repl.ReplaceAll(au.Port, "")
- au.logger.Debug("refreshing A upstreams",
- zap.String("version", ipVersion),
- zap.String("name", name),
- zap.String("port", port))
+ if c := au.logger.Check(zapcore.DebugLevel, "refreshing A upstreams"); c != nil {
+ c.Write(
+ zap.String("version", ipVersion),
+ zap.String("name", name),
+ zap.String("port", port),
+ )
+ }
ips, err := au.resolver.LookupIP(r.Context(), ipVersion, name)
if err != nil {
@@ -373,8 +387,9 @@ func (au AUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
upstreams := make([]Upstream, len(ips))
for i, ip := range ips {
- au.logger.Debug("discovered A record",
- zap.String("ip", ip.String()))
+ if c := au.logger.Check(zapcore.DebugLevel, "discovered A record"); c != nil {
+ c.Write(zap.String("ip", ip.String()))
+ }
upstreams[i] = Upstream{
Dial: net.JoinHostPort(ip.String(), port),
}
@@ -467,11 +482,16 @@ func (mu MultiUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
up, err := src.GetUpstreams(r)
if err != nil {
- mu.logger.Error("upstream source returned error",
- zap.Int("source_idx", i),
- zap.Error(err))
+ if c := mu.logger.Check(zapcore.ErrorLevel, "upstream source returned error"); c != nil {
+ c.Write(
+ zap.Int("source_idx", i),
+ zap.Error(err),
+ )
+ }
} else if len(up) == 0 {
- mu.logger.Warn("upstream source returned 0 upstreams", zap.Int("source_idx", i))
+ if c := mu.logger.Check(zapcore.WarnLevel, "upstream source returned 0 upstreams"); c != nil {
+ c.Write(zap.Int("source_idx", i))
+ }
} else {
upstreams = append(upstreams, up...)
}