Skip to content

Commit

Permalink
Otel levels sev mapping (open-telemetry#225)
Browse files Browse the repository at this point in the history
* Allow otel log levels to be used as keys in a severity mapping

* Updated severity docs to reflect newly allowed mapping keys
  • Loading branch information
djaglowski authored Jul 21, 2021
1 parent 319fab1 commit 038b640
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
36 changes: 36 additions & 0 deletions docs/types/severity.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@ The following named severity levels are supported.
| --- | --- | --- |
| Default | 0 | `default` |
| Trace | 10 | `trace` |
| Trace2 | 12 | `trace2` |
| Trace3 | 13 | `trace3` |
| Trace4 | 14 | `trace4` |
| Debug | 20 | `debug` |
| Debug2 | 22 | `debug2` |
| Debug3 | 23 | `debug3` |
| Debug4 | 24 | `debug4` |
| Info | 30 | `info` |
| Info2 | 32 | `info2` |
| Info3 | 33 | `info3` |
| Info4 | 34 | `info4` |
| Notice | 40 | `notice` |
| Warning | 50 | `warning` |
| Warning2 | 52 | `warning2` |
| Warning3 | 53 | `warning3` |
| Warning4 | 54 | `warning4` |
| Error | 60 | `error` |
| Error2 | 62 | `error2` |
| Error3 | 63 | `error3` |
| Error4 | 64 | `error4` |
| Critical | 70 | `critical` |
| Alert | 80 | `alert` |
| Emergency | 90 | `emergency` |
| Emergency2 | 92 | `emergency2` |
| Emergency3 | 93 | `emergency3` |
| Emergency4 | 94 | `emergency4` |
| Catastrophe | 100 | `catastrophe` |


Expand Down Expand Up @@ -102,21 +120,39 @@ The following configurations are equivalent:
preset: none
mapping:
trace: trace
trace2: trace2
trace3: trace3
trace4: trace4
debug: debug
debug2: debug2
debug3: debug3
debug4: debug4
info: info
info2: info2
info3: info3
info4: info4
notice: notice
warning:
- warning
- warn
warning2: warning2
warning3: warning3
warning4: warning4
error:
- error
- err
- 404
error2: error2
error3: error3
error4: error4
critical:
- critical
- crit
alert: alert
emergency: emergency
emergency2: emergency2
emergency3: emergency3
emergency4: emergency4
catastrophe: catastrophe
```

Expand Down
50 changes: 28 additions & 22 deletions operator/helper/severity_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,53 @@ func getBuiltinMapping(name string) severityMap {
return map[string]entry.Severity{
"default": entry.Default,
"trace": entry.Trace,
"trace2": entry.Trace2,
"trace3": entry.Trace3,
"trace4": entry.Trace4,
"debug": entry.Debug,
"debug2": entry.Debug2,
"debug3": entry.Debug3,
"debug4": entry.Debug4,
"info": entry.Info,
"info2": entry.Info2,
"info3": entry.Info3,
"info4": entry.Info4,
"notice": entry.Notice,
"warning": entry.Warning,
"warning2": entry.Warning2,
"warning3": entry.Warning3,
"warning4": entry.Warning4,
"error": entry.Error,
"error2": entry.Error2,
"error3": entry.Error3,
"error4": entry.Error4,
"critical": entry.Critical,
"alert": entry.Alert,
"emergency": entry.Emergency,
"emergency2": entry.Emergency2,
"emergency3": entry.Emergency3,
"emergency4": entry.Emergency4,
"catastrophe": entry.Catastrophe,
}
default:
// Add some additional values that are automatically recognized
mapping := getBuiltinMapping("aliases")

mapping.add(entry.Trace2, "trace2")
mapping.add(entry.Trace3, "trace3")
mapping.add(entry.Trace4, "trace4")

mapping.add(entry.Debug2, "debug2")
mapping.add(entry.Debug3, "debug3")
mapping.add(entry.Debug4, "debug4")

mapping.add(entry.Info2, "info2")
mapping.add(entry.Info3, "info3")
mapping.add(entry.Info4, "info4")

mapping.add(entry.Warning, "warn")
mapping.add(entry.Warning2, "warning2", "warn2")
mapping.add(entry.Warning3, "warning3", "warn3")
mapping.add(entry.Warning4, "warning4", "warn4")
mapping.add(entry.Warning2, "warn2")
mapping.add(entry.Warning3, "warn3")
mapping.add(entry.Warning4, "warn4")

mapping.add(entry.Error, "err")
mapping.add(entry.Error2, "error2")
mapping.add(entry.Error3, "error3")
mapping.add(entry.Error4, "error4")
mapping.add(entry.Error2, "err2")
mapping.add(entry.Error3, "err3")
mapping.add(entry.Error4, "err4")

mapping.add(entry.Critical, "crit")

mapping.add(entry.Emergency, "fatal")
mapping.add(entry.Emergency2, "emergency2", "fatal2")
mapping.add(entry.Emergency3, "emergency3", "fatal3")
mapping.add(entry.Emergency4, "emergency4", "fatal4")
mapping.add(entry.Emergency2, "fatal2")
mapping.add(entry.Emergency3, "fatal3")
mapping.add(entry.Emergency4, "fatal4")

return mapping
}
Expand Down
54 changes: 54 additions & 0 deletions operator/helper/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,59 @@ type severityTestCase struct {
expected entry.Severity
}

// These tests ensure that users may build a mapping that
// maps values into any of the predefined keys.
// For example, this ensures that users can do this:
// mapping:
// warning3: warn_three
func validMappingKeyCases() []severityTestCase {
aliasedMapping := map[string]entry.Severity{
"trace": entry.Trace,
"trace2": entry.Trace2,
"trace3": entry.Trace3,
"trace4": entry.Trace4,
"debug": entry.Debug,
"debug2": entry.Debug2,
"debug3": entry.Debug3,
"debug4": entry.Debug4,
"info": entry.Info,
"info2": entry.Info2,
"info3": entry.Info3,
"info4": entry.Info4,
"notice": entry.Notice,
"warning": entry.Warning,
"warning2": entry.Warning2,
"warning3": entry.Warning3,
"warning4": entry.Warning4,
"error": entry.Error,
"error2": entry.Error2,
"error3": entry.Error3,
"error4": entry.Error4,
"critical": entry.Critical,
"alert": entry.Alert,
"emergency": entry.Emergency,
"emergency2": entry.Emergency2,
"emergency3": entry.Emergency3,
"emergency4": entry.Emergency4,
"catastrophe": entry.Catastrophe,
}

cases := []severityTestCase{}
for k, v := range aliasedMapping {
cases = append(cases,
severityTestCase{
name: k,
sample: "my_custom_value",
mapping: map[interface{}]interface{}{k: "my_custom_value"},
expected: v,
})
}

return cases
}

// These cases ensure that text representing OTLP severity
// levels are automatically recognized by the default mapping.
func otlpSevCases() []severityTestCase {
mustParse := map[string]entry.Severity{
"tRaCe": entry.Trace,
Expand Down Expand Up @@ -359,6 +412,7 @@ func TestSeverityParser(t *testing.T) {
}

testCases = append(testCases, otlpSevCases()...)
testCases = append(testCases, validMappingKeyCases()...)

rootField := entry.NewBodyField()
someField := entry.NewBodyField("some_field")
Expand Down

0 comments on commit 038b640

Please sign in to comment.