Search in sources :

Example 6 with ClientStreamTracer

use of io.grpc.ClientStreamTracer in project grpc-java by grpc.

the class CensusModulesTest method subtestClientBasicStatsDefaultContext.

private void subtestClientBasicStatsDefaultContext(boolean recordStarts, boolean recordFinishes, boolean recordRealTime) {
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), true, recordStarts, recordFinishes, recordRealTime, true);
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(localCensusStats, tagger.empty(), method.getFullMethodName());
    Metadata headers = new Metadata();
    ClientStreamTracer tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, headers);
    if (recordStarts) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoServerContent(record);
        assertEquals(1, record.tags.size());
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
    fakeClock.forwardTime(30, MILLISECONDS);
    tracer.outboundHeaders();
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, true);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, 1028, recordRealTime, true);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(16, MILLISECONDS);
    tracer.inboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, true);
    tracer.inboundWireSize(33);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_BYTES_PER_METHOD, 33, recordRealTime, true);
    tracer.inboundUncompressedSize(67);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, true);
    tracer.outboundWireSize(99);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, 99, recordRealTime, true);
    tracer.outboundUncompressedSize(865);
    fakeClock.forwardTime(24, MILLISECONDS);
    tracer.inboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, true);
    tracer.inboundWireSize(154);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_BYTES_PER_METHOD, 154, recordRealTime, true);
    tracer.inboundUncompressedSize(552);
    tracer.streamClosed(Status.OK);
    callAttemptsTracerFactory.callEnded(Status.OK);
    if (recordFinishes) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoServerContent(record);
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        assertEquals(Status.Code.OK.toString(), statusTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
        assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
        assertEquals(1028 + 99, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
        assertEquals(1128 + 865, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
        assertEquals(33 + 154, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
        assertEquals(67 + 552, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
        assertEquals(30 + 100 + 16 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
        assertZeroRetryRecorded();
    } else {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)

Example 7 with ClientStreamTracer

use of io.grpc.ClientStreamTracer in project grpc-java by grpc.

the class CensusModulesTest method newTagsPopulateOldViews.

@Deprecated
@Test
public void newTagsPopulateOldViews() throws InterruptedException {
    StatsComponent localStats = new StatsComponentImpl();
    // Test views that contain both of the remap tags: method & status.
    localStats.getViewManager().registerView(RpcViewConstants.RPC_CLIENT_ERROR_COUNT_VIEW);
    localStats.getViewManager().registerView(RpcViewConstants.GRPC_CLIENT_COMPLETED_RPC_VIEW);
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, localStats.getStatsRecorder(), fakeClock.getStopwatchSupplier(), false, false, true, false, /* real-time */
    true);
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(localCensusStats, tagger.empty(), method.getFullMethodName());
    Metadata headers = new Metadata();
    ClientStreamTracer tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, headers);
    tracer.streamCreated(Attributes.EMPTY, headers);
    fakeClock.forwardTime(30, MILLISECONDS);
    Status status = Status.PERMISSION_DENIED.withDescription("No you don't");
    tracer.streamClosed(status);
    callAttemptsTracerFactory.callEnded(status);
    // Give OpenCensus a chance to update the views asynchronously.
    Thread.sleep(100);
    assertWithMessage("Legacy error count view had unexpected count").that(getAggregationValueAsLong(localStats, RpcViewConstants.RPC_CLIENT_ERROR_COUNT_VIEW, ImmutableList.of(TagValue.create("PERMISSION_DENIED"), TagValue.create(method.getFullMethodName())))).isEqualTo(1);
    assertWithMessage("New error count view had unexpected count").that(getAggregationValueAsLong(localStats, RpcViewConstants.GRPC_CLIENT_COMPLETED_RPC_VIEW, ImmutableList.of(TagValue.create(method.getFullMethodName()), TagValue.create("PERMISSION_DENIED")))).isEqualTo(1);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) StatsComponentImpl(io.opencensus.impl.stats.StatsComponentImpl) Metadata(io.grpc.Metadata) StatsComponent(io.opencensus.stats.StatsComponent) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 8 with ClientStreamTracer

use of io.grpc.ClientStreamTracer in project grpc-java by grpc.

the class CensusModulesTest method clientStreamNeverCreatedStillRecordStats.

@Test
public void clientStreamNeverCreatedStillRecordStats() {
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(censusStats, tagger.empty(), method.getFullMethodName());
    ClientStreamTracer streamTracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    fakeClock.forwardTime(3000, MILLISECONDS);
    Status status = Status.DEADLINE_EXCEEDED.withDescription("3 seconds");
    streamTracer.streamClosed(status);
    callAttemptsTracerFactory.callEnded(status);
    // Upstart record
    StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
    assertNotNull(record);
    assertNoServerContent(record);
    assertEquals(1, record.tags.size());
    TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    // Completion record
    record = statsRecorder.pollRecord();
    assertNotNull(record);
    assertNoServerContent(record);
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.DEADLINE_EXCEEDED.toString(), statusTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
    assertEquals(3000, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
    assertZeroRetryRecorded();
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 9 with ClientStreamTracer

use of io.grpc.ClientStreamTracer in project grpc-java by grpc.

the class GrpcUtil method getTransportFromPickResult.

/**
 * Returns a transport out of a PickResult, or {@code null} if the result is "buffer".
 */
@Nullable
static ClientTransport getTransportFromPickResult(PickResult result, boolean isWaitForReady) {
    final ClientTransport transport;
    Subchannel subchannel = result.getSubchannel();
    if (subchannel != null) {
        transport = ((TransportProvider) subchannel.getInternalSubchannel()).obtainActiveTransport();
    } else {
        transport = null;
    }
    if (transport != null) {
        final ClientStreamTracer.Factory streamTracerFactory = result.getStreamTracerFactory();
        if (streamTracerFactory == null) {
            return transport;
        }
        return new ClientTransport() {

            @Override
            public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers) {
                StreamInfo info = StreamInfo.newBuilder().setCallOptions(callOptions).build();
                ClientStreamTracer streamTracer = streamTracerFactory.newClientStreamTracer(info, headers);
                checkState(tracers[tracers.length - 1] == NOOP_TRACER, "lb tracer already assigned");
                tracers[tracers.length - 1] = streamTracer;
                return transport.newStream(method, headers, callOptions, tracers);
            }

            @Override
            public void ping(PingCallback callback, Executor executor) {
                transport.ping(callback, executor);
            }

            @Override
            public InternalLogId getLogId() {
                return transport.getLogId();
            }

            @Override
            public ListenableFuture<SocketStats> getStats() {
                return transport.getStats();
            }
        };
    }
    if (!result.getStatus().isOk()) {
        if (result.isDrop()) {
            return new FailingClientTransport(result.getStatus(), RpcProgress.DROPPED);
        }
        if (!isWaitForReady) {
            return new FailingClientTransport(result.getStatus(), RpcProgress.PROCESSED);
        }
    }
    return null;
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Executor(java.util.concurrent.Executor) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) SocketStats(io.grpc.InternalChannelz.SocketStats) Nullable(javax.annotation.Nullable)

Example 10 with ClientStreamTracer

use of io.grpc.ClientStreamTracer in project grpc-java by grpc.

the class TokenAttachingTracerFactory method newClientStreamTracer.

@Override
public ClientStreamTracer newClientStreamTracer(ClientStreamTracer.StreamInfo info, Metadata headers) {
    if (delegate == null) {
        return NOOP_TRACER;
    }
    final ClientStreamTracer clientStreamTracer = delegate.newClientStreamTracer(info, headers);
    class TokenPropagationTracer extends ForwardingClientStreamTracer {

        @Override
        protected ClientStreamTracer delegate() {
            return clientStreamTracer;
        }

        @Override
        public void streamCreated(Attributes transportAttrs, Metadata headers) {
            Attributes eagAttrs = checkNotNull(transportAttrs.get(GrpcAttributes.ATTR_CLIENT_EAG_ATTRS), "eagAttrs");
            String token = eagAttrs.get(GrpclbConstants.TOKEN_ATTRIBUTE_KEY);
            headers.discardAll(GrpclbConstants.TOKEN_METADATA_KEY);
            if (token != null) {
                headers.put(GrpclbConstants.TOKEN_METADATA_KEY, token);
            }
            delegate().streamCreated(transportAttrs, headers);
        }
    }
    return new TokenPropagationTracer();
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) ForwardingClientStreamTracer(io.grpc.internal.ForwardingClientStreamTracer) ForwardingClientStreamTracer(io.grpc.internal.ForwardingClientStreamTracer) GrpcAttributes(io.grpc.internal.GrpcAttributes) Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata)

Aggregations

ClientStreamTracer (io.grpc.ClientStreamTracer)57 Metadata (io.grpc.Metadata)54 Test (org.junit.Test)44 CallOptions (io.grpc.CallOptions)28 Subchannel (io.grpc.LoadBalancer.Subchannel)22 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)21 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)19 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)18 MethodDescriptor (io.grpc.MethodDescriptor)16 Status (io.grpc.Status)14 ChannelLogger (io.grpc.ChannelLogger)11 ClientTransportOptions (io.grpc.internal.ClientTransportFactory.ClientTransportOptions)10 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)9 StreamInfo (io.grpc.ClientStreamTracer.StreamInfo)8 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)8 SocketAddress (java.net.SocketAddress)8 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 Helper (io.grpc.LoadBalancer.Helper)7 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)7 ManagedChannel (io.grpc.ManagedChannel)7