diff --git a/.travis.yml b/.travis.yml index 67986011e..a125d54c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,7 @@ env: - TEST_TYPE=core - TEST_TYPE=plugin - TEST_TYPE=fmt + - TEST_TYPE=metrics # - TEST_TYPE=fuzzit FUZZIT_TYPE=local-regression # - TEST_TYPE=fuzzit FUZZIT_TYPE=fuzzing diff --git a/Makefile b/Makefile index b981fcb05..6cf1f6eb7 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,12 @@ endif ifeq ($(TEST_TYPE),fmt) ( echo "fmt"; gofmt -w -s . | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi ) endif +ifeq ($(TEST_TYPE),metrics) + ( echo "metrics"; go get github.com/fatih/faillint) + ( faillint -paths "github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,\ + NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}=github.com/prometheus/client_golang/prometheus/promauto.{NewCounter,\ + NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}" ./...) +endif ifeq ($(TEST_TYPE),plugin) ( cd plugin; go test -race ./... ) endif diff --git a/go.mod b/go.mod index 99167b3e6..915af2dc7 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/cenkalti/backoff/v4 v4.0.2 github.com/dnstap/golang-dnstap v0.2.0 github.com/farsightsec/golang-framestream v0.0.0-20190425193708-fa4b164d59b8 + github.com/fatih/faillint v1.5.0 // indirect github.com/golang/protobuf v1.4.2 github.com/gophercloud/gophercloud v0.9.0 // indirect github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 @@ -30,7 +31,7 @@ require ( github.com/prometheus/client_golang v1.7.1 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.10.0 - github.com/prometheus/prometheus v2.5.0+incompatible + github.com/prometheus/prometheus v2.5.0+incompatible // indirect go.etcd.io/etcd v0.5.0-alpha.5.0.20200306183522-221f0cc107cb go.uber.org/zap v1.14.1 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 diff --git a/go.sum b/go.sum index 496b246c1..e995b86d7 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiy cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= contrib.go.opencensus.io/exporter/ocagent v0.4.12/go.mod h1:450APlNTSR6FrvC3CTRqYosuDstRB9un7SOx2k/9ckA= +dmitri.shuralyov.com/go/generated v0.0.0-20170818220700-b1254a446363 h1:o4lAkfETerCnr1kF9/qwkwjICnU+YLHNDCM8h2xj7as= +dmitri.shuralyov.com/go/generated v0.0.0-20170818220700-b1254a446363/go.mod h1:WG7q7swWsS2f9PYpt5DoEP/EBYWx8We5UoRltn9vJl8= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/azure-sdk-for-go v32.4.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v40.6.0+incompatible h1:ULjp/a/UsBfnZcl45jjywhcBKex/k/A1cG9s9NapLFw= @@ -158,6 +160,8 @@ github.com/exoscale/egoscale v0.18.1/go.mod h1:Z7OOdzzTOz1Q1PjQXumlz9Wn/CddH0zSY github.com/farsightsec/golang-framestream v0.0.0-20190425193708-fa4b164d59b8 h1:/iPdQppoAsTfML+yqFSq2EBChiEMnRkh5WvhFgtWwcU= github.com/farsightsec/golang-framestream v0.0.0-20190425193708-fa4b164d59b8/go.mod h1:eNde4IQyEiA5br02AouhEHCu3p3UzrCdFR4LuQHklMI= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/faillint v1.5.0 h1:fUolG+EsD6zdRW4rapzrM0tSf7VdpxWG3GLCPafUOcE= +github.com/fatih/faillint v1.5.0/go.mod h1:e+3n7K3aI9fnZS4aNCggnoB+uBF4hwjqqR7BmA35PCE= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= @@ -667,6 +671,7 @@ golang.org/x/tools v0.0.0-20200122220014-bf1340f18c4a/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200207224406-61798d64f025/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4 h1:kDtqNkeBrZb8B+atrj50B5XLHpzXXqcCdZPP/ApQ5NY= diff --git a/plugin/acl/metrics.go b/plugin/acl/metrics.go index 719f24675..b0357cab1 100644 --- a/plugin/acl/metrics.go +++ b/plugin/acl/metrics.go @@ -4,18 +4,19 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // RequestBlockCount is the number of DNS requests being blocked. - RequestBlockCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestBlockCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: pluginName, Name: "blocked_requests_total", Help: "Counter of DNS requests being blocked.", }, []string{"server", "zone"}) // RequestAllowCount is the number of DNS requests being Allowed. - RequestAllowCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestAllowCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: pluginName, Name: "allowed_requests_total", diff --git a/plugin/acl/setup.go b/plugin/acl/setup.go index 1bc891b50..98327b6b0 100644 --- a/plugin/acl/setup.go +++ b/plugin/acl/setup.go @@ -6,7 +6,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/caddyserver/caddy" "github.com/infobloxopen/go-trees/iptree" @@ -37,11 +36,6 @@ func setup(c *caddy.Controller) error { return a }) - // Register all metrics. - c.OnStartup(func() error { - metrics.MustRegister(c, RequestBlockCount, RequestAllowCount) - return nil - }) return nil } diff --git a/plugin/autopath/metrics.go b/plugin/autopath/metrics.go index a74a94f27..65a6cbd89 100644 --- a/plugin/autopath/metrics.go +++ b/plugin/autopath/metrics.go @@ -4,11 +4,12 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // autoPathCount is counter of successfully autopath-ed queries. - autoPathCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + autoPathCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "autopath", Name: "success_total", diff --git a/plugin/autopath/setup.go b/plugin/autopath/setup.go index 94dde5953..a22105f7d 100644 --- a/plugin/autopath/setup.go +++ b/plugin/autopath/setup.go @@ -5,7 +5,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/caddyserver/caddy" "github.com/miekg/dns" @@ -19,11 +18,6 @@ func setup(c *caddy.Controller) error { return plugin.Error("autopath", err) } - c.OnStartup(func() error { - metrics.MustRegister(c, autoPathCount) - return nil - }) - // Do this in OnStartup, so all plugin has been initialized. c.OnStartup(func() error { m := dnsserver.GetConfig(c).Handler(mw) diff --git a/plugin/cache/metrics.go b/plugin/cache/metrics.go index 79aa805c8..b4b11ae26 100644 --- a/plugin/cache/metrics.go +++ b/plugin/cache/metrics.go @@ -4,46 +4,47 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // cacheSize is total elements in the cache by cache type. - cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + cacheSize = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "entries", Help: "The number of elements in the cache.", }, []string{"server", "type"}) // cacheHits is counter of cache hits by cache type. - cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{ + cacheHits = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "hits_total", Help: "The count of cache hits.", }, []string{"server", "type"}) // cacheMisses is the counter of cache misses. - cacheMisses = prometheus.NewCounterVec(prometheus.CounterOpts{ + cacheMisses = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "misses_total", Help: "The count of cache misses.", }, []string{"server"}) // cachePrefetches is the number of time the cache has prefetched a cached item. - cachePrefetches = prometheus.NewCounterVec(prometheus.CounterOpts{ + cachePrefetches = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "prefetch_total", Help: "The number of times the cache has prefetched a cached item.", }, []string{"server"}) // cacheDrops is the number responses that are not cached, because the reply is malformed. - cacheDrops = prometheus.NewCounterVec(prometheus.CounterOpts{ + cacheDrops = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "drops_total", Help: "The number responses that are not cached, because the reply is malformed.", }, []string{"server"}) // servedStale is the number of requests served from stale cache entries. - servedStale = prometheus.NewCounterVec(prometheus.CounterOpts{ + servedStale = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "cache", Name: "served_stale_total", diff --git a/plugin/cache/setup.go b/plugin/cache/setup.go index 62c5c9d2c..97493eb77 100644 --- a/plugin/cache/setup.go +++ b/plugin/cache/setup.go @@ -8,7 +8,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/pkg/cache" clog "github.com/coredns/coredns/plugin/pkg/log" @@ -29,13 +28,6 @@ func setup(c *caddy.Controller) error { return ca }) - c.OnStartup(func() error { - metrics.MustRegister(c, - cacheSize, cacheHits, cacheMisses, - cachePrefetches, cacheDrops, servedStale) - return nil - }) - return nil } diff --git a/plugin/dns64/metrics.go b/plugin/dns64/metrics.go index 7383c715e..955231667 100644 --- a/plugin/dns64/metrics.go +++ b/plugin/dns64/metrics.go @@ -4,11 +4,12 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // RequestsTranslatedCount is the number of DNS requests translated by dns64. - RequestsTranslatedCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestsTranslatedCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: pluginName, Name: "requests_translated_total", diff --git a/plugin/dns64/setup.go b/plugin/dns64/setup.go index bc87ac9df..44bc01a2d 100644 --- a/plugin/dns64/setup.go +++ b/plugin/dns64/setup.go @@ -5,7 +5,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/coredns/coredns/plugin/pkg/upstream" @@ -29,12 +28,6 @@ func setup(c *caddy.Controller) error { return dns64 }) - // Register all metrics. - c.OnStartup(func() error { - metrics.MustRegister(c, RequestsTranslatedCount) - return nil - }) - return nil } diff --git a/plugin/dnssec/metrics.go b/plugin/dnssec/metrics.go index 20da38f36..e69dbf5c3 100644 --- a/plugin/dnssec/metrics.go +++ b/plugin/dnssec/metrics.go @@ -4,25 +4,26 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // cacheSize is the number of elements in the dnssec cache. - cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + cacheSize = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "dnssec", Name: "cache_entries", Help: "The number of elements in the dnssec cache.", }, []string{"server", "type"}) // cacheHits is the count of cache hits. - cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{ + cacheHits = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "dnssec", Name: "cache_hits_total", Help: "The count of cache hits.", }, []string{"server"}) // cacheMisses is the count of cache misses. - cacheMisses = prometheus.NewCounterVec(prometheus.CounterOpts{ + cacheMisses = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "dnssec", Name: "cache_misses_total", diff --git a/plugin/dnssec/setup.go b/plugin/dnssec/setup.go index f410fb8b5..25ef91a67 100644 --- a/plugin/dnssec/setup.go +++ b/plugin/dnssec/setup.go @@ -8,7 +8,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/pkg/cache" clog "github.com/coredns/coredns/plugin/pkg/log" @@ -30,11 +29,6 @@ func setup(c *caddy.Controller) error { return New(zones, keys, splitkeys, next, ca) }) - c.OnStartup(func() error { - metrics.MustRegister(c, cacheSize, cacheHits, cacheMisses) - return nil - }) - return nil } diff --git a/plugin/forward/metrics.go b/plugin/forward/metrics.go index 58936caa0..0b63ce1e7 100644 --- a/plugin/forward/metrics.go +++ b/plugin/forward/metrics.go @@ -4,48 +4,49 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) // Variables declared for monitoring. var ( - RequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "requests_total", Help: "Counter of requests made per upstream.", }, []string{"to"}) - RcodeCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RcodeCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "responses_total", Help: "Counter of responses received per upstream.", }, []string{"rcode", "to"}) - RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + RequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "request_duration_seconds", Buckets: plugin.TimeBuckets, Help: "Histogram of the time each request took.", }, []string{"to"}) - HealthcheckFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + HealthcheckFailureCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "healthcheck_failures_total", Help: "Counter of the number of failed healthchecks.", }, []string{"to"}) - HealthcheckBrokenCount = prometheus.NewCounter(prometheus.CounterOpts{ + HealthcheckBrokenCount = promauto.NewCounter(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "healthcheck_broken_total", Help: "Counter of the number of complete failures of the healthchecks.", }) - SocketGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + SocketGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "sockets_open", Help: "Gauge of open sockets per upstream.", }, []string{"to"}) - MaxConcurrentRejectCount = prometheus.NewCounter(prometheus.CounterOpts{ + MaxConcurrentRejectCount = promauto.NewCounter(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "max_concurrent_rejects_total", diff --git a/plugin/forward/setup.go b/plugin/forward/setup.go index 48c4ee2f2..fff6ce33f 100644 --- a/plugin/forward/setup.go +++ b/plugin/forward/setup.go @@ -8,7 +8,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/pkg/parse" pkgtls "github.com/coredns/coredns/plugin/pkg/tls" "github.com/coredns/coredns/plugin/pkg/transport" @@ -33,7 +32,6 @@ func setup(c *caddy.Controller) error { }) c.OnStartup(func() error { - metrics.MustRegister(c, RequestCount, RcodeCount, RequestDuration, HealthcheckFailureCount, HealthcheckBrokenCount, SocketGauge, MaxConcurrentRejectCount) return f.OnStartup() }) diff --git a/plugin/grpc/metrics.go b/plugin/grpc/metrics.go index e4a3ce2f5..2857042cd 100644 --- a/plugin/grpc/metrics.go +++ b/plugin/grpc/metrics.go @@ -4,23 +4,24 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) // Variables declared for monitoring. var ( - RequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "grpc", Name: "requests_total", Help: "Counter of requests made per upstream.", }, []string{"to"}) - RcodeCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RcodeCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "grpc", Name: "responses_total", Help: "Counter of requests made per upstream.", }, []string{"rcode", "to"}) - RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + RequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: "grpc", Name: "request_duration_seconds", diff --git a/plugin/grpc/setup.go b/plugin/grpc/setup.go index a234efb37..7deb731b4 100644 --- a/plugin/grpc/setup.go +++ b/plugin/grpc/setup.go @@ -6,7 +6,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/pkg/parse" pkgtls "github.com/coredns/coredns/plugin/pkg/tls" @@ -30,11 +29,6 @@ func setup(c *caddy.Controller) error { return g }) - c.OnStartup(func() error { - metrics.MustRegister(c, RequestCount, RcodeCount, RequestDuration) - return nil - }) - return nil } diff --git a/plugin/health/overloaded.go b/plugin/health/overloaded.go index 1c6602b82..8668d525d 100644 --- a/plugin/health/overloaded.go +++ b/plugin/health/overloaded.go @@ -7,6 +7,7 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) // overloaded queries the health end point and updates a metrics showing how long it took. @@ -39,7 +40,7 @@ func (h *health) overloaded() { var ( // HealthDuration is the metric used for exporting how fast we can retrieve the /health endpoint. - HealthDuration = prometheus.NewHistogram(prometheus.HistogramOpts{ + HealthDuration = promauto.NewHistogram(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: "health", Name: "request_duration_seconds", diff --git a/plugin/health/setup.go b/plugin/health/setup.go index 1f72cb245..630b11647 100644 --- a/plugin/health/setup.go +++ b/plugin/health/setup.go @@ -6,7 +6,6 @@ import ( "time" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/caddyserver/caddy" ) @@ -21,11 +20,6 @@ func setup(c *caddy.Controller) error { h := &health{Addr: addr, stop: make(chan bool), lameduck: lame} - c.OnStartup(func() error { - metrics.MustRegister(c, HealthDuration) - return nil - }) - c.OnStartup(h.OnStartup) c.OnRestart(h.OnFinalShutdown) c.OnFinalShutdown(h.OnFinalShutdown) diff --git a/plugin/hosts/metrics.go b/plugin/hosts/metrics.go index f20a07775..f97497bf2 100644 --- a/plugin/hosts/metrics.go +++ b/plugin/hosts/metrics.go @@ -4,18 +4,19 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // hostsEntries is the combined number of entries in hosts and Corefile. - hostsEntries = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + hostsEntries = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "hosts", Name: "entries", Help: "The combined number of entries in hosts and Corefile.", }, []string{}) // hostsReloadTime is the timestamp of the last reload of hosts file. - hostsReloadTime = prometheus.NewGauge(prometheus.GaugeOpts{ + hostsReloadTime = promauto.NewGauge(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "hosts", Name: "reload_timestamp_seconds", diff --git a/plugin/hosts/setup.go b/plugin/hosts/setup.go index 09f030842..a66795faf 100644 --- a/plugin/hosts/setup.go +++ b/plugin/hosts/setup.go @@ -9,7 +9,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/caddyserver/caddy" @@ -53,12 +52,6 @@ func setup(c *caddy.Controller) error { return nil }) - c.OnStartup(func() error { - metrics.MustRegister(c, hostsEntries) - metrics.MustRegister(c, hostsReloadTime) - return nil - }) - c.OnShutdown(func() error { close(parseChan) return nil diff --git a/plugin/kubernetes/metrics.go b/plugin/kubernetes/metrics.go index 7c6bfa1b0..07fe770c1 100644 --- a/plugin/kubernetes/metrics.go +++ b/plugin/kubernetes/metrics.go @@ -5,7 +5,9 @@ import ( "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/kubernetes/object" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" api "k8s.io/api/core/v1" ) @@ -21,7 +23,7 @@ var ( // * cluster_ip // * headless_with_selector // * headless_without_selector - DnsProgrammingLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + DnsProgrammingLatency = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: pluginName, Name: "dns_programming_duration_seconds", diff --git a/plugin/kubernetes/setup.go b/plugin/kubernetes/setup.go index 0c46a3ab6..395bef333 100644 --- a/plugin/kubernetes/setup.go +++ b/plugin/kubernetes/setup.go @@ -11,7 +11,6 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/pkg/dnsutil" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/coredns/coredns/plugin/pkg/parse" @@ -48,11 +47,6 @@ func setup(c *caddy.Controller) error { k.RegisterKubeCache(c) - c.OnStartup(func() error { - metrics.MustRegister(c, DnsProgrammingLatency) - return nil - }) - dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { k.Next = next return k diff --git a/plugin/metrics/metrics.go b/plugin/metrics/metrics.go index 896e1bc2e..f6c1e6c8c 100644 --- a/plugin/metrics/metrics.go +++ b/plugin/metrics/metrics.go @@ -9,10 +9,10 @@ import ( "time" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics/vars" "github.com/coredns/coredns/plugin/pkg/reuseport" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -37,23 +37,9 @@ type Metrics struct { func New(addr string) *Metrics { met := &Metrics{ Addr: addr, - Reg: prometheus.NewRegistry(), + Reg: prometheus.DefaultRegisterer.(*prometheus.Registry), zoneMap: make(map[string]struct{}), } - // Add the default collectors - met.MustRegister(prometheus.NewGoCollector()) - met.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) - - // Add all of our collectors - met.MustRegister(buildInfo) - met.MustRegister(vars.Panic) - met.MustRegister(vars.RequestCount) - met.MustRegister(vars.RequestDuration) - met.MustRegister(vars.RequestSize) - met.MustRegister(vars.RequestDo) - met.MustRegister(vars.ResponseSize) - met.MustRegister(vars.ResponseRcode) - met.MustRegister(vars.PluginEnabled) return met } @@ -162,7 +148,7 @@ var ListenAddr string // before erroring when it tries to close the metrics server const shutdownTimeout time.Duration = time.Second * 5 -var buildInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{ +var buildInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Name: "build_info", Help: "A metric with a constant '1' value labeled by version, revision, and goversion from which CoreDNS was built.", diff --git a/plugin/metrics/register.go b/plugin/metrics/register.go deleted file mode 100644 index 704192ffb..000000000 --- a/plugin/metrics/register.go +++ /dev/null @@ -1,23 +0,0 @@ -package metrics - -import ( - "github.com/coredns/coredns/core/dnsserver" - - "github.com/caddyserver/caddy" - "github.com/prometheus/client_golang/prometheus" -) - -// MustRegister registers the prometheus Collectors when the metrics plugin is used. -func MustRegister(c *caddy.Controller, cs ...prometheus.Collector) { - m := dnsserver.GetConfig(c).Handler("prometheus") - if m == nil { - return - } - x, ok := m.(*Metrics) - if !ok { - return - } - for _, c := range cs { - x.MustRegister(c) - } -} diff --git a/plugin/metrics/vars/vars.go b/plugin/metrics/vars/vars.go index 16e20280a..2187e13f0 100644 --- a/plugin/metrics/vars/vars.go +++ b/plugin/metrics/vars/vars.go @@ -4,18 +4,19 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) // Request* and Response* are the prometheus counters and gauges we are using for exporting metrics. var ( - RequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "requests_total", Help: "Counter of DNS requests made per zone, protocol and family.", }, []string{"server", "zone", "proto", "family", "type"}) - RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + RequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "request_duration_seconds", @@ -23,7 +24,7 @@ var ( Help: "Histogram of the time (in seconds) each request took.", }, []string{"server", "zone", "type"}) - RequestSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + RequestSize = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "request_size_bytes", @@ -31,14 +32,14 @@ var ( Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3}, }, []string{"server", "zone", "proto"}) - RequestDo = prometheus.NewCounterVec(prometheus.CounterOpts{ + RequestDo = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "do_requests_total", Help: "Counter of DNS requests with DO bit set per zone.", }, []string{"server", "zone"}) - ResponseSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + ResponseSize = promauto.NewHistogramVec(prometheus.HistogramOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "response_size_bytes", @@ -46,20 +47,20 @@ var ( Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3}, }, []string{"server", "zone", "proto"}) - ResponseRcode = prometheus.NewCounterVec(prometheus.CounterOpts{ + ResponseRcode = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: subsystem, Name: "responses_total", Help: "Counter of response status codes.", }, []string{"server", "zone", "rcode"}) - Panic = prometheus.NewCounter(prometheus.CounterOpts{ + Panic = promauto.NewCounter(prometheus.CounterOpts{ Namespace: plugin.Namespace, Name: "panics_total", Help: "A metrics that counts the number of panics.", }) - PluginEnabled = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + PluginEnabled = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Name: "plugin_enabled", Help: "A metric that indicates whether a plugin is enabled on per server and zone basis.", diff --git a/plugin/reload/metrics.go b/plugin/reload/metrics.go index 657d4b62f..722479147 100644 --- a/plugin/reload/metrics.go +++ b/plugin/reload/metrics.go @@ -4,19 +4,20 @@ import ( "github.com/coredns/coredns/plugin" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) // Metrics for the reload plugin var ( // failedCount is the counter of the number of failed reload attempts. - failedCount = prometheus.NewCounter(prometheus.CounterOpts{ + failedCount = promauto.NewCounter(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "reload", Name: "failed_total", Help: "Counter of the number of failed reload attempts.", }) // reloadInfo is record the hash value during reload. - reloadInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + reloadInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{ Namespace: plugin.Namespace, Subsystem: "reload", Name: "version_info", diff --git a/plugin/reload/setup.go b/plugin/reload/setup.go index acd0c5432..557349a9d 100644 --- a/plugin/reload/setup.go +++ b/plugin/reload/setup.go @@ -7,7 +7,6 @@ import ( "time" "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/caddyserver/caddy" @@ -70,10 +69,6 @@ func setup(c *caddy.Controller) error { r.setUsage(used) once.Do(func() { caddy.RegisterEventHook("reload", hook) - c.OnRestart(func() error { - metrics.MustRegister(c, reloadInfo, failedCount) - return nil - }) }) // re-register on finalShutDown as the instance most-likely will be changed shutOnce.Do(func() { diff --git a/plugin/template/metrics.go b/plugin/template/metrics.go index 56aa63a44..db930dd77 100644 --- a/plugin/template/metrics.go +++ b/plugin/template/metrics.go @@ -2,42 +2,30 @@ package template import ( "github.com/coredns/coredns/plugin" - "github.com/coredns/coredns/plugin/metrics" - - "github.com/caddyserver/caddy" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) var ( // templateMatchesCount is the counter of template regex matches. - templateMatchesCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + templateMatchesCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "template", Name: "matches_total", Help: "Counter of template regex matches.", }, []string{"server", "zone", "class", "type"}) // templateFailureCount is the counter of go template failures. - templateFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + templateFailureCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "template", Name: "template_failures_total", Help: "Counter of go template failures.", }, []string{"server", "zone", "class", "type", "section", "template"}) // templateRRFailureCount is the counter of mis-templated RRs. - templateRRFailureCount = prometheus.NewCounterVec(prometheus.CounterOpts{ + templateRRFailureCount = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "template", Name: "rr_failures_total", Help: "Counter of mis-templated RRs.", }, []string{"server", "zone", "class", "type", "section", "template"}) ) - -// OnStartupMetrics sets up the metrics on startup. -func setupMetrics(c *caddy.Controller) error { - c.OnStartup(func() error { - metrics.MustRegister(c, templateMatchesCount, templateFailureCount, templateRRFailureCount) - return nil - }) - - return nil -} diff --git a/plugin/template/setup.go b/plugin/template/setup.go index 908266f29..d79dcbb86 100644 --- a/plugin/template/setup.go +++ b/plugin/template/setup.go @@ -20,10 +20,6 @@ func setupTemplate(c *caddy.Controller) error { return plugin.Error("template", err) } - if err := setupMetrics(c); err != nil { - return plugin.Error("template", err) - } - dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { handler.Next = next return handler