diff --git a/.chloggen/json_error_codes.yaml b/.chloggen/json_error_codes.yaml new file mode 100755 index 000000000000..d6a7d4f1a968 --- /dev/null +++ b/.chloggen/json_error_codes.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: splunkhecreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Do not encode JSON response objects as string. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27604] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/splunkhecreceiver/receiver.go b/receiver/splunkhecreceiver/receiver.go index f71364e4bbca..4e7ce883a91d 100644 --- a/receiver/splunkhecreceiver/receiver.go +++ b/receiver/splunkhecreceiver/receiver.go @@ -34,16 +34,16 @@ const ( responseOK = `{"text": "Success", "code": 0}` responseHecHealthy = `{"text": "HEC is healthy", "code": 17}` - responseInvalidMethod = `Only "POST" method is supported` - responseInvalidEncoding = `"Content-Encoding" must be "gzip" or empty` + responseInvalidMethod = `"Only \"POST\" method is supported"` + responseInvalidEncoding = `"\"Content-Encoding\" must be \"gzip\" or empty"` responseInvalidDataFormat = `{"text":"Invalid data format","code":6}` responseErrEventRequired = `{"text":"Event field is required","code":12}` responseErrEventBlank = `{"text":"Event field cannot be blank","code":13}` - responseErrGzipReader = "Error on gzip body" - responseErrUnmarshalBody = "Failed to unmarshal message body" - responseErrInternalServerError = "Internal Server Error" - responseErrUnsupportedMetricEvent = "Unsupported metric event" - responseErrUnsupportedLogEvent = "Unsupported log event" + responseErrGzipReader = `"Error on gzip body"` + responseErrUnmarshalBody = `"Failed to unmarshal message body"` + responseErrInternalServerError = `"Internal Server Error"` + responseErrUnsupportedMetricEvent = `"Unsupported metric event"` + responseErrUnsupportedLogEvent = `"Unsupported log event"` responseErrHandlingIndexedFields = `{"text":"Error in handling indexed fields","code":15,"invalid-event-number":%d}` responseNoData = `{"text":"No data","code":5}` // Centralizing some HTTP and related string constants. @@ -58,18 +58,18 @@ var ( errInvalidMethod = errors.New("invalid http method") errInvalidEncoding = errors.New("invalid encoding") - okRespBody = initJSONResponse(responseOK) - eventRequiredRespBody = initJSONResponse(responseErrEventRequired) - eventBlankRespBody = initJSONResponse(responseErrEventBlank) - invalidEncodingRespBody = initJSONResponse(responseInvalidEncoding) - invalidFormatRespBody = initJSONResponse(responseInvalidDataFormat) - invalidMethodRespBody = initJSONResponse(responseInvalidMethod) - errGzipReaderRespBody = initJSONResponse(responseErrGzipReader) - errUnmarshalBodyRespBody = initJSONResponse(responseErrUnmarshalBody) - errInternalServerError = initJSONResponse(responseErrInternalServerError) - errUnsupportedMetricEvent = initJSONResponse(responseErrUnsupportedMetricEvent) - errUnsupportedLogEvent = initJSONResponse(responseErrUnsupportedLogEvent) - noDataRespBody = initJSONResponse(responseNoData) + okRespBody = []byte(responseOK) + eventRequiredRespBody = []byte(responseErrEventRequired) + eventBlankRespBody = []byte(responseErrEventBlank) + invalidEncodingRespBody = []byte(responseInvalidEncoding) + invalidFormatRespBody = []byte(responseInvalidDataFormat) + invalidMethodRespBody = []byte(responseInvalidMethod) + errGzipReaderRespBody = []byte(responseErrGzipReader) + errUnmarshalBodyRespBody = []byte(responseErrUnmarshalBody) + errInternalServerError = []byte(responseErrInternalServerError) + errUnsupportedMetricEvent = []byte(responseErrUnsupportedMetricEvent) + errUnsupportedLogEvent = []byte(responseErrUnsupportedLogEvent) + noDataRespBody = []byte(responseNoData) ) // splunkReceiver implements the receiver.Metrics for Splunk HEC metric protocol. @@ -464,15 +464,6 @@ func (r *splunkReceiver) handleHealthReq(writer http.ResponseWriter, _ *http.Req _, _ = writer.Write([]byte(responseHecHealthy)) } -func initJSONResponse(s string) []byte { - respBody, err := jsoniter.Marshal(s) - if err != nil { - // This is to be used in initialization so panic here is fine. - panic(err) - } - return respBody -} - func isFlatJSONField(field interface{}) bool { switch value := field.(type) { case map[string]interface{}: diff --git a/receiver/splunkhecreceiver/receiver_test.go b/receiver/splunkhecreceiver/receiver_test.go index e638eed716d5..271427d7e6cc 100644 --- a/receiver/splunkhecreceiver/receiver_test.go +++ b/receiver/splunkhecreceiver/receiver_test.go @@ -167,16 +167,16 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { tests := []struct { name string req *http.Request - assertResponse func(t *testing.T, status int, body string) + assertResponse func(t *testing.T, status int, body any) assertSink func(t *testing.T, sink *consumertest.LogsSink) assertMetricsSink func(t *testing.T, sink *consumertest.MetricsSink) }{ { name: "incorrect_method", req: httptest.NewRequest("PUT", "http://localhost/foo", nil), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidMethod, body) + assert.Equal(t, "Only \"POST\" method is supported", body) }, }, { @@ -188,9 +188,12 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req.Header.Set("Content-Type", "application/not-json") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) - assert.Equal(t, responseOK, body) + assert.Equal(t, map[string]interface{}{ + "text": "Success", + "code": float64(0), + }, body) }, }, { @@ -200,9 +203,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req.Header.Set("Content-Encoding", "superzipper") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusUnsupportedMediaType, status) - assert.Equal(t, responseInvalidEncoding, body) + assert.Equal(t, `"Content-Encoding" must be "gzip" or empty`, body) }, }, { @@ -211,9 +214,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader([]byte{1, 2, 3, 4})) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidDataFormat, body) + assert.Equal(t, map[string]interface{}{"code": float64(6), "text": "Invalid data format"}, body) }, }, { @@ -222,9 +225,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(nil)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseNoData, body) + assert.Equal(t, map[string]interface{}{"code": float64(5), "text": "No data"}, body) }, }, { @@ -235,9 +238,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidDataFormat, body) + assert.Equal(t, map[string]interface{}{"code": float64(6), "text": "Invalid data format"}, body) }, }, { @@ -250,9 +253,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseErrEventRequired, body) + assert.Equal(t, map[string]interface{}{"code": float64(12), "text": "Event field is required"}, body) }, }, { @@ -265,9 +268,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseErrEventBlank, body) + assert.Equal(t, map[string]interface{}{"code": float64(13), "text": "Event field cannot be blank"}, body) }, }, { @@ -278,9 +281,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) - assert.Equal(t, responseOK, body) + assert.Equal(t, map[string]interface{}{"code": float64(0), "text": "Success"}, body) }, assertSink: func(t *testing.T, sink *consumertest.LogsSink) { assert.Equal(t, 1, len(sink.AllLogs())) @@ -297,9 +300,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) - assert.Equal(t, responseOK, body) + assert.Equal(t, map[string]interface{}{"code": float64(0), "text": "Success"}, body) }, assertSink: func(t *testing.T, sink *consumertest.LogsSink) { assert.Equal(t, 0, len(sink.AllLogs())) @@ -324,9 +327,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) - assert.Equal(t, responseOK, body) + assert.Equal(t, map[string]interface{}{"code": float64(0), "text": "Success"}, body) }, }, { @@ -339,9 +342,9 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseErrGzipReader, body) + assert.Equal(t, `Error on gzip body`, body) }, }, } @@ -366,10 +369,11 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) { respBytes, err := io.ReadAll(resp.Body) assert.NoError(t, err) - var bodyStr string - assert.NoError(t, json.Unmarshal(respBytes, &bodyStr)) + var body any + fmt.Println(string(respBytes)) + assert.NoError(t, json.Unmarshal(respBytes, &body)) - tt.assertResponse(t, resp.StatusCode, bodyStr) + tt.assertResponse(t, resp.StatusCode, body) if tt.assertSink != nil { tt.assertSink(t, sink) } @@ -737,10 +741,13 @@ func Test_Logs_splunkhecReceiver_IndexSourceTypePassthrough(t *testing.T) { respBytes, err := io.ReadAll(resp.Body) assert.NoError(t, err) defer resp.Body.Close() - var bodyStr string - assert.NoError(t, json.Unmarshal(respBytes, &bodyStr)) + var body any + assert.NoError(t, json.Unmarshal(respBytes, &body)) assert.Equal(t, http.StatusOK, resp.StatusCode) - assert.Equal(t, responseOK, bodyStr) + assert.Equal(t, map[string]interface{}{ + "text": "Success", + "code": float64(0), + }, body) select { case <-done: break @@ -832,10 +839,13 @@ func Test_Metrics_splunkhecReceiver_IndexSourceTypePassthrough(t *testing.T) { respBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() assert.NoError(t, err) - var bodyStr string - assert.NoError(t, json.Unmarshal(respBytes, &bodyStr)) + var body any + assert.NoError(t, json.Unmarshal(respBytes, &body)) assert.Equal(t, http.StatusOK, resp.StatusCode) - assert.Equal(t, responseOK, bodyStr) + assert.Equal(t, map[string]interface{}{ + "text": "Success", + "code": float64(0), + }, body) select { case <-done: break @@ -918,14 +928,14 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { tests := []struct { name string req *http.Request - assertResponse func(t *testing.T, status int, body string) + assertResponse func(t *testing.T, status int, body any) }{ { name: "incorrect_method", req: httptest.NewRequest("PUT", "http://localhost/foo", nil), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidMethod, body) + assert.Equal(t, `Only "POST" method is supported`, body) }, }, { @@ -935,7 +945,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req.Header.Set("Content-Type", "application/not-json") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) }, }, @@ -946,9 +956,9 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req.Header.Set("Content-Encoding", "superzipper") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusUnsupportedMediaType, status) - assert.Equal(t, responseInvalidEncoding, body) + assert.Equal(t, `"Content-Encoding" must be "gzip" or empty`, body) }, }, { @@ -957,9 +967,9 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(nil)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseNoData, body) + assert.Equal(t, map[string]interface{}{"code": float64(5), "text": "No data"}, body) }, }, @@ -969,7 +979,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", strings.NewReader("foo\nbar")) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) }, }, @@ -981,7 +991,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes)) return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) }, }, @@ -1001,7 +1011,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusOK, status) }, }, @@ -1015,9 +1025,9 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseErrGzipReader, body) + assert.Equal(t, `Error on gzip body`, body) }, }, { @@ -1034,9 +1044,9 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidDataFormat, body) + assert.Equal(t, map[string]interface{}{"code": float64(6), "text": "Invalid data format"}, body) }, }, { @@ -1053,9 +1063,9 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { return req }(), - assertResponse: func(t *testing.T, status int, body string) { + assertResponse: func(t *testing.T, status int, body any) { assert.Equal(t, http.StatusBadRequest, status) - assert.Equal(t, responseInvalidDataFormat, body) + assert.Equal(t, map[string]interface{}{"code": float64(6), "text": "Invalid data format"}, body) }, }, } @@ -1079,14 +1089,12 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) { assert.NoError(t, err) defer resp.Body.Close() - var bodyStr string + var body any if len(respBytes) > 0 { - assert.NoError(t, json.Unmarshal(respBytes, &bodyStr)) - } else { - bodyStr = "" + assert.NoError(t, json.Unmarshal(respBytes, &body)) } - tt.assertResponse(t, resp.StatusCode, bodyStr) + tt.assertResponse(t, resp.StatusCode, body) }) } }