gorilla / rpc

Package gorilla/rpc is a golang foundation for RPC over HTTP services.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
590 stars 179 forks source link

rpc/v2 middleware #50

Open cv21 opened 7 years ago

cv21 commented 7 years ago

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?

tejasmanohar commented 7 years ago

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

elithrar commented 7 years ago

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 notifications@github.com 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 .

tejasmanohar commented 7 years ago

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 ?

prisamuel commented 6 years ago

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.

Oppodelldog commented 5 years ago

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.

the729 commented 1 year ago

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