| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:15:55 +01:00
										 |  |  | const namespace = "coredns"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	requestCount    *prometheus.CounterVec
 | 
					
						
							|  |  |  | 	requestDuration *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	responseSize    *prometheus.HistogramVec
 | 
					
						
							|  |  |  | 	responseRcode   *prometheus.CounterVec
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = "/metrics"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics
 | 
					
						
							|  |  |  | type Metrics struct {
 | 
					
						
							|  |  |  | 	Next      middleware.Handler
 | 
					
						
							|  |  |  | 	Addr      string // where to we listen
 | 
					
						
							|  |  |  | 	Once      sync.Once
 | 
					
						
							|  |  |  | 	ZoneNames []string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (m *Metrics) Start() error {
 | 
					
						
							|  |  |  | 	m.Once.Do(func() {
 | 
					
						
							|  |  |  | 		define("")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		prometheus.MustRegister(requestCount)
 | 
					
						
							|  |  |  | 		prometheus.MustRegister(requestDuration)
 | 
					
						
							|  |  |  | 		prometheus.MustRegister(responseSize)
 | 
					
						
							|  |  |  | 		prometheus.MustRegister(responseRcode)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		http.Handle(path, prometheus.Handler())
 | 
					
						
							|  |  |  | 		go func() {
 | 
					
						
							|  |  |  | 			fmt.Errorf("%s", http.ListenAndServe(m.Addr, nil))
 | 
					
						
							|  |  |  | 		}()
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func define(subsystem string) {
 | 
					
						
							|  |  |  | 	if subsystem == "" {
 | 
					
						
							|  |  |  | 		subsystem = "dns"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_count_total",
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		Help:      "Counter of DNS requests made per zone and type and opcode.",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}, []string{"zone", "qtype"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							|  |  |  | 		Namespace: namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "request_duration_seconds",
 | 
					
						
							|  |  |  | 		Help:      "Histogram of the time (in seconds) each request took.",
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	}, []string{"zone", "qtype"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	responseSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
 | 
					
						
							|  |  |  | 		Namespace: namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "response_size_bytes",
 | 
					
						
							|  |  |  | 		Help:      "Size of the returns response in bytes.",
 | 
					
						
							|  |  |  | 		Buckets:   []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	}, []string{"zone", "qtype"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	responseRcode = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: namespace,
 | 
					
						
							|  |  |  | 		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-05 10:53:23 +01:00
										 |  |  | 	}, []string{"zone", "rcode", "qtype"})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |