Skip to content

Commit ce75979

Browse files
authored
return JSON ok response for hec receiver raw path (#29875)
**Description:** Adds enhancement for splunk hec receiver raw path - previously it returns no response in http body: it nows returns json response **Link to tracking Issue:** #29745
1 parent 5ddd0ba commit ce75979

File tree

3 files changed

+94
-39
lines changed

3 files changed

+94
-39
lines changed
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: enhancement
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: Returns json response in raw endpoint when it is successful
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: [20766]
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: [user]

receiver/splunkhecreceiver/receiver.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const (
4949
// Centralizing some HTTP and related string constants.
5050
gzipEncoding = "gzip"
5151
httpContentEncodingHeader = "Content-Encoding"
52+
httpContentTypeHeader = "Content-Type"
53+
httpJSONTypeHeader = "application/json"
5254
)
5355

5456
var (
@@ -229,6 +231,14 @@ func (r *splunkReceiver) Shutdown(context.Context) error {
229231
return err
230232
}
231233

234+
func (r *splunkReceiver) writeSuccessResponse(ctx context.Context, resp http.ResponseWriter, eventCount int) {
235+
resp.Header().Set(httpContentTypeHeader, httpJSONTypeHeader)
236+
resp.WriteHeader(http.StatusOK)
237+
if _, err := resp.Write(okRespBody); err != nil {
238+
r.failRequest(ctx, resp, http.StatusInternalServerError, errInternalServerError, eventCount, err)
239+
}
240+
}
241+
232242
func (r *splunkReceiver) handleRawReq(resp http.ResponseWriter, req *http.Request) {
233243
ctx := req.Context()
234244
ctx = r.obsrecv.StartLogsOp(ctx)
@@ -292,7 +302,7 @@ func (r *splunkReceiver) handleRawReq(resp http.ResponseWriter, req *http.Reques
292302
if consumerErr != nil {
293303
r.failRequest(ctx, resp, http.StatusInternalServerError, errInternalServerError, slLen, consumerErr)
294304
} else {
295-
resp.WriteHeader(http.StatusOK)
305+
r.writeSuccessResponse(ctx, resp, ld.LogRecordCount())
296306
r.obsrecv.EndLogsOp(ctx, metadata.Type, slLen, nil)
297307
}
298308
}
@@ -404,10 +414,7 @@ func (r *splunkReceiver) handleReq(resp http.ResponseWriter, req *http.Request)
404414
}
405415
}
406416

407-
resp.WriteHeader(http.StatusOK)
408-
if _, err := resp.Write(okRespBody); err != nil {
409-
r.failRequest(ctx, resp, http.StatusInternalServerError, errInternalServerError, len(events)+len(metricEvents), err)
410-
}
417+
r.writeSuccessResponse(ctx, resp, len(events)+len(metricEvents))
411418
}
412419

413420
func (r *splunkReceiver) createResourceCustomizer(req *http.Request) func(resource pcommon.Resource) {

receiver/splunkhecreceiver/receiver_test.go

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ import (
3737
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
3838
)
3939

40+
func assertHecSuccessResponse(t *testing.T, resp *http.Response, body any) {
41+
status := resp.StatusCode
42+
assert.Equal(t, http.StatusOK, status)
43+
assert.Equal(t, httpJSONTypeHeader, resp.Header.Get(httpContentTypeHeader))
44+
assert.Equal(t, map[string]any{"code": float64(0), "text": "Success"}, body)
45+
}
46+
4047
func Test_splunkhecreceiver_NewLogsReceiver(t *testing.T) {
4148
defaultConfig := createDefaultConfig().(*Config)
4249
emptyEndpointConfig := createDefaultConfig().(*Config)
@@ -167,14 +174,15 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
167174
tests := []struct {
168175
name string
169176
req *http.Request
170-
assertResponse func(t *testing.T, status int, body any)
177+
assertResponse func(t *testing.T, resp *http.Response, body any)
171178
assertSink func(t *testing.T, sink *consumertest.LogsSink)
172179
assertMetricsSink func(t *testing.T, sink *consumertest.MetricsSink)
173180
}{
174181
{
175182
name: "incorrect_method",
176183
req: httptest.NewRequest("PUT", "http://localhost/foo", nil),
177-
assertResponse: func(t *testing.T, status int, body any) {
184+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
185+
status := resp.StatusCode
178186
assert.Equal(t, http.StatusBadRequest, status)
179187
assert.Equal(t, "Only \"POST\" method is supported", body)
180188
},
@@ -188,7 +196,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
188196
req.Header.Set("Content-Type", "application/not-json")
189197
return req
190198
}(),
191-
assertResponse: func(t *testing.T, status int, body any) {
199+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
200+
status := resp.StatusCode
192201
assert.Equal(t, http.StatusOK, status)
193202
assert.Equal(t, map[string]any{
194203
"text": "Success",
@@ -203,7 +212,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
203212
req.Header.Set("Content-Encoding", "superzipper")
204213
return req
205214
}(),
206-
assertResponse: func(t *testing.T, status int, body any) {
215+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
216+
status := resp.StatusCode
207217
assert.Equal(t, http.StatusUnsupportedMediaType, status)
208218
assert.Equal(t, `"Content-Encoding" must be "gzip" or empty`, body)
209219
},
@@ -214,7 +224,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
214224
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader([]byte{1, 2, 3, 4}))
215225
return req
216226
}(),
217-
assertResponse: func(t *testing.T, status int, body any) {
227+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
228+
status := resp.StatusCode
218229
assert.Equal(t, http.StatusBadRequest, status)
219230
assert.Equal(t, map[string]any{"code": float64(6), "text": "Invalid data format"}, body)
220231
},
@@ -225,7 +236,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
225236
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(nil))
226237
return req
227238
}(),
228-
assertResponse: func(t *testing.T, status int, body any) {
239+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
240+
status := resp.StatusCode
229241
assert.Equal(t, http.StatusBadRequest, status)
230242
assert.Equal(t, map[string]any{"code": float64(5), "text": "No data"}, body)
231243
},
@@ -238,7 +250,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
238250
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
239251
return req
240252
}(),
241-
assertResponse: func(t *testing.T, status int, body any) {
253+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
254+
status := resp.StatusCode
242255
assert.Equal(t, http.StatusBadRequest, status)
243256
assert.Equal(t, map[string]any{"code": float64(6), "text": "Invalid data format"}, body)
244257
},
@@ -253,7 +266,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
253266
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
254267
return req
255268
}(),
256-
assertResponse: func(t *testing.T, status int, body any) {
269+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
270+
status := resp.StatusCode
257271
assert.Equal(t, http.StatusBadRequest, status)
258272
assert.Equal(t, map[string]any{"code": float64(12), "text": "Event field is required"}, body)
259273
},
@@ -268,7 +282,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
268282
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
269283
return req
270284
}(),
271-
assertResponse: func(t *testing.T, status int, body any) {
285+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
286+
status := resp.StatusCode
272287
assert.Equal(t, http.StatusBadRequest, status)
273288
assert.Equal(t, map[string]any{"code": float64(13), "text": "Event field cannot be blank"}, body)
274289
},
@@ -281,9 +296,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
281296
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
282297
return req
283298
}(),
284-
assertResponse: func(t *testing.T, status int, body any) {
285-
assert.Equal(t, http.StatusOK, status)
286-
assert.Equal(t, map[string]any{"code": float64(0), "text": "Success"}, body)
299+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
300+
assertHecSuccessResponse(t, resp, body)
287301
},
288302
assertSink: func(t *testing.T, sink *consumertest.LogsSink) {
289303
assert.Equal(t, 1, len(sink.AllLogs()))
@@ -300,9 +314,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
300314
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
301315
return req
302316
}(),
303-
assertResponse: func(t *testing.T, status int, body any) {
304-
assert.Equal(t, http.StatusOK, status)
305-
assert.Equal(t, map[string]any{"code": float64(0), "text": "Success"}, body)
317+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
318+
assertHecSuccessResponse(t, resp, body)
306319
},
307320
assertSink: func(t *testing.T, sink *consumertest.LogsSink) {
308321
assert.Equal(t, 0, len(sink.AllLogs()))
@@ -327,9 +340,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
327340
req.Header.Set("Content-Encoding", "gzip")
328341
return req
329342
}(),
330-
assertResponse: func(t *testing.T, status int, body any) {
331-
assert.Equal(t, http.StatusOK, status)
332-
assert.Equal(t, map[string]any{"code": float64(0), "text": "Success"}, body)
343+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
344+
assertHecSuccessResponse(t, resp, body)
333345
},
334346
},
335347
{
@@ -342,7 +354,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
342354
req.Header.Set("Content-Encoding", "gzip")
343355
return req
344356
}(),
345-
assertResponse: func(t *testing.T, status int, body any) {
357+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
358+
status := resp.StatusCode
346359
assert.Equal(t, http.StatusBadRequest, status)
347360
assert.Equal(t, `Error on gzip body`, body)
348361
},
@@ -373,7 +386,7 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
373386
fmt.Println(string(respBytes))
374387
assert.NoError(t, json.Unmarshal(respBytes, &body))
375388

376-
tt.assertResponse(t, resp.StatusCode, body)
389+
tt.assertResponse(t, resp, body)
377390
if tt.assertSink != nil {
378391
tt.assertSink(t, sink)
379392
}
@@ -928,12 +941,13 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
928941
tests := []struct {
929942
name string
930943
req *http.Request
931-
assertResponse func(t *testing.T, status int, body any)
944+
assertResponse func(t *testing.T, resp *http.Response, body any)
932945
}{
933946
{
934947
name: "incorrect_method",
935948
req: httptest.NewRequest("PUT", "http://localhost/foo", nil),
936-
assertResponse: func(t *testing.T, status int, body any) {
949+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
950+
status := resp.StatusCode
937951
assert.Equal(t, http.StatusBadRequest, status)
938952
assert.Equal(t, `Only "POST" method is supported`, body)
939953
},
@@ -945,7 +959,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
945959
req.Header.Set("Content-Type", "application/not-json")
946960
return req
947961
}(),
948-
assertResponse: func(t *testing.T, status int, body any) {
962+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
963+
status := resp.StatusCode
949964
assert.Equal(t, http.StatusOK, status)
950965
},
951966
},
@@ -956,7 +971,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
956971
req.Header.Set("Content-Encoding", "superzipper")
957972
return req
958973
}(),
959-
assertResponse: func(t *testing.T, status int, body any) {
974+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
975+
status := resp.StatusCode
960976
assert.Equal(t, http.StatusUnsupportedMediaType, status)
961977
assert.Equal(t, `"Content-Encoding" must be "gzip" or empty`, body)
962978
},
@@ -967,7 +983,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
967983
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(nil))
968984
return req
969985
}(),
970-
assertResponse: func(t *testing.T, status int, body any) {
986+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
987+
status := resp.StatusCode
971988
assert.Equal(t, http.StatusBadRequest, status)
972989
assert.Equal(t, map[string]any{"code": float64(5), "text": "No data"}, body)
973990
},
@@ -979,7 +996,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
979996
req := httptest.NewRequest("POST", "http://localhost/foo", strings.NewReader("foo\nbar"))
980997
return req
981998
}(),
982-
assertResponse: func(t *testing.T, status int, body any) {
999+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1000+
status := resp.StatusCode
9831001
assert.Equal(t, http.StatusOK, status)
9841002
},
9851003
},
@@ -991,8 +1009,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
9911009
req := httptest.NewRequest("POST", "http://localhost/foo", bytes.NewReader(msgBytes))
9921010
return req
9931011
}(),
994-
assertResponse: func(t *testing.T, status int, body any) {
995-
assert.Equal(t, http.StatusOK, status)
1012+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1013+
assertHecSuccessResponse(t, resp, body)
9961014
},
9971015
},
9981016
{
@@ -1011,8 +1029,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
10111029
req.Header.Set("Content-Encoding", "gzip")
10121030
return req
10131031
}(),
1014-
assertResponse: func(t *testing.T, status int, body any) {
1015-
assert.Equal(t, http.StatusOK, status)
1032+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1033+
assertHecSuccessResponse(t, resp, body)
10161034
},
10171035
},
10181036
{
@@ -1025,7 +1043,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
10251043
req.Header.Set("Content-Encoding", "gzip")
10261044
return req
10271045
}(),
1028-
assertResponse: func(t *testing.T, status int, body any) {
1046+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1047+
status := resp.StatusCode
10291048
assert.Equal(t, http.StatusBadRequest, status)
10301049
assert.Equal(t, `Error on gzip body`, body)
10311050
},
@@ -1044,7 +1063,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
10441063

10451064
return req
10461065
}(),
1047-
assertResponse: func(t *testing.T, status int, body any) {
1066+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1067+
status := resp.StatusCode
10481068
assert.Equal(t, http.StatusBadRequest, status)
10491069
assert.Equal(t, map[string]any{"code": float64(6), "text": "Invalid data format"}, body)
10501070
},
@@ -1063,7 +1083,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
10631083

10641084
return req
10651085
}(),
1066-
assertResponse: func(t *testing.T, status int, body any) {
1086+
assertResponse: func(t *testing.T, resp *http.Response, body any) {
1087+
status := resp.StatusCode
10671088
assert.Equal(t, http.StatusBadRequest, status)
10681089
assert.Equal(t, map[string]any{"code": float64(6), "text": "Invalid data format"}, body)
10691090
},
@@ -1094,7 +1115,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
10941115
assert.NoError(t, json.Unmarshal(respBytes, &body))
10951116
}
10961117

1097-
tt.assertResponse(t, resp.StatusCode, body)
1118+
tt.assertResponse(t, resp, body)
10981119
})
10991120
}
11001121
}

0 commit comments

Comments
 (0)