Add configurable method to http input

This commit is contained in:
Daniel Nelson 2018-02-15 18:36:01 -08:00
parent a55456b56c
commit 3fe3d75bb3
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
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://localhost/metrics"
] ]
## HTTP method
# method = "GET"
## Optional HTTP headers ## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"} # headers = {"X-Special-Header" = "Special-Value"}

View File

@ -17,6 +17,7 @@ import (
type HTTP struct { type HTTP struct {
URLs []string `toml:"urls"` URLs []string `toml:"urls"`
Method string
Headers map[string]string Headers map[string]string
@ -48,6 +49,9 @@ var sampleConfig = `
"http://localhost/metrics" "http://localhost/metrics"
] ]
## HTTP method
# method = "GET"
## Optional HTTP headers ## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"} # headers = {"X-Special-Header" = "Special-Value"}
@ -138,7 +142,7 @@ func (h *HTTP) gatherURL(
acc telegraf.Accumulator, acc telegraf.Accumulator,
url string, url string,
) error { ) error {
request, err := http.NewRequest("GET", url, nil) request, err := http.NewRequest(h.Method, url, nil)
if err != nil { if err != nil {
return err return err
} }
@ -193,6 +197,7 @@ func init() {
inputs.Add("http", func() telegraf.Input { inputs.Add("http", func() telegraf.Input {
return &HTTP{ return &HTTP{
Timeout: internal.Duration{Duration: time.Second * 5}, 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)) 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) { func TestParserNotSet(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/endpoint" { if r.URL.Path == "/endpoint" {