gorilla/rpc

rpc/v2 middleware

cv21 opened this issue · 6 comments

cv21 commented

When I writing rpc server based on rpc/v2, I need to use middleware for logging or, for example, for writing some metrics. I can use handlerFunc middlewares in style

type Log struct {
	L *log.Logger
}

func (m *Log) Use(next http.Handler) http.Handler {
	return http.HandlerFunc(func(r http.ResponseWriter, req *http.Request) {
		m.L.Println("Incoming request start")
		next.ServeHTTP(r, req)
		m.L.Println("Incoming request end")
	})
}

But it is not comfortably. I would like to know rpc method name and parsed request.
Just in rpc server (not v2) I found RegisterBeforeFunc that looking helpful, but in v2 it is not exist.

Please, suggest me how can I solve that problem?

+💯 for a system of middleware. I ran into a similar situation and came looking for a solution. It'd be incredibly useful for implementing standard logging as well as other application conventions.

Sounds good! ... But, now that I think of it, is there anything that can't be done with BeforeFunc / AfterFunc model atm? I think the main change would be exposing things like parsed request/response on the Before and After rather than just metadata like RequestInfo

Would you all be open to this? I can PR to show what I mean. It shouldn't have to be a breaking change... we can just add nill-able req/res fields to RequestInfo. Would this also solve your use case, @cv21 ?

Just in rpc server (not v2) I found RegisterBeforeFunc that looking helpful, but in v2 it is not exist

At dotmesh.com we use gorilla/rpc and this was a feature I required to send out metrics to via prometheus instrumentation.

I've added RegisterBeforeFunc, RegisterAfterFunc, and RegisterInterceptFunc into v2 on a fork here. Will submit a PR shortly.

I was just looking for this capability and as I found the feature request might have been resolved since the named hooks are already merged.

From code review I'd say this issue can be closed.

You can write a Codec wrapper which acts like a middleware.