Search in sources :

Example 1 with CallOptions

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions in project grpc-java by grpc.

the class TransportCompressionTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", getPort()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).decompressorRegistry(decompressors).compressorRegistry(compressors).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            final ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new ForwardingClientCall<ReqT, RespT>() {

                @Override
                protected ClientCall<ReqT, RespT> delegate() {
                    return call;
                }

                @Override
                public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() {

                        @Override
                        protected io.grpc.ClientCall.Listener<RespT> delegate() {
                            return responseListener;
                        }

                        @Override
                        public void onHeaders(Metadata headers) {
                            super.onHeaders(headers);
                            if (expectFzip) {
                                String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
                                assertEquals(encoding, FZIPPER.getMessageEncoding());
                            }
                        }
                    };
                    super.start(listener, headers);
                    setMessageCompression(true);
                }
            };
        }
    }).usePlaintext(true);
    io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
    return builder.build();
}
Also used : ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) MethodDescriptor(io.grpc.MethodDescriptor) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener)

Example 2 with CallOptions

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions in project google-cloud-java by GoogleCloudPlatform.

the class WatchdogInterceptor method newDefaultWatchdogInterceptor.

/**
   * Creates a default instance based on the system property {@code
   * com.google.cloud.spanner.watchdogTimeoutSeconds}, or a no-op interceptor if none configured.
   */
@Nullable
static ClientInterceptor newDefaultWatchdogInterceptor() {
    int timeoutSeconds = systemProperty(PROPERTY_TIMEOUT_SECONDS, DEFAULT_TIMEOUT_SECONDS);
    if (timeoutSeconds <= 0) {
        return new ClientInterceptor() {

            @Override
            public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
                return channel.newCall(methodDescriptor, callOptions);
            }
        };
    }
    int periodSeconds = systemProperty(PROPERTY_PERIOD_SECONDS, DEFAULT_PERIOD_SECONDS);
    final WatchdogInterceptor interceptor = new WatchdogInterceptor(timeoutSeconds, TimeUnit.SECONDS);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Cloud-Spanner-WatchdogInterceptor-%d").build());
    executor.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            interceptor.tick();
        }
    }, periodSeconds, periodSeconds, TimeUnit.SECONDS);
    logger.log(Level.FINE, "Created watchdog interceptor with activity timeout of {0}s and period {1}s", new Object[] { timeoutSeconds, periodSeconds });
    return interceptor;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Channel(io.grpc.Channel) ClientInterceptor(io.grpc.ClientInterceptor) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Nullable(javax.annotation.Nullable)

Example 3 with CallOptions

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions in project brave by openzipkin.

the class ITTracingServerInterceptor method usesExistingTraceId.

@Test
public void usesExistingTraceId() throws Exception {
    final String traceId = "463ac35c9f6413ad";
    final String parentId = traceId;
    final String spanId = "48485a3953bb6124";
    Channel channel = ClientInterceptors.intercept(client, new ClientInterceptor() {

        @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) {
                    headers.put(Key.of("X-B3-TraceId", ASCII_STRING_MARSHALLER), traceId);
                    headers.put(Key.of("X-B3-ParentSpanId", ASCII_STRING_MARSHALLER), parentId);
                    headers.put(Key.of("X-B3-SpanId", ASCII_STRING_MARSHALLER), spanId);
                    headers.put(Key.of("X-B3-Sampled", ASCII_STRING_MARSHALLER), "1");
                    super.start(responseListener, headers);
                }
            };
        }
    });
    GreeterGrpc.newBlockingStub(channel).sayHello(HELLO_REQUEST);
    Span span = spans.take();
    assertThat(span.traceId()).isEqualTo(traceId);
    assertThat(span.parentId()).isEqualTo(parentId);
    assertThat(span.id()).isEqualTo(spanId);
    assertThat(span.shared()).isTrue();
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Span(zipkin2.Span) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) Test(org.junit.Test)

Example 4 with CallOptions

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions in project brave by openzipkin.

the class ITTracingServerInterceptor method createsChildWhenJoinDisabled.

@Test
public void createsChildWhenJoinDisabled() throws Exception {
    grpcTracing = GrpcTracing.create(tracingBuilder(NEVER_SAMPLE).supportsJoin(false).build());
    init();
    final String traceId = "463ac35c9f6413ad";
    final String parentId = traceId;
    final String spanId = "48485a3953bb6124";
    Channel channel = ClientInterceptors.intercept(client, new ClientInterceptor() {

        @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) {
                    headers.put(Key.of("X-B3-TraceId", ASCII_STRING_MARSHALLER), traceId);
                    headers.put(Key.of("X-B3-ParentSpanId", ASCII_STRING_MARSHALLER), parentId);
                    headers.put(Key.of("X-B3-SpanId", ASCII_STRING_MARSHALLER), spanId);
                    headers.put(Key.of("X-B3-Sampled", ASCII_STRING_MARSHALLER), "1");
                    super.start(responseListener, headers);
                }
            };
        }
    });
    GreeterGrpc.newBlockingStub(channel).sayHello(HELLO_REQUEST);
    Span span = spans.take();
    assertThat(span.traceId()).isEqualTo(traceId);
    assertThat(span.parentId()).isEqualTo(spanId);
    assertThat(span.id()).isNotEqualTo(spanId);
    assertThat(span.shared()).isNull();
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Span(zipkin2.Span) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) Test(org.junit.Test)

Example 5 with CallOptions

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions in project brave by openzipkin.

the class BaseITTracingClientInterceptor method initMessageTaggingClient.

void initMessageTaggingClient() {
    SpanCustomizer customizer = CurrentSpanCustomizer.create(tracing);
    AtomicInteger sends = new AtomicInteger(1);
    AtomicInteger recvs = new AtomicInteger(1);
    closeClient(client);
    client = newClient(new ClientInterceptor() {

        @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) {
                    super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {

                        @Override
                        public void onMessage(RespT message) {
                            customizer.tag("grpc.message_recv." + recvs.getAndIncrement(), message.toString());
                            delegate().onMessage(message);
                        }
                    }, headers);
                }

                @Override
                public void sendMessage(ReqT message) {
                    customizer.tag("grpc.message_send." + sends.getAndIncrement(), message.toString());
                    delegate().sendMessage(message);
                }
            };
        }
    }, grpcTracing.newClientInterceptor());
}
Also used : SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientInterceptor(io.grpc.ClientInterceptor) CurrentSpanCustomizer(brave.CurrentSpanCustomizer) SpanCustomizer(brave.SpanCustomizer) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall)

Aggregations

CallOptions (io.grpc.CallOptions)81 Test (org.junit.Test)61 Metadata (io.grpc.Metadata)50 Channel (io.grpc.Channel)36 MethodDescriptor (io.grpc.MethodDescriptor)28 ClientInterceptor (io.grpc.ClientInterceptor)23 ManagedChannel (io.grpc.ManagedChannel)18 ClientStreamTracer (io.grpc.ClientStreamTracer)17 ClientCall (io.grpc.ClientCall)15 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)11 Status (io.grpc.Status)11 Context (io.grpc.Context)10 Subchannel (io.grpc.LoadBalancer.Subchannel)10 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)10 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)9 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)8 NoopClientCall (io.grpc.internal.NoopClientCall)8 SocketAddress (java.net.SocketAddress)8 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)7 ByteString (com.google.protobuf.ByteString)6