Skip to content

Commit

Permalink
Query servers in parallel in dns_query input (influxdata#4754)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyo authored and otherpirate committed Mar 15, 2019
1 parent 8d1be4e commit d525a17
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions plugins/inputs/dns_query/dns_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"strconv"
"sync"
"time"

"github.com/miekg/dns"
Expand Down Expand Up @@ -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,
}

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

acc.AddFields("dns_query", fields, tags)
}(domain, server)
}
}

wg.Wait()
return nil
}

Expand Down

0 comments on commit d525a17

Please sign in to comment.