How to intercept every client request and add some attributes to it and collect at the server end

403 views
Skip to first unread message

shailendra kumar

unread,
Aug 5, 2018, 4:11:02 AM8/5/18
to grpc.io

Whenever client call to server, i want to add some attribute like account, accountHolder, accountLocation  and their values along with client request. At the server side, i want to collect these info. Please suggest for grpc call as well as rest call

Josh Humphries

unread,
Aug 5, 2018, 1:24:14 PM8/5/18
to shaile...@gmail.com, grpc.io
I think several of the languages support the interceptor pattern (I am quite familiar with the Java and Go runtime libraries, which do). This allows you to register a client interceptor that will get to see every RPC. (Intercepting streaming RPCs in Go is a bit more complicated due to having a different interface than the interceptor for unary RPCs.)

When you have cross-cutting attributes to associate with every RPC, metadata is probably the way to go. So the interceptor could add the attributes you mention as request metadata. For Go, you'd probably need to have this data stored in a context.Context, which the interceptor will query and then store in request metadata. For Java, there is also a context type, but it uses thread-local storage (so it can be easier to interact with and does not require you to explicitly pass the context to/through every function).

----
Josh Humphries
jh...@bluegosling.com



On Sun, Aug 5, 2018 at 4:11 AM shailendra kumar <shaile...@gmail.com> wrote:

Whenever client call to server, i want to add some attribute like account, accountHolder, accountLocation  and their values along with client request. At the server side, i want to collect these info. Please suggest for grpc call as well as rest call

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/3e68e3d6-74d2-4082-90b0-5361bd4238d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shailendra kumar

unread,
Aug 6, 2018, 3:13:31 PM8/6/18
to Josh Humphries, grpc.io
Thanks Josh. 
I tried with ClientInterceptor and ServerInterceptor.   Its working fine. 
I have implemented in java. 
Will this interceptor work for http request through postman ?
e.g -  for path /v1/health    
will these interceptor invoke the request ??
rpc healthCheck(google.protobuf.Empty) returns (HealthCheckResponse) {
option (google.api.http) = {
get: "/v1/health"
};
}

On Sun, Aug 5, 2018 at 5:23 PM, Josh Humphries <jh...@bluegosling.com> wrote:
I think several of the languages support the interceptor pattern (I am quite familiar with the Java and Go runtime libraries, which do). This allows you to register a client interceptor that will get to see every RPC. (Intercepting streaming RPCs in Go is a bit more complicated due to having a different interface than the interceptor for unary RPCs.)

When you have cross-cutting attributes to associate with every RPC, metadata is probably the way to go. So the interceptor could add the attributes you mention as request metadata. For Go, you'd probably need to have this data stored in a context.Context, which the interceptor will query and then store in request metadata. For Java, there is also a context type, but it uses thread-local storage (so it can be easier to interact with and does not require you to explicitly pass the context to/through every function).

----
Josh Humphries
jh...@bluegosling.com


On Sun, Aug 5, 2018 at 4:11 AM shailendra kumar <shaile...@gmail.com> wrote:

Whenever client call to server, i want to add some attribute like account, accountHolder, accountLocation  and their values along with client request. At the server side, i want to collect these info. Please suggest for grpc call as well as rest call

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

Josh Humphries

unread,
Aug 6, 2018, 4:52:30 PM8/6/18
to shaile...@gmail.com, grpc.io
On Mon, Aug 6, 2018 at 3:13 PM shailendra kumar <shaile...@gmail.com> wrote:
Thanks Josh. 
I tried with ClientInterceptor and ServerInterceptor.   Its working fine. 
I have implemented in java. 
Will this interceptor work for http request through postman ?

I assume you are using grpc-gateway (or something very similar) in front of your service, to proxy JSON+HTTP 1.1 to gRPC. Is that correct? If so, yes, the interceptor will be invoked for these requests.
 
e.g -  for path /v1/health    
will these interceptor invoke the request ??

Sorry, I don't understand the question. If you are asking whether the interceptor will be invoked for these requests, see previous answer. If not, do you mind re-wording, perhaps adding a little more detail? 
rpc healthCheck(google.protobuf.Empty) returns (HealthCheckResponse) {
option (google.api.http) = {
get: "/v1/health"
};
}
On Sun, Aug 5, 2018 at 5:23 PM, Josh Humphries <jh...@bluegosling.com> wrote:
I think several of the languages support the interceptor pattern (I am quite familiar with the Java and Go runtime libraries, which do). This allows you to register a client interceptor that will get to see every RPC. (Intercepting streaming RPCs in Go is a bit more complicated due to having a different interface than the interceptor for unary RPCs.)

When you have cross-cutting attributes to associate with every RPC, metadata is probably the way to go. So the interceptor could add the attributes you mention as request metadata. For Go, you'd probably need to have this data stored in a context.Context, which the interceptor will query and then store in request metadata. For Java, there is also a context type, but it uses thread-local storage (so it can be easier to interact with and does not require you to explicitly pass the context to/through every function).

----
Josh Humphries
jh...@bluegosling.com


On Sun, Aug 5, 2018 at 4:11 AM shailendra kumar <shaile...@gmail.com> wrote:

Whenever client call to server, i want to add some attribute like account, accountHolder, accountLocation  and their values along with client request. At the server side, i want to collect these info. Please suggest for grpc call as well as rest call

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages