Search in sources :

Example 11 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory in project grpc-java by grpc.

the class CensusModulesTest method recordRetryStats.

// This test is only unit-testing the stat recording logic. The retry behavior is faked.
@Test
public void recordRetryStats() {
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), true, true, true, true, true);
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(localCensusStats, tagger.empty(), method.getFullMethodName());
    ClientStreamTracer tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
    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));
    fakeClock.forwardTime(30, MILLISECONDS);
    tracer.outboundHeaders();
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, 1028, true, true);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(24, MILLISECONDS);
    tracer.streamClosed(Status.UNAVAILABLE);
    record = statsRecorder.pollRecord();
    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.UNAVAILABLE.toString(), statusTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
    assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
    assertEquals(1028, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(1128, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(30 + 100 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    // faking retry
    fakeClock.forwardTime(1000, MILLISECONDS);
    tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    record = statsRecorder.pollRecord();
    assertEquals(1, record.tags.size());
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    tracer.outboundHeaders();
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, 1028, true, true);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.streamClosed(Status.NOT_FOUND);
    record = statsRecorder.pollRecord();
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.NOT_FOUND.toString(), statusTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
    assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
    assertEquals(1028, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(1128, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(100, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    // fake transparent retry
    fakeClock.forwardTime(10, MILLISECONDS);
    tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO.toBuilder().setIsTransparentRetry(true).build(), new Metadata());
    record = statsRecorder.pollRecord();
    assertEquals(1, record.tags.size());
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    tracer.streamClosed(Status.UNAVAILABLE);
    record = statsRecorder.pollRecord();
    statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.UNAVAILABLE.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));
    // fake another transparent retry
    fakeClock.forwardTime(10, MILLISECONDS);
    tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO.toBuilder().setIsTransparentRetry(true).build(), new Metadata());
    record = statsRecorder.pollRecord();
    assertEquals(1, record.tags.size());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    tracer.outboundHeaders();
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD, 1, true, true);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_SENT_BYTES_PER_METHOD, 1028, true, true);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(16, MILLISECONDS);
    tracer.inboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_MESSAGES_PER_METHOD, 1, true, true);
    tracer.inboundWireSize(33);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_BYTES_PER_METHOD, 33, true, true);
    tracer.inboundUncompressedSize(67);
    fakeClock.forwardTime(24, MILLISECONDS);
    // RPC succeeded
    tracer.streamClosed(Status.OK);
    callAttemptsTracerFactory.callEnded(Status.OK);
    record = statsRecorder.pollRecord();
    statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.OK.toString(), statusTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
    assertThat(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT)).isNull();
    assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
    assertEquals(1028, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(1128, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
    assertEquals(33, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
    assertEquals(67, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
    assertEquals(16 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    record = statsRecorder.pollRecord();
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.OK.toString(), statusTag.asString());
    assertThat(record.getMetric(RETRIES_PER_CALL)).isEqualTo(1);
    assertThat(record.getMetric(TRANSPARENT_RETRIES_PER_CALL)).isEqualTo(2);
    assertThat(record.getMetric(RETRY_DELAY_PER_CALL)).isEqualTo(1000D + 10 + 10);
}
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) Test(org.junit.Test)

Example 12 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory in project grpc-java by grpc.

the class CensusModulesTest method traceHeaders_missingCensusImpl_notPropagateSpanContext.

@Test
public void traceHeaders_missingCensusImpl_notPropagateSpanContext() throws Exception {
    reset(spyClientSpanBuilder);
    reset(spyAttemptSpanBuilder);
    when(spyClientSpanBuilder.startSpan()).thenReturn(BlankSpan.INSTANCE);
    when(spyAttemptSpanBuilder.startSpan()).thenReturn(BlankSpan.INSTANCE);
    Metadata headers = new Metadata();
    CallAttemptsTracerFactory callTracer = censusTracing.newClientCallTracer(BlankSpan.INSTANCE, method);
    callTracer.newClientStreamTracer(STREAM_INFO, headers).streamCreated(Attributes.EMPTY, headers);
    assertThat(headers.keys()).isEmpty();
}
Also used : Metadata(io.grpc.Metadata) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 13 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory in project grpc-java by grpc.

the class CensusModulesTest method clientBasicTracingDefaultSpan.

@Test
public void clientBasicTracingDefaultSpan() {
    CallAttemptsTracerFactory callTracer = censusTracing.newClientCallTracer(null, method);
    Metadata headers = new Metadata();
    ClientStreamTracer clientStreamTracer = callTracer.newClientStreamTracer(STREAM_INFO, headers);
    clientStreamTracer.streamCreated(Attributes.EMPTY, headers);
    verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNull());
    verify(tracer).spanBuilderWithExplicitParent(eq("Attempt.package1.service2.method3"), eq(spyClientSpan));
    verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
    verify(spyAttemptSpan, never()).end(any(EndSpanOptions.class));
    clientStreamTracer.outboundMessage(0);
    clientStreamTracer.outboundMessageSent(0, 882, -1);
    clientStreamTracer.inboundMessage(0);
    clientStreamTracer.outboundMessage(1);
    clientStreamTracer.outboundMessageSent(1, -1, 27);
    clientStreamTracer.inboundMessageRead(0, 255, 90);
    clientStreamTracer.streamClosed(Status.OK);
    callTracer.callEnded(Status.OK);
    InOrder inOrder = inOrder(spyClientSpan, spyAttemptSpan);
    inOrder.verify(spyAttemptSpan).putAttribute("previous-rpc-attempts", AttributeValue.longAttributeValue(0));
    inOrder.verify(spyAttemptSpan).putAttribute("transparent-retry", AttributeValue.booleanAttributeValue(false));
    inOrder.verify(spyAttemptSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
    List<MessageEvent> events = messageEventCaptor.getAllValues();
    assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
    assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
    assertEquals(MessageEvent.builder(MessageEvent.Type.RECEIVED, 0).setCompressedMessageSize(255).setUncompressedMessageSize(90).build(), events.get(2));
    inOrder.verify(spyAttemptSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.OK).setSampleToLocalSpanStore(false).build());
    inOrder.verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.OK).setSampleToLocalSpanStore(false).build());
    inOrder.verifyNoMoreInteractions();
    verifyNoMoreInteractions(tracer);
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) InOrder(org.mockito.InOrder) MessageEvent(io.opencensus.trace.MessageEvent) EndSpanOptions(io.opencensus.trace.EndSpanOptions) Metadata(io.grpc.Metadata) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 14 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory in project grpc-java by grpc.

the class CensusModulesTest method subtestStatsHeadersPropagateTags.

private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordStats) {
    // EXTRA_TAG is propagated by the FakeStatsContextFactory. Note that not all tags are
    // propagated.  The StatsContextFactory decides which tags are to propagated.  gRPC facilitates
    // the propagation by putting them in the headers.
    TagContext clientCtx = tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra-tag-value-897")).build();
    CensusStatsModule census = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), propagate, recordStats, recordStats, recordStats, recordStats);
    Metadata headers = new Metadata();
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(census, clientCtx, method.getFullMethodName());
    // This propagates clientCtx to headers if propagates==true
    ClientStreamTracer streamTracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, headers);
    streamTracer.streamCreated(Attributes.EMPTY, headers);
    if (recordStats) {
        // Client upstart record
        StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
        assertNotNull(clientRecord);
        assertNoServerContent(clientRecord);
        assertEquals(2, clientRecord.tags.size());
        TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
        assertEquals(method.getFullMethodName(), clientMethodTag.asString());
        TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", clientPropagatedTag.asString());
    }
    if (propagate) {
        assertTrue(headers.containsKey(census.statsHeader));
    } else {
        assertFalse(headers.containsKey(census.statsHeader));
        return;
    }
    ServerStreamTracer serverTracer = census.getServerTracerFactory().newServerStreamTracer(method.getFullMethodName(), headers);
    // Server tracer deserializes clientCtx from the headers, so that it records stats with the
    // propagated tags.
    Context serverContext = serverTracer.filterContext(Context.ROOT);
    // It also put clientCtx in the Context seen by the call handler
    assertEquals(tagger.toBuilder(clientCtx).putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, TagValue.create(method.getFullMethodName())).build(), io.opencensus.tags.unsafe.ContextUtils.getValue(serverContext));
    // Verifies that the server tracer records the status with the propagated tag
    serverTracer.streamClosed(Status.OK);
    if (recordStats) {
        // Server upstart record
        StatsTestUtils.MetricsRecord serverRecord = statsRecorder.pollRecord();
        assertNotNull(serverRecord);
        assertNoClientContent(serverRecord);
        assertEquals(2, serverRecord.tags.size());
        TagValue serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), serverMethodTag.asString());
        TagValue serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", serverPropagatedTag.asString());
        // Server completion record
        serverRecord = statsRecorder.pollRecord();
        assertNotNull(serverRecord);
        assertNoClientContent(serverRecord);
        serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), serverMethodTag.asString());
        TagValue serverStatusTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_STATUS);
        assertEquals(Status.Code.OK.toString(), serverStatusTag.asString());
        assertNull(serverRecord.getMetric(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT));
        serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", serverPropagatedTag.asString());
    }
    // Verifies that the client tracer factory uses clientCtx, which includes the custom tags, to
    // record stats.
    streamTracer.streamClosed(Status.OK);
    callAttemptsTracerFactory.callEnded(Status.OK);
    if (recordStats) {
        // Client completion record
        StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
        assertNotNull(clientRecord);
        assertNoServerContent(clientRecord);
        TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
        assertEquals(method.getFullMethodName(), clientMethodTag.asString());
        TagValue clientStatusTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        assertEquals(Status.Code.OK.toString(), clientStatusTag.asString());
        assertNull(clientRecord.getMetric(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
        TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", clientPropagatedTag.asString());
        assertZeroRetryRecorded();
    }
    if (!recordStats) {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) ClientStreamTracer(io.grpc.ClientStreamTracer) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) ServerStreamTracer(io.grpc.ServerStreamTracer) TagContext(io.opencensus.tags.TagContext) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)

Aggregations

CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)14 Metadata (io.grpc.Metadata)12 Test (org.junit.Test)11 ClientStreamTracer (io.grpc.ClientStreamTracer)8 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)5 TagValue (io.opencensus.tags.TagValue)5 Context (io.grpc.Context)3 Status (io.grpc.Status)3 TagContext (io.opencensus.tags.TagContext)3 SpanContext (io.opencensus.trace.SpanContext)3 ServerStreamTracer (io.grpc.ServerStreamTracer)2 EndSpanOptions (io.opencensus.trace.EndSpanOptions)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1 MethodDescriptor (io.grpc.MethodDescriptor)1 StatsComponentImpl (io.opencensus.impl.stats.StatsComponentImpl)1 StatsComponent (io.opencensus.stats.StatsComponent)1 MessageEvent (io.opencensus.trace.MessageEvent)1