db80
db80

Reputation: 4427

Java gRPC get the service name from ServerInterceptor

I am using gRPC and I need to get the name of the service of the request from the ServerInterceptor but it seems it is not possible.

Basically from an implementation of ServerInterceptor I need to know the name of the ServiceGrpc (as a string) that will be invoked.

public class PermissionInterceptor implements ServerInterceptor {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
                ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> handler
        ) {
            // here I need the name of RPC service that has been requested

            return handler.startCall(serverCall, metadata);
        }
}

Please note that I have tried with serverCall.getMethodDescriptor() but it returns the name of the proto service rather than the real name of the (java) service.

It returns:

co.test.domain.transaction.TransactionService

But I need this:

co.test.domain.transaction.TransactionNeoBasicImpl

Thanks

Upvotes: 5

Views: 2406

Answers (2)

user7183889
user7183889

Reputation:

There is a way to do this.

String fullMethodName = serverCall.getMethodDescriptor().getFullMethodName();
String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName);

Basically fullMethodName has the fully qualified service name as prefix (followed by a '/'). Something like this fullyqualified.ServiceName/MethodName

Upvotes: 4

Eric Anderson
Eric Anderson

Reputation: 26414

That is not possible.

If your interceptor is the last one before the application, you may be able to inspect handler's class and see its parent class' name. But that's very limited and not necessarily supported.

Upvotes: 1

Related Questions