Compare commits

...

2 commits

Author SHA1 Message Date
Gabriel Simmer 9382391c30
Formatting 2024-04-24 08:48:45 +01:00
Gabriel Simmer 5107e16233
Better collection of DNS queries
SingleInflight is now a noop in the dns package, so we have to
implement it ourselves. I've found that dnsmasq just times out when
sending all the questions together.
2024-04-24 08:47:23 +01:00
2 changed files with 36 additions and 40 deletions

View file

@ -159,9 +159,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
"servers.bind.",
}
responses := make([]dns.RR, len(questions))
for i, q := range questions {
for _, q := range questions {
msg := &dns.Msg{
MsgHdr: dns.MsgHdr{
Id: dns.Id(),
@ -177,45 +175,43 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
return err
}
responses[i] = in.Answer[0]
}
for _, a := range responses {
txt, ok := a.(*dns.TXT)
if !ok {
continue
}
switch txt.Hdr.Name {
case "servers.bind.":
for _, str := range txt.Txt {
arr := strings.Fields(str)
if got, want := len(arr), 3; got != want {
return fmt.Errorf("stats DNS record servers.bind.: unexpeced number of argument in record: got %d, want %d", got, want)
}
queries, err := strconv.ParseFloat(arr[1], 64)
if err != nil {
return err
}
failedQueries, err := strconv.ParseFloat(arr[2], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(serversMetrics["queries"], prometheus.GaugeValue, queries, arr[0])
ch <- prometheus.MustNewConstMetric(serversMetrics["queries_failed"], prometheus.GaugeValue, failedQueries, arr[0])
}
default:
g, ok := floatMetrics[txt.Hdr.Name]
for _, a := range in.Answer {
txt, ok := a.(*dns.TXT)
if !ok {
continue // ignore unexpected answer from dnsmasq
continue
}
if got, want := len(txt.Txt), 1; got != want {
return fmt.Errorf("stats DNS record %q: unexpected number of replies: got %d, want %d", txt.Hdr.Name, got, want)
switch txt.Hdr.Name {
case "servers.bind.":
for _, str := range txt.Txt {
arr := strings.Fields(str)
if got, want := len(arr), 3; got != want {
return fmt.Errorf("stats DNS record servers.bind.: unexpeced number of argument in record: got %d, want %d", got, want)
}
queries, err := strconv.ParseFloat(arr[1], 64)
if err != nil {
return err
}
failedQueries, err := strconv.ParseFloat(arr[2], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(serversMetrics["queries"], prometheus.GaugeValue, queries, arr[0])
ch <- prometheus.MustNewConstMetric(serversMetrics["queries_failed"], prometheus.GaugeValue, failedQueries, arr[0])
}
default:
g, ok := floatMetrics[txt.Hdr.Name]
if !ok {
continue // ignore unexpected answer from dnsmasq
}
if got, want := len(txt.Txt), 1; got != want {
return fmt.Errorf("stats DNS record %q: unexpected number of replies: got %d, want %d", txt.Hdr.Name, got, want)
}
f, err := strconv.ParseFloat(txt.Txt[0], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(g, prometheus.GaugeValue, f)
}
f, err := strconv.ParseFloat(txt.Txt[0], 64)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(g, prometheus.GaugeValue, f)
}
}
return nil

View file

@ -23,8 +23,8 @@ import (
"github.com/google/dnsmasq_exporter/collector"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (