Skip to content

Commit

Permalink
Add metric for number of endpoints resolved for each configured addre…
Browse files Browse the repository at this point in the history
…ss. (#1260)

`count(dns_provider_results)` tells us how many addresses have been
configured through flags and file-SD, while `sum(dns_provider_results)`
gives us the number of endpoints actually produced from these addresses.
  • Loading branch information
devnev authored and bwplotka committed Jun 18, 2019
1 parent d97e310 commit c7da9f2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/discovery/dns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Provider struct {
resolved map[string][]string
logger log.Logger

resolverAddrs *prometheus.GaugeVec
resolverLookupsCount prometheus.Counter
resolverFailuresCount prometheus.Counter
}
Expand Down Expand Up @@ -52,6 +53,10 @@ func NewProvider(logger log.Logger, reg prometheus.Registerer, resolverType Reso
resolver: NewResolver(resolverType.ToResolver(logger)),
resolved: make(map[string][]string),
logger: logger,
resolverAddrs: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "dns_provider_results",
Help: "The number of resolved endpoints for each configured address",
}, []string{"addr"}),
resolverLookupsCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "dns_lookups_total",
Help: "The number of DNS lookups resolutions attempts",
Expand All @@ -63,6 +68,7 @@ func NewProvider(logger log.Logger, reg prometheus.Registerer, resolverType Reso
}

if reg != nil {
reg.MustRegister(p.resolverAddrs)
reg.MustRegister(p.resolverLookupsCount)
reg.MustRegister(p.resolverFailuresCount)
}
Expand Down Expand Up @@ -91,6 +97,7 @@ func (p *Provider) Resolve(ctx context.Context, addrs []string) {
p.resolverLookupsCount.Inc()
if err != nil {
// The DNS resolution failed. Continue without modifying the old records.
p.resolved[addr] = p.resolved[addr] // Ensure metrics capture the result even if empty.
p.resolverFailuresCount.Inc()
level.Error(p.logger).Log("msg", "dns resolution failed", "addr", addr, "err", err)
continue
Expand All @@ -99,15 +106,14 @@ func (p *Provider) Resolve(ctx context.Context, addrs []string) {
}

// Remove stored addresses that are no longer requested.
var entriesToDelete []string
for existingAddr := range p.resolved {
if !contains(addrs, existingAddr) {
entriesToDelete = append(entriesToDelete, existingAddr)
delete(p.resolved, existingAddr)
p.resolverAddrs.DeleteLabelValues(existingAddr)
} else {
p.resolverAddrs.WithLabelValues(existingAddr).Set(float64(len(p.resolved[existingAddr])))
}
}
for _, toDelete := range entriesToDelete {
delete(p.resolved, toDelete)
}
}

// Addresses returns the latest addresses present in the Provider.
Expand Down

0 comments on commit c7da9f2

Please sign in to comment.