From 54eec9a8b4e443e698ec184b28478817717a52f9 Mon Sep 17 00:00:00 2001 From: "Zhipeng (David) Tan" Date: Sat, 27 Sep 2025 14:38:37 +0000 Subject: [PATCH 1/2] Add logging for dns lookups with timestamps --- dns/dnsperfgo/main.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/dns/dnsperfgo/main.go b/dns/dnsperfgo/main.go index 7d24ab396e..c46b9d204b 100644 --- a/dns/dnsperfgo/main.go +++ b/dns/dnsperfgo/main.go @@ -20,6 +20,7 @@ import ( "flag" "fmt" "log" + "log/slog" "net" "net/http" "os" @@ -39,10 +40,10 @@ import ( const clusterDomain = "cluster.local" type Config struct { - qps int - testDuration, idleDuration, queryTimeout time.Duration - hostnameFile string - queryClusterNames, logQueries bool + qps int + testDuration, idleDuration, queryTimeout time.Duration + hostnameFile string + queryClusterNames, logQueries, enableErrorLogging, enableLatencyLogging bool } type dnsClient struct { @@ -59,6 +60,13 @@ type dnsClient struct { type LookupFunc func(string) ([]string, error) +var ( + // infoLogger writes to stdout, initialized if flag is set. + infoLogger *slog.Logger + // errorLogger writes to stderr, initialized if flag is set. + errorLogger *slog.Logger +) + func main() { config := Config{} flag.IntVar(&config.qps, "qps", 10, "The number of DNS queries per second to issue") @@ -68,8 +76,15 @@ func main() { flag.StringVar(&config.hostnameFile, "inputfile", "", "Path to the file containing hostnames to lookup. Hostnames should be newline-separated.") flag.BoolVar(&config.queryClusterNames, "query-cluster-names", false, "Indicates whether the query names should be the service names in the cluster.") flag.BoolVar(&config.logQueries, "log-queries", false, "Indicates whether each query should be logged.") - + flag.BoolVar(&config.enableLatencyLogging, "enable-latency-logging", false, "Indicate whether to enable structured logging for each DNS lookup latency.") + flag.BoolVar(&config.enableErrorLogging, "enable-error-logging", false, "Indicate whether to enable structured logging for each DNS lookup errors.") flag.Parse() + if config.enableLatencyLogging { + infoLogger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) + } + if config.enableErrorLogging { + errorLogger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + } log.Printf("Starting dnstest with config parameters - %+v", config) client := &dnsClient{config: &config, stopChan: make(chan os.Signal, 1)} signal.Notify(client.stopChan, syscall.SIGTERM) @@ -199,10 +214,17 @@ func (c *dnsClient) logResults() { } -func (c *dnsClient) updateResults(timedOut bool, err error) { +func (c *dnsClient) updateResults(timedOut bool, err error, name string) { c.resultsLock.Lock() defer c.resultsLock.Unlock() if err != nil { + if errorLogger != nil { + errorLogger.Error("DNS lookup failed", + "hostname", name, + "error", err.Error(), + "timed_out", timedOut, + ) + } c.result.errorCount++ dnsErrorsCounter.Inc() } @@ -223,6 +245,12 @@ func (c *dnsClient) runQuery(name string, timeout time.Duration, lookupFunc Look startTime := time.Now() _, err := lookupFunc(name) latency := time.Since(startTime) + if infoLogger != nil && err == nil { + infoLogger.Info("DNS lookup successful, latency recorded", + "hostname", name, + "latency_seconds", latency.Seconds(), + ) + } dnsLatency.Observe(latency.Seconds()) resultChan <- err }(resultChan) @@ -237,7 +265,7 @@ func (c *dnsClient) runQuery(name string, timeout time.Duration, lookupFunc Look if err != nil { log.Printf("Failed DNS lookup of name %q, err - %v\n", name, err) } - c.updateResults(timedOut, err) + c.updateResults(timedOut, err, name) }() for { From d09cd7324495f1ab7846e407ef1cace1c9ed2c84 Mon Sep 17 00:00:00 2001 From: "Zhipeng (David) Tan" Date: Mon, 6 Oct 2025 16:22:44 +0000 Subject: [PATCH 2/2] always enable errorLogger and replace the error message with errorLogger --- dns/dnsperfgo/main.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/dns/dnsperfgo/main.go b/dns/dnsperfgo/main.go index c46b9d204b..89f446e4e6 100644 --- a/dns/dnsperfgo/main.go +++ b/dns/dnsperfgo/main.go @@ -40,10 +40,10 @@ import ( const clusterDomain = "cluster.local" type Config struct { - qps int - testDuration, idleDuration, queryTimeout time.Duration - hostnameFile string - queryClusterNames, logQueries, enableErrorLogging, enableLatencyLogging bool + qps int + testDuration, idleDuration, queryTimeout time.Duration + hostnameFile string + queryClusterNames, logQueries, enableLatencyLogging bool } type dnsClient struct { @@ -77,14 +77,11 @@ func main() { flag.BoolVar(&config.queryClusterNames, "query-cluster-names", false, "Indicates whether the query names should be the service names in the cluster.") flag.BoolVar(&config.logQueries, "log-queries", false, "Indicates whether each query should be logged.") flag.BoolVar(&config.enableLatencyLogging, "enable-latency-logging", false, "Indicate whether to enable structured logging for each DNS lookup latency.") - flag.BoolVar(&config.enableErrorLogging, "enable-error-logging", false, "Indicate whether to enable structured logging for each DNS lookup errors.") flag.Parse() if config.enableLatencyLogging { infoLogger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) } - if config.enableErrorLogging { - errorLogger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) - } + errorLogger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) log.Printf("Starting dnstest with config parameters - %+v", config) client := &dnsClient{config: &config, stopChan: make(chan os.Signal, 1)} signal.Notify(client.stopChan, syscall.SIGTERM) @@ -218,13 +215,6 @@ func (c *dnsClient) updateResults(timedOut bool, err error, name string) { c.resultsLock.Lock() defer c.resultsLock.Unlock() if err != nil { - if errorLogger != nil { - errorLogger.Error("DNS lookup failed", - "hostname", name, - "error", err.Error(), - "timed_out", timedOut, - ) - } c.result.errorCount++ dnsErrorsCounter.Inc() } @@ -263,7 +253,11 @@ func (c *dnsClient) runQuery(name string, timeout time.Duration, lookupFunc Look log.Printf("DNS lookup of name %q, err - %v\n", name, err) } if err != nil { - log.Printf("Failed DNS lookup of name %q, err - %v\n", name, err) + errorLogger.Error("Failed DNS lookup", + "hostname", name, + "error", err.Error(), + "timed_out", timedOut, + ) } c.updateResults(timedOut, err, name) }()