Skip to content

Commit

Permalink
feat(queryLog): log instance hostname to distinguish log entries in m…
Browse files Browse the repository at this point in the history
…ulti-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
  • Loading branch information
kwitsch authored Nov 24, 2022
1 parent e63d9fb commit d4813a6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 72,17 @@ 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"
- "4000:4000/tcp"
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
```
Expand Down
2 changes: 2 additions & 0 deletions querylog/database_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 31,7 @@ type logEntry struct {
EffectiveTLDP string
Answer string
ResponseCode string
Hostname string
}

type DatabaseWriter struct {
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions querylog/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
1 change: 0 additions & 1 deletion querylog/file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 186,6 @@ var _ = Describe("FileWriter", func() {
DurationMs: 20,
})
})
fmt.Println(tmpDir.Path)

Eventually(func(g Gomega) int {
filesCount, err := tmpDir.CountFiles()
Expand Down
1 change: 1 addition & 0 deletions querylog/logger_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 40 additions & 0 deletions util/hostname.go
Original file line number Diff line number Diff line change
@@ -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 changes: 52 additions & 0 deletions util/hostname_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})

0 comments on commit d4813a6

Please sign in to comment.