Skip to content

Commit

Permalink
home: add stats qlog custom dir
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Feb 5, 2024
1 parent 34aa81c commit 739b158
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ type tlsConfigSettings struct {
}

type queryLogConfig struct {
// DirPath is the custom directory for logs.
DirPath string `yaml:"dir_path"`

// Ignored is the list of host names, which should not be written to log.
// "." is considered to be the root domain.
Ignored []string `yaml:"ignored"`
Expand All @@ -278,6 +281,9 @@ type queryLogConfig struct {
}

type statsConfig struct {
// DirPath is the custom directory for statistics.
DirPath string `yaml:"dir_path"`

// Ignored is the list of host names, which should not be counted.
Ignored []string `yaml:"ignored"`

Expand Down
57 changes: 53 additions & 4 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ func onConfigModified() {
// server and initializes it at last. It also must not be called unless
// [config] and [Context] are initialized.
func initDNS() (err error) {
baseDir := Context.getDataDir()

anonymizer := config.anonymizer()

statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config)
if err != nil {
return err
}

statsConf := stats.Config{
Filename: filepath.Join(baseDir, "stats.db"),
Filename: filepath.Join(statsDir, "stats.db"),
Limit: config.Stats.Interval.Duration,
ConfigModified: onConfigModified,
HTTPRegister: httpRegister,
Expand All @@ -75,7 +78,7 @@ func initDNS() (err error) {
ConfigModified: onConfigModified,
HTTPRegister: httpRegister,
FindClient: Context.clients.findMultiple,
BaseDir: baseDir,
BaseDir: querylogDir,
AnonymizeClientIP: config.DNS.AnonymizeClientIP,
RotationIvl: config.QueryLog.Interval.Duration,
MemSize: config.QueryLog.MemSize,
Expand Down Expand Up @@ -545,3 +548,49 @@ func (r safeSearchResolver) LookupIP(

return ips, nil
}

// checkStatsAndQuerylogDirs checks and returns directory paths to store
// statistics and query log.
func checkStatsAndQuerylogDirs(
ctx *homeContext,
conf *configuration,
) (statsDir, querylogDir string, err error) {
baseDir := ctx.getDataDir()

statsDir = conf.Stats.DirPath
if statsDir == "" {
statsDir = baseDir
} else {
err = checkDir(statsDir)
if err != nil {
return "", "", fmt.Errorf("statistics: custom directory: %w", err)
}
}

querylogDir = conf.QueryLog.DirPath
if querylogDir == "" {
querylogDir = baseDir
} else {
err = checkDir(querylogDir)
if err != nil {
return "", "", fmt.Errorf("querylog: custom directory: %w", err)
}
}

return statsDir, querylogDir, nil
}

// checkDir checks if the path is a directory.
func checkDir(path string) (err error) {
var fi os.FileInfo
if fi, err = os.Stat(path); err != nil {
// Don't wrap the error, since it's informative enough as is.
return err
}

if !fi.IsDir() {
return fmt.Errorf("%q is not a directory", path)
}

return nil
}

0 comments on commit 739b158

Please sign in to comment.