Skip to content

Commit

Permalink
fix(otellogrus): follow the otel spec on exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
relistan committed Apr 23, 2024
1 parent 31fd20c commit bda77f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion otellogrus/otellogrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (hook *Hook) Fire(entry *logrus.Entry) error {
return nil
}

eventName := "log"

attrs := make([]attribute.KeyValue, 0, len(entry.Data)+2+3)

attrs = append(attrs, logSeverityKey.String(levelString(entry.Level)))
Expand All @@ -79,14 +81,17 @@ func (hook *Hook) Fire(entry *logrus.Entry) error {
typ := reflect.TypeOf(err).String()
attrs = append(attrs, semconv.ExceptionTypeKey.String(typ))
attrs = append(attrs, semconv.ExceptionMessageKey.String(err.Error()))
// The spec requires that the event name be 'exception' in this case
// https://opentelemetry.io/docs/specs/otel/trace/exceptions/#attributes
eventName = "exception"
continue
}
}

attrs = append(attrs, otelutil.Attribute(k, v))
}

span.AddEvent("log", trace.WithAttributes(attrs...))
span.AddEvent(eventName, trace.WithAttributes(attrs...))

if entry.Level <= hook.errorStatusLevel {
span.SetStatus(codes.Error, entry.Message)
Expand Down
11 changes: 8 additions & 3 deletions otellogrus/otellogrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

type Test struct {
log func(context.Context)
require func(sdktrace.Event)
log func(context.Context)
eventName string
require func(sdktrace.Event)
}

func TestOtelLogrus(t *testing.T) {
Expand All @@ -25,6 +26,7 @@ func TestOtelLogrus(t *testing.T) {
log: func(ctx context.Context) {
logrus.WithContext(ctx).Info("hello")
},
eventName: "log",
require: func(event sdktrace.Event) {
m := attrMap(event.Attributes)

Expand All @@ -41,6 +43,7 @@ func TestOtelLogrus(t *testing.T) {
log: func(ctx context.Context) {
logrus.WithContext(ctx).WithField("foo", "bar").Warn("hello")
},
eventName: "log",
require: func(event sdktrace.Event) {
m := attrMap(event.Attributes)

Expand All @@ -62,6 +65,7 @@ func TestOtelLogrus(t *testing.T) {
err := errors.New("some error")
logrus.WithContext(ctx).WithError(err).Error("hello")
},
eventName: "exception",
require: func(event sdktrace.Event) {
m := attrMap(event.Attributes)

Expand All @@ -88,6 +92,7 @@ func TestOtelLogrus(t *testing.T) {
logrus.WithContext(ctx).Info("hello")
logrus.SetReportCaller(false)
},
eventName: "log",
require: func(event sdktrace.Event) {
m := attrMap(event.Attributes)

Expand Down Expand Up @@ -133,7 +138,7 @@ func TestOtelLogrus(t *testing.T) {
require.Equal(t, 1, len(events))

event := events[0]
require.Equal(t, "log", event.Name)
require.Equal(t, test.eventName, event.Name)
test.require(event)
})
}
Expand Down

0 comments on commit bda77f4

Please sign in to comment.