Search in sources :

Example 1 with ServerStreamTracer

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

the class CensusModulesTest method serverTracingSampledToLocalSpanStore.

@Test
public void serverTracingSampledToLocalSpanStore() {
    ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
    ServerStreamTracer serverStreamTracer = tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());
    serverStreamTracer.filterContext(Context.ROOT);
    serverStreamTracer.serverCallStarted(new CallInfo<>(sampledMethod, Attributes.EMPTY, null));
    serverStreamTracer.streamClosed(Status.CANCELLED);
    verify(spyServerSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.CANCELLED).setSampleToLocalSpanStore(true).build());
}
Also used : ServerStreamTracer(io.grpc.ServerStreamTracer) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 2 with ServerStreamTracer

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

the class CensusModulesTest method serverBasicTracingNoHeaders.

@Test
public void serverBasicTracingNoHeaders() {
    ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
    ServerStreamTracer serverStreamTracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
    verifyNoInteractions(mockTracingPropagationHandler);
    verify(tracer).spanBuilderWithRemoteParent(eq("Recv.package1.service2.method3"), ArgumentMatchers.<SpanContext>isNull());
    verify(spyServerSpanBuilder).setRecordEvents(eq(true));
    Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
    assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
    serverStreamTracer.serverCallStarted(new CallInfo<>(method, Attributes.EMPTY, null));
    verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
    serverStreamTracer.outboundMessage(0);
    serverStreamTracer.outboundMessageSent(0, 882, -1);
    serverStreamTracer.inboundMessage(0);
    serverStreamTracer.outboundMessage(1);
    serverStreamTracer.outboundMessageSent(1, -1, 27);
    serverStreamTracer.inboundMessageRead(0, 255, 90);
    serverStreamTracer.streamClosed(Status.CANCELLED);
    InOrder inOrder = inOrder(spyServerSpan);
    inOrder.verify(spyServerSpan, 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(spyServerSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.CANCELLED).setSampleToLocalSpanStore(false).build());
    verifyNoMoreInteractions(spyServerSpan);
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) InOrder(org.mockito.InOrder) ServerStreamTracer(io.grpc.ServerStreamTracer) MessageEvent(io.opencensus.trace.MessageEvent) EndSpanOptions(io.opencensus.trace.EndSpanOptions) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with ServerStreamTracer

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

the class OrcaMetricReportingServerInterceptorTest method shareCallMetricRecorderInContext.

@Test
public void shareCallMetricRecorderInContext() throws IOException {
    final CallMetricRecorder callMetricRecorder = InternalCallMetricRecorder.newCallMetricRecorder();
    ServerStreamTracer.Factory callMetricRecorderSharingStreamTracerFactory = new ServerStreamTracer.Factory() {

        @Override
        public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) {
            return new ServerStreamTracer() {

                @Override
                public Context filterContext(Context context) {
                    return context.withValue(InternalCallMetricRecorder.CONTEXT_KEY, callMetricRecorder);
                }
            };
        }
    };
    final AtomicReference<CallMetricRecorder> callMetricRecorderCapture = new AtomicReference<>();
    SimpleServiceGrpc.SimpleServiceImplBase simpleServiceImpl = new SimpleServiceGrpc.SimpleServiceImplBase() {

        @Override
        public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
            callMetricRecorderCapture.set(CallMetricRecorder.getCurrent());
            SimpleResponse response = SimpleResponse.newBuilder().setResponseMessage("Simple response").build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    };
    ServerInterceptor metricReportingServerInterceptor = new OrcaMetricReportingServerInterceptor();
    String serverName = InProcessServerBuilder.generateName();
    grpcCleanupRule.register(InProcessServerBuilder.forName(serverName).directExecutor().addStreamTracerFactory(callMetricRecorderSharingStreamTracerFactory).addService(ServerInterceptors.intercept(simpleServiceImpl, metricReportingServerInterceptor)).build().start());
    ManagedChannel channel = grpcCleanupRule.register(InProcessChannelBuilder.forName(serverName).build());
    ClientCalls.blockingUnaryCall(channel, SIMPLE_METHOD, CallOptions.DEFAULT, REQUEST);
    assertThat(callMetricRecorderCapture.get()).isSameInstanceAs(callMetricRecorder);
}
Also used : Context(io.grpc.Context) StreamObserver(io.grpc.stub.StreamObserver) ServerStreamTracer(io.grpc.ServerStreamTracer) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleServiceGrpc(io.grpc.testing.protobuf.SimpleServiceGrpc) SimpleRequest(io.grpc.testing.protobuf.SimpleRequest) SimpleResponse(io.grpc.testing.protobuf.SimpleResponse) ServerInterceptor(io.grpc.ServerInterceptor) ManagedChannel(io.grpc.ManagedChannel) InternalCallMetricRecorder(io.grpc.services.InternalCallMetricRecorder) CallMetricRecorder(io.grpc.services.CallMetricRecorder) Test(org.junit.Test)

Example 4 with ServerStreamTracer

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

the class StatsTraceContext method serverFilterContext.

/**
 * See {@link ServerStreamTracer#filterContext}.  For server-side only.
 *
 * <p>Called from {@link io.grpc.internal.ServerImpl}.
 */
public <ReqT, RespT> Context serverFilterContext(Context context) {
    Context ctx = checkNotNull(context, "context");
    for (StreamTracer tracer : tracers) {
        ctx = ((ServerStreamTracer) tracer).filterContext(ctx);
        checkNotNull(ctx, "%s returns null context", tracer);
    }
    return ctx;
}
Also used : Context(io.grpc.Context) ServerStreamTracer(io.grpc.ServerStreamTracer) StreamTracer(io.grpc.StreamTracer) ClientStreamTracer(io.grpc.ClientStreamTracer)

Example 5 with ServerStreamTracer

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

the class CensusModulesTest method serverTracingNotSampledToLocalSpanStore_whenServerCallNotCreated.

@Test
public void serverTracingNotSampledToLocalSpanStore_whenServerCallNotCreated() {
    ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
    ServerStreamTracer serverStreamTracer = tracerFactory.newServerStreamTracer(sampledMethod.getFullMethodName(), new Metadata());
    serverStreamTracer.streamClosed(Status.CANCELLED);
    verify(spyServerSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.CANCELLED).setSampleToLocalSpanStore(false).build());
}
Also used : ServerStreamTracer(io.grpc.ServerStreamTracer) Metadata(io.grpc.Metadata) Test(org.junit.Test)

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