From 2793d7dc2707c0d3ba55b74cda92bd3633ccebde Mon Sep 17 00:00:00 2001 From: Daniel Jaglowski Date: Mon, 22 Aug 2022 10:29:24 -0400 Subject: [PATCH] [pkg/stanza] Fix bug where original severity text was not preserved (#13265) When converting from entry.Entry to plog.Logs, the severity text was not copied over. Instead, it was automatically assigned based on the severity number. This change does two things: 1. If the severity text is not empty, it is preserved 2. If the severity text is empty, it is assigned based on the severity number. However, the values assigned were also incorrect, in that they did not match the case defined in the data model. Previously, a capitalized style (eg "Info") was used. Now, an all caps style is used. It is rare that severity number is specified while severity text is not, but the data model explicitly calls for this handling, so it is implemented as such. --- pkg/stanza/adapter/converter.go | 56 +++++++------ pkg/stanza/adapter/converter_test.go | 107 ++++++++++++++++++------ unreleased/pkg-stanza-sevtext-conv.yaml | 16 ++++ 3 files changed, 127 insertions(+), 52 deletions(-) create mode 100755 unreleased/pkg-stanza-sevtext-conv.yaml diff --git a/pkg/stanza/adapter/converter.go b/pkg/stanza/adapter/converter.go index 8d49bf9165a5..378f04b0dd83 100644 --- a/pkg/stanza/adapter/converter.go +++ b/pkg/stanza/adapter/converter.go @@ -338,7 +338,11 @@ func convertInto(ent *entry.Entry, dest plog.LogRecord) { } dest.SetObservedTimestamp(pcommon.NewTimestampFromTime(ent.ObservedTimestamp)) dest.SetSeverityNumber(sevMap[ent.Severity]) - dest.SetSeverityText(sevTextMap[ent.Severity]) + if ent.SeverityText == "" { + dest.SetSeverityText(defaultSevTextMap[ent.Severity]) + } else { + dest.SetSeverityText(ent.SeverityText) + } insertToAttributeMap(ent.Attributes, dest.Attributes()) insertToAttributeVal(ent.Body, dest.Body()) @@ -510,32 +514,32 @@ var sevMap = map[entry.Severity]plog.SeverityNumber{ entry.Fatal4: plog.SeverityNumberFATAL4, } -var sevTextMap = map[entry.Severity]string{ +var defaultSevTextMap = map[entry.Severity]string{ entry.Default: "", - entry.Trace: "Trace", - entry.Trace2: "Trace2", - entry.Trace3: "Trace3", - entry.Trace4: "Trace4", - entry.Debug: "Debug", - entry.Debug2: "Debug2", - entry.Debug3: "Debug3", - entry.Debug4: "Debug4", - entry.Info: "Info", - entry.Info2: "Info2", - entry.Info3: "Info3", - entry.Info4: "Info4", - entry.Warn: "Warn", - entry.Warn2: "Warn2", - entry.Warn3: "Warn3", - entry.Warn4: "Warn4", - entry.Error: "Error", - entry.Error2: "Error2", - entry.Error3: "Error3", - entry.Error4: "Error4", - entry.Fatal: "Fatal", - entry.Fatal2: "Fatal2", - entry.Fatal3: "Fatal3", - entry.Fatal4: "Fatal4", + entry.Trace: "TRACE", + entry.Trace2: "TRACE2", + entry.Trace3: "TRACE3", + entry.Trace4: "TRACE4", + entry.Debug: "DEBUG", + entry.Debug2: "DEBUG2", + entry.Debug3: "DEBUG3", + entry.Debug4: "DEBUG4", + entry.Info: "INFO", + entry.Info2: "INFO2", + entry.Info3: "INFO3", + entry.Info4: "INFO4", + entry.Warn: "WARN", + entry.Warn2: "WARN2", + entry.Warn3: "WARN3", + entry.Warn4: "WARN4", + entry.Error: "ERROR", + entry.Error2: "ERROR2", + entry.Error3: "ERROR3", + entry.Error4: "ERROR4", + entry.Fatal: "FATAL", + entry.Fatal2: "FATAL2", + entry.Fatal3: "FATAL3", + entry.Fatal4: "FATAL4", } // pairSep is chosen to be an invalid byte for a utf-8 sequence diff --git a/pkg/stanza/adapter/converter_test.go b/pkg/stanza/adapter/converter_test.go index ded2970fa0af..220625e44d69 100644 --- a/pkg/stanza/adapter/converter_test.go +++ b/pkg/stanza/adapter/converter_test.go @@ -154,6 +154,7 @@ func TestConvert(t *testing.T) { ent := func() *entry.Entry { e := entry.New() e.Severity = entry.Error + e.SeverityText = "E" e.Resource = map[string]interface{}{ "bool": true, "int": 123, @@ -201,7 +202,7 @@ func TestConvert(t *testing.T) { lr := logs.At(0) assert.Equal(t, plog.SeverityNumberERROR, lr.SeverityNumber()) - assert.Equal(t, "Error", lr.SeverityText()) + assert.Equal(t, "E", lr.SeverityText()) if atts := lr.Attributes(); assert.Equal(t, 5, atts.Len()) { m := pcommon.NewMap() @@ -755,40 +756,94 @@ func convertAndDrill(entry *entry.Entry) plog.LogRecord { func TestConvertSeverity(t *testing.T) { cases := []struct { severity entry.Severity + severityText string expectedNumber plog.SeverityNumber expectedText string }{ - {entry.Default, plog.SeverityNumberUNDEFINED, ""}, - {entry.Trace, plog.SeverityNumberTRACE, "Trace"}, - {entry.Trace2, plog.SeverityNumberTRACE2, "Trace2"}, - {entry.Trace3, plog.SeverityNumberTRACE3, "Trace3"}, - {entry.Trace4, plog.SeverityNumberTRACE4, "Trace4"}, - {entry.Debug, plog.SeverityNumberDEBUG, "Debug"}, - {entry.Debug2, plog.SeverityNumberDEBUG2, "Debug2"}, - {entry.Debug3, plog.SeverityNumberDEBUG3, "Debug3"}, - {entry.Debug4, plog.SeverityNumberDEBUG4, "Debug4"}, - {entry.Info, plog.SeverityNumberINFO, "Info"}, - {entry.Info2, plog.SeverityNumberINFO2, "Info2"}, - {entry.Info3, plog.SeverityNumberINFO3, "Info3"}, - {entry.Info4, plog.SeverityNumberINFO4, "Info4"}, - {entry.Warn, plog.SeverityNumberWARN, "Warn"}, - {entry.Warn2, plog.SeverityNumberWARN2, "Warn2"}, - {entry.Warn3, plog.SeverityNumberWARN3, "Warn3"}, - {entry.Warn4, plog.SeverityNumberWARN4, "Warn4"}, - {entry.Error, plog.SeverityNumberERROR, "Error"}, - {entry.Error2, plog.SeverityNumberERROR2, "Error2"}, - {entry.Error3, plog.SeverityNumberERROR3, "Error3"}, - {entry.Error4, plog.SeverityNumberERROR4, "Error4"}, - {entry.Fatal, plog.SeverityNumberFATAL, "Fatal"}, - {entry.Fatal2, plog.SeverityNumberFATAL2, "Fatal2"}, - {entry.Fatal3, plog.SeverityNumberFATAL3, "Fatal3"}, - {entry.Fatal4, plog.SeverityNumberFATAL4, "Fatal4"}, + {entry.Default, "", plog.SeverityNumberUNDEFINED, ""}, + {entry.Trace, "Trace", plog.SeverityNumberTRACE, "Trace"}, + {entry.Trace2, "Trace2", plog.SeverityNumberTRACE2, "Trace2"}, + {entry.Trace3, "Trace3", plog.SeverityNumberTRACE3, "Trace3"}, + {entry.Trace4, "Trace4", plog.SeverityNumberTRACE4, "Trace4"}, + {entry.Debug, "Debug", plog.SeverityNumberDEBUG, "Debug"}, + {entry.Debug2, "Debug2", plog.SeverityNumberDEBUG2, "Debug2"}, + {entry.Debug3, "Debug3", plog.SeverityNumberDEBUG3, "Debug3"}, + {entry.Debug4, "Debug4", plog.SeverityNumberDEBUG4, "Debug4"}, + {entry.Info, "Info", plog.SeverityNumberINFO, "Info"}, + {entry.Info2, "Info2", plog.SeverityNumberINFO2, "Info2"}, + {entry.Info3, "Info3", plog.SeverityNumberINFO3, "Info3"}, + {entry.Info4, "Info4", plog.SeverityNumberINFO4, "Info4"}, + {entry.Warn, "Warn", plog.SeverityNumberWARN, "Warn"}, + {entry.Warn2, "Warn2", plog.SeverityNumberWARN2, "Warn2"}, + {entry.Warn3, "Warn3", plog.SeverityNumberWARN3, "Warn3"}, + {entry.Warn4, "Warn4", plog.SeverityNumberWARN4, "Warn4"}, + {entry.Error, "Error", plog.SeverityNumberERROR, "Error"}, + {entry.Error2, "Error2", plog.SeverityNumberERROR2, "Error2"}, + {entry.Error3, "Error3", plog.SeverityNumberERROR3, "Error3"}, + {entry.Error4, "Error4", plog.SeverityNumberERROR4, "Error4"}, + {entry.Fatal, "Fatal", plog.SeverityNumberFATAL, "Fatal"}, + {entry.Fatal2, "Fatal2", plog.SeverityNumberFATAL2, "Fatal2"}, + {entry.Fatal3, "Fatal3", plog.SeverityNumberFATAL3, "Fatal3"}, + {entry.Fatal4, "Fatal4", plog.SeverityNumberFATAL4, "Fatal4"}, + + // Original severity text should be preserved if present + {entry.Trace, "other", plog.SeverityNumberTRACE, "other"}, + {entry.Trace2, "other", plog.SeverityNumberTRACE2, "other"}, + {entry.Trace3, "other", plog.SeverityNumberTRACE3, "other"}, + {entry.Trace4, "other", plog.SeverityNumberTRACE4, "other"}, + {entry.Debug, "other", plog.SeverityNumberDEBUG, "other"}, + {entry.Debug2, "other", plog.SeverityNumberDEBUG2, "other"}, + {entry.Debug3, "other", plog.SeverityNumberDEBUG3, "other"}, + {entry.Debug4, "other", plog.SeverityNumberDEBUG4, "other"}, + {entry.Info, "other", plog.SeverityNumberINFO, "other"}, + {entry.Info2, "other", plog.SeverityNumberINFO2, "other"}, + {entry.Info3, "other", plog.SeverityNumberINFO3, "other"}, + {entry.Info4, "other", plog.SeverityNumberINFO4, "other"}, + {entry.Warn, "other", plog.SeverityNumberWARN, "other"}, + {entry.Warn2, "other", plog.SeverityNumberWARN2, "other"}, + {entry.Warn3, "other", plog.SeverityNumberWARN3, "other"}, + {entry.Warn4, "other", plog.SeverityNumberWARN4, "other"}, + {entry.Error, "other", plog.SeverityNumberERROR, "other"}, + {entry.Error2, "other", plog.SeverityNumberERROR2, "other"}, + {entry.Error3, "other", plog.SeverityNumberERROR3, "other"}, + {entry.Error4, "other", plog.SeverityNumberERROR4, "other"}, + {entry.Fatal, "other", plog.SeverityNumberFATAL, "other"}, + {entry.Fatal2, "other", plog.SeverityNumberFATAL2, "other"}, + {entry.Fatal3, "other", plog.SeverityNumberFATAL3, "other"}, + {entry.Fatal4, "other", plog.SeverityNumberFATAL4, "other"}, + + // Sev text should be set to severity "Short Name" if not present + {entry.Trace, "", plog.SeverityNumberTRACE, "TRACE"}, + {entry.Trace2, "", plog.SeverityNumberTRACE2, "TRACE2"}, + {entry.Trace3, "", plog.SeverityNumberTRACE3, "TRACE3"}, + {entry.Trace4, "", plog.SeverityNumberTRACE4, "TRACE4"}, + {entry.Debug, "", plog.SeverityNumberDEBUG, "DEBUG"}, + {entry.Debug2, "", plog.SeverityNumberDEBUG2, "DEBUG2"}, + {entry.Debug3, "", plog.SeverityNumberDEBUG3, "DEBUG3"}, + {entry.Debug4, "", plog.SeverityNumberDEBUG4, "DEBUG4"}, + {entry.Info, "", plog.SeverityNumberINFO, "INFO"}, + {entry.Info2, "", plog.SeverityNumberINFO2, "INFO2"}, + {entry.Info3, "", plog.SeverityNumberINFO3, "INFO3"}, + {entry.Info4, "", plog.SeverityNumberINFO4, "INFO4"}, + {entry.Warn, "", plog.SeverityNumberWARN, "WARN"}, + {entry.Warn2, "", plog.SeverityNumberWARN2, "WARN2"}, + {entry.Warn3, "", plog.SeverityNumberWARN3, "WARN3"}, + {entry.Warn4, "", plog.SeverityNumberWARN4, "WARN4"}, + {entry.Error, "", plog.SeverityNumberERROR, "ERROR"}, + {entry.Error2, "", plog.SeverityNumberERROR2, "ERROR2"}, + {entry.Error3, "", plog.SeverityNumberERROR3, "ERROR3"}, + {entry.Error4, "", plog.SeverityNumberERROR4, "ERROR4"}, + {entry.Fatal, "", plog.SeverityNumberFATAL, "FATAL"}, + {entry.Fatal2, "", plog.SeverityNumberFATAL2, "FATAL2"}, + {entry.Fatal3, "", plog.SeverityNumberFATAL3, "FATAL3"}, + {entry.Fatal4, "", plog.SeverityNumberFATAL4, "FATAL4"}, } for _, tc := range cases { t.Run(fmt.Sprintf("%v", tc.severity), func(t *testing.T) { entry := entry.New() entry.Severity = tc.severity + entry.SeverityText = tc.severityText log := convertAndDrill(entry) require.Equal(t, tc.expectedNumber, log.SeverityNumber()) require.Equal(t, tc.expectedText, log.SeverityText()) diff --git a/unreleased/pkg-stanza-sevtext-conv.yaml b/unreleased/pkg-stanza-sevtext-conv.yaml new file mode 100755 index 000000000000..f9a0821e7cc1 --- /dev/null +++ b/unreleased/pkg-stanza-sevtext-conv.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: "`filelog`, `journald`, `syslog`, `tcplog`, `udplog`, and `windowseventlog` receivers" + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix a bug where original severity text was not preserved. + +# One or more tracking issues related to the change +issues: [13263] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: