| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	state := middleware.State{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	qname := state.Name()
 | 
					
						
							|  |  |  | 	qtype := state.Type()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	zone := middleware.Zones(m.ZoneNames).Matches(qname)
 | 
					
						
							|  |  |  | 	if zone == "" {
 | 
					
						
							|  |  |  | 		zone = "."
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Record response to get status code and size of the reply.
 | 
					
						
							|  |  |  | 	rw := middleware.NewResponseRecorder(w)
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	status, err := m.Next.ServeDNS(ctx, rw, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	requestCount.WithLabelValues(zone, qtype).Inc()
 | 
					
						
							|  |  |  | 	requestDuration.WithLabelValues(zone).Observe(float64(time.Since(rw.Start()) / time.Second))
 | 
					
						
							|  |  |  | 	responseSize.WithLabelValues(zone).Observe(float64(rw.Size()))
 | 
					
						
							|  |  |  | 	responseRcode.WithLabelValues(zone, strconv.Itoa(rw.Rcode())).Inc()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return status, err
 | 
					
						
							|  |  |  | }
 |