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 support for body_path in HTTP probe config. #392

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
39 changes: 26 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"sync"
"time"
Expand Down Expand Up @@ -52,19 +53,21 @@ type Module struct {

type HTTPProbe struct {
// Defaults to 2xx.
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp,omitempty"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp,omitempty"`
Body string `yaml:"body,omitempty"`
HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp,omitempty"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp,omitempty"`
Body string `yaml:"body,omitempty"`
BodyFile string `yaml:"body_file,omitempty"`

HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
}

type QueryResponse struct {
Expand Down Expand Up @@ -135,6 +138,16 @@ func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := s.HTTPClientConfig.Validate(); err != nil {
return err
}
if len(s.Body) > 0 && len(s.BodyFile) > 0 {
return errors.New("at most one of body & body_file may be configured")
}
if s.BodyFile != "" {
file, err := os.Open(s.BodyFile)
if err != nil {
return err
}
defer file.Close()
}
return nil
}

Expand Down
13 changes: 12 additions & 1 deletion prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http/cookiejar"
"net/http/httptrace"
"net/url"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -257,10 +258,20 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr

var body io.Reader

// If a body is configured, add it to the request.
// If a body string is configured, add it to the request.
if httpConfig.Body != "" {
body = strings.NewReader(httpConfig.Body)
}
// If a body path is configured, add it to the request.
if httpConfig.BodyFile != "" {
file, err := os.Open(httpConfig.BodyFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure this gets closed

if err != nil {
level.Error(logger).Log("msg", "Error opening body file", "err", err)
return
}
defer file.Close()
body = file
}

request, err := http.NewRequest(httpConfig.Method, targetURL.String(), body)
request.Host = origHost
Expand Down
27 changes: 27 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,33 @@ func TestPost(t *testing.T) {
}
}

func TestPostBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
}
}))
defer ts.Close()

recorder := httptest.NewRecorder()
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result := ProbeHTTP(testCTX, ts.URL,
config.Module{
Timeout: time.Second,
HTTP: config.HTTPProbe{
IPProtocolFallback: true,
Method: "POST",
BodyFile: "./http.go",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a testdata directory, and a file with known contents there - plus verify that's what's coming through.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should still change this to use explicit testdata files, rather than the source itself

},
}, registry, log.NewNopLogger())
body := recorder.Body.String()
if !result {
t.Fatalf("Post test failed unexpectedly, got %s", body)
}
}

func TestBasicAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
Expand Down