diff --git a/docs/prometheus_grafana.md b/docs/prometheus_grafana.md index 4fcb4ea0..17ab5644 100644 --- a/docs/prometheus_grafana.md +++ b/docs/prometheus_grafana.md @@ -20,6 +20,7 @@ Following metrics will be exported: | blocky_cache_hit_count / blocky_cache_miss_count | Cache hit/miss counters | | blocky_prefetch_count | Amount of prefetched DNS responses | | blocky_prefetch_domain_name_cache_count | Amount of domain names being prefetched | +| blocky_failed_download_count | Number of failed list downloads | ### Grafana dashboard diff --git a/evt/events.go b/evt/events.go index 62276f7c..12774623 100644 --- a/evt/events.go +++ b/evt/events.go @@ -29,6 +29,9 @@ const ( // CachingDomainsToPrefetchCountChanged fires, if a number of domains being prefetched changed, Parameter: new count CachingDomainsToPrefetchCountChanged = "caching:domainsToPrefetchCountChanged" + // CachingFailedDownloadChanged fires, if a download of a blocking list fails + CachingFailedDownloadChanged = "caching:failedDownload" + // ApplicationStarted fires on start of the application. Parameter: version number, build time ApplicationStarted = "application:started" ) diff --git a/lists/list_cache.go b/lists/list_cache.go index 51d45a7d..36569b0d 100644 --- a/lists/list_cache.go +++ b/lists/list_cache.go @@ -272,6 +272,8 @@ func (b *ListCache) downloadFile(link string) (io.ReadCloser, error) { default: logger.Warnf("Can't download file: %s", err) } + + evt.Bus().Publish(evt.CachingFailedDownloadChanged, link) })) return body, err diff --git a/lists/list_cache_test.go b/lists/list_cache_test.go index 29c67ae4..a2b8faad 100644 --- a/lists/list_cache_test.go +++ b/lists/list_cache_test.go @@ -7,7 +7,7 @@ import ( "sync/atomic" "time" - "github.com/0xERR0R/blocky/evt" + . "github.com/0xERR0R/blocky/evt" . "github.com/0xERR0R/blocky/helpertest" . "github.com/onsi/ginkgo" @@ -69,6 +69,11 @@ var _ = Describe("ListCache", func() { When("If timeout occurs", func() { var attempt uint64 = 1 It("Should perform a retry", func() { + failedDownloadCount := 0 + _ = Bus().SubscribeOnce(CachingFailedDownloadChanged, func(_ string) { + failedDownloadCount++ + }) + // should produce a timeout on first attempt s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { a := atomic.LoadUint64(&attempt) @@ -92,6 +97,7 @@ var _ = Describe("ListCache", func() { g.Expect(group).Should(Equal("gr1")) }, "1s").Should(Succeed()) + Expect(failedDownloadCount).Should(Equal(1)) }) }) When("a temporary err occurs on download", func() { @@ -214,7 +220,7 @@ var _ = Describe("ListCache", func() { resultCnt := 0 - _ = evt.Bus().SubscribeOnce(evt.BlockingCacheGroupChanged, func(listType ListCacheType, group string, cnt int) { + _ = Bus().SubscribeOnce(BlockingCacheGroupChanged, func(listType ListCacheType, group string, cnt int) { resultCnt = cnt }) diff --git a/metrics/metrics_event_publisher.go b/metrics/metrics_event_publisher.go index e904b8ee..82f96e85 100644 --- a/metrics/metrics_event_publisher.go +++ b/metrics/metrics_event_publisher.go @@ -120,6 +120,7 @@ func registerCachingEventListeners() { missCount := cacheMissCount() prefetchCount := domainPrefetchCount() prefetchHitCount := domainPrefetchHitCount() + failedDownloadCount := failedDownloadCount() RegisterMetric(entryCount) RegisterMetric(prefetchDomainCount) @@ -127,6 +128,7 @@ func registerCachingEventListeners() { RegisterMetric(missCount) RegisterMetric(prefetchCount) RegisterMetric(prefetchHitCount) + RegisterMetric(failedDownloadCount) subscribe(evt.CachingDomainsToPrefetchCountChanged, func(cnt int) { prefetchDomainCount.Set(float64(cnt)) @@ -151,6 +153,17 @@ func registerCachingEventListeners() { subscribe(evt.CachingResultCacheChanged, func(cnt int) { entryCount.Set(float64(cnt)) }) + + subscribe(evt.CachingFailedDownloadChanged, func(_ string) { + failedDownloadCount.Inc() + }) +} + +func failedDownloadCount() prometheus.Counter { + return prometheus.NewCounter(prometheus.CounterOpts{ + Name: "blocky_failed_download_count", + Help: "Failed download counter", + }) } func cacheHitCount() prometheus.Counter {