Skip to content

Commit

Permalink
feat(inputs.http_listener): Allow setting custom success return code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Jun 5, 2024
1 parent ceba179 commit 297c64d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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

0 comments on commit 297c64d

Please sign in to comment.