Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http endpoint support #795

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ The other placeholders are specified separately.
# The HTTP method the probe will use.
[ method: <string> | default = "GET" ]

# Endpint IP (IP or IP:Portand port) to send HTTP requests, skip DNS lookup.
[ endpoint: <string> ]

# The HTTP headers set for the probe.
headers:
[ <string>: <string> ... ]
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type HTTPProbe struct {
NoFollowRedirects *bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Endpoint string `yaml:"endpoint,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfBodyMatchesRegexp []Regexp `yaml:"fail_if_body_matches_regexp,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ modules:
insecure_skip_verify: false
preferred_ip_protocol: "ip4" # defaults to "ip6"
ip_protocol_fallback: false # no fallback to "ip6"
http_2xx_example_with_endpoint:
prober: http
timeout: 5s
http:
endpoint: "1.1.1.1:443"
valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
valid_status_codes: [] # Defaults to 2xx
method: GET
preferred_ip_protocol: "ip4" # defaults to "ip6"
ip_protocol_fallback: false # no fallback to "ip6"
http_post_2xx:
prober: http
timeout: 5s
Expand Down
34 changes: 22 additions & 12 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func (bc *byteCounter) Read(p []byte) (int, error) {

func ProbeHTTP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger log.Logger) (success bool) {
var redirects int
var ip *net.IPAddr
var (
durationGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_http_duration_seconds",
Expand Down Expand Up @@ -332,12 +333,16 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
targetHost := targetURL.Hostname()
targetPort := targetURL.Port()

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
if len(module.HTTP.Endpoint) == 0 {
// do DNS lookup when no custom endpoint is set
lookupIp, 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)
ip = lookupIp
}
durationGaugeVec.WithLabelValues("resolve").Add(lookupTime)

httpClientConfig := module.HTTP.HTTPClientConfig
if len(httpClientConfig.TLSConfig.ServerName) == 0 {
Expand Down Expand Up @@ -384,16 +389,21 @@ 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.
origHost := targetURL.Host
if targetPort == "" {
if strings.Contains(ip.String(), ":") {
targetURL.Host = "[" + ip.String() + "]"
if len(module.HTTP.Endpoint) != 0 {
// Use endpoint from configuration (without DNS lookup)
targetURL.Host = module.HTTP.Endpoint
} else {
// Replace the host field in the URL with the IP we resolved.
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
Expand Down