opentelemetry-go-contrib icon indicating copy to clipboard operation
opentelemetry-go-contrib copied to clipboard

Ignore context cancel error in otelgrpc

Open mostafafarzaneh opened this issue 2 years ago • 2 comments

We usually close gRPC streams by canceling the context. The problem is the otelgrpc fill the error part of the span(context canceled) and the backend(X-Ray in my case) renders it as an error while it is just a simple close. One may argue that you should close the stream gracefully by sending CloseSend to prevent that. While it is true, it complicates the closing process and I think it is much easier to cancel the context to close the stream.

Is there any way to ignore the context cancel error being reported?

mostafafarzaneh avatar Aug 10 '22 02:08 mostafafarzaneh

Re CloseSend, it will make the client code much more complicated, since any layer above the gRPC may cancel the request used for the call. To protect against that, you'd have to invent a new context just for the gRPC, and then "proxy" the cancelation into a CloseSend. You'd also lose all information in that context chain, e.g. loggers and authentication details. I don't think this is viable.

I'm in the same position, where I'd like to ignore [codes](https://pkg.go.dev/google.golang.org/[email protected]/codes).Canceled. However, I think it should only be ignored if the client's context is also canceled (errors.Is(ctx.Err(), context.Canceled)). There may have been something canceled on the server side that caused propagation to the client, and that is useful to differentiate.

Likewise, the server interceptor should ignore codes.Canceled and context.Canceled if the server context is canceled.

Since this logic is not trivial, I'd suggest making it optional to avoid hiding exceptional cases.

In the rest of my code, whenever I see a cancellation, I set a canceled=true attribute, and the span's status is codes.Ok. I'd like something similar for otelgrpc.

tommie avatar Aug 12 '22 17:08 tommie