Search in sources :

Example 6 with TagValue

use of com.google.instrumentation.stats.TagValue in project grpc-java by grpc.

the class StatsTraceContextTest method clientNotSent.

@Test
public void clientNotSent() {
    String methodName = MethodDescriptor.generateFullMethodName("Service1", "method2");
    StatsTraceContext ctx = StatsTraceContext.newClientContextForTesting(methodName, statsCtxFactory, statsCtxFactory.getDefault(), fakeClock.getStopwatchSupplier());
    fakeClock.forwardMillis(3000);
    ctx.callEnded(Status.DEADLINE_EXCEEDED.withDescription("3 seconds"));
    StatsTestUtils.MetricsRecord record = statsCtxFactory.pollRecord();
    assertNotNull(record);
    assertNoServerContent(record);
    TagValue methodTag = record.tags.get(RpcConstants.RPC_CLIENT_METHOD);
    assertEquals(methodName, methodTag.toString());
    TagValue statusTag = record.tags.get(RpcConstants.RPC_STATUS);
    assertEquals(Status.Code.DEADLINE_EXCEEDED.toString(), statusTag.toString());
    assertEquals(1, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_ERROR_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_RESPONSE_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
    assertEquals(3000, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    assertNull(record.getMetric(RpcConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
}
Also used : StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) TagValue(com.google.instrumentation.stats.TagValue) Test(org.junit.Test)

Example 7 with TagValue

use of com.google.instrumentation.stats.TagValue in project grpc-java by grpc.

the class StatsTraceContextTest method serverBasic.

@Test
public void serverBasic() {
    String methodName = MethodDescriptor.generateFullMethodName("Service1", "method4");
    StatsTraceContext ctx = StatsTraceContext.newServerContext(methodName, statsCtxFactory, new Metadata(), fakeClock.getStopwatchSupplier());
    ctx.wireBytesReceived(34);
    ctx.uncompressedBytesReceived(67);
    fakeClock.forwardMillis(100);
    ctx.wireBytesSent(1028);
    ctx.uncompressedBytesSent(1128);
    fakeClock.forwardMillis(16);
    ctx.wireBytesReceived(154);
    ctx.uncompressedBytesReceived(552);
    ctx.wireBytesSent(99);
    ctx.uncompressedBytesSent(865);
    fakeClock.forwardMillis(24);
    ctx.callEnded(Status.CANCELLED);
    StatsTestUtils.MetricsRecord record = statsCtxFactory.pollRecord();
    assertNotNull(record);
    assertNoClientContent(record);
    TagValue methodTag = record.tags.get(RpcConstants.RPC_SERVER_METHOD);
    assertEquals(methodName, methodTag.toString());
    TagValue statusTag = record.tags.get(RpcConstants.RPC_STATUS);
    assertEquals(Status.Code.CANCELLED.toString(), statusTag.toString());
    assertEquals(1, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_ERROR_COUNT));
    assertEquals(1028 + 99, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_RESPONSE_BYTES));
    assertEquals(1128 + 865, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
    assertEquals(34 + 154, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_REQUEST_BYTES));
    assertEquals(67 + 552, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(100 + 16 + 24, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_SERVER_LATENCY));
}
Also used : StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Metadata(io.grpc.Metadata) TagValue(com.google.instrumentation.stats.TagValue) Test(org.junit.Test)

Example 8 with TagValue

use of com.google.instrumentation.stats.TagValue in project grpc-java by grpc.

the class ServerImplTest method methodNotFound.

@Test
public void methodNotFound() throws Exception {
    createAndStartServer(NO_FILTERS);
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/nonexist", requestHeaders);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/nonexist", requestHeaders);
    verify(stream).setListener(isA(ServerStreamListener.class));
    verify(stream, atLeast(1)).statsTraceContext();
    assertEquals(1, executor.runDueTasks());
    verify(stream).close(statusCaptor.capture(), any(Metadata.class));
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.UNIMPLEMENTED, status.getCode());
    assertEquals("Method not found: Waiter/nonexist", status.getDescription());
    StatsTestUtils.MetricsRecord record = statsCtxFactory.pollRecord();
    assertNotNull(record);
    TagValue methodTag = record.tags.get(RpcConstants.RPC_SERVER_METHOD);
    assertNotNull(methodTag);
    assertEquals("Waiter/nonexist", methodTag.toString());
    TagValue statusTag = record.tags.get(RpcConstants.RPC_STATUS);
    assertNotNull(statusTag);
    assertEquals(Status.Code.UNIMPLEMENTED.toString(), statusTag.toString());
}
Also used : Status(io.grpc.Status) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Metadata(io.grpc.Metadata) TagValue(com.google.instrumentation.stats.TagValue) Test(org.junit.Test)

Example 9 with TagValue

use of com.google.instrumentation.stats.TagValue in project grpc-java by grpc.

the class ServerImplTest method basicExchangeSuccessful.

@Test
public void basicExchangeSuccessful() throws Exception {
    createAndStartServer(NO_FILTERS);
    final Metadata.Key<String> metadataKey = Metadata.Key.of("inception", Metadata.ASCII_STRING_MARSHALLER);
    final Metadata.Key<StatsContext> statsHeaderKey = StatsTraceContext.createStatsHeader(statsCtxFactory);
    final AtomicReference<ServerCall<String, Integer>> callReference = new AtomicReference<ServerCall<String, Integer>>();
    MethodDescriptor<String, Integer> method = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Waiter/serve").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
    mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", method)).addMethod(method, new ServerCallHandler<String, Integer>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
            assertEquals("Waiter/serve", call.getMethodDescriptor().getFullMethodName());
            assertNotNull(call);
            assertNotNull(headers);
            assertEquals("value", headers.get(metadataKey));
            callReference.set(call);
            return callListener;
        }
    }).build());
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Metadata requestHeaders = new Metadata();
    requestHeaders.put(metadataKey, "value");
    StatsContext statsContextOnClient = statsCtxFactory.getDefault().with(StatsTestUtils.EXTRA_TAG, TagValue.create("extraTagValue"));
    requestHeaders.put(statsHeaderKey, statsContextOnClient);
    StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
    assertNotNull(statsTraceCtx);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
    verify(stream).setListener(streamListenerCaptor.capture());
    ServerStreamListener streamListener = streamListenerCaptor.getValue();
    assertNotNull(streamListener);
    verify(stream, atLeast(1)).statsTraceContext();
    assertEquals(1, executor.runDueTasks());
    ServerCall<String, Integer> call = callReference.get();
    assertNotNull(call);
    verify(stream).getAuthority();
    String order = "Lots of pizza, please";
    streamListener.messageRead(STRING_MARSHALLER.stream(order));
    assertEquals(1, executor.runDueTasks());
    verify(callListener).onMessage(order);
    Metadata responseHeaders = new Metadata();
    responseHeaders.put(metadataKey, "response value");
    call.sendHeaders(responseHeaders);
    verify(stream).writeHeaders(responseHeaders);
    verify(stream).setCompressor(isA(Compressor.class));
    call.sendMessage(314);
    ArgumentCaptor<InputStream> inputCaptor = ArgumentCaptor.forClass(InputStream.class);
    verify(stream).writeMessage(inputCaptor.capture());
    verify(stream).flush();
    assertEquals(314, INTEGER_MARSHALLER.parse(inputCaptor.getValue()).intValue());
    // All full; no dessert.
    streamListener.halfClosed();
    assertEquals(1, executor.runDueTasks());
    verify(callListener).onHalfClose();
    call.sendMessage(50);
    verify(stream, times(2)).writeMessage(inputCaptor.capture());
    verify(stream, times(2)).flush();
    assertEquals(50, INTEGER_MARSHALLER.parse(inputCaptor.getValue()).intValue());
    Metadata trailers = new Metadata();
    trailers.put(metadataKey, "another value");
    Status status = Status.OK.withDescription("A okay");
    call.close(status, trailers);
    verify(stream).close(status, trailers);
    streamListener.closed(Status.OK);
    assertEquals(1, executor.runDueTasks());
    verify(callListener).onComplete();
    verify(stream, atLeast(1)).statsTraceContext();
    verifyNoMoreInteractions(stream);
    verifyNoMoreInteractions(callListener);
    // Check stats
    StatsTestUtils.MetricsRecord record = statsCtxFactory.pollRecord();
    assertNotNull(record);
    TagValue methodTag = record.tags.get(RpcConstants.RPC_SERVER_METHOD);
    assertNotNull(methodTag);
    assertEquals("Waiter/serve", methodTag.toString());
    TagValue statusTag = record.tags.get(RpcConstants.RPC_STATUS);
    assertNotNull(statusTag);
    assertEquals(Status.Code.OK.toString(), statusTag.toString());
    TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
    assertNotNull(extraTag);
    assertEquals("extraTagValue", extraTag.toString());
    assertNull(record.getMetric(RpcConstants.RPC_CLIENT_REQUEST_BYTES));
    assertNull(record.getMetric(RpcConstants.RPC_CLIENT_RESPONSE_BYTES));
    assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertNull(record.getMetric(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
    // The test doesn't invoke MessageFramer and MessageDeframer which keep the sizes.
    // Thus the sizes reported to stats would be zero.
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_RESPONSE_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
}
Also used : JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Metadata(io.grpc.Metadata) ServerCall(io.grpc.ServerCall) ServiceDescriptor(io.grpc.ServiceDescriptor) StatsContext(com.google.instrumentation.stats.StatsContext) TagValue(com.google.instrumentation.stats.TagValue) Status(io.grpc.Status) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Compressor(io.grpc.Compressor) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Aggregations

TagValue (com.google.instrumentation.stats.TagValue)9 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)8 Test (org.junit.Test)6 Metadata (io.grpc.Metadata)4 StatsContext (com.google.instrumentation.stats.StatsContext)2 Status (io.grpc.Status)2 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)2 Compressor (io.grpc.Compressor)1 ServerCall (io.grpc.ServerCall)1 ServiceDescriptor (io.grpc.ServiceDescriptor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1