From d4813a6448219d9425db7055ab6f3c9c8b361519 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Thu, 24 Nov 2022 21:39:58 +0100 Subject: [PATCH] feat(queryLog): log instance hostname to distinguish log entries in multi-instance installation(#319) (#756) * added hostname to util * added HostnameString * some leftover debug output? * added hostname to querylog * add optional volume mounts to documentation * changed documentation --- docs/installation.md | 4 +++ querylog/database_writer.go | 2 ++ querylog/file_writer.go | 1 + querylog/file_writer_test.go | 1 - querylog/logger_writer.go | 1 + util/hostname.go | 40 +++++++++++++++++++++++++++ util/hostname_test.go | 52 ++++++++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 util/hostname.go create mode 100644 util/hostname_test.go diff --git a/docs/installation.md b/docs/installation.md index a81d7b0a..831df57f 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -72,6 +72,8 @@ services: image: spx01/blocky container_name: blocky restart: unless-stopped + # Optional the instance hostname for logging purpose + hostname: blocky-hostname ports: - "53:53/tcp" - "53:53/udp" @@ -79,6 +81,8 @@ services: environment: - TZ=Europe/Berlin # Optional to synchronize the log timestamp with host volumes: + # Optional to synchronize the log timestamp with host + - /etc/localtime:/etc/localtime:ro # config file - ./config.yml:/app/config.yml ``` diff --git a/querylog/database_writer.go b/querylog/database_writer.go index 0c9e351c..9f800db3 100644 --- a/querylog/database_writer.go +++ b/querylog/database_writer.go @@ -31,6 +31,7 @@ type logEntry struct { EffectiveTLDP string Answer string ResponseCode string + Hostname string } type DatabaseWriter struct { @@ -140,6 +141,7 @@ func (d *DatabaseWriter) Write(entry *LogEntry) { EffectiveTLDP: eTLD, Answer: util.AnswerToString(entry.Response.Res.Answer), ResponseCode: dns.RcodeToString[entry.Response.Res.Rcode], + Hostname: util.HostnameString(), } d.lock.Lock() diff --git a/querylog/file_writer.go b/querylog/file_writer.go index 1d7eca27..0b6af69c 100644 --- a/querylog/file_writer.go +++ b/querylog/file_writer.go @@ -114,6 +114,7 @@ func createQueryLogRow(logEntry *LogEntry) []string { util.QuestionToString(request.Req.Question), util.AnswerToString(response.Res.Answer), dns.RcodeToString[response.Res.Rcode], + util.HostnameString(), } } diff --git a/querylog/file_writer_test.go b/querylog/file_writer_test.go index 875cfa42..6c9400d2 100644 --- a/querylog/file_writer_test.go +++ b/querylog/file_writer_test.go @@ -186,7 +186,6 @@ var _ = Describe("FileWriter", func() { DurationMs: 20, }) }) - fmt.Println(tmpDir.Path) Eventually(func(g Gomega) int { filesCount, err := tmpDir.CountFiles() diff --git a/querylog/logger_writer.go b/querylog/logger_writer.go index 307c024c..188a67c8 100644 --- a/querylog/logger_writer.go +++ b/querylog/logger_writer.go @@ -29,6 +29,7 @@ func (d *LoggerWriter) Write(entry *LogEntry) { "response_code": dns.RcodeToString[entry.Response.Res.Rcode], "answer": util.AnswerToString(entry.Response.Res.Answer), "duration_ms": entry.DurationMs, + "hostname": util.HostnameString(), }, ).Infof("query resolved") } diff --git a/util/hostname.go b/util/hostname.go new file mode 100644 index 00000000..81bb033e --- /dev/null +++ b/util/hostname.go @@ -0,0 +1,40 @@ +package util + +import ( + "os" + "strings" +) + +//nolint:gochecknoglobals +var ( + hostname string + hostnameErr error +) + +const hostnameFile string = "/etc/hostname" + +//nolint:gochecknoinits +func init() { + getHostname(hostnameFile) +} + +// Direct replacement for os.Hostname +func Hostname() (string, error) { + return hostname, hostnameErr +} + +// Only return the hostname(may be empty if there was an error) +func HostnameString() string { + return hostname +} + +func getHostname(location string) { + hostname, hostnameErr = os.Hostname() + + if hn, err := os.ReadFile(location); err == nil { + hostname = strings.TrimSpace(string(hn)) + hostnameErr = nil + + return + } +} diff --git a/util/hostname_test.go b/util/hostname_test.go new file mode 100644 index 00000000..d99cac4e --- /dev/null +++ b/util/hostname_test.go @@ -0,0 +1,52 @@ +package util + +import ( + "os" + "strings" + + "github.com/0xERR0R/blocky/helpertest" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Hostname function tests", func() { + When("file is present", func() { + var ( + tmpDir *helpertest.TmpFolder + ) + + BeforeEach(func() { + tmpDir = helpertest.NewTmpFolder("hostname") + Expect(tmpDir.Error).Should(Succeed()) + DeferCleanup(tmpDir.Clean) + }) + + It("should be used", func() { + tmpFile := tmpDir.CreateStringFile("filetest1", "TestName ") + Expect(tmpFile.Error).Should(Succeed()) + getHostname(tmpFile.Path) + + fhn, err := os.ReadFile(tmpFile.Path) + Expect(err).Should(Succeed()) + + hn, err := Hostname() + Expect(err).Should(Succeed()) + + Expect(hn).Should(Equal(strings.TrimSpace(string(fhn)))) + }) + }) + + When("file is not present", func() { + It("should use os.Hostname", func() { + getHostname("/does-not-exist") + + _, err := Hostname() + Expect(err).Should(Succeed()) + + ohn, err := os.Hostname() + Expect(err).Should(Succeed()) + + Expect(HostnameString()).Should(Equal(ohn)) + }) + }) +})