Search in sources :

Example 31 with HelloReply

use of io.grpc.examples.helloworld.HelloReply 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 32 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project brave by openzipkin.

the class GreeterImpl method sayHello.

@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
    TraceContext currentTraceContext = tracing != null ? tracing.currentTraceContext().get() : null;
    if (req.getName().equals("bad")) {
        responseObserver.onError(new IllegalArgumentException("bad"));
        return;
    }
    if (req.getName().equals("testerror")) {
        throw new RuntimeException("testerror");
    }
    String message = currentTraceContext != null ? currentTraceContext.traceIdString() : "";
    HelloReply reply = HelloReply.newBuilder().setMessage(message).build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
}
Also used : TraceContext(brave.propagation.TraceContext) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 33 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project brave by openzipkin.

the class ITTracingClientInterceptor method clientParserTestStreamingResponse.

@Test
public void clientParserTestStreamingResponse() throws Exception {
    closeClient(client);
    tracing = tracing.toBuilder().clientParser(new GrpcClientParser() {

        int receiveCount = 0;

        @Override
        protected <M> void onMessageReceived(M message, SpanCustomizer span) {
            span.tag("grpc.message_received." + receiveCount++, message.toString());
        }
    }).build();
    client = newClient();
    Iterator<HelloReply> replies = GreeterGrpc.newBlockingStub(client).sayHelloWithManyReplies(HelloRequest.newBuilder().setName("this is dog").build());
    assertThat(replies).hasSize(10);
    Span span = spans.take();
    // all response messages are tagged to the same span
    assertThat(span.tags()).hasSize(10);
}
Also used : HelloReply(io.grpc.examples.helloworld.HelloReply) SpanCustomizer(brave.SpanCustomizer) Span(zipkin2.Span) Test(org.junit.Test)

Example 34 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    VertxServer server = VertxServerBuilder.forPort(vertx, 8080).addService(new GreeterGrpc.GreeterVertxImplBase() {

        @Override
        public void sayHello(HelloRequest request, Future<HelloReply> future) {
            System.out.println("Hello " + request.getName());
            future.complete(HelloReply.newBuilder().setMessage(request.getName()).build());
        }
    }).useSsl(options -> options.setSsl(true).setUseAlpn(true).setKeyStoreOptions(new JksOptions().setPath("tls/server-keystore.jks").setPassword("wibble"))).build();
    server.start(ar -> {
        if (ar.succeeded()) {
            System.out.println("gRPC service started");
        } else {
            System.out.println("Could not start server " + ar.cause().getMessage());
        }
    });
}
Also used : AbstractVerticle(io.vertx.core.AbstractVerticle) JksOptions(io.vertx.core.net.JksOptions) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Runner(io.vertx.example.grpc.util.Runner) VertxServerBuilder(io.vertx.grpc.VertxServerBuilder) Future(io.vertx.core.Future) HelloReply(io.grpc.examples.helloworld.HelloReply) VertxServer(io.vertx.grpc.VertxServer) JksOptions(io.vertx.core.net.JksOptions) VertxServer(io.vertx.grpc.VertxServer) HelloRequest(io.grpc.examples.helloworld.HelloRequest) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 35 with HelloReply

use of io.grpc.examples.helloworld.HelloReply in project grpc-java by grpc.

the class SafeMethodCachingInterceptorTest method unsafeCallsAreNotCached.

@Test
public void unsafeCallsAreNotCached() {
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
    HelloReply reply1 = stub.sayHello(message);
    HelloReply reply2 = stub.sayHello(message);
    assertNotEquals(reply1, reply2);
}
Also used : GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) AnotherGreeterGrpc(io.grpc.examples.helloworld.AnotherGreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Aggregations

HelloReply (io.grpc.examples.helloworld.HelloReply)55 Test (org.junit.Test)30 HelloRequest (io.grpc.examples.helloworld.HelloRequest)20 StatusRuntimeException (io.grpc.StatusRuntimeException)8 GreeterGrpc (io.grpc.examples.helloworld.GreeterGrpc)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Metadata (io.grpc.Metadata)5 Status (io.grpc.Status)5 GreeterFutureStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub)4 SpanCustomizer (brave.SpanCustomizer)3 ManagedChannel (io.grpc.ManagedChannel)3 Span (zipkin2.Span)3 TraceContext (brave.propagation.TraceContext)2 ClientCall (io.grpc.ClientCall)2 Server (io.grpc.Server)2 GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HostnameGreeter (io.grpc.examples.hostname.HostnameGreeter)2 StreamObserver (io.grpc.stub.StreamObserver)2 ExecutionException (java.util.concurrent.ExecutionException)2 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1