Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/inputs/http_listener_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion plugins/inputs/http_listener_v2/http_listener_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions plugins/inputs/http_listener_v2/http_listener_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/http_listener_v2/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading