From bc96f658810afa135c47754c59e3a9eeef6c771e Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Thu, 15 Feb 2018 18:36:01 -0800 Subject: [PATCH] Add configurable method to http input --- plugins/inputs/http/README.md | 3 +++ plugins/inputs/http/http.go | 9 +++++++-- plugins/inputs/http/http_test.go | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/http/README.md b/plugins/inputs/http/README.md index 2c22aaa42..2c0441364 100644 --- a/plugins/inputs/http/README.md +++ b/plugins/inputs/http/README.md @@ -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"} diff --git a/plugins/inputs/http/http.go b/plugins/inputs/http/http.go index 5bf895ca5..bdbb5f6fa 100644 --- a/plugins/inputs/http/http.go +++ b/plugins/inputs/http/http.go @@ -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", } }) } diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index 843015990..486edabc9 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -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" {