Skip to content

Commit 54eec9a

Browse files
committed
Add logging for dns lookups with timestamps
1 parent 48123e4 commit 54eec9a

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

dns/dnsperfgo/main.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"flag"
2121
"fmt"
2222
"log"
23+
"log/slog"
2324
"net"
2425
"net/http"
2526
"os"
@@ -39,10 +40,10 @@ import (
3940
const clusterDomain = "cluster.local"
4041

4142
type Config struct {
42-
qps int
43-
testDuration, idleDuration, queryTimeout time.Duration
44-
hostnameFile string
45-
queryClusterNames, logQueries bool
43+
qps int
44+
testDuration, idleDuration, queryTimeout time.Duration
45+
hostnameFile string
46+
queryClusterNames, logQueries, enableErrorLogging, enableLatencyLogging bool
4647
}
4748

4849
type dnsClient struct {
@@ -59,6 +60,13 @@ type dnsClient struct {
5960

6061
type LookupFunc func(string) ([]string, error)
6162

63+
var (
64+
// infoLogger writes to stdout, initialized if flag is set.
65+
infoLogger *slog.Logger
66+
// errorLogger writes to stderr, initialized if flag is set.
67+
errorLogger *slog.Logger
68+
)
69+
6270
func main() {
6371
config := Config{}
6472
flag.IntVar(&config.qps, "qps", 10, "The number of DNS queries per second to issue")
@@ -68,8 +76,15 @@ func main() {
6876
flag.StringVar(&config.hostnameFile, "inputfile", "", "Path to the file containing hostnames to lookup. Hostnames should be newline-separated.")
6977
flag.BoolVar(&config.queryClusterNames, "query-cluster-names", false, "Indicates whether the query names should be the service names in the cluster.")
7078
flag.BoolVar(&config.logQueries, "log-queries", false, "Indicates whether each query should be logged.")
71-
79+
flag.BoolVar(&config.enableLatencyLogging, "enable-latency-logging", false, "Indicate whether to enable structured logging for each DNS lookup latency.")
80+
flag.BoolVar(&config.enableErrorLogging, "enable-error-logging", false, "Indicate whether to enable structured logging for each DNS lookup errors.")
7281
flag.Parse()
82+
if config.enableLatencyLogging {
83+
infoLogger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
84+
}
85+
if config.enableErrorLogging {
86+
errorLogger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
87+
}
7388
log.Printf("Starting dnstest with config parameters - %+v", config)
7489
client := &dnsClient{config: &config, stopChan: make(chan os.Signal, 1)}
7590
signal.Notify(client.stopChan, syscall.SIGTERM)
@@ -199,10 +214,17 @@ func (c *dnsClient) logResults() {
199214

200215
}
201216

202-
func (c *dnsClient) updateResults(timedOut bool, err error) {
217+
func (c *dnsClient) updateResults(timedOut bool, err error, name string) {
203218
c.resultsLock.Lock()
204219
defer c.resultsLock.Unlock()
205220
if err != nil {
221+
if errorLogger != nil {
222+
errorLogger.Error("DNS lookup failed",
223+
"hostname", name,
224+
"error", err.Error(),
225+
"timed_out", timedOut,
226+
)
227+
}
206228
c.result.errorCount++
207229
dnsErrorsCounter.Inc()
208230
}
@@ -223,6 +245,12 @@ func (c *dnsClient) runQuery(name string, timeout time.Duration, lookupFunc Look
223245
startTime := time.Now()
224246
_, err := lookupFunc(name)
225247
latency := time.Since(startTime)
248+
if infoLogger != nil && err == nil {
249+
infoLogger.Info("DNS lookup successful, latency recorded",
250+
"hostname", name,
251+
"latency_seconds", latency.Seconds(),
252+
)
253+
}
226254
dnsLatency.Observe(latency.Seconds())
227255
resultChan <- err
228256
}(resultChan)
@@ -237,7 +265,7 @@ func (c *dnsClient) runQuery(name string, timeout time.Duration, lookupFunc Look
237265
if err != nil {
238266
log.Printf("Failed DNS lookup of name %q, err - %v\n", name, err)
239267
}
240-
c.updateResults(timedOut, err)
268+
c.updateResults(timedOut, err, name)
241269
}()
242270

243271
for {

0 commit comments

Comments
 (0)