text stringlengths 0 1.82k |
|---|
**Erik St. Martin:** We were talking about the standard library and evolving it based on new knowledge gain from years of writing code in what is the 1.0 spec. Some of the stuff that we're starting to see and one of the topics you're primarily advocating is the way error handling is done. It seems like some of the sent... |
**Dave Cheney:** Sure. First of all, before I say anything, I need to be very clear that I stand a very small amount above the shoulders of many giants who've done a lot of work before. The errors package that I wrote is directly influenced by the one that we wrote at Canonical for Juju, which is based on Roger Peppe's... |
I got on stage and talked about it at dotGo. The idea keeps percolating, and the latest evolution of it is in gRPC - they used that heavily in the constructives for all the gRPC types, the formalization around the naming of options and the name they're gonna call it. What I see in the broader perspective is there's kin... |
To talk about errors is kind of two stories that sit side by side. The first story is that I've written a lot of blog posts about how I think error handling should be done, and this is separate from stack traces and things like that. In fact, embarrassingly, I actually wrote the same blog post twice, about a month in-b... |
I've talked about ideas of rather than checking the error matches a particular value, or the error is a particular type, you should instead try and think about it as, "Okay, if I need to know something about this error - is it a temporary error? Does it fit the temporary interface? (which you get from net dot com or yo... |
\[32:06\] That's a way for designing Go programs that deal with errors in that fail-fast kind of way. At every stage you're not trying to, "Oh, something went wrong. I'm gonna look at that error and see if it matches a dozen of the kinds that I know about, and in those particular cases I'm gonna retry, or I'm gonna adj... |
**Erik St. Martin:** In the case of the is-temporary check - I know in your talk you kind of brought that up - do you advocate that if it's temporary people do some kind of exponential backoff with some backpressure to eventually fail out? Or are you advocating just always return and pass up the chain and handle it at ... |
**Dave Cheney:** It's always a trade-off. I think in the places where your code does actually know it's dealing with a network -- this is to come back to the idea that you want to have modular design; the way that your modules interact with each other are interfaces, rather than concrete types. In the case that your co... |
Often times when you're working in this binary package and you're passed a ReadWriter or a ReadWriteCloser you don't know where that came from - it could be a buffer, it could be a file on disk, it could be anything. It really depends on what is the goal of your package and to take that responsibility and just wrap up.... |
I think it general you don't know have that kind of visibility to how your package is working. Many times perhaps you shouldn't; perhaps you should try and treat them more like black boxes, because that makes them easier to just clip together. There are less implicit agreements between code; they have to be explicit, o... |
**Erik St. Martin:** I guess that makes sense... Because even if you think about it from the network concept - what are you doing? Some temporary error may have occurred, but you can't guarantee idempotency, right? You could retry, but that may cause some undesired effect on the other side because it half-completed, or... |
**Dave Cheney:** \[35:46\] Exactly. In those situations, it shouldn't be so easy to just blindly put in a retry. You perhaps wanna think about how this operation failed. That means that you need to know very intimately about all the parts of the code downstream for you, which means a lot more coupling, you have a lot m... |
To give an example, the SSH package, which built on top of networking public keys, SSH agents... The interfaces that those types implement are just the usual read/write closer. We worked really hard to make the con interface and the session interface look pretty much like a read/write closer or a similar thing that you... |
There's a separate part of error handling, which is when error does happen, how do you tell the developer or the operations person -- what I was saying earlier, you just kind of wave your hands and say "I'm just gonna give the error back to the person above me, the code above me. It will figure out what to do." |
Eventually, you're gonna reach the top of your function or the main handler of your web server or whatever it is, and if that is gonna come to you, you're gonna have to figure out what happened. In that case, you want to get as much information about the error that happened. You want to encode as much information as yo... |
So the second part of error handling is using the fact that the error is a value, and we've just talked about it from the caller's point of view - just making it opaque, just making it, "An error happened" and you don't know anything more than that. Then we can use this fact and we can stick extra information into it. ... |
That's been a pattern we've seen in the standard library a lot, Donovan and Kernighan talk about it in their book... There's a lot of Go code written out there, "if err != nil { return fmt.Errorf} some description that says what happened, and then the text of the error. \[\\00:39:06.14\] |
And that's good, because at the top you get what Roger used to call 'breadcrumbs' of "This failed because this failed:because this failed:because this failed:because this failed", and you can kind of grep for those little individual strings and kind of manually construct a stack trace of where you were in that code. |
That's good, but it has problems that... There are cases -as few as they are and as many as I would prefer they weren't - in the standard library where you do actually want to check for a specific value. io.EOF is the super example of this. Any I/O reader must return io.EOF. It can't return ReadFile:io.EOF. It must ret... |
\[40:06\] In certain cases you can't do this annotation, because taking io.EOF, printing its string out, pending to another string and then returning an entirely different value from fmt.Errorf gives you something which doesn't compare, and you can't strip off that prefix anymore, because you've forever damaged it. So ... |
If we're stacking them one on top of another, let's have a method that we can undo the stacking, so that if we do need this behavior of saying, "Is this io.EOF?" or for example if you use "OS does not exist", that knows about a certain bunch of types from syscall from Windows; there's a few other ones that it knows spe... |
**Erik St. Martin:** Right, and there is cases we've seen where people have masked the sentinel error values based on the type that they're returning in their error, too. Relying on those sentinel error values becomes problematic. |
The nice thing I like about this approach too is the other pattern we've seen people trying to solve these same problems with is tagged log, but that only helps in the log messages that are going out; that doesn't help the callers that the messages are being passed back up the stack, too. |
**Dave Cheney:** Oh, this comes into my other big rant, which is only handle the error once. Handle means basically I've inspected the error value; if error != nil, that's your inspection, I've looked at it. Then you get to make exactly one decision. That decision could be to log the message; you've written it out, so ... |
Now, what were some cases you might log it... Say you're searching in a search path, you're looking for a particular file. It's not in your home directory, it's not in the shared directory, isn't in the system one. You're not gonna bail out on that first time around; if it's not in your home directory, you're gonna loo... |
What I see a lot is at every level in the call stack, if error != nil, I'm gonna write out "Log - some error happened", and then return that error to the caller. That means however much you apply this pattern all the way in your code, you're gonna get 10 or 15 different log messages basically telling you the same thing... |
\[44:11\] I strongly advocate, if there is an error, just return it to the caller, and the error's package with the wrap method gives you the ability for that little piece of log context that you're gonna caught in log.error if, just put that in the error itself (Errors.wrap) with that message text, and return that to ... |
**Carlisia Thompson:** I have a question about that. How does structure logging fit with this philosophy? I thought I would see the point of what you're saying, but the repeats, especially if you're not really crafting your logging message strings, how do you handle if you want to do structured logging? Do you use the ... |
**Dave Cheney:** This is probably the most opinionated thing I'm gonna say, but I don't see the point in structured logging. Not because I think it's not a useful thing, rather than just have a free text blob of text; this idea of key/value key/value is useful, but I think there are really two consumers of logs - there... |
So if the only things that you log are things that the user needs to take action on, then I don't see a lot of value in investing in a framework for describing keys and values for logs. To be very clear, this is my opinion; I don't want to push that onto anybody else. I know there's a lot of people who see a lot of val... |
The second persona is obviously the developer, and I keep them separate from the person running the program versus the person debugging the program. The developer wants all the logging and tracing on, so I think in that case your structured logging is something that you use in development, and maybe interpret that to b... |
As the operator, I only want programs to output when there's something that I need to do. They shouldn't just tell me that they're still running, they shouldn't just tell me information like, "Couldn't dial the socket, but I'm retrying. It's okay, don't worry." That's not something I need to care about, you shouldn't t... |
**Erik St. Martin:** \[48:15\] We had a conversation with Scott Mansfield from Netflix too, and he was talking about how they don't really rely on logs so much as they do counters. They use metrics for everything; they would count the number of reconnect errors that are occurring and measure that over time, and they co... |
I think the other case where people like structured logging is in distributed application tracing. I can look for a tag that says a "request ID" and I can get all the logs associated with a given request. But to your point, when you get to large scales, it's really hard to manage all those logs anyway, so you kind of h... |
I want to roll back a little bit where we talk about trying the best you can to return upstream. One case that I see a lot of people use logging for in those cases is when you're in kind of a select loop; you're pulling from a channel, something happened, you don't want to return because you're just a parallel workstri... |
**Dave Cheney:** Yeah, so you've gotta consider the persona that you're in. Are you in the developer persona? "I want to observe the operation of this select loop. It's one of many that's going on concurrently; I believe that if I can get inside into how all these different intermeshing parts are moving, I'll be able t... |
The operator persona, if that was just dumping out information like, "I'm going back through the select loop again, "what event conditions fired?" that would make me furious. I've worked in environments in trading companies where we would produce gigabytes and gigabytes of logs per application - there were many applica... |
In terms of logging, there are definitely exceptions to this case. Ordered logs are probably a perfect example of where structured logging is useful. User ID, in-group ID, with permissions, set, did, operation. Because you do need, in logical systems and well-regulated ones, you do need ordered logs. But again, the ord... |
**Carlisia Thompson:** On the same subject, I wanted to ask your opinion because I see logging and instrumentation of metrics as serving different purposes. For example, I can start tracking how many times a certain request came true because I usually get 500,000 a day, and if I suddenly start getting 200,000 I wanna b... |
**Dave Cheney:** \[52:09\] Yeah, they're absolutely separate things. Logging is for the human, instrumentation is for the machines, for your monitoring, for your automated alerts, for your historysis, your automatic retry, your scaling up, your scaling down. If you're driving those processes off tailing a log file, you... |
**Erik St. Martin:** Yeah, and I think logging too should be something that you should be able to back off of. I've worked on systems commonly where it's streamed over UDP; you don't want logging to slow down your application because there's some slowdown on the disk, or things start catastrophically failing because yo... |
I guess it depends on how important your logs are. If you're doing something for a bank, you probably want every single message, it's probably of big importance to keep that for audit purposes. But in other cases, if your logging requests to your site, if you lose a minute and a half of logs because there was some slow... |
**Dave Cheney:** Yeah, they're different use cases - the ordered log, the HTTP request log, if you have to keep them for analytics, fraud detection or something like that... And then there's the log of your actual application code that was -- every time it speaks to you, does it tell you "This is something you need to ... |
**Erik St. Martin:** It's hard, because there's no cardinal rule, right? It's just like the other topics we've had here today... It's about looking at your programming, determining how important these things are to you. Are they a necessity to operate and maintain this application, or are they really just fluff that ma... |
At high scale it also becomes its own problem, because that's another system that can fail, when you're doing your distributed logging out to one place, and you have to make the decisions, how worthwhile is that additional complexity to you, and the additional storage to store all these logs, or are you really trying t... |
**Carlisia Thompson:** In companies that handle that for you, they charge a lot of money. It's very expensive. |
**Dave Cheney:** Oh yeah. |
**Erik St. Martin:** Those companies love when you throw them just needless logs. \[laughter\] It's usually some kind of like a rate charge. |
**Dave Cheney:** There's a strong moral hazard in there for them to not help you become better. They'll just write you a better tool to handle larger volumes of logs; by all means, don't change, just keep going how you're going. |
**Erik St. Martin:** \[55:53\] I think the difficulty is that when you look at a line of code and you're thinking about, "If something were to go wrong here", you think about all the times that you're tried to debug an application and you didn't have enough information. I think people err on the side of providing too m... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.