You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
transport/http ServerFinalizer should accept error for better logging support #807
We have three systems where logs are sent: our internal ELK stack, NewRelic and Airbrake.
Some of our endpoints send errors to all of this systems, some endpoints send errors to only one of it.
We want log entries to have both error message and http status code. And also some http request context (uri, method, user-agent, ip...)
The only place which has access to both error and http status is ServerErrorEncoder.
But since different endpoints need different sets of log outputs, we would have to create multiple error encoders just for sake of logging. Also, ServerErrorEncoder is not a good place for logging, because we would like it to do only one job - encode errors.
ServerFinalizer seems to be a great place to send logs, but there is no access to actual error message.
changed the title [-]transport/http ServerFinalizer should accept error for better logging[/-][+]transport/http ServerFinalizer should accept error for better logging support[/+]on Dec 3, 2018
If you want your ServerFinalizer to act on information from the service domain (e.g. an error) you can inject that error into the context, and extract it again.
@peterbourgon
What is the proper way to inject endpoint error into context?
I can think only of using mutable (pointer to error interface) key in context and update it in middleware.
But if i want to include request decoding errors as well, i will have to inject error into context in every request decoder (we do not generate them, unfortunatelly).
Probably, i will stick to logging in ErrorEncoder
Still, it would be nice to have error as an argument of Finalizer.
It would break the API, and it would be inelegant and awkward to implement, especially if you wanted to capture both service- and transport-level errors.
I have a hard time grasping the concept of returning service errors inside response structure.
Why do we need this if endpoint has explicit error return argument?
How do i determine what is transport and what is service error (and do i really need to differentiate them)? Is DB query error transport or service?
Why don't we use some (painful in current Go) way to distinguish between error types (i.e. error implements some ServiceError interface)?
Regardless of whether it is a service or transport error, i would typically return the same error response from service.
In the regular Go code i don't see people returning errors as part of return argument structure.
Transport errors are discoverable by endpoint- and transport-layer middlewares, and indicate a problem with the mechanics of the request. For example, the underlying service is unavailable, or a network gateway is down. They may be counted as errors from the perspective of an e.g. circuit breaker, and eventually cause further requests to fail until the error is resolved.
Service errors are not detectable by endpoint- and transport-layer middlewares, and indicate that the mechanics of the request were/are fine, but that there was a problem with the business logic. For example, a user attempted to withdraw too much money from their account. These errors don't indicate anything mechanically wrong with the service, the network, etc. and so shouldn't count toward a circuit breaker limit.
Another way of thinking of it: given some request, transport errors can be automatically retried because they might go away by themselves (e.g. 502 Bad Gateway), but service errors should not be because they won't go away unless either the request or the state of the service changes (e.g. 401 Bad Request).
edit: But I understand that this distinction may be more noise than signal to many developers. A future version of Go kit may eliminate it, or encode it in a different way.
Would that have been better? I don't know. Maybe. I find it harder to mentally model, but I understand you find it easier. It's just an engineering decision that was made early in the life of the project. Hopefully, with generics, we will be able to make better decisions in the future.
Activity
[-]transport/http ServerFinalizer should accept error for better logging[/-][+]transport/http ServerFinalizer should accept error for better logging support[/+]peterbourgon commentedon Feb 27, 2019
If you want your ServerFinalizer to act on information from the service domain (e.g. an error) you can inject that error into the context, and extract it again.
drscre commentedon Feb 28, 2019
@peterbourgon
What is the proper way to inject endpoint error into context?
I can think only of using mutable (pointer to error interface) key in context and update it in middleware.
peterbourgon commentedon Feb 28, 2019
Yes, basically. You might look at e.g. ctxlog for an example of something like this.
drscre commentedon Feb 28, 2019
Thanks, this will work for endpoint errors.
But if i want to include request decoding errors as well, i will have to inject error into context in every request decoder (we do not generate them, unfortunatelly).
Probably, i will stick to logging in ErrorEncoder
Still, it would be nice to have error as an argument of Finalizer.
peterbourgon commentedon Feb 28, 2019
If your RequestDecoder returns an error, it is given directly to the ErrorEncoder. Is that not sufficient?
drscre commentedon Feb 28, 2019
Yes, it will work.
But since one of the main reasons finalizer exist is logging, i really would prefer to do it there.
peterbourgon commentedon Feb 28, 2019
peterbourgon commentedon Feb 28, 2019
drscre commentedon Mar 1, 2019
Thanks! This is more or less what i am trying to achieve.
But, again, what is wrong with having err as an argument to finalizer? (except compatibility break)
peterbourgon commentedon Mar 1, 2019
It would break the API, and it would be inelegant and awkward to implement, especially if you wanted to capture both service- and transport-level errors.
drscre commentedon Mar 11, 2019
I have a hard time grasping the concept of returning service errors inside response structure.
Why do we need this if endpoint has explicit
errorreturn argument?How do i determine what is transport and what is service error (and do i really need to differentiate them)? Is DB query error transport or service?
Why don't we use some (painful in current Go) way to distinguish between error types (i.e. error implements some ServiceError interface)?
Regardless of whether it is a service or transport error, i would typically return the same error response from service.
In the regular Go code i don't see people returning errors as part of return argument structure.
peterbourgon commentedon Mar 11, 2019
Transport errors are discoverable by endpoint- and transport-layer middlewares, and indicate a problem with the mechanics of the request. For example, the underlying service is unavailable, or a network gateway is down. They may be counted as errors from the perspective of an e.g. circuit breaker, and eventually cause further requests to fail until the error is resolved.
Service errors are not detectable by endpoint- and transport-layer middlewares, and indicate that the mechanics of the request were/are fine, but that there was a problem with the business logic. For example, a user attempted to withdraw too much money from their account. These errors don't indicate anything mechanically wrong with the service, the network, etc. and so shouldn't count toward a circuit breaker limit.
Another way of thinking of it: given some request, transport errors can be automatically retried because they might go away by themselves (e.g. 502 Bad Gateway), but service errors should not be because they won't go away unless either the request or the state of the service changes (e.g. 401 Bad Request).
edit: But I understand that this distinction may be more noise than signal to many developers. A future version of Go kit may eliminate it, or encode it in a different way.
drscre commentedon Mar 12, 2019
Thanks for great explanation of difference between transport and service errors.
Still don't get why you prefer returning service errors inside response structure instead of using
errorreturn argument.peterbourgon commentedon Mar 12, 2019
It is not a preference, it is a requirement. Your service should still return errors directly, e.g.
it is only the translation from service to transport layer where they get wrapped up into types, e.g.
Go kit leverages the RPC abstraction to provide most of its value add, and (due to Go's poor type system) we are forced to codify all RPC as
To lift the service error into the endpoint error would mean exploding the response type, e.g.
Would that have been better? I don't know. Maybe. I find it harder to mentally model, but I understand you find it easier. It's just an engineering decision that was made early in the life of the project. Hopefully, with generics, we will be able to make better decisions in the future.