Search in sources :

Example 1 with SimpleForwardingServerCall

use of io.grpc.ForwardingServerCall.SimpleForwardingServerCall in project grpc-java by grpc.

the class OrcaMetricReportingServerInterceptor method interceptCall.

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
    Context ctx = Context.current();
    CallMetricRecorder callMetricRecorder = InternalCallMetricRecorder.CONTEXT_KEY.get(ctx);
    if (callMetricRecorder == null) {
        callMetricRecorder = InternalCallMetricRecorder.newCallMetricRecorder();
        ctx = ctx.withValue(InternalCallMetricRecorder.CONTEXT_KEY, callMetricRecorder);
    }
    final CallMetricRecorder finalCallMetricRecorder = callMetricRecorder;
    ServerCall<ReqT, RespT> trailerAttachingCall = new SimpleForwardingServerCall<ReqT, RespT>(call) {

        @Override
        public void close(Status status, Metadata trailers) {
            Map<String, Double> metricValues = InternalCallMetricRecorder.finalizeAndDump(finalCallMetricRecorder);
            // Only attach a metric report if there are some metric values to be reported.
            if (!metricValues.isEmpty()) {
                OrcaLoadReport report = OrcaLoadReport.newBuilder().putAllRequestCost(metricValues).build();
                trailers.put(ORCA_ENDPOINT_LOAD_METRICS_KEY, report);
            }
            super.close(status, trailers);
        }
    };
    return Contexts.interceptCall(ctx, trailerAttachingCall, headers, next);
}
Also used : Context(io.grpc.Context) Status(io.grpc.Status) Metadata(io.grpc.Metadata) OrcaLoadReport(com.github.xds.data.orca.v3.OrcaLoadReport) InternalCallMetricRecorder(io.grpc.services.InternalCallMetricRecorder) CallMetricRecorder(io.grpc.services.CallMetricRecorder) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall)

Example 2 with SimpleForwardingServerCall

use of io.grpc.ForwardingServerCall.SimpleForwardingServerCall in project brave by openzipkin.

the class BaseITTracingServerInterceptor method userInterceptor_throwsOnSendMessage.

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

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

                @Override
                public void sendMessage(RespT message) {
                    throw new IllegalStateException("I'm a bad interceptor.");
                }
            }, metadata);
        }
    });
    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) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) Test(org.junit.Test)

Example 3 with SimpleForwardingServerCall

use of io.grpc.ForwardingServerCall.SimpleForwardingServerCall 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 4 with SimpleForwardingServerCall

use of io.grpc.ForwardingServerCall.SimpleForwardingServerCall in project brave by openzipkin.

the class BaseITTracingServerInterceptor method userInterceptor_throwsOnClose.

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

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

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

Example 5 with SimpleForwardingServerCall

use of io.grpc.ForwardingServerCall.SimpleForwardingServerCall 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

SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)5 Metadata (io.grpc.Metadata)5 SimpleForwardingServerCallListener (io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener)4 ServerInterceptor (io.grpc.ServerInterceptor)4 Status (io.grpc.Status)3 Test (org.junit.Test)3 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1 SpanCustomizer (brave.SpanCustomizer)1 OrcaLoadReport (com.github.xds.data.orca.v3.OrcaLoadReport)1 ByteString (com.google.protobuf.ByteString)1 Duration (com.google.protobuf.Duration)1 Context (io.grpc.Context)1 Deadline (io.grpc.Deadline)1 InternalMetadata (io.grpc.InternalMetadata)1 ServerCall (io.grpc.ServerCall)1 ServerCallHandler (io.grpc.ServerCallHandler)1 HelloReply (io.grpc.examples.helloworld.HelloReply)1 CallMetricRecorder (io.grpc.services.CallMetricRecorder)1 InternalCallMetricRecorder (io.grpc.services.InternalCallMetricRecorder)1 InetSocketAddress (java.net.InetSocketAddress)1