func probeHTTP(target string, module Module, registry *prometheus.Registry) (success bool) {
var redirects int
- var dialProtocol string
var (
contentLengthGauge = prometheus.NewGauge(prometheus.GaugeOpts{
return false
}
- if ip.IP.To4() == nil {
- dialProtocol = "tcp6"
- } else {
- dialProtocol = "tcp4"
- }
-
- client := &http.Client{
- Timeout: module.Timeout,
- }
+ httpClientConfig := &module.HTTP.HTTPClientConfig
- tlsconfig, err := config.NewTLSConfig(&module.HTTP.TLSConfig)
+ client, err := config.NewHTTPClientFromConfig(httpClientConfig)
if err != nil {
- log.Errorf("Error generating TLS config: %s", err)
+ log.Errorf("Error generating HTTP client: %v", err)
return false
}
- dial := func(network, address string) (net.Conn, error) {
- return net.Dial(dialProtocol, address)
- }
- client.Transport = &http.Transport{
- TLSClientConfig: tlsconfig,
- Dial: dial,
- Proxy: http.ProxyFromEnvironment,
- DisableKeepAlives: true,
- }
+ client.Timeout = module.Timeout
client.CheckRedirect = func(_ *http.Request, via []*http.Request) error {
redirects = len(via)
}
}
+func TestBasicAuth(t *testing.T) {
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ }))
+ defer ts.Close()
+
+ recorder := httptest.NewRecorder()
+ registry := prometheus.NewRegistry()
+ result := probeHTTP(ts.URL,
+ Module{Timeout: time.Second, HTTP: HTTPProbe{
+ HTTPClientConfig: config.HTTPClientConfig{
+ TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+ BasicAuth: &config.BasicAuth{Username: "username", Password: "password"},
+ },
+ }}, registry)
+ body := recorder.Body.String()
+ if !result {
+ t.Fatalf("HTTP probe failed, got %s", body)
+ }
+}
+
+func TestBearerToken(t *testing.T) {
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ }))
+ defer ts.Close()
+
+ recorder := httptest.NewRecorder()
+ registry := prometheus.NewRegistry()
+ result := probeHTTP(ts.URL,
+ Module{Timeout: time.Second, HTTP: HTTPProbe{
+ HTTPClientConfig: config.HTTPClientConfig{
+ BearerToken: config.Secret("mysecret"),
+ },
+ }}, registry)
+ body := recorder.Body.String()
+ if !result {
+ t.Fatalf("HTTP probe failed, got %s", body)
+ }
+}
+
func TestFailIfNotSSL(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
registry := prometheus.NewRegistry()
result := probeHTTP(ts.URL,
Module{Timeout: time.Second, HTTP: HTTPProbe{
- TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+ HTTPClientConfig: config.HTTPClientConfig{
+ TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+ },
}}, registry)
body := recorder.Body.String()
if result {
registry := prometheus.NewRegistry()
result := probeHTTP(ts.URL,
Module{Timeout: time.Second, HTTP: HTTPProbe{
- TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
+ HTTPClientConfig: config.HTTPClientConfig{
+ TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
+ },
}}, registry)
body := recorder.Body.String()
if !result {
registry := prometheus.NewRegistry()
result := probeHTTP(ts.URL,
Module{Timeout: time.Second, HTTP: HTTPProbe{
- TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+ HTTPClientConfig: config.HTTPClientConfig{
+ TLSConfig: config.TLSConfig{InsecureSkipVerify: false},
+ },
}}, registry)
body := recorder.Body.String()
if !result {
type HTTPProbe struct {
// Defaults to 2xx.
- ValidStatusCodes []int `yaml:"valid_status_codes"`
- PreferredIPProtocol string `yaml:"preferred_ip_protocol"`
- NoFollowRedirects bool `yaml:"no_follow_redirects"`
- FailIfSSL bool `yaml:"fail_if_ssl"`
- FailIfNotSSL bool `yaml:"fail_if_not_ssl"`
- Method string `yaml:"method"`
- Headers map[string]string `yaml:"headers"`
- FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp"`
- FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"`
- TLSConfig config.TLSConfig `yaml:"tls_config"`
- Body string `yaml:"body"`
+ ValidStatusCodes []int `yaml:"valid_status_codes"`
+ PreferredIPProtocol string `yaml:"preferred_ip_protocol"`
+ NoFollowRedirects bool `yaml:"no_follow_redirects"`
+ FailIfSSL bool `yaml:"fail_if_ssl"`
+ FailIfNotSSL bool `yaml:"fail_if_not_ssl"`
+ Method string `yaml:"method"`
+ Headers map[string]string `yaml:"headers"`
+ FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp"`
+ FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"`
+ Body string `yaml:"body"`
+ HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
}
type QueryResponse struct {