-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an API for extracting details from errors #447
Comments
Something like this: diff --git a/error.go b/error.go
index 9ba0e4ec..a50a5fec 100644
--- a/error.go
+++ b/error.go
@@ -38,8 +38,20 @@ var (
runtimeFrameType = reflect.TypeOf(runtime.Frame{})
errorsStackTraceUintptr = uintptrType.ConvertibleTo(reflect.TypeOf(*new(errors.Frame)))
errorsStackTraceFrame = reflect.TypeOf(*new(errors.Frame)).ConvertibleTo(runtimeFrameType)
+ errorTypeWrappers = make(map[reflect.Type]func(error) error)
)
+// RegisterErrorWrapper registers function f with type t, which should
+// be the named type of an error. When errors of type t are presented to
+// the tracer, then f will be used to wrap the value in order to influence
+// how its details are extracted.
+//
+// This function must not be called concurrently with creation of Error
+// values; it is intended to be used at program initialisaton time.
+func RegisterErrorWrapper(t reflect.Type, f func(error) error) {
+ errorTypeWrappers[t] = f
+}
+
// Recovered creates an Error with t.NewError(err), where
// err is either v (if v implements error), or otherwise
// fmt.Errorf("%v", v). The value v is expected to have
@@ -380,6 +392,15 @@ func initException(e *exceptionData, err error) {
e.codeNumber = float64(uintptr(err))
default:
t := reflect.TypeOf(err)
+ if wrap, ok := errorTypeWrappers[t]; ok {
+ err = wrap(err)
+ }
+
+ // TODO(axw) define more interfaces which the error can
+ // implement to specify the package path and type name.
+ //
+ // Only do the below bits if those interfaces are not
+ // implemented.
if t.Name() == "" && t.Kind() == reflect.Ptr {
t = t.Elem()
}
@@ -406,6 +427,12 @@ func initException(e *exceptionData, err error) {
}:
e.codeNumber = err.Code()
}
+
+ if err, ok := err.(interface {
+ Attrs(func(string, interface{}))
+ }); ok {
+ err.Attrs(setAttr)
+ }
}
if errTemporary(err) {
setAttr("temporary", true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When an error value is translated to an apm.Error (i.e. via NewError, CaptureError, or Recovered), we currently extract details from various error types in the standard library:
apm-agent-go/error.go
Lines 358 to 415 in 525c5c4
Some libraries define their own error types, and contain interesting details that we should surface. For example, https://godoc.org/github.com/mongodb/mongo-go-driver/x/network/command#Error contains an error code, labels, and error name that we should ideally surface.
We should provide an API whereby instrumentation modules can register an error type and function to use to extract information from it, e.g. by wrapping the error such that it provides methods like "Code" and "Type".
The text was updated successfully, but these errors were encountered: