feat(inputs.http_listener): Allow setting custom success return code (#15454)

This commit is contained in:
Joshua Powers 2024-06-05 04:58:42 -06:00 committed by GitHub
parent ceba179596
commit 297c64d771
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 1 deletions

View File

@ -53,6 +53,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## requests and included in responses.
# http_headers = {"HTTP_HEADER" = "TAG_NAME"}
## HTTP Return Success Code
## This is the HTTP code that will be returned on success
# http_success_code = 204
## maximum duration before timing out read of the request
# read_timeout = "10s"
## maximum duration before timing out write of the response

View File

@ -54,6 +54,7 @@ type HTTPListenerV2 struct {
WriteTimeout config.Duration `toml:"write_timeout"`
MaxBodySize config.Size `toml:"max_body_size"`
Port int `toml:"port"`
SuccessCode int `toml:"http_success_code"`
BasicUsername string `toml:"basic_username"`
BasicPassword string `toml:"basic_password"`
HTTPHeaderTags map[string]string `toml:"http_header_tags"`
@ -160,6 +161,10 @@ func (h *HTTPListenerV2) Init() error {
h.listener = listener
h.Port = listener.Addr().(*net.TCPAddr).Port
if h.SuccessCode == 0 {
h.SuccessCode = http.StatusNoContent
}
return nil
}
@ -246,7 +251,7 @@ func (h *HTTPListenerV2) serveWrite(res http.ResponseWriter, req *http.Request)
h.acc.AddMetric(m)
}
res.WriteHeader(http.StatusNoContent)
res.WriteHeader(h.SuccessCode)
}
func (h *HTTPListenerV2) collectBody(res http.ResponseWriter, req *http.Request) ([]byte, bool) {

View File

@ -277,6 +277,23 @@ func TestWriteHTTPWithPathTag(t *testing.T) {
)
}
func TestWriteHTTPWithReturnCode(t *testing.T) {
listener, err := newTestHTTPListenerV2()
require.NoError(t, err)
listener.SuccessCode = 200
acc := &testutil.Accumulator{}
require.NoError(t, listener.Init())
require.NoError(t, listener.Start(acc))
defer listener.Stop()
// post single message to listener
resp, err := http.Post(createURL(listener, "http", "/write", "db=mydb"), "", bytes.NewBuffer([]byte(testMsgNoNewline)))
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.EqualValues(t, 200, resp.StatusCode)
}
// http listener should add request path as configured path_tag (trimming it before)
func TestWriteHTTPWithMultiplePaths(t *testing.T) {
listener, err := newTestHTTPListenerV2()

View File

@ -17,6 +17,10 @@
## requests and included in responses.
# http_headers = {"HTTP_HEADER" = "TAG_NAME"}
## HTTP Return Success Code
## This is the HTTP code that will be returned on success
# http_success_code = 204
## maximum duration before timing out read of the request
# read_timeout = "10s"
## maximum duration before timing out write of the response