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

[ClickHouse org] Update CH Severity parsing #315

Merged
merged 3 commits into from
Feb 1, 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
31 changes: 9 additions & 22 deletions processor/loghouseprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,18 @@ func updateSeverity(sev string, l *plog.LogRecord) {
}

func parseCHSeverity(l *plog.LogRecord) bool {
sevText, ok := l.Attributes().Get("level")
levelVal, ok := l.Attributes().Get("level")
if !ok {
return false
}
switch sevText.Str() {
case "0":
updateSeverity(FATAL, l)
case "1":
updateSeverity(FATAL, l)
case "2":
updateSeverity(FATAL, l)
case "3":
updateSeverity(ERROR, l)
case "4":
updateSeverity(WARN, l)
case "5":
updateSeverity(INFO, l)
case "6":
updateSeverity(INFO, l)
case "7":
updateSeverity(DEBUG, l)
case "8":
updateSeverity(TRACE, l)
}
l.SetSeverityText(strings.ToUpper(l.SeverityNumber().String()))
level := strings.ToUpper(levelVal.Str())
switch level {
case "INFORMATION":
level = INFO
case "WARNING":
level = WARN
}
updateSeverity(level, l)
return true
}

Expand Down
83 changes: 67 additions & 16 deletions processor/loghouseprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,66 @@ func Test_plaintextSeverity(t *testing.T) {
assert.Equal(t, WARN, log.SeverityText())
assert.Equal(t, plog.SeverityNumberWarn, log.SeverityNumber())
})
}

func Test_chSeverity(t *testing.T) {
type testCase struct {
in string
exStr string
exNum plog.SeverityNumber
}
testCases := []testCase{
{
in: "Info",
exStr: INFO,
exNum: plog.SeverityNumberInfo,
},
{
in: "inforMation",
exStr: INFO,
exNum: plog.SeverityNumberInfo,
},
{
in: "warn",
exStr: WARN,
exNum: plog.SeverityNumberWarn,
},
{
in: "warnIng",
exStr: WARN,
exNum: plog.SeverityNumberWarn,
},
{
in: "debug",
exStr: DEBUG,
exNum: plog.SeverityNumberDebug,
},
{
in: "tracE",
exStr: TRACE,
exNum: plog.SeverityNumberTrace,
},
}
for _, tc := range testCases {
t.Run(tc.in, func(t *testing.T) {
line := plog.NewLogRecord()
line.Attributes().PutStr("level", tc.in)

ok := parseCHSeverity(&line)
assert.True(t, ok)

assert.Equal(t, tc.exStr, line.SeverityText())
assert.Equal(t, tc.exNum, line.SeverityNumber())
})
}

}

func Test_trimK8sPreamble(t *testing.T) {
type testCase struct {
in string
exOut string
exOk bool
comment string
in string
exOut string
exOk bool
}
testCases := []testCase{
{
Expand All @@ -68,22 +119,22 @@ func Test_trimK8sPreamble(t *testing.T) {
exOk: false,
},
{
in: "junk {BLAH} blah",
exOut: "junk {BLAH} blah",
exOk: false,
comment: "we ignore the {} where it just exists within the string, as it doesnt look like a valid json log line",
in: "junk {BLAH} blah",
exOut: "junk {BLAH} blah",
exOk: false,
// we ignore the {} where it just exists within the string, as it doesnt look like a valid json log line
},
{
in: "junk {BLAH}",
exOut: "{BLAH}",
exOk: true,
comment: "where there is some preamble before a json like string, we trim the preamble.",
in: "junk {BLAH}",
exOut: "{BLAH}",
exOk: true,
// where there is some preamble before a json like string, we trim the preamble.
},
{
in: "junk {} something else",
exOut: "junk {} something else",
exOk: false,
comment: "we ignore the {} where it just exists within the string, as it doesnt look like a valid json log line",
in: "junk {} something else",
exOut: "junk {} something else",
exOk: false,
// we ignore the {} where it just exists within the string, as it doesnt look like a valid json log line
},
}
for _, tc := range testCases {
Expand Down