| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"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-04-05 15:54:06 +01:00
										 |  |  | func (m Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	state := middleware.State{W: w, Req: r}
 | 
					
						
							|  |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 14:13:29 +01:00
										 |  |  | 	Report(zone, qtype, rw.Rcode(), rw.Size(), rw.Start())
 | 
					
						
							| 
									
										
										
										
											2016-04-06 13:42:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return status, err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 14:13:29 +01:00
										 |  |  | // Report is a plain reporting function that the server can use for REFUSED and other
 | 
					
						
							|  |  |  | // queries that are turned down because they don't match any middleware.
 | 
					
						
							|  |  |  | func Report(zone, qtype, rcode string, size int, start time.Time) {
 | 
					
						
							|  |  |  | 	if requestCount == nil {
 | 
					
						
							|  |  |  | 		// no metrics are enabled
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	requestCount.WithLabelValues(zone, qtype).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-04-06 14:13:29 +01:00
										 |  |  | 	requestDuration.WithLabelValues(zone, qtype).Observe(float64(time.Since(start) / time.Second))
 | 
					
						
							|  |  |  | 	responseSize.WithLabelValues(zone, qtype).Observe(float64(size))
 | 
					
						
							|  |  |  | 	responseRcode.WithLabelValues(zone, rcode, qtype).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |