mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
5.8 KiB
237 lines
5.8 KiB
9 years ago
|
// Copyright 2016 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
|
package dns
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"strings"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"github.com/miekg/dns"
|
||
|
"github.com/prometheus/client_golang/prometheus"
|
||
|
"github.com/prometheus/common/log"
|
||
|
"github.com/prometheus/common/model"
|
||
|
"golang.org/x/net/context"
|
||
|
|
||
|
"github.com/prometheus/prometheus/config"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
resolvConf = "/etc/resolv.conf"
|
||
|
|
||
|
dnsNameLabel = model.MetaLabelPrefix + "dns_name"
|
||
|
|
||
|
// Constants for instrumentation.
|
||
|
namespace = "prometheus"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
dnsSDLookupsCount = prometheus.NewCounter(
|
||
|
prometheus.CounterOpts{
|
||
|
Namespace: namespace,
|
||
8 years ago
|
Name: "sd_dns_lookups_total",
|
||
9 years ago
|
Help: "The number of DNS-SD lookups.",
|
||
|
})
|
||
|
dnsSDLookupFailuresCount = prometheus.NewCounter(
|
||
|
prometheus.CounterOpts{
|
||
|
Namespace: namespace,
|
||
8 years ago
|
Name: "sd_dns_lookup_failures_total",
|
||
9 years ago
|
Help: "The number of DNS-SD lookup failures.",
|
||
|
})
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
prometheus.MustRegister(dnsSDLookupFailuresCount)
|
||
|
prometheus.MustRegister(dnsSDLookupsCount)
|
||
|
}
|
||
|
|
||
|
// Discovery periodically performs DNS-SD requests. It implements
|
||
|
// the TargetProvider interface.
|
||
|
type Discovery struct {
|
||
|
names []string
|
||
|
|
||
|
interval time.Duration
|
||
|
m sync.RWMutex
|
||
|
port int
|
||
|
qtype uint16
|
||
|
}
|
||
|
|
||
|
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
|
||
|
func NewDiscovery(conf *config.DNSSDConfig) *Discovery {
|
||
|
qtype := dns.TypeSRV
|
||
|
switch strings.ToUpper(conf.Type) {
|
||
|
case "A":
|
||
|
qtype = dns.TypeA
|
||
|
case "AAAA":
|
||
|
qtype = dns.TypeAAAA
|
||
|
case "SRV":
|
||
|
qtype = dns.TypeSRV
|
||
|
}
|
||
|
return &Discovery{
|
||
|
names: conf.Names,
|
||
|
interval: time.Duration(conf.RefreshInterval),
|
||
|
qtype: qtype,
|
||
|
port: conf.Port,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Run implements the TargetProvider interface.
|
||
|
func (dd *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
||
|
ticker := time.NewTicker(dd.interval)
|
||
|
defer ticker.Stop()
|
||
|
|
||
|
// Get an initial set right away.
|
||
|
dd.refreshAll(ctx, ch)
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case <-ticker.C:
|
||
|
dd.refreshAll(ctx, ch)
|
||
|
case <-ctx.Done():
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (dd *Discovery) refreshAll(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
||
|
var wg sync.WaitGroup
|
||
|
|
||
|
wg.Add(len(dd.names))
|
||
|
for _, name := range dd.names {
|
||
|
go func(n string) {
|
||
9 years ago
|
if err := dd.refresh(ctx, n, ch); err != nil {
|
||
9 years ago
|
log.Errorf("Error refreshing DNS targets: %s", err)
|
||
|
}
|
||
|
wg.Done()
|
||
|
}(name)
|
||
|
}
|
||
|
|
||
|
wg.Wait()
|
||
|
}
|
||
|
|
||
9 years ago
|
func (dd *Discovery) refresh(ctx context.Context, name string, ch chan<- []*config.TargetGroup) error {
|
||
9 years ago
|
response, err := lookupAll(name, dd.qtype)
|
||
|
dnsSDLookupsCount.Inc()
|
||
|
if err != nil {
|
||
|
dnsSDLookupFailuresCount.Inc()
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
tg := &config.TargetGroup{}
|
||
8 years ago
|
hostPort := func(a string, p int) model.LabelValue {
|
||
|
return model.LabelValue(net.JoinHostPort(a, fmt.Sprintf("%d", p)))
|
||
|
}
|
||
9 years ago
|
|
||
|
for _, record := range response.Answer {
|
||
|
target := model.LabelValue("")
|
||
|
switch addr := record.(type) {
|
||
|
case *dns.SRV:
|
||
|
// Remove the final dot from rooted DNS names to make them look more usual.
|
||
|
addr.Target = strings.TrimRight(addr.Target, ".")
|
||
|
|
||
8 years ago
|
target = hostPort(addr.Target, int(addr.Port))
|
||
9 years ago
|
case *dns.A:
|
||
8 years ago
|
target = hostPort(addr.A.String(), dd.port)
|
||
9 years ago
|
case *dns.AAAA:
|
||
8 years ago
|
target = hostPort(addr.AAAA.String(), dd.port)
|
||
9 years ago
|
default:
|
||
|
log.Warnf("%q is not a valid SRV record", record)
|
||
|
continue
|
||
|
|
||
|
}
|
||
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
||
|
model.AddressLabel: target,
|
||
|
dnsNameLabel: model.LabelValue(name),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
tg.Source = name
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
return ctx.Err()
|
||
|
case ch <- []*config.TargetGroup{tg}:
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func lookupAll(name string, qtype uint16) (*dns.Msg, error) {
|
||
|
conf, err := dns.ClientConfigFromFile(resolvConf)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not load resolv.conf: %s", err)
|
||
|
}
|
||
|
|
||
|
client := &dns.Client{}
|
||
|
response := &dns.Msg{}
|
||
|
|
||
|
for _, server := range conf.Servers {
|
||
|
servAddr := net.JoinHostPort(server, conf.Port)
|
||
|
for _, suffix := range conf.Search {
|
||
|
response, err = lookup(name, qtype, client, servAddr, suffix, false)
|
||
|
if err != nil {
|
||
8 years ago
|
log.
|
||
|
With("server", server).
|
||
|
With("name", name).
|
||
|
With("suffix", suffix).
|
||
|
With("reason", err).
|
||
|
Warn("DNS resolution failed.")
|
||
9 years ago
|
continue
|
||
|
}
|
||
|
if len(response.Answer) > 0 {
|
||
|
return response, nil
|
||
|
}
|
||
|
}
|
||
|
response, err = lookup(name, qtype, client, servAddr, "", false)
|
||
|
if err == nil {
|
||
|
return response, nil
|
||
|
}
|
||
8 years ago
|
log.
|
||
|
With("server", server).
|
||
|
With("name", name).
|
||
|
With("reason", err).
|
||
|
Warn("DNS resolution failed.")
|
||
9 years ago
|
}
|
||
8 years ago
|
return response, fmt.Errorf("could not resolve %s: no server responded", name)
|
||
9 years ago
|
}
|
||
|
|
||
|
func lookup(name string, queryType uint16, client *dns.Client, servAddr string, suffix string, edns bool) (*dns.Msg, error) {
|
||
|
msg := &dns.Msg{}
|
||
|
lname := strings.Join([]string{name, suffix}, ".")
|
||
|
msg.SetQuestion(dns.Fqdn(lname), queryType)
|
||
|
|
||
|
if edns {
|
||
8 years ago
|
msg.SetEdns0(dns.DefaultMsgSize, false)
|
||
9 years ago
|
}
|
||
|
|
||
|
response, _, err := client.Exchange(msg, servAddr)
|
||
8 years ago
|
if err == dns.ErrTruncated {
|
||
9 years ago
|
if client.Net == "tcp" {
|
||
8 years ago
|
return nil, fmt.Errorf("got truncated message on TCP (64kiB limit exceeded?)")
|
||
9 years ago
|
}
|
||
|
if edns { // Truncated even though EDNS is used
|
||
|
client.Net = "tcp"
|
||
|
}
|
||
|
return lookup(name, queryType, client, servAddr, suffix, !edns)
|
||
|
}
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if msg.Id != response.Id {
|
||
|
return nil, fmt.Errorf("DNS ID mismatch, request: %d, response: %d", msg.Id, response.Id)
|
||
|
}
|
||
9 years ago
|
return response, nil
|
||
|
}
|