| 
									
										
										
										
											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} | 
					
						
							|  |  |  | 	qname := state.Name() | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	net := state.Proto() | 
					
						
							| 
									
										
										
										
											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-09 17:42:31 +01:00
										 |  |  | 	Report(zone, net, 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-04-09 17:42:31 +01:00
										 |  |  | func Report(zone, net, rcode string, size int, start time.Time) { | 
					
						
							| 
									
										
										
										
											2016-04-06 14:13:29 +01:00
										 |  |  | 	if requestCount == nil { | 
					
						
							|  |  |  | 		// no metrics are enabled | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	requestCount.WithLabelValues(zone, net).Inc() | 
					
						
							|  |  |  | 	requestDuration.WithLabelValues(zone).Observe(float64(time.Since(start) / time.Second)) | 
					
						
							|  |  |  | 	responseSize.WithLabelValues(zone).Observe(float64(size)) | 
					
						
							|  |  |  | 	responseRcode.WithLabelValues(zone, rcode).Inc() | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | } |