diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index 0f426f809..05551a966 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -300,6 +300,9 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) { } func (h *HTTPListener) parse(b []byte, t time.Time) error { + if !bytes.HasSuffix(b, []byte("\n")) { + b = append(b, '\n') + } metrics, err := h.parser.ParseWithDefaultTime(b, t) for _, m := range metrics { diff --git a/plugins/inputs/http_listener/http_listener_test.go b/plugins/inputs/http_listener/http_listener_test.go index 84cf209ff..b5f858fde 100644 --- a/plugins/inputs/http_listener/http_listener_test.go +++ b/plugins/inputs/http_listener/http_listener_test.go @@ -16,6 +16,8 @@ import ( const ( testMsg = "cpu_load_short,host=server01 value=12.0 1422568543702900257\n" + testMsgNoNewline = "cpu_load_short,host=server01 value=12.0 1422568543702900257" + testMsgs = `cpu_load_short,host=server02 value=12.0 1422568543702900257 cpu_load_short,host=server03 value=12.0 1422568543702900257 cpu_load_short,host=server04 value=12.0 1422568543702900257 @@ -81,6 +83,28 @@ func TestWriteHTTP(t *testing.T) { ) } +// http listener should add a newline at the end of the buffer if it's not there +func TestWriteHTTPNoNewline(t *testing.T) { + listener := newTestHTTPListener() + + acc := &testutil.Accumulator{} + require.NoError(t, listener.Start(acc)) + defer listener.Stop() + + time.Sleep(time.Millisecond * 25) + + // post single message to listener + resp, err := http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsgNoNewline))) + require.NoError(t, err) + require.EqualValues(t, 204, resp.StatusCode) + + time.Sleep(time.Millisecond * 15) + acc.AssertContainsTaggedFields(t, "cpu_load_short", + map[string]interface{}{"value": float64(12)}, + map[string]string{"host": "server01"}, + ) +} + func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) { listener := &HTTPListener{ ServiceAddress: ":8296",