dd-trace-go icon indicating copy to clipboard operation
dd-trace-go copied to clipboard

proposal: contrib/google.golang.org/grpc: add a `WithErrorCheck ` option

Open BIwashi opened this issue 2 years ago • 2 comments

Hello! I would like to discuss the possibility of adding a WithErrorCheck option for gRPC.

There are cases where an application produces errors, but they should not be sent to DataDog as errors. While the NonErrorCodes option allows for not treating specific gRPC status codes as errors, there are use cases where a more granular decision is needed based on the specific error, rather than just the status code.

The WithErrorCheck allows you to make decisions based on a combination of gRPC error codes and FullMethod in cases like this. Of course, it also allows you to freely set other conditions as needed.

For example, a user might want to consider a NotFound error as non-error for a specific RPC.

opts = []rpc.Option{
	...
	rpc.WithUnaryInterceptors(
		ddgrpc.UnaryServerInterceptor(
			ddgrpc.WithErrorCheck(func(method string, err error) bool {
				if err == nil {
					return true
				}
				errCode := status.Code(err)
				if errCode == codes.NotFound && method == "/example.ExampleService/GetInfo" {
					return true
				}
				return false
			}),
		),
	),
	...
}
...
rpcServer = rpc.NewServer(rpcHandler, opts...)

I must apologize, as I have already created a PR for this feature before submitting this issue. I sincerely apologize for not following the proper procedure. If you are willing, I would greatly appreciate it if you could review the PR I have created.

BIwashi avatar Jun 14 '23 08:06 BIwashi

Moving this to ecosystems for review.

knusbaum avatar Aug 10 '23 17:08 knusbaum

This looks good from our perspective with a few minor changes, we have a similar function across a number of contrib packages like was introduced here: https://github.com/DataDog/dd-trace-go/pull/1315 named WithErrorCheck let's use that same name here (note that this flips the meaning of the return value).

ajgajg1134 avatar Oct 31 '23 14:10 ajgajg1134