mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* when no response is written, fallback to status of next plugin in prometheus plugin Signed-off-by: Ondrej Benkovsky <ondrej.benkovsky@wandera.com> * fixup! when no response is written, fallback to status of next plugin in prometheus plugin Signed-off-by: Ondrej Benkovsky <ondrej.benkovsky@wandera.com>
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package metrics
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 
 | |
| 	"github.com/coredns/coredns/plugin"
 | |
| 	"github.com/coredns/coredns/plugin/metrics/vars"
 | |
| 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | |
| 	"github.com/coredns/coredns/plugin/pkg/rcode"
 | |
| 	"github.com/coredns/coredns/request"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| // ServeDNS implements the Handler interface.
 | |
| func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | |
| 	state := request.Request{W: w, Req: r}
 | |
| 
 | |
| 	qname := state.QName()
 | |
| 	zone := plugin.Zones(m.ZoneNames()).Matches(qname)
 | |
| 	if zone == "" {
 | |
| 		zone = "."
 | |
| 	}
 | |
| 
 | |
| 	// Record response to get status code and size of the reply.
 | |
| 	rw := dnstest.NewRecorder(w)
 | |
| 	status, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, rw, r)
 | |
| 
 | |
| 	rc := rw.Rcode
 | |
| 	if !plugin.ClientWrite(status) {
 | |
| 		// when no response was written, fallback to status returned from next plugin as this status
 | |
| 		// is actually used as rcode of DNS response
 | |
| 		// see https://github.com/coredns/coredns/blob/master/core/dnsserver/server.go#L318
 | |
| 		rc = status
 | |
| 	}
 | |
| 	vars.Report(WithServer(ctx), state, zone, rcode.ToString(rc), rw.Len, rw.Start)
 | |
| 
 | |
| 	return status, err
 | |
| }
 | |
| 
 | |
| // Name implements the Handler interface.
 | |
| func (m *Metrics) Name() string { return "prometheus" }
 |