-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
errorreporting: Stack incompatible with pkg/errors #1084
Comments
Hello! The problem here is probably
Does your stack look something like that? Could you post what you're seeing? |
Hi Jean! Thanks for the quick reply! When I run this small program: package main
import (
"fmt"
"github.com/pkg/errors"
)
type StackTracer interface {
StackTrace() errors.StackTrace
}
func main() {
stackTrace := fmt.Sprintf("%+v", errors.New("hello there").(StackTracer).StackTrace())
fmt.Print("Start ---> #####")
fmt.Print(stackTrace)
fmt.Print("##### <---- End")
} I get the following output:
|
I assume that the error I'm seeing is a validation error from the gRPC service called by the error reporting client. The format of the https://github.com/pkg/errors stack trace is indeed slightly different than the default stack trace as formatted by Would it be possible to document the expected format of the stack trace so that users of pkg/errors can mangle it before reporting? Alternatively, if possible, it would of course be very convenient if the error reporting service understood stack traces on the format emitted by pkg/errors. 😉 |
Hi @odsod - I totally missed the title where it mentioned |
Thank you @jadekler for looking into this! |
This is being tracked by internal bug 112529621. |
Thanks for the update @jba! |
@jadekler Any updates on this? I'm also facing the same issue |
I've just followed up again on the bug. No status updates at the moment, unfortunately - apologies. |
There are (more than?) a handful of issues on the Checking for the stacktrace interface introduces a dependency, certainly, but if this package wants to integrate, perhaps that's one way to go. |
You may be able to use something like that, yes. However, parts of the service are unfortunately pretty tied to the parser's logic, so you'd have to find a parser that fits what the expectation is. I've dug some this morning but didn't come up with anything great; I'll keep the conversation going with that team. |
We hacked together a formatter that Stackdriver seems happy with. Some frame parts (routine id, routine status, func args) are either hard-coded or omitted, but the important parts work. Note that package microservice
import (
"bytes"
"fmt"
"runtime"
"strings"
"cloud.google.com/go/errorreporting"
"github.com/pkg/errors"
)
type causer interface {
Cause() error
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
// ReportError logs the given errorreporting entry to Stackdriver
func ReportError(entry errorreporting.Entry) {
if ErrorReportingClient != nil {
if len(entry.Stack) == 0 {
// attempt to add a formatted stack based on the error
entry.Stack = FormatStack(entry.Error)
}
ErrorReportingClient.Report(entry)
}
}
// FormatStack is a best attempt at recreating the output of runtime.Stack
// Stackdriver currently only supports the stack output from runtime.Stack
// Tracking this issue for pkg/errors support: https://github.com/googleapis/google-cloud-go/issues/1084
func FormatStack(err error) (buffer []byte) {
if err == nil {
return
}
// find the inner most error with a stack
inner := err
for inner != nil {
if cause, ok := inner.(causer); ok {
inner = cause.Cause()
if _, ok := inner.(stackTracer); ok {
err = inner
}
} else {
break
}
}
if stackTrace, ok := err.(stackTracer); ok {
buf := bytes.Buffer{}
// routine id and state aren't available in pure go, so we hard-coded these
buf.WriteString(fmt.Sprintf("%s\ngoroutine 1 [running]:\n", err.Error()))
// format each frame of the stack to match runtime.Stack's format
var lines []string
for _, frame := range stackTrace.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"))
buffer = buf.Bytes()
}
return
} |
I am getting the same error with go-errors, which claims to use the same format as runtime/debug.Stack() |
Cool @cmoad! Wanna spin that code in #1084 (comment) into a package? Kindly pinging the various parties to checkout and perhaps use @cmoad's work in #1084 (comment) but also to follow-up with the internal bug filing @jba as per #1084 (comment) |
I made a package that can be used to create and format stacktraces that work for gcp. We use it for our logger in all of our projects and it gets the job done. |
@odeke-em I released an error handler library for Stackdriver that supports pkg/errors stack traces (based on the above code to format the stack trace): |
Thanks everyone! Great to see your converters @kamronbatman @sagikazarmark -- so perhaps we can link folks to your packages. I've spent sometime this evening examining this issue and also thinking type StackSourceLocationer interface {
Stack() []byte
SourceLocation() (filepath string, lineno int32, functionName string)
} that the various error packages and wrappers could use, but unfortunately that seems a little Thus, I have instead sent a CL which documents this expectation so the various If/when the product can consume Sorry that we couldn't do more but really the product expectation is what defines what we can do. |
@odeke-em what's the appropriate channel to communicate this to the stackdriver developers? I understand that this is not the place where this problem can be solved, but closing the issue without somehow following up with the them sounds...wrong. |
@sagikazarmark in deed, and that's why this issue has been open for 364 days (~1 year), the bug was filed internally at Google as per #1084 (comment). However, I've filed https://issuetracker.google.com/issues/138952283 which everyone can publicly track, please post up feedback and support on that issue. With that I am going to merge in the CL that documents this problem and as we've seen there at least 2 packages to help with this reformatting to runtime.Stack. Thank you all for the discourse and for using this package! |
Client
Error Reporting
Describe Your Environment
Go 1.10.1 on Ubuntu 18.04 (local development machine)
Use Case
Expected Behavior
Error gets reported successfully.
Actual Behavior
The text was updated successfully, but these errors were encountered: