From 7d97ae64218c63d3960121173c57bda3ec929253 Mon Sep 17 00:00:00 2001 From: Lee Jaeyong Date: Fri, 28 Sep 2018 09:26:36 +0900 Subject: [PATCH] Query servers in parallel in dns_query input (#4754) --- plugins/inputs/dns_query/dns_query.go | 40 +++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/plugins/inputs/dns_query/dns_query.go b/plugins/inputs/dns_query/dns_query.go index 98fcc09c2..ec524d95b 100644 --- a/plugins/inputs/dns_query/dns_query.go +++ b/plugins/inputs/dns_query/dns_query.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "strconv" + "sync" "time" "github.com/miekg/dns" @@ -70,32 +71,37 @@ func (d *DnsQuery) Description() string { return "Query given DNS server and gives statistics" } func (d *DnsQuery) Gather(acc telegraf.Accumulator) error { + var wg sync.WaitGroup d.setDefaultValues() for _, domain := range d.Domains { for _, server := range d.Servers { - fields := make(map[string]interface{}, 2) - tags := map[string]string{ - "server": server, - "domain": domain, - "record_type": d.RecordType, - } + wg.Add(1) + go func(domain, server string) { + fields := make(map[string]interface{}, 2) + tags := map[string]string{ + "server": server, + "domain": domain, + "record_type": d.RecordType, + } - dnsQueryTime, err := d.getDnsQueryTime(domain, server) - if err == nil { - setResult(Success, fields, tags) - fields["query_time_ms"] = dnsQueryTime - } else if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() { - setResult(Timeout, fields, tags) - } else if err != nil { - setResult(Error, fields, tags) - acc.AddError(err) - } + dnsQueryTime, err := d.getDnsQueryTime(domain, server) + if err == nil { + setResult(Success, fields, tags) + fields["query_time_ms"] = dnsQueryTime + } else if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() { + setResult(Timeout, fields, tags) + } else if err != nil { + setResult(Error, fields, tags) + acc.AddError(err) + } - acc.AddFields("dns_query", fields, tags) + acc.AddFields("dns_query", fields, tags) + }(domain, server) } } + wg.Wait() return nil }