rpc icon indicating copy to clipboard operation
rpc copied to clipboard

rpc/v2 middleware

Open cv21 opened this issue 7 years ago • 6 comments

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?

cv21 avatar May 01 '17 20:05 cv21

+💯 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.

tejasmanohar avatar May 15 '17 01:05 tejasmanohar

We'd be more than open to (in this order!):

a) a document covering the design of a middleware API b) a PR adding this to the library

I don't believe myself or @kisielk otherwise have time to contribute, but we're both more than happy to assist with design & code review. On Sun, May 14, 2017 at 6:19 PM Tejas Manohar [email protected] wrote:

+💯 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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/gorilla/rpc/issues/50#issuecomment-301354644, or mute the thread https://github.com/notifications/unsubscribe-auth/AABIcNtFEy70EZG_---197Z5BTYcSp_5ks5r56gegaJpZM4NNbhg .

elithrar avatar May 15 '17 02:05 elithrar

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 ?

tejasmanohar avatar May 15 '17 08:05 tejasmanohar

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.

prisamuel avatar May 24 '18 12:05 prisamuel

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.

Oppodelldog avatar Jan 16 '19 20:01 Oppodelldog

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

the729 avatar Nov 15 '22 05:11 the729