Skip to content

Commit 3987783

Browse files
shalper2jriguera
authored andcommitted
[receiver/webhookevent] fix for issue 35028 (open-telemetry#35065)
**Description:** Fixed a bug where request bodies containing newlines caused logs to split into separate entries. **Link to tracking Issue:** [35028](open-telemetry#35028) **Testing:** Added a new unit test to `req_to_log_test.go` for the case where request body data contains newline characters **Documentation:** Some comments in `req_to_log.go` for clarity were added.
1 parent d0f7c8d commit 3987783

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
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: 'bug_fix'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: webhookeventreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fixed a bug where request bodies containing newline characters caused the results to split into multiple log entries
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: [35028]
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/webhookeventreceiver/receiver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (er *eventReceiver) handleReq(w http.ResponseWriter, r *http.Request, _ htt
191191
defer er.gzipPool.Put(reader)
192192
}
193193

194-
// finish reading the body into a log
194+
// send body into a scanner and then convert the request body into a log
195195
sc := bufio.NewScanner(bodyReader)
196196
ld, numLogs := reqToLog(sc, r.URL.Query(), er.cfg, er.settings)
197197
consumerErr := er.logConsumer.ConsumeLogs(ctx, ld)

receiver/webhookeventreceiver/req_to_log.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ func reqToLog(sc *bufio.Scanner,
1919
query url.Values,
2020
_ *Config,
2121
settings receiver.Settings) (plog.Logs, int) {
22+
// we simply dont split the data passed into scan (i.e. scan the whole thing)
23+
// the downside to this approach is that only 1 log per request can be handled.
24+
// NOTE: logs will contain these newline characters which could have formatting
25+
// consequences downstream.
26+
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
27+
if !atEOF {
28+
return 0, nil, nil
29+
}
30+
return 0, data, bufio.ErrFinalToken
31+
}
32+
sc.Split(split)
33+
2234
log := plog.NewLogs()
2335
resourceLog := log.ResourceLogs().AppendEmpty()
2436
appendMetadata(resourceLog, query)

receiver/webhookeventreceiver/req_to_log_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ func TestReqToLog(t *testing.T) {
6060
}
6161
},
6262
},
63+
{
64+
desc: "new lines present in body",
65+
sc: func() *bufio.Scanner {
66+
reader := io.NopCloser(bytes.NewReader([]byte("{\n\"key\":\"value\"\n}")))
67+
return bufio.NewScanner(reader)
68+
}(),
69+
query: func() url.Values {
70+
v, err := url.ParseQuery(`qparam1=hello&qparam2=world`)
71+
if err != nil {
72+
log.Fatal("failed to parse query")
73+
}
74+
return v
75+
}(),
76+
tt: func(t *testing.T, reqLog plog.Logs, reqLen int, _ receiver.Settings) {
77+
require.Equal(t, 1, reqLen)
78+
79+
attributes := reqLog.ResourceLogs().At(0).Resource().Attributes()
80+
require.Equal(t, 2, attributes.Len())
81+
82+
scopeLogsScope := reqLog.ResourceLogs().At(0).ScopeLogs().At(0).Scope()
83+
require.Equal(t, 2, scopeLogsScope.Attributes().Len())
84+
85+
if v, ok := attributes.Get("qparam1"); ok {
86+
require.Equal(t, "hello", v.AsString())
87+
} else {
88+
require.Fail(t, "faild to set attribute from query parameter 1")
89+
}
90+
if v, ok := attributes.Get("qparam2"); ok {
91+
require.Equal(t, "world", v.AsString())
92+
} else {
93+
require.Fail(t, "faild to set attribute query parameter 2")
94+
}
95+
},
96+
},
6397
{
6498
desc: "Query is empty",
6599
sc: func() *bufio.Scanner {

0 commit comments

Comments
 (0)