-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e72df52
commit c8b43e2
Showing
8 changed files
with
205 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
package bodyparser | ||
|
||
import "go.opentelemetry.io/collector/pdata/plog" | ||
import ( | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type Default struct { | ||
} | ||
|
||
func (l *Default) Parse(body []byte, records plog.LogRecordSlice) { | ||
func (l *Default) Parse(body []byte) plog.Logs { | ||
// split by newline and return | ||
// TODO: add configuration for multiline | ||
ld := plog.NewLogs() | ||
rl := ld.ResourceLogs().AppendEmpty() | ||
sl := rl.ScopeLogs().AppendEmpty() | ||
data := string(body) | ||
loglines := strings.Split(data, "\n") | ||
for _, log := range loglines { | ||
sl.LogRecords().AppendEmpty().Body().SetStr(log) | ||
} | ||
return ld | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package bodyparser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
func TestDefaultParse(t *testing.T) { | ||
t.Parallel() | ||
d := Default{} | ||
tests := []struct { | ||
name string | ||
PayLoad string | ||
Logs func() plog.Logs | ||
isError bool | ||
}{ | ||
{ | ||
name: "Test 1", | ||
PayLoad: `This is a log line`, | ||
Logs: func() plog.Logs { | ||
ld := plog.NewLogs() | ||
rl := ld.ResourceLogs().AppendEmpty() | ||
sl := rl.ScopeLogs().AppendEmpty() | ||
sl.LogRecords().AppendEmpty().Body().SetStr("This is a log line") | ||
return ld | ||
}, | ||
}, | ||
{ | ||
name: "Test 2 - multiple lines", | ||
PayLoad: "This is a log line\nThis is another log line", | ||
Logs: func() plog.Logs { | ||
ld := plog.NewLogs() | ||
rl := ld.ResourceLogs().AppendEmpty() | ||
sl := rl.ScopeLogs().AppendEmpty() | ||
sl.LogRecords().AppendEmpty().Body().SetStr("This is a log line") | ||
sl.LogRecords().AppendEmpty().Body().SetStr("This is another log line") | ||
return ld | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
res := d.Parse([]byte(tt.PayLoad)) | ||
logs := tt.Logs() | ||
assert.Equal(t, logs, res) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters