Skip to content

Commit

Permalink
all: upd chlog
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Oct 31, 2023
1 parent 040b6b9 commit 4d7431c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ NOTE: Add new changes BELOW THIS COMMENT.

### Fixed

- Average request processing time calculation ([#6220]).
- `$important,dnsrewrite` rules do not take precedence over allowlist rules
([#6204]).
- Dark mode DNS rewrite background ([#6329]).
- Issues with QUIC and HTTP/3 upstreams on Linux ([#6335]).

[#4977]: https://github.com/AdguardTeam/AdGuardHome/issues/4977
[#6204]: https://github.com/AdguardTeam/AdGuardHome/issues/6204
[#6220]: https://github.com/AdguardTeam/AdGuardHome/issues/6220
[#6329]: https://github.com/AdguardTeam/AdGuardHome/issues/6329
[#6335]: https://github.com/AdguardTeam/AdGuardHome/issues/6335

Expand Down
33 changes: 12 additions & 21 deletions internal/dnsforward/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) {
defer log.Debug("dnsforward: finished processing querylog and stats")

pctx := dctx.proxyCtx
elapsed := pctx.QueryDuration

q := pctx.Req.Question[0]
host := aghnet.NormalizeDomain(q.Name)

Expand All @@ -43,7 +41,7 @@ func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) {
defer s.serverLock.RUnlock()

if s.shouldLog(host, qt, cl, ids) {
s.logQuery(dctx, pctx, elapsed, ip)
s.logQuery(dctx, ip)
} else {
log.Debug(
"dnsforward: request %s %s %q from %s ignored; not adding to querylog",
Expand All @@ -55,7 +53,7 @@ func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) {
}

if s.shouldCountStat(host, qt, cl, ids) {
s.updateStats(dctx, elapsed, *dctx.result, ipStr)
s.updateStats(dctx, ipStr)
} else {
log.Debug(
"dnsforward: request %s %s %q from %s ignored; not counting in stats",
Expand Down Expand Up @@ -90,12 +88,9 @@ func (s *Server) shouldCountStat(host string, qt, cl uint16, ids []string) (ok b
}

// logQuery pushes the request details into the query log.
func (s *Server) logQuery(
dctx *dnsContext,
pctx *proxy.DNSContext,
elapsed time.Duration,
ip net.IP,
) {
func (s *Server) logQuery(dctx *dnsContext, ip net.IP) {
pctx := dctx.proxyCtx

p := &querylog.AddParams{
Question: pctx.Req,
ReqECS: pctx.ReqECS,
Expand All @@ -104,7 +99,7 @@ func (s *Server) logQuery(
Result: dctx.result,
ClientID: dctx.clientID,
ClientIP: ip,
Elapsed: elapsed,
Elapsed: time.Since(dctx.startTime),
AuthenticatedData: dctx.responseAD,
}

Expand Down Expand Up @@ -132,30 +127,26 @@ func (s *Server) logQuery(
}

// updatesStats writes the request into statistics.
func (s *Server) updateStats(
ctx *dnsContext,
elapsed time.Duration,
res filtering.Result,
clientIP string,
) {
pctx := ctx.proxyCtx
func (s *Server) updateStats(dctx *dnsContext, clientIP string) {
pctx := dctx.proxyCtx

e := &stats.Entry{
Domain: aghnet.NormalizeDomain(pctx.Req.Question[0].Name),
Result: stats.RNotFiltered,
Time: elapsed,
Time: pctx.QueryDuration,
}

if pctx.Upstream != nil {
e.Upstream = pctx.Upstream.Address()
}

if clientID := ctx.clientID; clientID != "" {
if clientID := dctx.clientID; clientID != "" {
e.Client = clientID
} else {
e.Client = clientIP
}

switch res.Reason {
switch dctx.result.Reason {
case filtering.FilteredSafeBrowsing:
e.Result = stats.RSafeBrowsing
case filtering.FilteredParental:
Expand Down
5 changes: 2 additions & 3 deletions internal/stats/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,11 @@ func prepareTopUpstreamsAvgTime(
) (topUpstreamsAvgTime []topAddrsFloat) {
keys := maps.Keys(upstreamsAvgTime)

// Sort in ascending order.
slices.SortFunc(keys, func(a, b string) (res int) {
switch x, y := upstreamsAvgTime[a], upstreamsAvgTime[b]; {
case x < y:
return -1
case x > y:
return -1
case x < y:
return +1
default:
return 0
Expand Down
6 changes: 3 additions & 3 deletions internal/stats/unit_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func TestTopUpstreamsPairs(t *testing.T) {
{"1.1.1.1": 2},
},
wantAvgTime: []topAddrsFloat{
{"1.1.1.1": 1},
{"2.2.2.2": 10},
{"3.3.3.3": 100},
{"4.4.4.4": 1000},
{"3.3.3.3": 100},
{"2.2.2.2": 10},
{"1.1.1.1": 1},
},
}}

Expand Down

0 comments on commit 4d7431c

Please sign in to comment.