Search in sources :

Example 6 with CallAttemptsTracerFactory

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

the class CensusModulesTest method testClientInterceptors.

// Test that Census ClientInterceptors uses the TagContext and Span out of the current Context
// to create the ClientCallTracer, and that it intercepts ClientCall.Listener.onClose() to call
// ClientCallTracer.callEnded().
private void testClientInterceptors(boolean nonDefaultContext) {
    grpcServerRule.getServiceRegistry().addService(ServerServiceDefinition.builder("package1.service2").addMethod(method, new ServerCallHandler<String, String>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, String> call, Metadata headers) {
            call.sendHeaders(new Metadata());
            call.sendMessage("Hello");
            call.close(Status.PERMISSION_DENIED.withDescription("No you don't"), new Metadata());
            return mockServerCallListener;
        }
    }).build());
    final AtomicReference<CallOptions> capturedCallOptions = new AtomicReference<>();
    ClientInterceptor callOptionsCaptureInterceptor = new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            capturedCallOptions.set(callOptions);
            return next.newCall(method, callOptions);
        }
    };
    Channel interceptedChannel = ClientInterceptors.intercept(grpcServerRule.getChannel(), callOptionsCaptureInterceptor, censusStats.getClientInterceptor(), censusTracing.getClientInterceptor());
    ClientCall<String, String> call;
    if (nonDefaultContext) {
        Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
        ctx = ContextUtils.withValue(ctx, fakeClientParentSpan);
        Context origCtx = ctx.attach();
        try {
            call = interceptedChannel.newCall(method, CALL_OPTIONS);
        } finally {
            ctx.detach(origCtx);
        }
    } else {
        assertEquals(io.opencensus.tags.unsafe.ContextUtils.getValue(Context.ROOT), io.opencensus.tags.unsafe.ContextUtils.getValue(Context.current()));
        assertEquals(ContextUtils.getValue(Context.current()), BlankSpan.INSTANCE);
        call = interceptedChannel.newCall(method, CALL_OPTIONS);
    }
    // The interceptor adds tracer factory to CallOptions
    assertEquals("customvalue", capturedCallOptions.get().getOption(CUSTOM_OPTION));
    assertEquals(2, capturedCallOptions.get().getStreamTracerFactories().size());
    assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(0) instanceof CallAttemptsTracerFactory);
    assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(1) instanceof CensusStatsModule.CallAttemptsTracerFactory);
    // Make the call
    Metadata headers = new Metadata();
    call.start(mockClientCallListener, headers);
    StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
    assertNotNull(record);
    TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    if (nonDefaultContext) {
        TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra value", extraTag.asString());
        assertEquals(2, record.tags.size());
    } else {
        assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
        assertEquals(1, record.tags.size());
    }
    if (nonDefaultContext) {
        verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
        verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    } else {
        verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNotNull());
        verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    }
    verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
    // End the call
    call.halfClose();
    call.request(1);
    verify(mockClientCallListener).onClose(statusCaptor.capture(), any(Metadata.class));
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.PERMISSION_DENIED, status.getCode());
    assertEquals("No you don't", status.getDescription());
    // The intercepting listener calls callEnded() on ClientCallTracer, which records to Census.
    record = statsRecorder.pollRecord();
    assertNotNull(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.PERMISSION_DENIED.toString(), statusTag.asString());
    if (nonDefaultContext) {
        TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra value", extraTag.asString());
    } else {
        assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
    }
    verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.PERMISSION_DENIED.withDescription("No you don't")).setSampleToLocalSpanStore(false).build());
    verify(spyClientSpan, never()).end();
    assertZeroRetryRecorded();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) Status(io.grpc.Status) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) EndSpanOptions(io.opencensus.trace.EndSpanOptions) ClientInterceptor(io.grpc.ClientInterceptor) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)

Example 7 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory 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 8 with CallAttemptsTracerFactory

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

the class CensusModulesTest method clientStreamNeverCreatedStillRecordTracing.

@Test
public void clientStreamNeverCreatedStillRecordTracing() {
    CallAttemptsTracerFactory callTracer = censusTracing.newClientCallTracer(fakeClientParentSpan, method);
    verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
    verify(spyClientSpanBuilder).setRecordEvents(eq(true));
    callTracer.callEnded(Status.DEADLINE_EXCEEDED.withDescription("3 seconds"));
    verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.DEADLINE_EXCEEDED.withDescription("3 seconds")).setSampleToLocalSpanStore(false).build());
    verifyNoMoreInteractions(spyClientSpan);
}
Also used : CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 9 with CallAttemptsTracerFactory

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

the class CensusModulesTest method traceHeaders_propagateSpanContext.

@Test
public void traceHeaders_propagateSpanContext() throws Exception {
    CallAttemptsTracerFactory callTracer = censusTracing.newClientCallTracer(fakeClientParentSpan, method);
    Metadata headers = new Metadata();
    ClientStreamTracer streamTracer = callTracer.newClientStreamTracer(STREAM_INFO, headers);
    streamTracer.streamCreated(Attributes.EMPTY, headers);
    assertThat(headers.keys()).isNotEmpty();
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 10 with CallAttemptsTracerFactory

use of io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory 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)

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