fs2-grpc icon indicating copy to clipboard operation
fs2-grpc copied to clipboard

Server middleware support

Open lacarvalho91 opened this issue 2 years ago • 2 comments

Overview

I'd like to be able to configure a server with a middleware that would run on each request, similar to what is possible with http4s middleware.

Where this idea has come from is I've been writing servers that use otel4s to attach to existing traces (passed to the server as metadata). This works fine, but the problem is this needs to be done for every server implementation. I would instead like to be able to do this in a generic way and be able to configure the server with this behaviour.

I can't really use gRPC interceptors for this, because the state of the context in cats.mtl.Local (which otel4s and other libs use) is scoped only to the effect passed into local.

From looking into the code, we could intercept the request in the Fs2ServerCallHandler and wrap the implementation: (Request, Metadata) => F[Response] in a middleware. Then allow passing middlewares in the GeneratedCompanion.service.

lacarvalho91 avatar May 23 '23 12:05 lacarvalho91

Does this seem achievable?

lacarvalho91 avatar May 23 '23 12:05 lacarvalho91

I do not have many spare cycles these days, but you are welcome to give it a go with a PR and I will take a look :)

ahjohannessen avatar May 23 '23 14:05 ahjohannessen