-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add interceptor to add unique x-client-trace-id per RPC call
- Loading branch information
Showing
2 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
java/src/main/java/utility/XClientTraceIdClientInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package utility; | ||
|
||
import java.util.UUID; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.grpc.*; | ||
|
||
public class XClientTraceIdClientInterceptor implements ClientInterceptor { | ||
private static final Logger logger = LoggerFactory.getLogger(XClientTraceIdClientInterceptor.class.getClass()); | ||
private static final Metadata.Key<String> X_CLIENT_TRACE_ID = Metadata.Key.of("x-client-trace-id", Metadata.ASCII_STRING_MARSHALLER); | ||
|
||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, | ||
CallOptions callOptions, Channel next) { | ||
return new ForwardingClientCall.SimpleForwardingClientCall<>(next.newCall(method, callOptions)) { | ||
|
||
@Override | ||
public void start(Listener<RespT> responseListener, Metadata headers) { | ||
String xClientTraceId = UUID.randomUUID().toString(); | ||
headers.put(X_CLIENT_TRACE_ID, xClientTraceId); | ||
logger.info("sending request for xClientTraceId {}", xClientTraceId); | ||
|
||
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<>(responseListener) { | ||
@Override | ||
public void onClose(Status status, Metadata trailers) { | ||
logger.info("request completed for xClientTraceId {} with status {}", xClientTraceId, status); | ||
super.onClose(status, trailers); | ||
} | ||
}, headers); | ||
} | ||
}; | ||
} | ||
} |