Skip to content

Commit 5d35e16

Browse files
Fix issue with log_writer panic on line with all spaces (#4910) (#4915)
* Fix #4907. * Add changelog. * Apply suggestions from code review Co-authored-by: Julien Lind <julien.lind@elastic.co> --------- Co-authored-by: Julien Lind <julien.lind@elastic.co> (cherry picked from commit 3df9f8b) Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
1 parent bfdd8ca commit 5d35e16

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Fix possible crash in reading component logs
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/4910
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/4907

pkg/component/runtime/log_writer.go

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (r *logWriter) Write(p []byte) (int, error) {
106106
continue
107107
}
108108
str := strings.TrimSpace(string(line))
109+
if len(str) == 0 {
110+
// empty line after trim
111+
continue
112+
}
109113
// try to parse line as JSON
110114
if str[0] == '{' && r.handleJSON(str) {
111115
// handled as JSON

pkg/component/runtime/log_writer_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ func TestLogWriter(t *testing.T) {
122122
},
123123
},
124124
},
125+
{
126+
Name: "multi empty text line",
127+
LogLevel: zapcore.InfoLevel,
128+
LogSource: logSourceStdout,
129+
Lines: []string{
130+
"simple written line\r\n",
131+
"\r\n", // empty line
132+
" \r\n", // empty line with space
133+
"another line\n",
134+
},
135+
Wrote: []wrote{
136+
{
137+
entry: zapcore.Entry{
138+
Level: zapcore.InfoLevel,
139+
Time: time.Time{},
140+
Message: "simple written line",
141+
},
142+
},
143+
{
144+
entry: zapcore.Entry{
145+
Level: zapcore.InfoLevel,
146+
Time: time.Time{},
147+
Message: "another line",
148+
},
149+
},
150+
},
151+
},
125152
{
126153
Name: "json log line split",
127154
LogLevel: zapcore.DebugLevel,

0 commit comments

Comments
 (0)