2013-06-11 20:59:27 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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 retrieval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-09 22:17:17 +00:00
|
|
|
"net"
|
2013-06-11 20:59:27 +00:00
|
|
|
"net/url"
|
2013-10-29 15:34:00 +00:00
|
|
|
"strings"
|
2013-06-11 20:59:27 +00:00
|
|
|
"time"
|
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/miekg/dns"
|
2014-06-18 17:43:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-09-10 15:48:05 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-06-11 20:59:27 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/utility"
|
|
|
|
)
|
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
const resolvConf = "/etc/resolv.conf"
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
var (
|
|
|
|
dnsSDLookupsCount = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2014-07-23 17:55:33 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: "dns_sd_lookups_total",
|
|
|
|
Help: "The number of DNS-SD lookup successes/failures per pool.",
|
2014-06-18 17:43:15 +00:00
|
|
|
},
|
|
|
|
[]string{outcome},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(dnsSDLookupsCount)
|
|
|
|
}
|
|
|
|
|
2013-06-11 20:59:27 +00:00
|
|
|
// TargetProvider encapsulates retrieving all targets for a job.
|
|
|
|
type TargetProvider interface {
|
|
|
|
// Retrieves the current list of targets for this provider.
|
|
|
|
Targets() ([]Target, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sdTargetProvider struct {
|
|
|
|
job config.JobConfig
|
|
|
|
|
|
|
|
targets []Target
|
|
|
|
|
|
|
|
lastRefresh time.Time
|
|
|
|
refreshInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NewSdTargetProvider constructs a new sdTargetProvider for a job.
|
2013-06-11 20:59:27 +00:00
|
|
|
func NewSdTargetProvider(job config.JobConfig) *sdTargetProvider {
|
|
|
|
i, err := utility.StringToDuration(job.GetSdRefreshInterval())
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("illegal refresh duration string %s: %s", job.GetSdRefreshInterval(), err))
|
|
|
|
}
|
|
|
|
return &sdTargetProvider{
|
|
|
|
job: job,
|
|
|
|
refreshInterval: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sdTargetProvider) Targets() ([]Target, error) {
|
2013-12-14 01:50:48 +00:00
|
|
|
var err error
|
2014-06-18 17:43:15 +00:00
|
|
|
defer func() {
|
|
|
|
message := success
|
|
|
|
if err != nil {
|
|
|
|
message = failure
|
|
|
|
}
|
|
|
|
dnsSDLookupsCount.WithLabelValues(message).Inc()
|
|
|
|
}()
|
2013-12-14 01:50:48 +00:00
|
|
|
|
2013-06-13 14:10:39 +00:00
|
|
|
if time.Since(p.lastRefresh) < p.refreshInterval {
|
2013-06-11 20:59:27 +00:00
|
|
|
return p.targets, nil
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
response, err := lookupSRV(p.job.GetSdName())
|
2013-12-14 01:50:48 +00:00
|
|
|
|
2013-06-11 20:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
baseLabels := clientmodel.LabelSet{
|
|
|
|
clientmodel.JobLabel: clientmodel.LabelValue(p.job.GetName()),
|
2013-06-11 20:59:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
targets := make([]Target, 0, len(response.Answer))
|
2013-06-11 20:59:27 +00:00
|
|
|
endpoint := &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Path: p.job.GetMetricsPath(),
|
|
|
|
}
|
2013-09-10 15:48:05 +00:00
|
|
|
for _, record := range response.Answer {
|
|
|
|
addr, ok := record.(*dns.SRV)
|
|
|
|
if !ok {
|
|
|
|
glog.Warningf("%s is not a valid SRV record", addr)
|
|
|
|
continue
|
|
|
|
}
|
2013-06-11 20:59:27 +00:00
|
|
|
// Remove the final dot from rooted DNS names to make them look more usual.
|
|
|
|
if addr.Target[len(addr.Target)-1] == '.' {
|
|
|
|
addr.Target = addr.Target[:len(addr.Target)-1]
|
|
|
|
}
|
|
|
|
endpoint.Host = fmt.Sprintf("%s:%d", addr.Target, addr.Port)
|
2013-08-16 16:17:48 +00:00
|
|
|
t := NewTarget(endpoint.String(), p.job.ScrapeTimeout(), baseLabels)
|
2013-06-11 20:59:27 +00:00
|
|
|
targets = append(targets, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.targets = targets
|
|
|
|
return targets, nil
|
|
|
|
}
|
2013-09-10 15:48:05 +00:00
|
|
|
|
|
|
|
func lookupSRV(name string) (*dns.Msg, error) {
|
|
|
|
conf, err := dns.ClientConfigFromFile(resolvConf)
|
|
|
|
if err != nil {
|
2014-12-10 15:16:49 +00:00
|
|
|
return nil, fmt.Errorf("couldn't load resolv.conf: %s", err)
|
2013-09-10 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 15:34:00 +00:00
|
|
|
client := &dns.Client{}
|
2013-09-10 15:48:05 +00:00
|
|
|
response := &dns.Msg{}
|
2013-10-29 15:34:00 +00:00
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
for _, server := range conf.Servers {
|
2014-03-09 22:17:17 +00:00
|
|
|
servAddr := net.JoinHostPort(server, conf.Port)
|
2013-10-29 15:34:00 +00:00
|
|
|
for _, suffix := range conf.Search {
|
2014-03-09 22:17:17 +00:00
|
|
|
response, err = lookup(name, dns.TypeSRV, client, servAddr, suffix, false)
|
2013-10-29 15:34:00 +00:00
|
|
|
if err == nil {
|
|
|
|
if len(response.Answer) > 0 {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-10 15:16:49 +00:00
|
|
|
glog.Warningf("resolving %s.%s failed: %s", name, suffix, err)
|
2013-10-29 15:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-09 22:17:17 +00:00
|
|
|
response, err = lookup(name, dns.TypeSRV, client, servAddr, "", false)
|
2013-09-10 15:48:05 +00:00
|
|
|
if err == nil {
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 15:16:49 +00:00
|
|
|
return response, fmt.Errorf("couldn't resolve %s: No server responded", name)
|
2013-09-10 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:17 +00:00
|
|
|
func lookup(name string, queryType uint16, client *dns.Client, servAddr string, suffix string, edns bool) (*dns.Msg, error) {
|
2013-10-29 15:34:00 +00:00
|
|
|
msg := &dns.Msg{}
|
|
|
|
lname := strings.Join([]string{name, suffix}, ".")
|
|
|
|
msg.SetQuestion(dns.Fqdn(lname), queryType)
|
|
|
|
|
2013-09-10 15:48:05 +00:00
|
|
|
if edns {
|
|
|
|
opt := &dns.OPT{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: ".",
|
|
|
|
Rrtype: dns.TypeOPT,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
opt.SetUDPSize(dns.DefaultMsgSize)
|
|
|
|
msg.Extra = append(msg.Extra, opt)
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:17 +00:00
|
|
|
response, _, err := client.Exchange(msg, servAddr)
|
2013-09-10 15:48:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.MsgHdr.Truncated {
|
|
|
|
if client.Net == "tcp" {
|
2014-12-10 15:16:49 +00:00
|
|
|
return nil, fmt.Errorf("got truncated message on tcp")
|
2013-09-10 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if edns { // Truncated even though EDNS is used
|
|
|
|
client.Net = "tcp"
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:17 +00:00
|
|
|
return lookup(name, queryType, client, servAddr, suffix, !edns)
|
2013-09-10 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|