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
This commit is contained in:
Kwitsch 2022-11-24 21:39:58 +01:00 committed by GitHub
parent e63d9fbdad
commit d4813a6448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 100 additions and 1 deletions

View File

@ -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
```

View File

@ -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()

View File

@ -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(),
}
}

View File

@ -186,7 +186,6 @@ var _ = Describe("FileWriter", func() {
DurationMs: 20,
})
})
fmt.Println(tmpDir.Path)
Eventually(func(g Gomega) int {
filesCount, err := tmpDir.CountFiles()

View File

@ -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")
}

40
util/hostname.go Normal file
View File

@ -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
}
}

52
util/hostname_test.go Normal file
View File

@ -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))
})
})
})