Skip to content

Commit

Permalink
Revert "Merge pull request prometheus#317 from prometheus/fix/miekg-d…
Browse files Browse the repository at this point in the history
…ns-for-srv"

This reverts commit e3bc6fc, reversing
changes made to 1cf9e58.

Conflicts:
	retrieval/target_provider.go

Change-Id: Icb6e98fb30419e9e2fe9b686c243702ced372014
  • Loading branch information
discordianfish committed Aug 30, 2013
1 parent 7910f6e commit 8809932
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 75 deletions.
6 changes: 1 addition & 5 deletions .build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cc-implementation-Linux-stamp:
[ -x "$$(which cc)" ] || $(APT_GET_INSTALL) build-essential
touch $@

dependencies-stamp: cache-stamp cc-stamp leveldb-stamp snappy-stamp godns-stamp
dependencies-stamp: cache-stamp cc-stamp leveldb-stamp snappy-stamp
touch $@

goprotobuf-protoc-gen-go-stamp: protoc-stamp goprotobuf-stamp
Expand All @@ -56,10 +56,6 @@ goprotobuf-stamp: protoc-stamp
$(GO_GET) code.google.com/p/goprotobuf/proto $(THIRD_PARTY_BUILD_OUTPUT)
touch $@

godns-stamp:
$(GO_GET) github.com/miekg/dns $(THIRD_PARTY_BUILD_OUTPUT)
touch $@

leveldb-stamp: cache-stamp cache/leveldb-$(LEVELDB_VERSION).tar.gz cc-stamp rsync-stamp snappy-stamp
tar xzvf cache/leveldb-$(LEVELDB_VERSION).tar.gz -C dirty $(THIRD_PARTY_BUILD_OUTPUT)
cd dirty/leveldb-$(LEVELDB_VERSION) && CFLAGS="$(CFLAGS) -lsnappy" CXXFLAGS="$(CXXFLAGS) -lsnappy $(LDFLAGS)" LDFLAGS="-lsnappy $(LDFLAGS)" bash -x ./build_detect_platform build_config.mk ./
Expand Down
74 changes: 4 additions & 70 deletions retrieval/target_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ package retrieval

import (
"fmt"
"net"
"net/url"
"time"

"github.com/golang/glog"
"github.com/miekg/dns"

clientmodel "github.com/prometheus/client_golang/model"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/utility"
)

const resolvConf = "/etc/resolv.conf"

// TargetProvider encapsulates retrieving all targets for a job.
type TargetProvider interface {
// Retrieves the current list of targets for this provider.
Expand Down Expand Up @@ -61,7 +57,7 @@ func (p *sdTargetProvider) Targets() ([]Target, error) {
return p.targets, nil
}

response, err := lookupSRV(p.job.GetSdName())
_, addrs, err := net.LookupSRV("", "", p.job.GetSdName())
if err != nil {
return nil, err
}
Expand All @@ -70,17 +66,12 @@ func (p *sdTargetProvider) Targets() ([]Target, error) {
clientmodel.JobLabel: clientmodel.LabelValue(p.job.GetName()),
}

targets := make([]Target, 0, len(response.Answer))
targets := make([]Target, 0, len(addrs))
endpoint := &url.URL{
Scheme: "http",
Path: p.job.GetMetricsPath(),
}
for _, record := range response.Answer {
addr, ok := record.(*dns.SRV)
if !ok {
glog.Warningf("%s is not a valid SRV record", addr)
continue
}
for _, addr := range addrs {
// 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]
Expand All @@ -93,60 +84,3 @@ func (p *sdTargetProvider) Targets() ([]Target, error) {
p.targets = targets
return targets, nil
}

func lookupSRV(name string) (*dns.Msg, error) {
name = dns.Fqdn(name)
conf, err := dns.ClientConfigFromFile(resolvConf)
if err != nil {
return nil, fmt.Errorf("Couldn't load resolv.conf: %s", err)
}
client := &dns.Client{}
msg := &dns.Msg{}
msg.SetQuestion(name, dns.TypeSRV)

response := &dns.Msg{}
for _, server := range conf.Servers {
server := fmt.Sprintf("%s:%s", server, conf.Port)
response, err = lookup(msg, client, server, false)
if err == nil {
return response, nil
}
}
return response, fmt.Errorf("Couldn't resolve %s: No server responded", name)
}

func lookup(msg *dns.Msg, client *dns.Client, server string, edns bool) (*dns.Msg, error) {
if edns {
opt := &dns.OPT{
Hdr: dns.RR_Header{
Name: ".",
Rrtype: dns.TypeOPT,
},
}
opt.SetUDPSize(dns.DefaultMsgSize)
msg.Extra = append(msg.Extra, opt)
}

response, _, err := client.Exchange(msg, server)
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" {
return nil, fmt.Errorf("Got truncated message on tcp")
}

if edns { // Truncated even though EDNS is used
client.Net = "tcp"
}

return lookup(msg, client, server, !edns)
}

return response, nil
}

0 comments on commit 8809932

Please sign in to comment.