-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to log RPC requests properly? #45
Comments
Thanks for your comment. |
Yep hook sounds great! |
Share, I welcome your PR. |
Great, will work on it! I think about at least 2 hooks, before handling and after response. At the first hook, it should be possible to save the start timestamp to context. Then logger inserted in "after" hook gonna be able to track response time. |
You can implement middleware to log queries var LoggingMiddleware = func(next jsonrpc.HandlerFunc) jsonrpc.HandlerFunc {
// one time scope setup area for middleware
return func(c context.Context, params *fastjson.RawMessage) (result interface{}, err *jsonrpc.Error) {
// here get logger from context, also get additional info with jsonrpc.RequestID(), GetMetadata() and MethodName()
} next, during launch-time use jsonrpc.BuildChain() to wrap your handler with middlewares and then call MethodRepository.RegisterMethod to register your wrapped handler |
Of course you can implement usual http middlewares to you http.ServeMux, around jsonrpc handler. But they will not be able to access jsonrpc method name without additional parsing |
I will test LoggingMiddleware, looks like a promising approach! Logger on HTTP mux level is not an option - it would require manipulation with the copiing of request.Body ReaderCloser and duplicated body parsing, which is already done in the package. |
Ok, so finally what I've got: this part is not ideal, so it would be nice to have something like and this is my logger implementation:
|
Yes, middleware support can be better in this library. |
So far I've implemented a logger call inside each handler, which doesn't look like the best idea.
I think the best place to log requests is right after the parser, so it's really nice to have some kind of middleware support.
Probably somewhere in here?
jsonrpc/handler.go
Lines 45 to 53 in 352acaa
The text was updated successfully, but these errors were encountered: