Skip to content
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

Closed
ferrari9999 opened this issue Mar 21, 2019 · 7 comments
Closed

Grpc Server read client ip address and send to service #5489

ferrari9999 opened this issue Mar 21, 2019 · 7 comments
Assignees
Labels

Comments

@ferrari9999
Copy link

ferrari9999 commented Mar 21, 2019

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

public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
			ServerCallHandler<ReqT, RespT> next) {
        String inetSocketString = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString();
		logger.debug("inetSocketString={},headers={},methodNameDesctiptor={}", inetSocketString, headers,call.getMethodDescriptor());
		return next.startCall(call, headers);
	}

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".

@creamsoup
Copy link
Contributor

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.

see metadata-context-example

@ferrari9999
Copy link
Author

ferrari9999 commented Mar 22, 2019

@creamsoup Thanks for response.
whatever solution you suggested will work upto interceptor in server but how to send that information to my service implementation method for logging.

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.

@creamsoup
Copy link
Contributor

@ferrari9999 thanks for the clarification.

You can use ServerInterceptor with SimpleForwardingServerCall to intercept response and SimpleForwardingServerCallListner to intercept request in the same interceptor. It also has access to Metatada to access the ip address.
Something looks like following

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;
  }
}

@ferrari9999
Copy link
Author

Thank you very much this helps.

@ferrari9999
Copy link
Author

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.

@creamsoup
Copy link
Contributor

@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?

@creamsoup
Copy link
Contributor

closing due to inactivity. Feel free to reopen it.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants