diff --git a/sdk/trace/span.go b/sdk/trace/span.go index 4945f508303..88a92869d2d 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -469,7 +469,14 @@ func (s *recordingSpan) RecordError(err error, opts ...trace.EventOption) { s.addEvent(semconv.ExceptionEventName, opts...) } +type exceptionTyper interface { + ExceptionType() string +} + func typeStr(i interface{}) string { + if i, ok := i.(exceptionTyper); ok { + return i.ExceptionType() + } t := reflect.TypeOf(i) if t.PkgPath() == "" && t.Name() == "" { // Likely a builtin type. diff --git a/sdk/trace/trace_test.go b/sdk/trace/trace_test.go index 87247d1f167..38628b244fe 100644 --- a/sdk/trace/trace_test.go +++ b/sdk/trace/trace_test.go @@ -1189,6 +1189,14 @@ func TestCustomStartEndTime(t *testing.T) { } } +type errWithExceptionType struct { + error +} + +func (errWithExceptionType) ExceptionType() string { + return "CustomExceptionType" +} + func TestRecordError(t *testing.T) { scenarios := []struct { err error @@ -1205,6 +1213,11 @@ func TestRecordError(t *testing.T) { typ: "*errors.errorString", msg: "test error 2", }, + { + err: errWithExceptionType{errors.New("test error 3")}, + typ: "CustomExceptionType", + msg: "test error 3", + }, } for _, s := range scenarios {