| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/pkg/rcode"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							| 
									
										
										
										
											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-09-23 09:14:12 +01:00
										 |  |  | // ServeDNS implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := request.Request{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.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	rw := dnsrecorder.New(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-09-07 11:10:16 +01:00
										 |  |  | 	Report(state, zone, rcode.ToString(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-09-07 11:10:16 +01:00
										 |  |  | func Report(req request.Request, 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
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	net := req.Proto()
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	fam := "1"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if req.Family() == 2 {
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		fam = "2"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	typ := req.QType()
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	requestCount.WithLabelValues(zone, net, fam).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	requestDuration.WithLabelValues(zone).Observe(float64(time.Since(start) / time.Millisecond))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	if req.Do() {
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		requestDo.WithLabelValues(zone).Inc()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 	if _, known := monitorType[typ]; known {
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:47:54 +01:00
										 |  |  | 		requestType.WithLabelValues(zone, dns.Type(typ).String()).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 	} else {
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:47:54 +01:00
										 |  |  | 		requestType.WithLabelValues(zone, other).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	if typ == dns.TypeIXFR || typ == dns.TypeAXFR {
 | 
					
						
							|  |  |  | 		responseTransferSize.WithLabelValues(zone, net).Observe(float64(size))
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		requestTransferSize.WithLabelValues(zone, net).Observe(float64(req.Size()))
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	} else {
 | 
					
						
							|  |  |  | 		responseSize.WithLabelValues(zone, net).Observe(float64(size))
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		requestSize.WithLabelValues(zone, net).Observe(float64(req.Size()))
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	responseRcode.WithLabelValues(zone, rcode).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | var monitorType = map[uint16]bool{
 | 
					
						
							|  |  |  | 	dns.TypeAAAA:   true,
 | 
					
						
							|  |  |  | 	dns.TypeA:      true,
 | 
					
						
							|  |  |  | 	dns.TypeCNAME:  true,
 | 
					
						
							|  |  |  | 	dns.TypeDNSKEY: true,
 | 
					
						
							|  |  |  | 	dns.TypeDS:     true,
 | 
					
						
							|  |  |  | 	dns.TypeMX:     true,
 | 
					
						
							|  |  |  | 	dns.TypeNSEC3:  true,
 | 
					
						
							|  |  |  | 	dns.TypeNSEC:   true,
 | 
					
						
							|  |  |  | 	dns.TypeNS:     true,
 | 
					
						
							|  |  |  | 	dns.TypePTR:    true,
 | 
					
						
							|  |  |  | 	dns.TypeRRSIG:  true,
 | 
					
						
							|  |  |  | 	dns.TypeSOA:    true,
 | 
					
						
							|  |  |  | 	dns.TypeSRV:    true,
 | 
					
						
							|  |  |  | 	dns.TypeTXT:    true,
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	// Meta Qtypes
 | 
					
						
							|  |  |  | 	dns.TypeIXFR: true,
 | 
					
						
							|  |  |  | 	dns.TypeAXFR: true,
 | 
					
						
							|  |  |  | 	dns.TypeANY:  true,
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const other = "other"
 |