Search in sources :

Example 6 with ServerStreamTracer

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

the class CensusModulesTest method traceHeadersPropagateSpanContext.

@Test
public void traceHeadersPropagateSpanContext() throws Exception {
    CallAttemptsTracerFactory callTracer = censusTracing.newClientCallTracer(fakeClientParentSpan, method);
    Metadata headers = new Metadata();
    ClientStreamTracer streamTracer = callTracer.newClientStreamTracer(STREAM_INFO, headers);
    streamTracer.streamCreated(Attributes.EMPTY, headers);
    verify(mockTracingPropagationHandler).toByteArray(same(fakeAttemptSpanContext));
    verifyNoMoreInteractions(mockTracingPropagationHandler);
    verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
    verify(tracer).spanBuilderWithExplicitParent(eq("Attempt.package1.service2.method3"), same(spyClientSpan));
    verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    verifyNoMoreInteractions(tracer);
    assertTrue(headers.containsKey(censusTracing.tracingHeader));
    ServerStreamTracer serverTracer = censusTracing.getServerTracerFactory().newServerStreamTracer(method.getFullMethodName(), headers);
    verify(mockTracingPropagationHandler).fromByteArray(same(binarySpanContext));
    verify(tracer).spanBuilderWithRemoteParent(eq("Recv.package1.service2.method3"), same(spyAttemptSpan.getContext()));
    verify(spyServerSpanBuilder).setRecordEvents(eq(true));
    Context filteredContext = serverTracer.filterContext(Context.ROOT);
    assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) ClientStreamTracer(io.grpc.ClientStreamTracer) ServerStreamTracer(io.grpc.ServerStreamTracer) Metadata(io.grpc.Metadata) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 7 with ServerStreamTracer

use of io.grpc.ServerStreamTracer 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)

Example 8 with ServerStreamTracer

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

the class CensusModulesTest method subtestServerBasicStatsNoHeaders.

private void subtestServerBasicStatsNoHeaders(boolean recordStarts, boolean recordFinishes, boolean recordRealTime) {
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), true, recordStarts, recordFinishes, recordRealTime, true);
    ServerStreamTracer.Factory tracerFactory = localCensusStats.getServerTracerFactory();
    ServerStreamTracer tracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
    if (recordStarts) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        assertEquals(1, record.tags.size());
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_STARTED_COUNT));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
    Context filteredContext = tracer.filterContext(Context.ROOT);
    TagContext statsCtx = io.opencensus.tags.unsafe.ContextUtils.getValue(filteredContext);
    assertEquals(tagger.emptyBuilder().putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, TagValue.create(method.getFullMethodName())).build(), statsCtx);
    tracer.inboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(34);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 34, recordRealTime, false);
    tracer.inboundUncompressedSize(67);
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 1028, recordRealTime, false);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(16, MILLISECONDS);
    tracer.inboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(154);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 154, recordRealTime, false);
    tracer.inboundUncompressedSize(552);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(99);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 99, recordRealTime, false);
    tracer.outboundUncompressedSize(865);
    fakeClock.forwardTime(24, MILLISECONDS);
    tracer.streamClosed(Status.CANCELLED);
    if (recordFinishes) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_STATUS);
        assertEquals(Status.Code.CANCELLED.toString(), statusTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_FINISHED_COUNT));
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT));
        assertEquals(1028 + 99, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES));
        assertEquals(1128 + 865, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT));
        assertEquals(34 + 154, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES));
        assertEquals(67 + 552, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(100 + 16 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) ServerStreamTracer(io.grpc.ServerStreamTracer) TagContext(io.opencensus.tags.TagContext) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue)

Aggregations

ServerStreamTracer (io.grpc.ServerStreamTracer)8 Metadata (io.grpc.Metadata)7 Context (io.grpc.Context)6 Test (org.junit.Test)5 TagContext (io.opencensus.tags.TagContext)4 SpanContext (io.opencensus.trace.SpanContext)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)2 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)2 TagValue (io.opencensus.tags.TagValue)2 ManagedChannel (io.grpc.ManagedChannel)1 ServerInterceptor (io.grpc.ServerInterceptor)1 StreamTracer (io.grpc.StreamTracer)1 CallMetricRecorder (io.grpc.services.CallMetricRecorder)1 InternalCallMetricRecorder (io.grpc.services.InternalCallMetricRecorder)1 StreamObserver (io.grpc.stub.StreamObserver)1 SimpleRequest (io.grpc.testing.protobuf.SimpleRequest)1 SimpleResponse (io.grpc.testing.protobuf.SimpleResponse)1 SimpleServiceGrpc (io.grpc.testing.protobuf.SimpleServiceGrpc)1 EndSpanOptions (io.opencensus.trace.EndSpanOptions)1