From b243821ec44b9aa12389bad2ae01dee35460384a Mon Sep 17 00:00:00 2001 From: Mark Nevill Date: Tue, 18 Jun 2019 15:12:46 +0100 Subject: [PATCH] Add metric for number of endpoints resolved for each configured address. `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. --- pkg/discovery/dns/provider.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/discovery/dns/provider.go b/pkg/discovery/dns/provider.go index c1380ec2ae..68fb0636eb 100644 --- a/pkg/discovery/dns/provider.go +++ b/pkg/discovery/dns/provider.go @@ -20,6 +20,7 @@ type Provider struct { resolved map[string][]string logger log.Logger + resolverAddrs *prometheus.GaugeVec resolverLookupsCount prometheus.Counter resolverFailuresCount prometheus.Counter } @@ -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", @@ -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) } @@ -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 @@ -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.