Skip to content

Commit 7d81ffd

Browse files
authored
Parse basic logging severity text values (#446)
* Parse basic logging severity text values * refactor from feedback
1 parent f69b19f commit 7d81ffd

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

exporter/collector/logs.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,39 @@ var severityMapping = []logging.Severity{
7878
logging.Emergency, // 24 -> Emergency
7979
}
8080

81+
// otelSeverityForText maps the generic aliases of SeverityTexts to SeverityNumbers.
82+
// This can be useful if SeverityText is manually set to one of the values from the data
83+
// model in a way that doesn't automatically parse the SeverityNumber as well
84+
// (see https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/issues/442)
85+
// Otherwise, this is the mapping that is automatically used by the Stanza log severity parser
86+
// (https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.54.0/pkg/stanza/operator/helper/severity_builder.go#L34-L57)
87+
var otelSeverityForText = map[string]plog.SeverityNumber{
88+
"trace": plog.SeverityNumberTRACE,
89+
"trace2": plog.SeverityNumberTRACE2,
90+
"trace3": plog.SeverityNumberTRACE3,
91+
"trace4": plog.SeverityNumberTRACE4,
92+
"debug": plog.SeverityNumberDEBUG,
93+
"debug2": plog.SeverityNumberDEBUG2,
94+
"debug3": plog.SeverityNumberDEBUG3,
95+
"debug4": plog.SeverityNumberDEBUG4,
96+
"info": plog.SeverityNumberINFO,
97+
"info2": plog.SeverityNumberINFO2,
98+
"info3": plog.SeverityNumberINFO3,
99+
"info4": plog.SeverityNumberINFO4,
100+
"warn": plog.SeverityNumberWARN,
101+
"warn2": plog.SeverityNumberWARN2,
102+
"warn3": plog.SeverityNumberWARN3,
103+
"warn4": plog.SeverityNumberWARN4,
104+
"error": plog.SeverityNumberERROR,
105+
"error2": plog.SeverityNumberERROR2,
106+
"error3": plog.SeverityNumberERROR3,
107+
"error4": plog.SeverityNumberERROR4,
108+
"fatal": plog.SeverityNumberFATAL,
109+
"fatal2": plog.SeverityNumberFATAL2,
110+
"fatal3": plog.SeverityNumberFATAL3,
111+
"fatal4": plog.SeverityNumberFATAL4,
112+
}
113+
81114
type LogsExporter struct {
82115
obs selfObservability
83116
loggingClient *loggingv2.Client
@@ -349,7 +382,18 @@ func (l logMapper) logToSplitEntries(
349382
if log.SeverityNumber() < 0 || int(log.SeverityNumber()) > len(severityMapping)-1 {
350383
return []logging.Entry{entry}, fmt.Errorf("Unknown SeverityNumber %v", log.SeverityNumber())
351384
}
352-
entry.Severity = severityMapping[log.SeverityNumber()]
385+
severityNumber := log.SeverityNumber()
386+
// Log severity levels are based on numerical values defined by Otel/GCP, which are informally mapped to generic text values such as "ALERT", "DEBUG", etc.
387+
// In some cases, a SeverityText value can be automatically mapped to a matching SeverityNumber.
388+
// If not (for example, when directly setting the SeverityText on a Log entry with the Transform processor), then the
389+
// SeverityText might be something like "ALERT" while the SeverityNumber is still "0".
390+
// In this case, we will attempt to map the text ourselves to one of the defined Otel SeverityNumbers.
391+
// We do this by checking that the SeverityText is NOT "default" (ie, it exists in our map) and that the SeverityNumber IS "0".
392+
// (This also excludes other unknown/custom severity text values, which may have user-defined mappings in the collector)
393+
if severityForText, ok := otelSeverityForText[strings.ToLower(log.SeverityText())]; ok && severityNumber == 0 {
394+
severityNumber = severityForText
395+
}
396+
entry.Severity = severityMapping[severityNumber]
353397

354398
if entry.Labels == nil &&
355399
(len(instrumentationSource) > 0 ||

exporter/collector/logs_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ func TestLogMapping(t *testing.T) {
290290
maxEntrySize: defaultMaxEntrySize,
291291
expectError: true,
292292
},
293+
{
294+
name: "log with severity text",
295+
mr: func() *monitoredres.MonitoredResource {
296+
return nil
297+
},
298+
log: func() plog.LogRecord {
299+
log := plog.NewLogRecord()
300+
log.SetSeverityText("fatal3")
301+
return log
302+
},
303+
expectedEntries: []logging.Entry{
304+
{
305+
Timestamp: testObservedTime,
306+
Severity: logging.Alert,
307+
},
308+
},
309+
maxEntrySize: defaultMaxEntrySize,
310+
},
293311
}
294312

295313
for _, testCase := range testCases {

0 commit comments

Comments
 (0)