From 038bae59d51497de1db7153e00e779db30f79721 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Thu, 3 Oct 2024 20:24:48 +0300 Subject: [PATCH] all: imp code --- internal/dnsforward/dnsforward_test.go | 2 +- internal/filtering/safesearch/safesearch.go | 27 ++++++++++--------- .../safesearch/safesearch_internal_test.go | 2 +- .../filtering/safesearch/safesearch_test.go | 8 +++--- internal/home/clients.go | 7 ++++- internal/home/clientshttp.go | 7 ++++- internal/home/home.go | 5 ++-- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/internal/dnsforward/dnsforward_test.go b/internal/dnsforward/dnsforward_test.go index 94811545..de9ef09c 100644 --- a/internal/dnsforward/dnsforward_test.go +++ b/internal/dnsforward/dnsforward_test.go @@ -516,7 +516,7 @@ func TestSafeSearch(t *testing.T) { ctx := testutil.ContextWithTimeout(t, testTimeout) safeSearch, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: safeSearchConf, CacheSize: filterConf.SafeSearchCacheSize, CacheTTL: time.Minute * time.Duration(filterConf.CacheTime), diff --git a/internal/filtering/safesearch/safesearch.go b/internal/filtering/safesearch/safesearch.go index 02227cf8..d422b971 100644 --- a/internal/filtering/safesearch/safesearch.go +++ b/internal/filtering/safesearch/safesearch.go @@ -24,6 +24,12 @@ import ( "github.com/miekg/dns" ) +// Attribute keys and values for logging. +const ( + LogPrefix = "safesearch" + LogKey = "client" +) + // Service is a enum with service names used as search providers. type Service string @@ -62,8 +68,8 @@ func isServiceProtected(s filtering.SafeSearchConfig, service Service) (ok bool) // DefaultConfig is the configuration structure for [Default]. type DefaultConfig struct { - // BaseLogger is used to create logger for [Default]. - BaseLogger *slog.Logger + // Logger is used for logging the operation of the safe search filter. + Logger *slog.Logger // ClientName is the name of the persistent client associated with the safe // search filter, if there is one. @@ -93,20 +99,17 @@ type Default struct { // engine may be nil, which means that this safe search filter is disabled. engine *urlfilter.DNSEngine - cache cache.Cache + // cache stores safe search filtering results. + cache cache.Cache + + // cacheTTL is the Time to Live duration for cached items. cacheTTL time.Duration } -// NewDefault returns an initialized default safe search filter. name is used -// for logging. +// NewDefault returns an initialized default safe search filter. func NewDefault(ctx context.Context, conf *DefaultConfig) (ss *Default, err error) { - logger := conf.BaseLogger.With(slogutil.KeyPrefix, "safesearch") - if conf.ClientName != "" { - logger = logger.With("client", conf.ClientName) - } - ss = &Default{ - logger: logger, + logger: conf.Logger, mu: &sync.RWMutex{}, cache: cache.New(cache.Config{ EnableLRU: true, @@ -132,7 +135,7 @@ func (ss *Default) resetEngine( conf filtering.SafeSearchConfig, ) (err error) { if !conf.Enabled { - ss.logger.InfoContext(ctx, "disabled") + ss.logger.DebugContext(ctx, "disabled") return nil } diff --git a/internal/filtering/safesearch/safesearch_internal_test.go b/internal/filtering/safesearch/safesearch_internal_test.go index 4779cc53..f8fbe176 100644 --- a/internal/filtering/safesearch/safesearch_internal_test.go +++ b/internal/filtering/safesearch/safesearch_internal_test.go @@ -41,7 +41,7 @@ var yandexIP = netip.AddrFrom4([4]byte{213, 180, 193, 56}) func newForTest(t testing.TB, ssConf filtering.SafeSearchConfig) (ss *Default) { ss, err := NewDefault(testutil.ContextWithTimeout(t, testTimeout), &DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: ssConf, CacheSize: testCacheSize, CacheTTL: testCacheTTL, diff --git a/internal/filtering/safesearch/safesearch_test.go b/internal/filtering/safesearch/safesearch_test.go index 688d54b5..77eca2f1 100644 --- a/internal/filtering/safesearch/safesearch_test.go +++ b/internal/filtering/safesearch/safesearch_test.go @@ -49,7 +49,7 @@ func TestDefault_CheckHost_yandex(t *testing.T) { conf := testConf ctx := testutil.ContextWithTimeout(t, testTimeout) ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: conf, CacheSize: testCacheSize, CacheTTL: testCacheTTL, @@ -111,7 +111,7 @@ func TestDefault_CheckHost_yandex(t *testing.T) { func TestDefault_CheckHost_google(t *testing.T) { ctx := testutil.ContextWithTimeout(t, testTimeout) ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: testConf, CacheSize: testCacheSize, CacheTTL: testCacheTTL, @@ -163,7 +163,7 @@ func (r *testResolver) LookupIP( func TestDefault_CheckHost_duckduckgoAAAA(t *testing.T) { ctx := testutil.ContextWithTimeout(t, testTimeout) ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: testConf, CacheSize: testCacheSize, CacheTTL: testCacheTTL, @@ -186,7 +186,7 @@ func TestDefault_Update(t *testing.T) { conf := testConf ctx := testutil.ContextWithTimeout(t, testTimeout) ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: slogutil.NewDiscardLogger(), + Logger: slogutil.NewDiscardLogger(), ServicesConfig: conf, CacheSize: testCacheSize, CacheTTL: testCacheTTL, diff --git a/internal/home/clients.go b/internal/home/clients.go index 5005cbf3..e6fcb99a 100644 --- a/internal/home/clients.go +++ b/internal/home/clients.go @@ -21,6 +21,7 @@ import ( "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/golibs/errors" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/stringutil" ) @@ -213,9 +214,13 @@ func (o *clientObject) toPersistent( } if o.SafeSearchConf.Enabled { + logger := baseLogger.With( + slogutil.KeyPrefix, safesearch.LogPrefix, + safesearch.LogKey, cli.Name, + ) var ss *safesearch.Default ss, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: baseLogger, + Logger: logger, ServicesConfig: o.SafeSearchConf, ClientName: cli.Name, CacheSize: safeSearchCacheSize, diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index c5c4913d..549a078a 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -14,6 +14,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch" "github.com/AdguardTeam/AdGuardHome/internal/schedule" "github.com/AdguardTeam/AdGuardHome/internal/whois" + "github.com/AdguardTeam/golibs/logutil/slogutil" ) // clientJSON is a common structure used by several handlers to deal with @@ -210,9 +211,13 @@ func (clients *clientsContainer) jsonToClient( c.UseOwnBlockedServices = !cj.UseGlobalBlockedServices if c.SafeSearchConf.Enabled { + logger := clients.baseLogger.With( + slogutil.KeyPrefix, safesearch.LogPrefix, + safesearch.LogKey, c.Name, + ) var ss *safesearch.Default ss, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: clients.baseLogger, + Logger: logger, ServicesConfig: c.SafeSearchConf, ClientName: c.Name, CacheSize: clients.safeSearchCacheSize, diff --git a/internal/home/home.go b/internal/home/home.go index 09812a91..5e7f8889 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -452,8 +452,9 @@ func setupDNSFilteringConf( conf.ParentalBlockHost = host } + logger := baseLogger.With(slogutil.KeyPrefix, safesearch.LogPrefix) conf.SafeSearch, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ - BaseLogger: baseLogger, + Logger: logger, ServicesConfig: conf.SafeSearchConf, CacheSize: conf.SafeSearchCacheSize, CacheTTL: cacheTime, @@ -590,7 +591,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) { // data first, but also to avoid relying on automatic Go init() function. filtering.InitModule() - // TODO(s.chzhen): Use it. + // TODO(s.chzhen): Use it for the entire initialization process. ctx := context.Background() err = initContextClients(ctx, slogLogger)