use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class BinlogHelper method getClientInterceptor.
public ClientInterceptor getClientInterceptor(final long callId) {
return new ClientInterceptor() {
boolean trailersOnlyResponse = true;
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
final AtomicLong seq = new AtomicLong(1);
final String methodName = method.getFullMethodName();
final String authority = next.authority();
// The timeout should reflect the time remaining when the call is started, so do not
// compute remaining time here.
final Deadline deadline = min(callOptions.getDeadline(), Context.current().getDeadline());
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
final Duration timeout = deadline == null ? null : Durations.fromNanos(deadline.timeRemaining(TimeUnit.NANOSECONDS));
writer.logClientHeader(seq.getAndIncrement(), methodName, authority, timeout, headers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
null);
ClientCall.Listener<RespT> wListener = new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onMessage(RespT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_SERVER_MESSAGE, method.getResponseMarshaller(), message, GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.onMessage(message);
}
@Override
public void onHeaders(Metadata headers) {
trailersOnlyResponse = false;
writer.logServerHeader(seq.getAndIncrement(), headers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, getPeerSocket(getAttributes()));
super.onHeaders(headers);
}
@Override
public void onClose(Status status, Metadata trailers) {
SocketAddress peer = trailersOnlyResponse ? getPeerSocket(getAttributes()) : null;
writer.logTrailer(seq.getAndIncrement(), status, trailers, GrpcLogEntry.Logger.LOGGER_CLIENT, callId, peer);
super.onClose(status, trailers);
}
};
super.start(wListener, headers);
}
@Override
public void sendMessage(ReqT message) {
writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_CLIENT_MESSAGE, method.getRequestMarshaller(), message, GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.sendMessage(message);
}
@Override
public void halfClose() {
writer.logHalfClose(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.halfClose();
}
@Override
public void cancel(String message, Throwable cause) {
writer.logCancel(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_CLIENT, callId);
super.cancel(message, cause);
}
};
}
};
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class HeaderServerInterceptorTest method serverHeaderDeliveredToClient.
@Test
public void serverHeaderDeliveredToClient() {
class SpyingClientInterceptor implements ClientInterceptor {
ClientCall.Listener<?> spyListener;
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
spyListener = responseListener = mock(ClientCall.Listener.class, delegatesTo(responseListener));
super.start(responseListener, headers);
}
};
}
}
SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel).withInterceptors(clientInterceptor);
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
blockingStub.sayHello(HelloRequest.getDefaultInstance());
assertNotNull(clientInterceptor.spyListener);
verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
assertEquals("customRespondValue", metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project grpc-java by grpc.
the class LoggingChannelProviderTest method newChannelBuilder_interceptorCalled.
@Test
public void newChannelBuilder_interceptorCalled() {
ClientInterceptor interceptor = mock(ClientInterceptor.class, delegatesTo(new NoopInterceptor()));
InternalLoggingChannelInterceptor.Factory factory = mock(InternalLoggingChannelInterceptor.Factory.class);
when(factory.create()).thenReturn(interceptor);
LoggingChannelProvider.init(factory);
ManagedChannelBuilder<?> builder = Grpc.newChannelBuilder("localhost", TlsChannelCredentials.create());
ManagedChannel channel = builder.build();
CallOptions callOptions = CallOptions.DEFAULT;
ClientCall<Void, Void> unused = channel.newCall(method, callOptions);
verify(interceptor).interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
channel.shutdownNow();
LoggingChannelProvider.finish();
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project pinpoint by naver.
the class SpanGrpcDataSenderProvider method newChannelFactoryBuilder.
private ChannelFactoryBuilder newChannelFactoryBuilder(boolean sslEnable) {
final int channelExecutorQueueSize = grpcTransportConfig.getSpanChannelExecutorQueueSize();
final ClientOption clientOption = grpcTransportConfig.getSpanClientOption();
ChannelFactoryBuilder channelFactoryBuilder = new DefaultChannelFactoryBuilder("SpanGrpcDataSender");
channelFactoryBuilder.setHeaderFactory(headerFactory);
channelFactoryBuilder.setNameResolverProvider(nameResolverProvider);
final ClientInterceptor unaryCallDeadlineInterceptor = new UnaryCallDeadlineInterceptor(grpcTransportConfig.getSpanRequestTimeout());
channelFactoryBuilder.addClientInterceptor(unaryCallDeadlineInterceptor);
// channelFactoryBuilder.addClientInterceptor(discardClientInterceptor);
if (clientInterceptorList != null) {
for (ClientInterceptor clientInterceptor : clientInterceptorList) {
logger.info("addClientInterceptor:{}", clientInterceptor);
channelFactoryBuilder.addClientInterceptor(clientInterceptor);
}
}
channelFactoryBuilder.setExecutorQueueSize(channelExecutorQueueSize);
channelFactoryBuilder.setClientOption(clientOption);
if (sslEnable) {
SslOption sslOption = grpcTransportConfig.getSslOption();
channelFactoryBuilder.setSslOption(sslOption);
}
return channelFactoryBuilder;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor in project pinpoint by naver.
the class DefaultChannelFactory method addHeader.
private void addHeader(NettyChannelBuilder channelBuilder) {
final Metadata extraHeaders = headerFactory.newHeader();
if (logger.isDebugEnabled()) {
logger.debug("addHeader {}", extraHeaders);
}
final ClientInterceptor headersInterceptor = MetadataUtils.newAttachHeadersInterceptor(extraHeaders);
channelBuilder.intercept(headersInterceptor);
}
Aggregations