Pull request 2304: AGDNS-2374-slog-querylog
Squashed commit of the following: commit e98e5efaaf5388551322933321df0707ad7b2a9c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 21 13:15:51 2024 +0300 all: imp code commit fbe728c9aa03a325c2733c214412f9071faba5ed Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Nov 18 20:57:15 2024 +0300 all: imp code commit ef715c58cb6621236424f55268390aa3f997f883 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Nov 18 16:39:35 2024 +0300 all: imp code commit cbb993f7ae4311b2a73ace7066a5dabf190291be Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Nov 18 14:03:42 2024 +0300 all: imp code commit 8d88d799303c7e3d15322fee87780fedb408ea13 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Nov 15 15:57:07 2024 +0300 all: slog querylog
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
package querylog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -11,7 +13,7 @@ import (
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
||||
"github.com/AdguardTeam/golibs/container"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
||||
"github.com/AdguardTeam/golibs/timeutil"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
@@ -22,6 +24,10 @@ const queryLogFileName = "querylog.json"
|
||||
|
||||
// queryLog is a structure that writes and reads the DNS query log.
|
||||
type queryLog struct {
|
||||
// logger is used for logging the operation of the query log. It must not
|
||||
// be nil.
|
||||
logger *slog.Logger
|
||||
|
||||
// confMu protects conf.
|
||||
confMu *sync.RWMutex
|
||||
|
||||
@@ -76,24 +82,34 @@ func NewClientProto(s string) (cp ClientProto, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *queryLog) Start() {
|
||||
// type check
|
||||
var _ QueryLog = (*queryLog)(nil)
|
||||
|
||||
// Start implements the [QueryLog] interface for *queryLog.
|
||||
func (l *queryLog) Start(ctx context.Context) (err error) {
|
||||
if l.conf.HTTPRegister != nil {
|
||||
l.initWeb()
|
||||
}
|
||||
|
||||
go l.periodicRotate()
|
||||
go l.periodicRotate(ctx)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *queryLog) Close() {
|
||||
// Shutdown implements the [QueryLog] interface for *queryLog.
|
||||
func (l *queryLog) Shutdown(ctx context.Context) (err error) {
|
||||
l.confMu.RLock()
|
||||
defer l.confMu.RUnlock()
|
||||
|
||||
if l.conf.FileEnabled {
|
||||
err := l.flushLogBuffer()
|
||||
err = l.flushLogBuffer(ctx)
|
||||
if err != nil {
|
||||
log.Error("querylog: closing: %s", err)
|
||||
// Don't wrap the error because it's informative enough as is.
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkInterval(ivl time.Duration) (ok bool) {
|
||||
@@ -123,6 +139,7 @@ func validateIvl(ivl time.Duration) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteDiskConfig implements the [QueryLog] interface for *queryLog.
|
||||
func (l *queryLog) WriteDiskConfig(c *Config) {
|
||||
l.confMu.RLock()
|
||||
defer l.confMu.RUnlock()
|
||||
@@ -131,7 +148,7 @@ func (l *queryLog) WriteDiskConfig(c *Config) {
|
||||
}
|
||||
|
||||
// Clear memory buffer and remove log files
|
||||
func (l *queryLog) clear() {
|
||||
func (l *queryLog) clear(ctx context.Context) {
|
||||
l.fileFlushLock.Lock()
|
||||
defer l.fileFlushLock.Unlock()
|
||||
|
||||
@@ -146,19 +163,24 @@ func (l *queryLog) clear() {
|
||||
oldLogFile := l.logFile + ".1"
|
||||
err := os.Remove(oldLogFile)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
log.Error("removing old log file %q: %s", oldLogFile, err)
|
||||
l.logger.ErrorContext(
|
||||
ctx,
|
||||
"removing old log file",
|
||||
"file", oldLogFile,
|
||||
slogutil.KeyError, err,
|
||||
)
|
||||
}
|
||||
|
||||
err = os.Remove(l.logFile)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
log.Error("removing log file %q: %s", l.logFile, err)
|
||||
l.logger.ErrorContext(ctx, "removing log file", "file", l.logFile, slogutil.KeyError, err)
|
||||
}
|
||||
|
||||
log.Debug("querylog: cleared")
|
||||
l.logger.DebugContext(ctx, "cleared")
|
||||
}
|
||||
|
||||
// newLogEntry creates an instance of logEntry from parameters.
|
||||
func newLogEntry(params *AddParams) (entry *logEntry) {
|
||||
func newLogEntry(ctx context.Context, logger *slog.Logger, params *AddParams) (entry *logEntry) {
|
||||
q := params.Question.Question[0]
|
||||
qHost := aghnet.NormalizeDomain(q.Name)
|
||||
|
||||
@@ -187,8 +209,8 @@ func newLogEntry(params *AddParams) (entry *logEntry) {
|
||||
entry.ReqECS = params.ReqECS.String()
|
||||
}
|
||||
|
||||
entry.addResponse(params.Answer, false)
|
||||
entry.addResponse(params.OrigAnswer, true)
|
||||
entry.addResponse(ctx, logger, params.Answer, false)
|
||||
entry.addResponse(ctx, logger, params.OrigAnswer, true)
|
||||
|
||||
return entry
|
||||
}
|
||||
@@ -209,9 +231,12 @@ func (l *queryLog) Add(params *AddParams) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO(s.chzhen): Pass context.
|
||||
ctx := context.TODO()
|
||||
|
||||
err := params.validate()
|
||||
if err != nil {
|
||||
log.Error("querylog: adding record: %s, skipping", err)
|
||||
l.logger.ErrorContext(ctx, "adding record", slogutil.KeyError, err)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -220,7 +245,7 @@ func (l *queryLog) Add(params *AddParams) {
|
||||
params.Result = &filtering.Result{}
|
||||
}
|
||||
|
||||
entry := newLogEntry(params)
|
||||
entry := newLogEntry(ctx, l.logger, params)
|
||||
|
||||
l.bufferLock.Lock()
|
||||
defer l.bufferLock.Unlock()
|
||||
@@ -232,9 +257,9 @@ func (l *queryLog) Add(params *AddParams) {
|
||||
|
||||
// TODO(s.chzhen): Fix occasional rewrite of entires.
|
||||
go func() {
|
||||
flushErr := l.flushLogBuffer()
|
||||
flushErr := l.flushLogBuffer(ctx)
|
||||
if flushErr != nil {
|
||||
log.Error("querylog: flushing after adding: %s", flushErr)
|
||||
l.logger.ErrorContext(ctx, "flushing after adding", slogutil.KeyError, flushErr)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -247,7 +272,8 @@ func (l *queryLog) ShouldLog(host string, _, _ uint16, ids []string) bool {
|
||||
|
||||
c, err := l.findClient(ids)
|
||||
if err != nil {
|
||||
log.Error("querylog: finding client: %s", err)
|
||||
// TODO(s.chzhen): Pass context.
|
||||
l.logger.ErrorContext(context.TODO(), "finding client", slogutil.KeyError, err)
|
||||
}
|
||||
|
||||
if c != nil && c.IgnoreQueryLog {
|
||||
|
||||
Reference in New Issue
Block a user