| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // Package metrics implement a handler and middleware that provides Prometheus
 | 
					
						
							|  |  |  | // metrics.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-04-06 09:21:46 +01:00
										 |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	requestCount        *prometheus.CounterVec
 | 
					
						
							|  |  |  | 	requestDuration     *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	requestSize         *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	requestTransferSize *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	requestDo           *prometheus.CounterVec
 | 
					
						
							|  |  |  | 	requestType         *prometheus.CounterVec
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	responseSize         *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	responseTransferSize *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	responseRcode        *prometheus.CounterVec
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics
 | 
					
						
							|  |  |  | type Metrics struct {
 | 
					
						
							|  |  |  | 	Next      middleware.Handler
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	Addr      string
 | 
					
						
							|  |  |  | 	ln        net.Listener
 | 
					
						
							|  |  |  | 	mux       *http.ServeMux
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 	Once      sync.Once
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	ZoneNames []string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // OnStartup sets up the metrics on startup.
 | 
					
						
							|  |  |  | func (m *Metrics) OnStartup() error {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	m.Once.Do(func() {
 | 
					
						
							| 
									
										
										
										
											2016-04-22 07:11:25 +01:00
										 |  |  | 		define()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 		ln, err := net.Listen("tcp", m.Addr)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 			log.Printf("[ERROR] Failed to start metrics handler: %s", err)
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		m.ln = ln
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 		m.mux = http.NewServeMux()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		prometheus.MustRegister(requestCount)
 | 
					
						
							|  |  |  | 		prometheus.MustRegister(requestDuration)
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		prometheus.MustRegister(requestSize)
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 		prometheus.MustRegister(requestTransferSize)
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		prometheus.MustRegister(requestDo)
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 		prometheus.MustRegister(requestType)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		prometheus.MustRegister(responseSize)
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 		prometheus.MustRegister(responseTransferSize)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		prometheus.MustRegister(responseRcode)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 		m.mux.Handle(path, prometheus.Handler())
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		go func() {
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 			http.Serve(m.ln, m.mux)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}()
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // OnShutdown tears down the metrics on shutdown.
 | 
					
						
							|  |  |  | func (m *Metrics) OnShutdown() error {
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	if m.ln != nil {
 | 
					
						
							|  |  |  | 		return m.ln.Close()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 07:11:25 +01:00
										 |  |  | func define() {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_count_total",
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		Help:      "Counter of DNS requests made per zone, protocol and family.",
 | 
					
						
							|  |  |  | 	}, []string{"zone", "proto", "family"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Subsystem: subsystem,
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 		Name:      "request_duration_milliseconds",
 | 
					
						
							|  |  |  | 		Buckets:   append(prometheus.DefBuckets, []float64{50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000}...),
 | 
					
						
							|  |  |  | 		Help:      "Histogram of the time (in milliseconds) each request took.",
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	}, []string{"zone"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	requestSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							| 
									
										
										
										
											2016-06-23 10:33:55 +00:00
										 |  |  | 		Name:      "request_size_bytes",
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 		Help:      "Size of the EDNS0 UDP buffer in bytes (64K for TCP).",
 | 
					
						
							|  |  |  | 		Buckets:   []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
 | 
					
						
							| 
									
										
										
										
											2016-06-24 08:37:23 +00:00
										 |  |  | 	}, []string{"zone", "proto"})
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 	requestTransferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_transfer_size_bytes",
 | 
					
						
							|  |  |  | 		Help:      "Size of the incoming zone transfer in bytes.",
 | 
					
						
							|  |  |  | 		Buckets:   []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
 | 
					
						
							|  |  |  | 	}, []string{"zone", "proto"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 11:21:12 +01:00
										 |  |  | 	requestDo = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_do_count_total",
 | 
					
						
							|  |  |  | 		Help:      "Counter of DNS requests with DO bit set per zone.",
 | 
					
						
							|  |  |  | 	}, []string{"zone"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:12:13 +01:00
										 |  |  | 	requestType = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_type_count_total",
 | 
					
						
							|  |  |  | 		Help:      "Counter of DNS requests per type, per zone.",
 | 
					
						
							|  |  |  | 	}, []string{"zone", "type"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	responseSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "response_size_bytes",
 | 
					
						
							| 
									
										
										
										
											2016-07-04 06:08:45 -04:00
										 |  |  | 		Help:      "Size of the returned response in bytes.",
 | 
					
						
							|  |  |  | 		Buckets:   []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
 | 
					
						
							|  |  |  | 	}, []string{"zone", "proto"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	responseTransferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "response_transfer_size_bytes",
 | 
					
						
							|  |  |  | 		Help:      "Size of the returned zone transfer in bytes.",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Buckets:   []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
 | 
					
						
							| 
									
										
										
										
											2016-06-24 08:47:28 +00:00
										 |  |  | 	}, []string{"zone", "proto"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	responseRcode = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Subsystem: subsystem,
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:15:55 +01:00
										 |  |  | 		Name:      "response_rcode_count_total",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Help:      "Counter of response status codes.",
 | 
					
						
							| 
									
										
										
										
											2016-04-09 17:42:31 +01:00
										 |  |  | 	}, []string{"zone", "rcode"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | const (
 | 
					
						
							|  |  |  | 	// Dropped indicates we dropped the query before any handling. It has no closing dot, so it can not be a valid zone.
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	Dropped   = "dropped"
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 	subsystem = "dns"
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	path      = "/metrics"
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | )
 |