Skip to content

Commit debafb8

Browse files
atoulmeRoryCrispin
authored andcommitted
[receiver/splunkhec] Fix the double encoding of JSON responses (open-telemetry#27606)
**Description:** We would double-encode JSON objects as strings. We typically have encoded strings as JSON strings as static responses to issues encountered during data processing. Along the way, we adopted JSON objects to match the Splunk Enterprise API. However, we didn't stop encoding as JSON strings, resulting in double encoded JSON strings. **Link to tracking Issue:** Fixes open-telemetry#27604 **Testing:** Unit test changes.
1 parent 29e7f84 commit debafb8

File tree

3 files changed

+110
-84
lines changed

3 files changed

+110
-84
lines changed

.chloggen/json_error_codes.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: splunkhecreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Do not encode JSON response objects as string.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27604]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/splunkhecreceiver/receiver.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ const (
3434

3535
responseOK = `{"text": "Success", "code": 0}`
3636
responseHecHealthy = `{"text": "HEC is healthy", "code": 17}`
37-
responseInvalidMethod = `Only "POST" method is supported`
38-
responseInvalidEncoding = `"Content-Encoding" must be "gzip" or empty`
37+
responseInvalidMethod = `"Only \"POST\" method is supported"`
38+
responseInvalidEncoding = `"\"Content-Encoding\" must be \"gzip\" or empty"`
3939
responseInvalidDataFormat = `{"text":"Invalid data format","code":6}`
4040
responseErrEventRequired = `{"text":"Event field is required","code":12}`
4141
responseErrEventBlank = `{"text":"Event field cannot be blank","code":13}`
42-
responseErrGzipReader = "Error on gzip body"
43-
responseErrUnmarshalBody = "Failed to unmarshal message body"
44-
responseErrInternalServerError = "Internal Server Error"
45-
responseErrUnsupportedMetricEvent = "Unsupported metric event"
46-
responseErrUnsupportedLogEvent = "Unsupported log event"
42+
responseErrGzipReader = `"Error on gzip body"`
43+
responseErrUnmarshalBody = `"Failed to unmarshal message body"`
44+
responseErrInternalServerError = `"Internal Server Error"`
45+
responseErrUnsupportedMetricEvent = `"Unsupported metric event"`
46+
responseErrUnsupportedLogEvent = `"Unsupported log event"`
4747
responseErrHandlingIndexedFields = `{"text":"Error in handling indexed fields","code":15,"invalid-event-number":%d}`
4848
responseNoData = `{"text":"No data","code":5}`
4949
// Centralizing some HTTP and related string constants.
@@ -58,18 +58,18 @@ var (
5858
errInvalidMethod = errors.New("invalid http method")
5959
errInvalidEncoding = errors.New("invalid encoding")
6060

61-
okRespBody = initJSONResponse(responseOK)
62-
eventRequiredRespBody = initJSONResponse(responseErrEventRequired)
63-
eventBlankRespBody = initJSONResponse(responseErrEventBlank)
64-
invalidEncodingRespBody = initJSONResponse(responseInvalidEncoding)
65-
invalidFormatRespBody = initJSONResponse(responseInvalidDataFormat)
66-
invalidMethodRespBody = initJSONResponse(responseInvalidMethod)
67-
errGzipReaderRespBody = initJSONResponse(responseErrGzipReader)
68-
errUnmarshalBodyRespBody = initJSONResponse(responseErrUnmarshalBody)
69-
errInternalServerError = initJSONResponse(responseErrInternalServerError)
70-
errUnsupportedMetricEvent = initJSONResponse(responseErrUnsupportedMetricEvent)
71-
errUnsupportedLogEvent = initJSONResponse(responseErrUnsupportedLogEvent)
72-
noDataRespBody = initJSONResponse(responseNoData)
61+
okRespBody = []byte(responseOK)
62+
eventRequiredRespBody = []byte(responseErrEventRequired)
63+
eventBlankRespBody = []byte(responseErrEventBlank)
64+
invalidEncodingRespBody = []byte(responseInvalidEncoding)
65+
invalidFormatRespBody = []byte(responseInvalidDataFormat)
66+
invalidMethodRespBody = []byte(responseInvalidMethod)
67+
errGzipReaderRespBody = []byte(responseErrGzipReader)
68+
errUnmarshalBodyRespBody = []byte(responseErrUnmarshalBody)
69+
errInternalServerError = []byte(responseErrInternalServerError)
70+
errUnsupportedMetricEvent = []byte(responseErrUnsupportedMetricEvent)
71+
errUnsupportedLogEvent = []byte(responseErrUnsupportedLogEvent)
72+
noDataRespBody = []byte(responseNoData)
7373
)
7474

7575
// splunkReceiver implements the receiver.Metrics for Splunk HEC metric protocol.
@@ -464,15 +464,6 @@ func (r *splunkReceiver) handleHealthReq(writer http.ResponseWriter, _ *http.Req
464464
_, _ = writer.Write([]byte(responseHecHealthy))
465465
}
466466

467-
func initJSONResponse(s string) []byte {
468-
respBody, err := jsoniter.Marshal(s)
469-
if err != nil {
470-
// This is to be used in initialization so panic here is fine.
471-
panic(err)
472-
}
473-
return respBody
474-
}
475-
476467
func isFlatJSONField(field interface{}) bool {
477468
switch value := field.(type) {
478469
case map[string]interface{}:

0 commit comments

Comments
 (0)