Add metrics for cache hits/misses (#375)

* Add metrics for cache hits/misses

Add counters for cache middleware hits and misses.

* Add test for cache middleware hit/miss counters.

* Fix cache hit metric incrementing.

* Add cache hit/miss metrics to dnssec middleware.

* Update README metric documentation.
This commit is contained in:
Ben Kochie 2016-10-31 19:50:50 +01:00 committed by Miek Gieben
parent 27d893cf33
commit 775d26c5e2
6 changed files with 63 additions and 6 deletions

View file

@ -39,8 +39,12 @@ The minimum TTL allowed on resource records is 5 seconds.
If monitoring is enabled (via the *prometheus* directive) then the following metrics are exported:
* coredns_cache_size{type} - total elements in the case, type is either "denial" or "success".
* coredns_cache_capacity{type} - total capacity of the cache, type is either "denial" or "success".
* coredns_cache_size{type} - Total elements in the cache by cache type.
* coredns_cache_capacity{type} - Total capacity of the cache by cache type.
* coredns_cache_hits_total{type} - Counter of cache hits by cache type.
* coredns_cache_misses_total - Counter of cache misses.
Cache types are either "denial" or "success".
## Examples

View file

@ -44,12 +44,15 @@ func (c *Cache) get(qname string, qtype uint16, do bool) (*item, bool, bool) {
k := rawKey(qname, qtype, do)
if i, ok := c.ncache.Get(k); ok {
cacheHits.WithLabelValues(Denial).Inc()
return i.(*item), ok, i.(*item).expired(time.Now())
}
if i, ok := c.pcache.Get(k); ok {
cacheHits.WithLabelValues(Success).Inc()
return i.(*item), ok, i.(*item).expired(time.Now())
}
cacheMisses.Inc()
return nil, false, false
}
@ -67,6 +70,20 @@ var (
Name: "capacity",
Help: "The cache's capacity.",
}, []string{"type"})
cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: middleware.Namespace,
Subsystem: subsystem,
Name: "hits_total",
Help: "The count of cache hits.",
}, []string{"type"})
cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: middleware.Namespace,
Subsystem: subsystem,
Name: "misses_total",
Help: "The count of cache misses.",
})
)
const subsystem = "cache"
@ -74,4 +91,6 @@ const subsystem = "cache"
func init() {
prometheus.MustRegister(cacheSize)
prometheus.MustRegister(cacheCapacity)
prometheus.MustRegister(cacheHits)
prometheus.MustRegister(cacheMisses)
}

View file

@ -43,5 +43,7 @@ If monitoring is enabled (via the *prometheus* directive) then the following met
* coredns_dnssec_cache_size{type} - total elements in the cache, type is "signature".
* coredns_dnssec_cache_capacity{type} - total capacity of the cache, type is "signature".
* coredns_dnssec_cache_hits_total - Counter of cache hits.
* coredns_dnssec_cache_misses_total - Counter of cache misses.
## Examples

View file

@ -115,8 +115,10 @@ func (d Dnssec) set(key string, sigs []dns.RR) {
func (d Dnssec) get(key string) ([]dns.RR, bool) {
if s, ok := d.cache.Get(key); ok {
cacheHits.Inc()
return s.([]dns.RR), true
}
cacheMisses.Inc()
return nil, false
}

View file

@ -53,6 +53,20 @@ var (
Name: "cache_capacity",
Help: "The dnssec cache's capacity.",
}, []string{"type"})
cacheHits = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: middleware.Namespace,
Subsystem: subsystem,
Name: "cache_hits_total",
Help: "The count of cache hits.",
})
cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: middleware.Namespace,
Subsystem: subsystem,
Name: "cache_misses_total",
Help: "The count of cache misses.",
})
)
// Name implements the Handler interface.
@ -63,4 +77,6 @@ const subsystem = "dnssec"
func init() {
prometheus.MustRegister(cacheSize)
prometheus.MustRegister(cacheCapacity)
prometheus.MustRegister(cacheHits)
prometheus.MustRegister(cacheMisses)
}

View file

@ -73,7 +73,8 @@ func TestMetricsRefused(t *testing.T) {
}
func TestMetricsCache(t *testing.T) {
metricName := "coredns_cache_size"
cacheSizeMetricName := "coredns_cache_size"
cacheHitMetricName := "coredns_cache_hits_total"
corefile := `example.net:0 {
proxy . 8.8.8.8:53
@ -97,11 +98,24 @@ func TestMetricsCache(t *testing.T) {
}
data := mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics")
// Get the value for the metrics where the one of the labels values matches "success".
got, _ := mtest.MetricValueLabel(metricName, cache.Success, data)
// Get the value for the cache size metric where the one of the labels values matches "success".
got, _ := mtest.MetricValueLabel(cacheSizeMetricName, cache.Success, data)
if got != "1" {
t.Errorf("Expected value %s for %s, but got %s", "1", metricName, got)
t.Errorf("Expected value %s for %s, but got %s", "1", cacheSizeMetricName, got)
}
// Second request for the same response to test hit counter.
if _, err = dns.Exchange(m, udp); err != nil {
t.Fatalf("Could not send message: %s", err)
}
data = mtest.Scrape(t, "http://"+metrics.ListenAddr+"/metrics")
// Get the value for the cache hit counter where the one of the labels values matches "success".
got, _ = mtest.MetricValueLabel(cacheHitMetricName, cache.Success, data)
if got != "1" {
t.Errorf("Expected value %s for %s, but got %s", "1", cacheHitMetricName, got)
}
}