Add configurable method to http input

This commit is contained in:
Daniel Nelson 2018-02-15 18:36:01 -08:00
parent 0d683f65a3
commit bc96f65881
3 changed files with 33 additions and 2 deletions

View File

@ -13,6 +13,9 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
"http://localhost/metrics"
]
## HTTP method
# method = "GET"
## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}

View File

@ -16,7 +16,8 @@ import (
)
type HTTP struct {
URLs []string `toml:"urls"`
URLs []string `toml:"urls"`
Method string
Headers map[string]string
@ -48,6 +49,9 @@ var sampleConfig = `
"http://localhost/metrics"
]
## HTTP method
# method = "GET"
## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}
@ -138,7 +142,7 @@ func (h *HTTP) gatherURL(
acc telegraf.Accumulator,
url string,
) error {
request, err := http.NewRequest("GET", url, nil)
request, err := http.NewRequest(h.Method, url, nil)
if err != nil {
return err
}
@ -193,6 +197,7 @@ func init() {
inputs.Add("http", func() telegraf.Input {
return &HTTP{
Timeout: internal.Duration{Duration: time.Second * 5},
Method: "GET",
}
})
}

View File

@ -90,6 +90,29 @@ func TestInvalidStatusCode(t *testing.T) {
require.Error(t, acc.GatherError(plugin.Gather))
}
func TestMethod(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer fakeServer.Close()
plugin := &plugin.HTTP{
URLs: []string{fakeServer.URL},
Method: "POST",
}
metricName := "metricName"
p, _ := parsers.NewJSONParser(metricName, nil, nil)
plugin.SetParser(p)
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))
}
func TestParserNotSet(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/endpoint" {