Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LogHouse][Processor] Handle json levels #327

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions processor/loghouseprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ func processOneLogLine(l *plog.LogRecord) error {
return nil
}

func parseSeverity(l *plog.LogRecord) error {
logLevel, ok := l.Attributes().Get("level")
if !ok {
return fmt.Errorf("no level specified")
}
updateSeverity(strings.ToUpper(logLevel.Str()), l)
return nil
}

func processJSONLog(l *plog.LogRecord) {
_ = parseSeverity(l)
extractBody(l)
isCH := parseCHTimestamp(l)
if isCH {
Expand Down
33 changes: 33 additions & 0 deletions processor/loghouseprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ import (
"go.opentelemetry.io/collector/pdata/plog"
)

func Test_dp(t *testing.T) {
t.Run("json severity", func(t *testing.T) {
// Define the test cases in a table format
tests := []struct {
name string
input string
expectedText string
expectedLevel plog.SeverityNumber
}{
{"Error severity", "{\"level\": \"fatal\"}", FATAL, plog.SeverityNumberFatal},
{"Error severity", "{\"level\": \"error\"}", ERROR, plog.SeverityNumberError},
{"Warning severity", "{\"level\": \"warn\"}", WARN, plog.SeverityNumberWarn},
{"Info severity", "{\"level\": \"info\"}", INFO, plog.SeverityNumberInfo},
{"Debug severity", "{\"level\": \"debug\"}", DEBUG, plog.SeverityNumberDebug},
{"Debug severity", "{\"level\": \"trace\"}", TRACE, plog.SeverityNumberTrace},
// Add more severity levels if needed
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
log := plog.NewLogRecord()
log.Body().SetStr(tc.input)

err := processOneLogLine(&log)
assert.NoError(t, err)

assert.Equal(t, tc.expectedText, log.SeverityText())
assert.Equal(t, tc.expectedLevel, log.SeverityNumber())
})
}
})
}

func Test_plaintextSeverity(t *testing.T) {
t.Run("trace", func(t *testing.T) {
line := "2023-01-02 11:20:30 <TRACE> some log line"
Expand Down
Loading