Skip to content

Commit

Permalink
otellogrus: Add more appropriate level mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Oct 10, 2024
1 parent d545e6f commit a3779ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
37 changes: 29 additions & 8 deletions bridges/otellogrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
// set.
// - Fields are transformed and set as the attributes.
//
// The Level is transformed by using the static offset to the OpenTelemetry
// Severity types. For example:
//
// The Level is transformed to the OpenTelemetry Severity types as follows.
// - [logrus.TraceLevel] is transformed to [log.SeverityTrace]
// - [logrus.DebugLevel] is transformed to [log.SeverityDebug]
// - [logrus.InfoLevel] is transformed to [log.SeverityTrace4]
// - [logrus.WarnLevel] is transformed to [log.SeverityTrace3]
// - [logrus.ErrorLevel] is transformed to [log.SeverityTrace2]
// - [logrus.InfoLevel] is transformed to [log.SeverityInfo]
// - [logrus.WarnLevel] is transformed to [log.SeverityWarn]
// - [logrus.ErrorLevel] is transformed to [log.SeverityError]
// - [logrus.FatalLevel] is transformed to [log.SeverityFatal1]
// - [logrus.PanicLevel] is transformed to [log.SeverityFatal2]
//
// Field values are transformed based on their type into log attributes, or
// into a string value if there is no matching type.
Expand Down Expand Up @@ -165,13 +166,33 @@ func (h *Hook) convertEntry(e *logrus.Entry) log.Record {
record.SetTimestamp(e.Time)
record.SetBody(log.StringValue(e.Message))

const sevOffset = logrus.Level(log.SeverityDebug) - logrus.DebugLevel
record.SetSeverity(log.Severity(e.Level + sevOffset))
record.SetSeverity(convertLevel(e.Level))
record.AddAttributes(convertFields(e.Data)...)

return record
}

func convertLevel(level logrus.Level) log.Severity {
switch level {
case logrus.TraceLevel:
return log.SeverityTrace
case logrus.DebugLevel:
return log.SeverityDebug
case logrus.InfoLevel:
return log.SeverityInfo
case logrus.WarnLevel:
return log.SeverityWarn
case logrus.ErrorLevel:
return log.SeverityError

Check warning on line 186 in bridges/otellogrus/hook.go

View check run for this annotation

Codecov / codecov/patch

bridges/otellogrus/hook.go#L177-L186

Added lines #L177 - L186 were not covered by tests
case logrus.FatalLevel:
return log.SeverityFatal1
case logrus.PanicLevel:
return log.SeverityFatal2
default:
return log.SeverityUndefined

Check warning on line 192 in bridges/otellogrus/hook.go

View check run for this annotation

Codecov / codecov/patch

bridges/otellogrus/hook.go#L191-L192

Added lines #L191 - L192 were not covered by tests
}
}

func convertFields(fields logrus.Fields) []log.KeyValue {
kvs := make([]log.KeyValue, 0, len(fields))
for k, v := range fields {
Expand Down
8 changes: 4 additions & 4 deletions bridges/otellogrus/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestHookFire(t *testing.T) {

wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue(""), time.Time{}, 0, nil),
buildRecord(log.StringValue(""), time.Time{}, log.SeverityFatal2, nil),
},
},
},
Expand All @@ -173,7 +173,7 @@ func TestHookFire(t *testing.T) {
},
wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue(""), now, 0, nil),
buildRecord(log.StringValue(""), now, log.SeverityFatal2, nil),
},
},
},
Expand All @@ -184,7 +184,7 @@ func TestHookFire(t *testing.T) {
},
wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue(""), time.Time{}, log.SeverityTrace1, nil),
buildRecord(log.StringValue(""), time.Time{}, log.SeverityFatal1, nil),
},
},
},
Expand All @@ -197,7 +197,7 @@ func TestHookFire(t *testing.T) {
},
wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue(""), time.Time{}, 0, []log.KeyValue{
buildRecord(log.StringValue(""), time.Time{}, log.SeverityFatal2, []log.KeyValue{
log.String("hello", "world"),
}),
},
Expand Down

0 comments on commit a3779ab

Please sign in to comment.