Merge pull request #8 in DNS/adguard-dns from parental_metrics to master

* commit '9682dc6bc19ea940cf71911f6281450c7027eb16':
  travis -- npm installation of dependencies belongs in install section
  makefile -- use npm --prefix syntax instead of doing cd into subdir
  travis -- don't use slow master or tip builds, just specify 1.x for latest go version
  travis -- move dependency installation to install section, simplify go test invocation to test all subdirs in one go
  dnsfilter -- small code cleanup
  coredns plugin metrics -- deduplicate code
  dnsfilter metrics -- parental cache hits were counted as safebrowsing cache hits
This commit is contained in:
Konstantin 🦄 Zamyakin 2018-09-07 17:11:21 +03:00
commit 38b3fe6718
4 changed files with 23 additions and 20 deletions

View File

@ -3,15 +3,17 @@ sudo: false
go:
- 1.10.x
- 1.11.x
- master
- tip
- 1.x
os:
- linux
- osx
script:
- cd dnsfilter && go get -d -t . && go test
- cd ../coredns_plugin && go get -d -t . && go test
- cd .. && make
install:
- go get -d -t ./...
- npm --prefix client install
script:
- go test ./...
- make

View File

@ -12,8 +12,8 @@ all: build
build: AdguardDNS coredns
$(STATIC):
cd client; npm install
cd client; npm run build-prod
npm --prefix client install
npm --prefix client run build-prod
AdguardDNS: $(STATIC) *.go
echo mkfile_dir = $(mkfile_dir)

View File

@ -261,16 +261,17 @@ func gen(ch interface{}, doFunc statsFunc, name string, text string, value float
doFunc(ch, name, text, value, valueType)
}
func doStatsLookup(ch interface{}, doFunc statsFunc, name string, lookupstats *dnsfilter.LookupStats) {
gen(ch, doFunc, fmt.Sprintf("coredns_dnsfilter_%s_requests", name), fmt.Sprintf("Number of %s HTTP requests that were sent", name), float64(lookupstats.Requests), prometheus.CounterValue)
gen(ch, doFunc, fmt.Sprintf("coredns_dnsfilter_%s_cachehits", name), fmt.Sprintf("Number of %s lookups that didn't need HTTP requests", name), float64(lookupstats.CacheHits), prometheus.CounterValue)
gen(ch, doFunc, fmt.Sprintf("coredns_dnsfilter_%s_pending", name), fmt.Sprintf("Number of currently pending %s HTTP requests", name), float64(lookupstats.Pending), prometheus.GaugeValue)
gen(ch, doFunc, fmt.Sprintf("coredns_dnsfilter_%s_pending_max", name), fmt.Sprintf("Maximum number of pending %s HTTP requests", name), float64(lookupstats.PendingMax), prometheus.GaugeValue)
}
func (d *Plugin) doStats(ch interface{}, doFunc statsFunc) {
stats := d.d.GetStats()
gen(ch, doFunc, "coredns_dnsfilter_safebrowsing_requests", "Number of safebrowsing HTTP requests that were sent", float64(stats.Safebrowsing.Requests), prometheus.CounterValue)
gen(ch, doFunc, "coredns_dnsfilter_safebrowsing_cachehits", "Number of safebrowsing lookups that didn't need HTTP requests", float64(stats.Safebrowsing.CacheHits), prometheus.CounterValue)
gen(ch, doFunc, "coredns_dnsfilter_safebrowsing_pending", "Number of currently pending safebrowsing HTTP requests", float64(stats.Safebrowsing.Pending), prometheus.GaugeValue)
gen(ch, doFunc, "coredns_dnsfilter_safebrowsing_pending_max", "Maximum number of pending safebrowsing HTTP requests", float64(stats.Safebrowsing.PendingMax), prometheus.GaugeValue)
gen(ch, doFunc, "coredns_dnsfilter_parental_requests", "Number of parental HTTP requests that were sent", float64(stats.Parental.Requests), prometheus.CounterValue)
gen(ch, doFunc, "coredns_dnsfilter_parental_cachehits", "Number of parental lookups that didn't need HTTP requests", float64(stats.Parental.CacheHits), prometheus.CounterValue)
gen(ch, doFunc, "coredns_dnsfilter_parental_pending", "Number of currently pending parental HTTP requests", float64(stats.Parental.Pending), prometheus.GaugeValue)
gen(ch, doFunc, "coredns_dnsfilter_parental_pending_max", "Maximum number of pending parental HTTP requests", float64(stats.Parental.PendingMax), prometheus.GaugeValue)
doStatsLookup(ch, doFunc, "safebrowsing", &stats.Safebrowsing)
doStatsLookup(ch, doFunc, "parental", &stats.Parental)
}
func (d *Plugin) Describe(ch chan<- *prometheus.Desc) {

View File

@ -521,11 +521,11 @@ func (d *Dnsfilter) checkSafeBrowsing(host string) (Result, error) {
}
func (d *Dnsfilter) checkParental(host string) (Result, error) {
format2 := func(hashparam string) string {
format := func(hashparam string) string {
url := fmt.Sprintf(defaultParentalURL, hashparam, d.config.parentalSensitivity)
return url
}
handleBody2 := func(body []byte, hashes map[string]bool) (Result, error) {
handleBody := func(body []byte, hashes map[string]bool) (Result, error) {
// parse json
var m []struct {
Blocked bool `json:"blocked"`
@ -551,7 +551,7 @@ func (d *Dnsfilter) checkParental(host string) (Result, error) {
}
return result, nil
}
result, err := d.lookupCommon(host, &stats.Parental, parentalCache, false, format2, handleBody2)
result, err := d.lookupCommon(host, &stats.Parental, parentalCache, false, format, handleBody)
return result, err
}
@ -563,7 +563,7 @@ func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, cache gc
// check cache
cachedValue, isFound, err := getCachedReason(cache, host)
if isFound {
atomic.AddUint64(&stats.Safebrowsing.CacheHits, 1)
atomic.AddUint64(&lookupstats.CacheHits, 1)
return cachedValue, nil
}
if err != nil {