feat(inputs.dns_query): Allow ignoring errors of specific types (#14992)

This commit is contained in:
Joshua Powers 2024-03-20 13:46:53 -06:00 committed by GitHub
parent f8905b270a
commit f9b1251058
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View File

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

View File

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