-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: errors: add (stack)trace at error annotation #60873
Comments
Note that you can already call a function that records a stack trace for an error, such as https://pkg.go.dev/github.com/go-errors/errors. Personally I think that if we want to add this kind of functionality into the standard library, we should do it using a different function, not using an annotation in the string passed to |
@ianlancetaylor I'm a aware of ways to add a full traceback and I writted about this, my english is not good. |
There are a number of third party packages with traces. The unique thing the standard library can do is to standardize how the traces are exposed. Maybe an interface in runtime like type ErrorTrace interface{
error
Trace() []Frames
} |
@flibustenet Thanks, I think I see what you are saying now. But I don't see why this has to be in the standard library. It can be a function defined in a separate package. |
@carlmjohnson my point was to embed only one Frame and not all the stack. |
I have errorx.Trace which adds a single trace line like this: func traceExample(ok bool) (err error) {
defer errorx.Trace(&err)
if !ok {
// returns "problem in github.com/carlmjohnson/errorx_test.traceExample (trace_example_test.go:13): oh no!"
return errors.New("oh no!")
}
return nil
} I think it's more interesting to think about what can the standard library do that user code cannot do. Otherwise, I can just use my own code. :-) |
@carlmjohnson fine to see similar ideas (I didn't search much). |
The original set of error proposals for 1.13 included adding stack frames to errors created with This proposal avoids these issues by making stack frames opt-in; you need to ask for one. The downside is that you don't get stack frames for errors unless the creator asks for them. Either this means most errors don't have stack frames, or we need to update the entire Go ecosystem to start asking for stack frames in errors. Updating the entire Go ecosystem seems infeasible. I don't know what the right approach to getting stacks into errors is. Clearly this would be useful; we dropped it from 1.13 because we couldn't make it work right, not because the goal was a bad idea. I think any successful proposal here will need to figure out how to get stacks into existing errors without rewriting the world, and without running into the issues the 1.13 implementation did. |
@ianlancetaylor you raise an important point. Why put this in the standard library when this can be written in third party code, especially when there are so many conflicting opinions on how error annotations should be handled? IMHO, the answer is that in some cases, having a single way to approach a problem in the standard library with a canonical interface is a great help to the go community. The go community has high trust in the standard library in terms of quality and the backwards compatibility guarantee. And for common needs it also removes one less third party dependency i need to pull in or have a discussion about with my team. Finally, it provides a contract that allows third party libraries to opt into so an application can handle cross cutting concerns in a consistent way. Consider the following examples:
I've been writing go since 2016. Error annotation comes up constantly on my team both writing go code, reading it, and most importantly debugging it. I am sure everyone has had a story of getting an error printed to standard out that repeats the same error message multiple times (hello improper wrapping), being forced to grep through the go source files, and them making an educated guess which error was the source of the problem. |
@neild I remember the discussion at the time and was disappointed about the loss of the stacktraces though I understood why. The problem as you describe it does seem intractable. So I'd like to reframe it. I would argue that the reason developers don't annotate errors is that it's not ergonomic or easy to do so. Developers are busy (or lazy) and we will tend to go with the path of least resistance. I'd wager that if there was an ergonomic way in the standard library to annotate and format errors (e.g for logging), then developers would start widely adopting them. In particular:
|
|
I believe errors are values and values doesn't need traceback by default, for example we use errors for EOF, we should not add a traceback to EOF. I use error for http redirect, i would not want that returning an error is slower or consume anythings more. Like |
Yes. I was thinking about my http client library. It marks 4xx and 5xx responses as an “error” but I don’t think anyone wants a traceback from my library. Whatever solution we have should be opt-in at application boundaries. |
Errors have three audiences:
This last case is not well served today, and is the case where stack information would be useful. When debugging a failure, you want to know exactly where that failure happened, and as much surrounding context as possible. I think the approach we tried in 1.13, where stack information is always present but only displayed on request, is still the right one. The only time you should need stack information in an error is when something unexpected has happened and you want to understand what. Adding stacks only at the places you expect to need them doesn't work well, because it's precisely the times that something unexpected happened that you need the stack. If we could record and save the caller's stack frame for every |
If we save the stack at @robpike https://commandcenter.blogspot.com/2017/12/error-handling-in-upspin.html was very instructive for me.
In practice, I added full stack with actionable flag and noticed the same thing. |
If there were GO_TRACEBACK=1 environment variable to turn on traceback recordings, I'm not sure if I would turn it on in prod or not. On the one hand, I don't think the performance hit would matter to me. On the other hand, I don't usually need a traceback to know where something went wrong because my errors are already decorated. In any event, it would need to be an interface, so custom error types could choose to opt-into it too. |
@neild during the 1.13 errors proposal, I don’t recall discussion of making the stacktraces opt in through an env variable or some other configuration. Was this already considered and rejected ? Speaking personally, I’d enthusiastically opt in. |
I don't recall whether we discussed that or not, but I don't think we'd want to add something that is off by default. Either it's good enough to be always there, or it isn't good enough to be part of the standard library. |
hi I was wondering if someting similar to that package would be viable for optionally enabling stack traces in the standard library, example:
In my head this should:
please correct me / help me where I am wrong or where you spot possible improvement to the idea |
Thanks. Our typical approach for a case like this would be to see how many programs adopt this package, or one like it. We would base a decision on whether to add to the standard library based on that. |
As somebody who wrote an errors package which in my mind is like v2 of github.com/pkg/errors, addressing many issues found there while keeping backwards compatibility to the maximum, I would strongly suggest that if there is a standard stack tracer interface defined, it should be:
In particular, the return type should be I am not too convinced that stack traces should be recorded as part of std lib, but maybe they could. It does becomes tricky through do you and when do you record additional stack traces when wrapping an existing error. Do you record it every time you wrap? In gitlab.com/tozd/go/errors I opted to not record it every time wrapping happens, but only when an explicit "cause" wrapping is done, so when you define a new error and record an existing error as an cause. Similarly, what do you do when you are joining multiple errors? (In my package, I record stack trace when joining always.) One thing worth standardizing would also be JSON serialization of errors and associated (possible) stack traces, especially because now those errors can wrap other errors and joined errors and so on. So the tree of errors can become large. And if we talk about structured logging, gitlab.com/tozd/go/errors also defines an interface for adding structured data to errors:
You can then modify that map in-place and add additional data. I have found it very useful to attach debugging data instead of pushing that all into the error string. One advantage is also that error string is then a constant, easy to google search, translate, while details is what changes between program runs. Maybe interface like this could be standardized as well. (Details consist of multiple possible layers of details, as wrapped errors can have their own details.) |
Hi following this conversation and a bit of playing with the new log/slog library I wrote github.com/golang-exp/errors-poc I thought:
|
This comment was marked as spam.
This comment was marked as spam.
I find it really irritating that I have to sprinkle I'd just wish to set |
@fsaintjacques Thanks, based on that idea I've opened https://go.dev/issue/63358. |
Yea, but what about other libraries which are not using standard errors package anymore, because they already want to attach stack traces? It will not enable it for them. I think the critical piece is that we should standardize what the interface of errors which have recorded a stack trace looks like. I suggest:
Then, we can discuss it adding it to standard errors with a flag or not. But without a standard way to obtain a stack trace, interoperability will suffer. |
This proposal try to address the lack of traceback in errors but keep the way we handle errors as value annotated where the error occur.
Errors are values, a string or a custom error.
To retrieve the stack of errors we need to follow the chain of strings, each annotation should be written carefully to retrieve easily from where the error comes. If we just
return err
one time we loose the path.Because of this we can embed all the stack in a custom error at the higher lever and print it at the end with the hope that we didn't loose the path with a
return fmt.Errorf("... %v")
. Like that we can justreturn err
everywhere, we'll just use the full traceback to see what's happen like in most of the others languages.Often we'll have both, a huge traceback and a chain of annotations in the end and full of
return err
everywhere. This is not fun and not idiomatic in Go.Sometimes we just need an annotation, in a lib for example, and sometimes it's interesting to have the method and the exact line in an application. In a web server we generaly need only the stack after the handler and before the lib but not in the server and middlewares.
My proposal is to can add easily the trace and just this trace when we annotate the error. Something like
fmt.Errorf("@trace I call foo: %v", err)
The result is a minimal traceback like that:
https://go.dev/play/p/VJSXkbOX7J_c
I believe it follow the Go way of handling errors.
The error is still a value, a value with a trace as annotation (not to do for an API).
We decide at each error if we need the trace or not and not for the whole chain.
We encourage to annotate.
Explicit.
Easy to implement.
It's easy to experiment, just replace fmt.Errorf with a function like this:
Hope it give at least inspiration.
The text was updated successfully, but these errors were encountered: