Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions processors/minsev/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ func (s *Severity) UnmarshalText(data []byte) error {
// If a fine-grained level of 0 is provided it is treaded as equivalent to the
// base severity level. For example, "INFO0" is equivalent to [SeverityInfo1].
func (s *Severity) parse(str string) (err error) {
if str == "" {
// Handle empty str as a special case and parse it as the default
// SeverityInfo1.
//
// Do not parse this below in the switch statement of the name. That
// will allow strings like "2", "-1", "2+1", "+3", etc. to be accepted
// and that adds ambiguity. For example, a user may expect that "2" is
// parsed as SeverityInfo2 based on an implied "SeverityInfo1" prifix,
// but they may also expect it be parsed as SeverityInfo3 which has a
// numeric value of 2. Avoide this ambiguity by treating those inputs
// as invalid, and only accept the empty string as a special case.

*s = SeverityInfo1 // Default severity.
return nil
}

defer func() {
if err != nil {
err = fmt.Errorf("minsev: severity string %q: %w", str, err)
Expand Down
7 changes: 7 additions & 0 deletions processors/minsev/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ var validDecodingTests = []struct {
{"SeverityFatal3", SeverityFatal3, "FATAL3"},
{"SeverityFatal4", SeverityFatal4, "FATAL4"},

// Use the default SeverityInfo for an empty name.
{"Default", SeverityInfo, ""},

// Test case insensitivity.
{"SeverityTraceLower", SeverityTrace1, "trace"},
{"SeverityDebugMixed", SeverityDebug1, "Debug"},
Expand Down Expand Up @@ -148,6 +151,10 @@ var invalidText = []string{
"INFO+abc",
"ERROR-xyz",
"not-a-level",
"+1",
"2",
"2+1",
"-1",
}

func TestSeverityString(t *testing.T) {
Expand Down
Loading