Add failed_download_count prometheus metric (#309) (#395)

Prometheus metric "failed_download_count" for failed downloads
This commit is contained in:
FileGo 2022-01-04 20:24:49 +00:00 committed by GitHub
parent b43c7aa2cb
commit 926f06ccdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 2 deletions

View File

@ -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

View File

@ -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"
)

View File

@ -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

View File

@ -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
})

View File

@ -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 {