forked from icco/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stacktrace.go
48 lines (40 loc) · 985 Bytes
/
stacktrace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package logadapter
import (
"bytes"
"errors"
"fmt"
"runtime"
"strings"
pkgErrors "github.com/pkg/errors"
)
type stackTracer interface {
StackTrace() pkgErrors.StackTrace
}
// extractStack pulls a call stack extracted from an error with GCP Error Reporting
// see: https://github.com/googleapis/google-cloud-go/issues/1084
func extractStackFromError(err error) []byte {
if err == nil {
return nil
}
var st stackTracer
if !errors.As(err, &st) {
return nil
}
buf := bytes.Buffer{}
// hardcode, I haven't access to this on the call stack
buf.WriteString(fmt.Sprintf("%s\ngoroutine 1 [running]:\n", err.Error()))
var lines []string
for _, frame := range st.StackTrace() {
pc := uintptr(frame) - 1
fn := runtime.FuncForPC(pc)
if fn != nil {
file, line := fn.FileLine(pc)
lines = append(
lines,
fmt.Sprintf("%s()\n\t%s:%d +%#x", fn.Name(), file, line, fn.Entry()),
)
}
}
buf.WriteString(strings.Join(lines, "\n"))
return buf.Bytes()
}