Add configurable method to http input
This commit is contained in:
parent
0d683f65a3
commit
bc96f65881
|
@ -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"}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,8 @@ 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",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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" {
|
||||||
|
|
Loading…
Reference in New Issue