Skip to content

Commit

Permalink
fix for issue 35028
Browse files Browse the repository at this point in the history
  • Loading branch information
shalper2 committed Sep 19, 2024
1 parent 5601dfb commit e3a7a6a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/35028-fix-scan-behavior.yaml
Original file line number Diff line number Diff line change
@@ -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: webhookeventreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed a bug where request bodies containing newline characters caused the results to split into multiple log entries

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35028]

# (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: [user]
2 changes: 1 addition & 1 deletion receiver/webhookeventreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (er *eventReceiver) handleReq(w http.ResponseWriter, r *http.Request, _ htt
defer er.gzipPool.Put(reader)
}

// finish reading the body into a log
// send body into a scanner and then convert the request body into a log
sc := bufio.NewScanner(bodyReader)
ld, numLogs := reqToLog(sc, r.URL.Query(), er.cfg, er.settings)
consumerErr := er.logConsumer.ConsumeLogs(ctx, ld)
Expand Down
12 changes: 12 additions & 0 deletions receiver/webhookeventreceiver/req_to_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func reqToLog(sc *bufio.Scanner,
query url.Values,
_ *Config,
settings receiver.Settings) (plog.Logs, int) {
// we simply dont split the data passed into scan (i.e. scan the whole thing)
// the downside to this approach is that only 1 log per request can be handled.
// NOTE: logs will contain these newline characters which could have formatting
// consequences downstream.
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if !atEOF {
return 0, nil, nil
}
return 0, data, bufio.ErrFinalToken
}
sc.Split(split)

log := plog.NewLogs()
resourceLog := log.ResourceLogs().AppendEmpty()
appendMetadata(resourceLog, query)
Expand Down
34 changes: 34 additions & 0 deletions receiver/webhookeventreceiver/req_to_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@ func TestReqToLog(t *testing.T) {
}
},
},
{
desc: "new lines present in body",
sc: func() *bufio.Scanner {
reader := io.NopCloser(bytes.NewReader([]byte("{\n\"key\":\"value\"\n}")))
return bufio.NewScanner(reader)
}(),
query: func() url.Values {
v, err := url.ParseQuery(`qparam1=hello&qparam2=world`)
if err != nil {
log.Fatal("failed to parse query")
}
return v
}(),
tt: func(t *testing.T, reqLog plog.Logs, reqLen int, _ receiver.Settings) {
require.Equal(t, 1, reqLen)

attributes := reqLog.ResourceLogs().At(0).Resource().Attributes()
require.Equal(t, 2, attributes.Len())

scopeLogsScope := reqLog.ResourceLogs().At(0).ScopeLogs().At(0).Scope()
require.Equal(t, 2, scopeLogsScope.Attributes().Len())

if v, ok := attributes.Get("qparam1"); ok {
require.Equal(t, "hello", v.AsString())
} else {
require.Fail(t, "faild to set attribute from query parameter 1")
}
if v, ok := attributes.Get("qparam2"); ok {
require.Equal(t, "world", v.AsString())
} else {
require.Fail(t, "faild to set attribute query parameter 2")
}
},
},
{
desc: "Query is empty",
sc: func() *bufio.Scanner {
Expand Down

0 comments on commit e3a7a6a

Please sign in to comment.