Hi all,
I've adopted slog and I am really loving the library. As I have used this on my team, I've noticed a repetitive problem with error wrapping and I am curious how other folks have handled this problem.
Lets say you are calling a sequence of functions where one function depends on the result of the next function in the sequence. Each function returns an error. For traceability, you want to log the error where it was encountered, then wrap the error in the current function and return it. You then repeat this all the way up the stack. See here for a simplified example:
This has the benefit of clearly communicating what caused the failure in each function. It has the drawback that you are repeatedly logging the same error information multiple times. Depending on the error text, this could turn into a wall of text that makes interpreting logs quite hard for an engineer. I learned this the hard way when i use
github.com/pkg/errors and would print out the stacktrace.
So then I wondered about using context to inject a traceId that would link log messages together. I banged out quickly a small library as a PoC. Here's what that looks like:
This allows me to trace back the cause across the logs by sorting by time and filtering for the trace id. Means I only need to log the error once. The negative here is you have to pass context around for any function that logs....which is probably most of them when most of those functions don't need to do cancellation. I suppose you could just do context.Background.
So that's what I've come up with. I am curious what others have done.
Thanks,
Joe
P.S Note yes i know the problem is distributed tracing, that logging is a poor way to do this, and one should use an observability platform like
honeycomb.io to send errors too. But I don't have a platform. I have slog and i am curious how to make error tracing work nicely with that.