Search in sources :

Example 1 with SimpleForwardingServerCallListener

use of io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener in project brave by openzipkin.

the class BaseITTracingServerInterceptor method bodyTaggingExample.

/**
 * This shows that a {@link ServerInterceptor} can see the server server span when processing the
 * request and response.
 */
@Test
public void bodyTaggingExample() throws IOException {
    SpanCustomizer customizer = CurrentSpanCustomizer.create(tracing);
    AtomicInteger sends = new AtomicInteger();
    AtomicInteger recvs = new AtomicInteger();
    init(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            call = new SimpleForwardingServerCall<ReqT, RespT>(call) {

                @Override
                public void sendMessage(RespT message) {
                    delegate().sendMessage(message);
                    customizer.tag("grpc.message_send." + sends.getAndIncrement(), message.toString());
                }
            };
            return new SimpleForwardingServerCallListener<ReqT>(next.startCall(call, headers)) {

                @Override
                public void onMessage(ReqT message) {
                    customizer.tag("grpc.message_recv." + recvs.getAndIncrement(), message.toString());
                    delegate().onMessage(message);
                }
            };
        }
    });
    GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST);
    assertThat(testSpanHandler.takeRemoteSpan(Span.Kind.SERVER).tags()).containsKeys("grpc.message_recv.0", "grpc.message_send.0");
    Iterator<HelloReply> replies = GreeterGrpc.newBlockingStub(client).sayHelloWithManyReplies(HELLO_REQUEST);
    assertThat(replies).toIterable().hasSize(10);
    // Intentionally verbose here to show that only one recv and 10 replies
    assertThat(testSpanHandler.takeRemoteSpan(Span.Kind.SERVER).tags()).containsKeys("grpc.message_recv.1", "grpc.message_send.1", "grpc.message_send.2", "grpc.message_send.3", "grpc.message_send.4", "grpc.message_send.5", "grpc.message_send.6", "grpc.message_send.7", "grpc.message_send.8", "grpc.message_send.9", "grpc.message_send.10");
}
Also used : SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) Metadata(io.grpc.Metadata) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerInterceptor(io.grpc.ServerInterceptor) HelloReply(io.grpc.examples.helloworld.HelloReply) CurrentSpanCustomizer(brave.CurrentSpanCustomizer) SpanCustomizer(brave.SpanCustomizer) Test(org.junit.Test)

Example 2 with SimpleForwardingServerCallListener

use of io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener in project brave by openzipkin.

the class BaseITTracingServerInterceptor method userInterceptor_throwsOnOnHalfClose.

@Test
public void userInterceptor_throwsOnOnHalfClose() throws IOException {
    init(new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata metadata, ServerCallHandler<ReqT, RespT> next) {
            return new SimpleForwardingServerCallListener<ReqT>(next.startCall(call, metadata)) {

                @Override
                public void onHalfClose() {
                    throw new IllegalStateException("I'm a bad interceptor.");
                }
            };
        }
    });
    assertThatThrownBy(() -> GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST)).isInstanceOf(StatusRuntimeException.class);
    testSpanHandler.takeRemoteSpanWithErrorMessage(Span.Kind.SERVER, "I'm a bad interceptor.");
}
Also used : SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) ServerInterceptor(io.grpc.ServerInterceptor) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with SimpleForwardingServerCallListener

use of io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener in project grpc-java by grpc.

the class BinlogHelper method getServerInterceptor.

public ServerInterceptor getServerInterceptor(final long callId) {
    return new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            final AtomicLong seq = new AtomicLong(1);
            SocketAddress peer = getPeerSocket(call.getAttributes());
            String methodName = call.getMethodDescriptor().getFullMethodName();
            String authority = call.getAuthority();
            Deadline deadline = Context.current().getDeadline();
            final Duration timeout = deadline == null ? null : Durations.fromNanos(deadline.timeRemaining(TimeUnit.NANOSECONDS));
            writer.logClientHeader(seq.getAndIncrement(), methodName, authority, timeout, headers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, peer);
            ServerCall<ReqT, RespT> wCall = new SimpleForwardingServerCall<ReqT, RespT>(call) {

                @Override
                public void sendMessage(RespT message) {
                    writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_SERVER_MESSAGE, call.getMethodDescriptor().getResponseMarshaller(), message, GrpcLogEntry.Logger.LOGGER_SERVER, callId);
                    super.sendMessage(message);
                }

                @Override
                public void sendHeaders(Metadata headers) {
                    writer.logServerHeader(seq.getAndIncrement(), headers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, /*peerAddress=*/
                    null);
                    super.sendHeaders(headers);
                }

                @Override
                public void close(Status status, Metadata trailers) {
                    writer.logTrailer(seq.getAndIncrement(), status, trailers, GrpcLogEntry.Logger.LOGGER_SERVER, callId, /*peerAddress=*/
                    null);
                    super.close(status, trailers);
                }
            };
            return new SimpleForwardingServerCallListener<ReqT>(next.startCall(wCall, headers)) {

                @Override
                public void onMessage(ReqT message) {
                    writer.logRpcMessage(seq.getAndIncrement(), EventType.EVENT_TYPE_CLIENT_MESSAGE, call.getMethodDescriptor().getRequestMarshaller(), message, GrpcLogEntry.Logger.LOGGER_SERVER, callId);
                    super.onMessage(message);
                }

                @Override
                public void onHalfClose() {
                    writer.logHalfClose(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_SERVER, callId);
                    super.onHalfClose();
                }

                @Override
                public void onCancel() {
                    writer.logCancel(seq.getAndIncrement(), GrpcLogEntry.Logger.LOGGER_SERVER, callId);
                    super.onCancel();
                }
            };
        }
    };
}
Also used : Status(io.grpc.Status) ServerCallHandler(io.grpc.ServerCallHandler) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) Duration(com.google.protobuf.Duration) ByteString(com.google.protobuf.ByteString) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) AtomicLong(java.util.concurrent.atomic.AtomicLong) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) ServerCall(io.grpc.ServerCall) ServerInterceptor(io.grpc.ServerInterceptor) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

SimpleForwardingServerCallListener (io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener)3 Metadata (io.grpc.Metadata)3 ServerInterceptor (io.grpc.ServerInterceptor)3 SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)2 Test (org.junit.Test)2 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1 SpanCustomizer (brave.SpanCustomizer)1 ByteString (com.google.protobuf.ByteString)1 Duration (com.google.protobuf.Duration)1 Deadline (io.grpc.Deadline)1 InternalMetadata (io.grpc.InternalMetadata)1 ServerCall (io.grpc.ServerCall)1 ServerCallHandler (io.grpc.ServerCallHandler)1 Status (io.grpc.Status)1 HelloReply (io.grpc.examples.helloworld.HelloReply)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1