| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	qname := state.QName()
 | 
					
						
							| 
									
										
										
										
											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-06-23 11:21:12 +01:00
										 |  |  | 	Report(state, zone, 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.
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | func Report(state middleware.State, zone, rcode string, size int, start time.Time) {
 | 
					
						
							| 
									
										
										
										
											2016-04-06 14:13:29 +01:00
										 |  |  | 	if requestCount == nil {
 | 
					
						
							|  |  |  | 		// no metrics are enabled
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	// Proto and Family
 | 
					
						
							|  |  |  | 	net := state.Proto()
 | 
					
						
							|  |  |  | 	fam := "1"
 | 
					
						
							|  |  |  | 	if state.Family() == 2 {
 | 
					
						
							|  |  |  | 		fam = "2"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	requestCount.WithLabelValues(zone, net, fam).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	requestDuration.WithLabelValues(zone).Observe(float64(time.Since(start) / time.Second))
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	requestSize.WithLabelValues(zone).Observe(float64(state.Size()))
 | 
					
						
							|  |  |  | 	if state.Do() {
 | 
					
						
							|  |  |  | 		requestDo.WithLabelValues(zone).Inc()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	responseSize.WithLabelValues(zone).Observe(float64(size))
 | 
					
						
							|  |  |  | 	responseRcode.WithLabelValues(zone, rcode).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |