Skip to content

Commit

Permalink
fix(log): create subloggers for middlewares, DoT and DoH servers
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 31, 2024
1 parent 1593121 commit a8ac5b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/dns/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/qdm12/dns/v2/pkg/blockbuilder"
"github.com/qdm12/log"
)

type Service interface {
Expand All @@ -19,6 +20,11 @@ type Logger interface {
Info(msg string)
Warn(msg string)
Error(msg string)
LoggerConstructor
}

type LoggerConstructor interface {
New(options ...log.Option) *log.Logger
}

type Cache interface {
Expand Down
13 changes: 8 additions & 5 deletions internal/setup/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
cachemiddleware "github.com/qdm12/dns/v2/pkg/middlewares/cache"
filtermiddleware "github.com/qdm12/dns/v2/pkg/middlewares/filter"
"github.com/qdm12/dns/v2/pkg/middlewares/localdns"
"github.com/qdm12/log"
)

type Service interface {
Expand All @@ -17,21 +18,22 @@ type Service interface {
}

func DNS(userSettings config.Settings, ipv6Support bool, //nolint:ireturn
cache Cache, filter Filter, logger Logger, promRegistry PrometheusRegistry) (
server Service, err error) {
cache Cache, filter Filter, loggerConstructor LoggerConstructor,
promRegistry PrometheusRegistry) (server Service, err error) {
commonPrometheus := prometheus.Settings{
Prefix: *userSettings.Metrics.Prometheus.Subsystem,
Registry: promRegistry,
}

middlewares, err := setupMiddlewares(userSettings, cache,
filter, logger, commonPrometheus)
filter, loggerConstructor, commonPrometheus)
if err != nil {
return nil, fmt.Errorf("setting up middlewares: %w", err)
}

switch userSettings.Upstream {
case "dot":
logger := loggerConstructor.New(log.SetComponent("DNS over TLS"))
dotMetrics, err := dotMetrics(userSettings.Metrics.Type, commonPrometheus)
if err != nil {
return nil, fmt.Errorf("DoT metrics: %w", err)
Expand All @@ -40,6 +42,7 @@ func DNS(userSettings config.Settings, ipv6Support bool, //nolint:ireturn
return dotServer(userSettings, ipv6Support, middlewares,
logger, dotMetrics)
case "doh":
logger := loggerConstructor.New(log.SetComponent("DNS over HTTPS"))
dohMetrics, err := dohMetrics(userSettings.Metrics.Type, commonPrometheus)
if err != nil {
return nil, fmt.Errorf("DoH metrics: %w", err)
Expand All @@ -53,7 +56,7 @@ func DNS(userSettings config.Settings, ipv6Support bool, //nolint:ireturn
}

func setupMiddlewares(userSettings config.Settings, cache Cache,
filter Filter, logger Logger, commonPrometheus prometheus.Settings) (
filter Filter, loggerConstructor log.ChildConstructor, commonPrometheus prometheus.Settings) (
middlewares []Middleware, err error) {
cacheMiddleware, err := cachemiddleware.New(cachemiddleware.Settings{Cache: cache})
if err != nil {
Expand All @@ -64,7 +67,7 @@ func setupMiddlewares(userSettings config.Settings, cache Cache,
if *userSettings.LocalDNS.Enabled {
localDNSMiddleware, err := localdns.New(localdns.Settings{
Resolvers: userSettings.LocalDNS.Resolvers, // possibly auto-detected
Logger: logger,
Logger: loggerConstructor.New(log.SetComponent("local redirector")),
})
if err != nil {
return nil, fmt.Errorf("creating local DNS middleware: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions internal/setup/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/qdm12/dns/v2/pkg/middlewares/filter/update"
"github.com/qdm12/log"
)

type Logger interface {
Expand All @@ -13,6 +14,10 @@ type Logger interface {
Error(s string)
}

type LoggerConstructor interface {
New(options ...log.Option) *log.Logger
}

type Filter interface {
FilterRequest(request *dns.Msg) (blocked bool)
FilterResponse(response *dns.Msg) (blocked bool)
Expand Down

0 comments on commit a8ac5b0

Please sign in to comment.