Skip to content

Commit

Permalink
Don't double report metrics on error
Browse files Browse the repository at this point in the history
When there is an error use a different function to report the metrics,
in case the plugin chain handled the request the metrics are already
reported.

Builds up on #2716
Fixes: #2717

Signed-off-by: Miek Gieben <miek@miek.nl>
  • Loading branch information
miekg committed Mar 24, 2019
1 parent e9b9a91 commit dc32dcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions core/dnsserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// The default dns.Mux checks the question section size, but we have our
// own mux here. Check if we have a question section. If not drop them here.
if r == nil || len(r.Question) == 0 {
errorFunc(ctx, w, r, dns.RcodeServerFailure)
errorAndMetricsFunc(ctx, w, r, dns.RcodeServerFailure)
return
}

Expand All @@ -215,13 +215,13 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// need to make sure that we stay alive up here
if rec := recover(); rec != nil {
vars.Panic.Inc()
errorFunc(ctx, w, r, dns.RcodeServerFailure)
errorAndMetricsFunc(ctx, w, r, dns.RcodeServerFailure)
}
}()
}

if !s.classChaos && r.Question[0].Qclass != dns.ClassINET {
errorFunc(ctx, w, r, dns.RcodeRefused)
errorAndMetricsFunc(ctx, w, r, dns.RcodeRefused)
return
}

Expand Down Expand Up @@ -310,7 +310,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
}

// Still here? Error out with REFUSED.
errorFunc(ctx, w, r, dns.RcodeRefused)
errorAndMetricsFunc(ctx, w, r, dns.RcodeRefused)
}

// OnStartupComplete lists the sites served by this server
Expand Down Expand Up @@ -338,6 +338,13 @@ func (s *Server) Tracer() ot.Tracer {

// errorFunc responds to an DNS request with an error.
func errorFunc(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, rc int) {
answer := new(dns.Msg)
answer.SetRcode(r, rc)
state.SizeAndDo(answer)
w.WriteMsg(answer)
}

func errorAndMetricsFunc(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, rc int) {
state := request.Request{W: w, Req: r}

answer := new(dns.Msg)
Expand Down
4 changes: 3 additions & 1 deletion plugin/metrics/vars/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/miekg/dns"
)

// Report reports the metrics data associated with request.
// Report reports the metrics data associated with request. This function is exported because it is also
// called from core/dnsserver to report requests hitting the server that should not be handled and are thus
// not sent down the plugin chain.
func Report(ctx context.Context, req request.Request, zone, rcode string, size int, start time.Time) {
// Proto and Family.
net := req.Proto()
Expand Down

0 comments on commit dc32dcf

Please sign in to comment.