Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Add use_proxy_dns to bypass local DNS resolution
Browse files Browse the repository at this point in the history
Changes picked up from original PR prometheus#554 which was rejected by maintainer.
  • Loading branch information
riuvshyn committed Apr 21, 2020
1 parent 991f898 commit dcc6616
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ The other placeholders are specified separately.
# HTTP proxy server to use to connect to the targets.
[ proxy_url: <string> ]

# Skip local DNS resolution when using an HTTP proxy server.
[ use_proxy_dns: <boolean> | default = false ]

# The IP protocol of the HTTP probe (ip4, ip6).
[ preferred_ip_protocol: <string> | default = "ip6" ]
[ ip_protocol_fallback: <boolean> | default = true ]
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type HTTPProbe struct {
FailIfHeaderMatchesRegexp []HeaderMatch `yaml:"fail_if_header_matches,omitempty"`
FailIfHeaderNotMatchesRegexp []HeaderMatch `yaml:"fail_if_header_not_matches,omitempty"`
Body string `yaml:"body,omitempty"`
UseProxyDNS bool `yaml:"use_proxy_dns,omitempty"`
HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
}

Expand Down
42 changes: 27 additions & 15 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,13 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
})
)

for _, lv := range []string{"resolve", "connect", "tls", "processing", "transfer"} {
// Check the option to send the url unresolved to the proxy
skipDNS := module.HTTP.UseProxyDNS && module.HTTP.HTTPClientConfig.ProxyURL.URL != nil

if !skipDNS {
durationGaugeVec.WithLabelValues("resolve")
}
for _, lv := range []string{"connect", "tls", "processing", "transfer"} {
durationGaugeVec.WithLabelValues(lv)
}

Expand Down Expand Up @@ -315,12 +321,17 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
targetHost = targetURL.Host
}

ip, lookupTime, err := chooseProtocol(ctx, module.HTTP.IPProtocol, module.HTTP.IPProtocolFallback, targetHost, registry, logger)
if err != nil {
level.Error(logger).Log("msg", "Error resolving address", "err", err)
return false
// Resolve the host unless letting the proxy do the job.
var ip *net.IPAddr
if !skipDNS {
var lookupTime float64
ip, lookupTime, err = chooseProtocol(ctx, module.HTTP.IPProtocol, module.HTTP.IPProtocolFallback, targetHost, registry, logger)
if err != nil {
level.Error(logger).Log("msg", "Error resolving address", "err", err)
return false
}
durationGaugeVec.WithLabelValues("resolve").Add(lookupTime)
}
durationGaugeVec.WithLabelValues("resolve").Add(lookupTime)

httpClientConfig := module.HTTP.HTTPClientConfig
if len(httpClientConfig.TLSConfig.ServerName) == 0 {
Expand Down Expand Up @@ -367,18 +378,19 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
httpConfig.Method = "GET"
}

// Replace the host field in the URL with the IP we resolved.
// Replace the host field in the URL with the IP we resolved unless letting a proxy server do the name resolution.
origHost := targetURL.Host
if targetPort == "" {
if strings.Contains(ip.String(), ":") {
targetURL.Host = "[" + ip.String() + "]"
if !skipDNS {
if targetPort == "" {
if strings.Contains(ip.String(), ":") {
targetURL.Host = "[" + ip.String() + "]"
} else {
targetURL.Host = ip.String()
}
} else {
targetURL.Host = ip.String()
targetURL.Host = net.JoinHostPort(ip.String(), targetPort)
}
} else {
targetURL.Host = net.JoinHostPort(ip.String(), targetPort)
}

var body io.Reader
var respBodyBytes int64

Expand Down Expand Up @@ -515,7 +527,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
"end", trace.end,
)
// We get the duration for the first request from chooseProtocol.
if i != 0 {
if i != 0 && !skipDNS {
durationGaugeVec.WithLabelValues("resolve").Add(trace.dnsDone.Sub(trace.start).Seconds())
}
// Continue here if we never got a connection because a request failed.
Expand Down

0 comments on commit dcc6616

Please sign in to comment.