mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	Some monitoring refactoring
This commit is contained in:
		| @@ -1,7 +1,6 @@ | |||||||
| package metrics | package metrics | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"strconv" |  | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"golang.org/x/net/context" | 	"golang.org/x/net/context" | ||||||
| @@ -12,7 +11,6 @@ import ( | |||||||
|  |  | ||||||
| func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	state := middleware.State{W: w, Req: r} | 	state := middleware.State{W: w, Req: r} | ||||||
|  |  | ||||||
| 	qname := state.Name() | 	qname := state.Name() | ||||||
| 	qtype := state.Type() | 	qtype := state.Type() | ||||||
| 	zone := middleware.Zones(m.ZoneNames).Matches(qname) | 	zone := middleware.Zones(m.ZoneNames).Matches(qname) | ||||||
| @@ -24,10 +22,14 @@ func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) | |||||||
| 	rw := middleware.NewResponseRecorder(w) | 	rw := middleware.NewResponseRecorder(w) | ||||||
| 	status, err := m.Next.ServeDNS(ctx, rw, r) | 	status, err := m.Next.ServeDNS(ctx, rw, r) | ||||||
|  |  | ||||||
| 	requestCount.WithLabelValues(zone, qtype).Inc() | 	m.Report(zone, qtype, rw) | ||||||
| 	requestDuration.WithLabelValues(zone, qtype).Observe(float64(time.Since(rw.Start()) / time.Second)) |  | ||||||
| 	responseSize.WithLabelValues(zone, qtype).Observe(float64(rw.Size())) |  | ||||||
| 	responseRcode.WithLabelValues(zone, strconv.Itoa(rw.Rcode()), qtype).Inc() |  | ||||||
|  |  | ||||||
| 	return status, err | 	return status, err | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (m Metrics) Report(zone, qtype string, rw *middleware.ResponseRecorder) { | ||||||
|  | 	requestCount.WithLabelValues(zone, qtype).Inc() | ||||||
|  | 	requestDuration.WithLabelValues(zone, qtype).Observe(float64(time.Since(rw.Start()) / time.Second)) | ||||||
|  | 	responseSize.WithLabelValues(zone, qtype).Observe(float64(rw.Size())) | ||||||
|  | 	responseRcode.WithLabelValues(zone, rw.Rcode(), qtype).Inc() | ||||||
|  | } | ||||||
|   | |||||||
| @@ -62,7 +62,7 @@ func define(subsystem string) { | |||||||
| 		Namespace: namespace, | 		Namespace: namespace, | ||||||
| 		Subsystem: subsystem, | 		Subsystem: subsystem, | ||||||
| 		Name:      "request_duration_seconds", | 		Name:      "request_duration_seconds", | ||||||
| 		Buckets:   append([]float64{0, 0001, 0.0005, 0.001, 0.0025}, prometheus.DefBuckets...), | 		Buckets:   append([]float64{.0001, .0005, .001, .0025}, prometheus.DefBuckets...), | ||||||
| 		Help:      "Histogram of the time (in seconds) each request took.", | 		Help:      "Histogram of the time (in seconds) each request took.", | ||||||
| 	}, []string{"zone", "qtype"}) | 	}, []string{"zone", "qtype"}) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| package middleware | package middleware | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"strconv" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/miekg/dns" | 	"github.com/miekg/dns" | ||||||
| @@ -58,8 +59,11 @@ func (r *ResponseRecorder) Size() int { | |||||||
| } | } | ||||||
|  |  | ||||||
| // Rcode returns the rcode. | // Rcode returns the rcode. | ||||||
| func (r *ResponseRecorder) Rcode() int { | func (r *ResponseRecorder) Rcode() string { | ||||||
| 	return r.rcode | 	if rcode, ok := dns.RcodeToString[r.rcode]; ok { | ||||||
|  | 		return rcode | ||||||
|  | 	} | ||||||
|  | 	return "RCODE" + strconv.Itoa(r.rcode) | ||||||
| } | } | ||||||
|  |  | ||||||
| // Start returns the start time of the ResponseRecorder. | // Start returns the start time of the ResponseRecorder. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user