From 534df7328b8fbac9265d1752ed875e5b7367255d Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Tue, 19 Mar 2024 09:36:06 -0600 Subject: [PATCH] fix(inputs.dns_query): Omit certain errors from logs fixes: #14941 --- plugins/inputs/dns_query/dns_query.go | 7 ++++++- plugins/inputs/dns_query/dns_query_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/dns_query/dns_query.go b/plugins/inputs/dns_query/dns_query.go index a8f872ac99c88..2d287e666333a 100644 --- a/plugins/inputs/dns_query/dns_query.go +++ b/plugins/inputs/dns_query/dns_query.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net" + "slices" "strconv" "sync" "time" @@ -20,6 +21,10 @@ import ( //go:embed sample.conf var sampleConfig string +var ignoredErrors = []string{ + "NXDOMAIN", +} + type ResultType uint64 const ( @@ -87,7 +92,7 @@ func (d *DNSQuery) Gather(acc telegraf.Accumulator) error { defer wg.Done() fields, tags, err := d.query(domain, server) - if err != nil { + if err != nil && !slices.Contains(ignoredErrors, tags["rcode"]) { var opErr *net.OpError if !errors.As(err, &opErr) || !opErr.Timeout() { acc.AddError(err) diff --git a/plugins/inputs/dns_query/dns_query_test.go b/plugins/inputs/dns_query/dns_query_test.go index 320c7d8783859..e21550399ea49 100644 --- a/plugins/inputs/dns_query/dns_query_test.go +++ b/plugins/inputs/dns_query/dns_query_test.go @@ -37,6 +37,23 @@ func TestGathering(t *testing.T) { require.NotEqual(t, float64(0), queryTime) } +func TestGatherInvalid(t *testing.T) { + if testing.Short() { + t.Skip("Skipping network-dependent test in short mode.") + } + + dnsConfig := DNSQuery{ + Servers: servers, + Domains: []string{"qwerty123.example.com"}, + Timeout: config.Duration(1 * time.Second), + } + + var acc testutil.Accumulator + require.NoError(t, dnsConfig.Init()) + require.NoError(t, dnsConfig.Gather(&acc)) + require.Empty(t, acc.Errors) +} + func TestGatheringMxRecord(t *testing.T) { if testing.Short() { t.Skip("Skipping network-dependent test in short mode.")