* Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
155 lines
4.7 KiB
Go
155 lines
4.7 KiB
Go
package metrics
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/miekg/coredns/middleware"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
var (
|
|
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
|
|
)
|
|
|
|
// Metrics holds the prometheus configuration. The metrics' path is fixed to be /metrics
|
|
type Metrics struct {
|
|
Next middleware.Handler
|
|
Addr string
|
|
ln net.Listener
|
|
mux *http.ServeMux
|
|
Once *sync.Once
|
|
ZoneNames []string
|
|
}
|
|
|
|
// OnStartup sets up the metrics on startup.
|
|
func (m *Metrics) OnStartup() error {
|
|
m.Once.Do(func() {
|
|
define()
|
|
|
|
ln, err := net.Listen("tcp", m.Addr)
|
|
if err != nil {
|
|
log.Printf("[ERROR] Failed to start metrics handler: %s", err)
|
|
return
|
|
}
|
|
|
|
m.ln = ln
|
|
|
|
m.mux = http.NewServeMux()
|
|
|
|
prometheus.MustRegister(requestCount)
|
|
prometheus.MustRegister(requestDuration)
|
|
prometheus.MustRegister(requestSize)
|
|
prometheus.MustRegister(requestTransferSize)
|
|
prometheus.MustRegister(requestDo)
|
|
prometheus.MustRegister(requestType)
|
|
|
|
prometheus.MustRegister(responseSize)
|
|
prometheus.MustRegister(responseTransferSize)
|
|
prometheus.MustRegister(responseRcode)
|
|
|
|
m.mux.Handle(path, prometheus.Handler())
|
|
|
|
go func() {
|
|
http.Serve(m.ln, m.mux)
|
|
}()
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// OnShutdown tears down the metrics on shutdown.
|
|
func (m *Metrics) OnShutdown() error {
|
|
if m.ln != nil {
|
|
return m.ln.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func define() {
|
|
requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: middleware.Namespace,
|
|
Subsystem: subsystem,
|
|
Name: "request_count_total",
|
|
Help: "Counter of DNS requests made per zone, protocol and family.",
|
|
}, []string{"zone", "proto", "family"})
|
|
|
|
requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Namespace: middleware.Namespace,
|
|
Subsystem: subsystem,
|
|
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.",
|
|
}, []string{"zone"})
|
|
|
|
requestSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Namespace: middleware.Namespace,
|
|
Subsystem: subsystem,
|
|
Name: "request_size_bytes",
|
|
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},
|
|
}, []string{"zone", "proto"})
|
|
|
|
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"})
|
|
|
|
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"})
|
|
|
|
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"})
|
|
|
|
responseSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Namespace: middleware.Namespace,
|
|
Subsystem: subsystem,
|
|
Name: "response_size_bytes",
|
|
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.",
|
|
Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
|
|
}, []string{"zone", "proto"})
|
|
|
|
responseRcode = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: middleware.Namespace,
|
|
Subsystem: subsystem,
|
|
Name: "response_rcode_count_total",
|
|
Help: "Counter of response status codes.",
|
|
}, []string{"zone", "rcode"})
|
|
}
|
|
|
|
const (
|
|
// Dropped indicates we dropped the query before any handling. It has no closing dot, so it can not be a valid zone.
|
|
Dropped = "dropped"
|
|
subsystem = "dns"
|
|
path = "/metrics"
|
|
)
|