-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Grpc Server read client ip address and send to service #5489
Comments
You can send arbitrary custom metadata (both ip and request id) from client to server which doesn't require to change proto definition unless I misunderstood the problem. |
@creamsoup Thanks for response. I hope ServerCallHandler will forward the request to method using MethodDescriptor object during that phase i need clinet ip address to this method so that i can dump both client ip address and request data for debug purpose. So my question mainly was how to send the client ip address that corresponds to the request to service method which i implemented.I hope i clarified the question. Pls suggest. |
@ferrari9999 thanks for the clarification. You can use class LoggingInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
final Metadata headers, ServerCallHandler<ReqT, RespT> next) {
SimpleForwardingServerCall<ReqT, RespT> responseInterceptingServerCall =
new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendMessage(RespT message) {
super.sendMessage(message);
System.out.println("resp message: " + message);
}
};
Listener<ReqT> nextListener = next.startCall(responseInterceptingServerCall, headers);
SimpleForwardingServerCallListener<ReqT> reqInterceptor =
new SimpleForwardingServerCallListener<ReqT>(nextListener) {
@Override
public void onMessage(ReqT message) {
super.onMessage(message);
System.out.println("req message: " + message);
}
};
return reqInterceptor;
}
} |
Thank you very much this helps. |
Sorry to reopen the issue any way we can directly send to the service method . Your solution was perfect but i need to understand any tweak we can do at serverCallHandler or implement custom serverCallHandler so that while calling method with request i will wrap my ip address. |
@ferrari9999, it is unclear what you are asking. You don't want to use metadata or do you need to access ip address in some of the methods? |
closing due to inactivity. Feel free to reopen it. |
Please answer these questions before submitting your issue.
What version of gRPC are you using?
1.18.0
What did you expect to see?
I wrote interceptor like this
now i want ip address which was captured above to be sent to my services whenever my method get called so that i can add debug log by creating client ip and request data dump in server logs.
Pls suggest a better way.
Approach 1:
if metadata which i send from the client have an unique id which i set in server context(key=unique id ,value=client ip address) and while making my method call i set this unique id in request params and compare both(context and request parameter of unique id) at service level. For this approach i need to rewrite my proto file which "I DON'T WANT TO DO".
The text was updated successfully, but these errors were encountered: