2019-10-07 23:59:08 +00:00
|
|
|
package traceutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-09 13:53:20 +00:00
|
|
|
"errors"
|
2019-10-30 22:40:39 +00:00
|
|
|
"fmt"
|
2019-11-09 13:53:20 +00:00
|
|
|
"runtime"
|
2019-10-07 23:59:08 +00:00
|
|
|
"runtime/debug"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
|
|
|
|
// The context can be used to extract request scoped metadata and context values.
|
|
|
|
func RecoveryHandlerFunc(ctx context.Context, p interface{}) error {
|
|
|
|
span := trace.FromContext(ctx)
|
|
|
|
if span != nil {
|
|
|
|
span.AddAttributes(trace.StringAttribute("stack", string(debug.Stack())))
|
|
|
|
}
|
2019-11-09 13:53:20 +00:00
|
|
|
var err error
|
|
|
|
switch v := p.(type) {
|
|
|
|
case runtime.Error:
|
|
|
|
err = errors.New(v.Error())
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("%v", p)
|
|
|
|
}
|
|
|
|
|
2019-10-07 23:59:08 +00:00
|
|
|
logrus.WithError(err).WithField("stack", string(debug.Stack())).Error("gRPC panicked!")
|
|
|
|
return err
|
|
|
|
}
|