Search in sources :

Example 1 with GrpcLogEntry

use of io.grpc.binarylog.v1.GrpcLogEntry in project grpc-java by grpc.

the class BinlogHelperTest method logTrailer.

@Test
public void logTrailer() throws Exception {
    long seq = 1;
    InetAddress address = InetAddress.getByName("127.0.0.1");
    int port = 12345;
    InetSocketAddress peerAddress = new InetSocketAddress(address, port);
    long callId = 1000;
    Status statusDescription = Status.INTERNAL.withDescription("my description");
    GrpcLogEntry.Builder builder = metadataToProtoTestHelper(EventType.EVENT_TYPE_SERVER_TRAILER, nonEmptyMetadata, 10).toBuilder().setTimestamp(timestamp).setSequenceIdWithinCall(seq).setLogger(Logger.LOGGER_CLIENT).setCallId(callId).setPeer(BinlogHelper.socketToProto(peerAddress));
    builder.getTrailerBuilder().setStatusCode(Status.INTERNAL.getCode().value()).setStatusMessage("my description");
    GrpcLogEntry base = builder.build();
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base);
    }
    // logger is server
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_SERVER, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().clearPeer().setLogger(Logger.LOGGER_SERVER).build());
    }
    // peerAddress is null
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().clearPeer().build());
    }
    // status code is present but description is null
    {
        sinkWriterImpl.logTrailer(seq, // strip the description
        statusDescription.getCode().toStatus(), nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base.toBuilder().setTrailer(base.getTrailer().toBuilder().clearStatusMessage()).build());
    }
    // status proto always logged if present (com.google.rpc.Status),
    {
        int zeroHeaderBytes = 0;
        SinkWriterImpl truncatingWriter = new SinkWriterImpl(sink, timeProvider, zeroHeaderBytes, MESSAGE_LIMIT);
        com.google.rpc.Status statusProto = com.google.rpc.Status.newBuilder().addDetails(Any.pack(StringValue.newBuilder().setValue("arbitrarypayload").build())).setCode(Status.INTERNAL.getCode().value()).setMessage("status detail string").build();
        StatusException statusException = StatusProto.toStatusException(statusProto, nonEmptyMetadata);
        truncatingWriter.logTrailer(seq, statusException.getStatus(), statusException.getTrailers(), Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base.toBuilder().setTrailer(builder.getTrailerBuilder().setStatusMessage("status detail string").setStatusDetails(ByteString.copyFrom(statusProto.toByteArray())).setMetadata(io.grpc.binarylog.v1.Metadata.getDefaultInstance())).build());
    }
}
Also used : Status(io.grpc.Status) StatusException(io.grpc.StatusException) InetSocketAddress(java.net.InetSocketAddress) SinkWriterImpl(io.grpc.protobuf.services.BinlogHelper.SinkWriterImpl) GrpcLogEntry(io.grpc.binarylog.v1.GrpcLogEntry) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with GrpcLogEntry

use of io.grpc.binarylog.v1.GrpcLogEntry in project grpc-java by grpc.

the class BinlogHelperTest method logRpcMessage.

@Test
public void logRpcMessage() throws Exception {
    long seq = 1;
    long callId = 1000;
    GrpcLogEntry base = messageToProtoTestHelper(message, MESSAGE_LIMIT).toBuilder().setTimestamp(timestamp).setType(EventType.EVENT_TYPE_CLIENT_MESSAGE).setLogger(Logger.LOGGER_CLIENT).setSequenceIdWithinCall(1).setCallId(callId).build();
    {
        sinkWriterImpl.logRpcMessage(seq, EventType.EVENT_TYPE_CLIENT_MESSAGE, BYTEARRAY_MARSHALLER, message, Logger.LOGGER_CLIENT, callId);
        verify(sink).write(base);
    }
    // server messsage
    {
        sinkWriterImpl.logRpcMessage(seq, EventType.EVENT_TYPE_SERVER_MESSAGE, BYTEARRAY_MARSHALLER, message, Logger.LOGGER_CLIENT, callId);
        verify(sink).write(base.toBuilder().setType(EventType.EVENT_TYPE_SERVER_MESSAGE).build());
    }
    // logger is server
    {
        sinkWriterImpl.logRpcMessage(seq, EventType.EVENT_TYPE_CLIENT_MESSAGE, BYTEARRAY_MARSHALLER, message, Logger.LOGGER_SERVER, callId);
        verify(sink).write(base.toBuilder().setLogger(Logger.LOGGER_SERVER).build());
    }
}
Also used : GrpcLogEntry(io.grpc.binarylog.v1.GrpcLogEntry) Test(org.junit.Test)

Example 3 with GrpcLogEntry

use of io.grpc.binarylog.v1.GrpcLogEntry in project grpc-java by grpc.

the class BinlogHelperTest method logClientHeader.

@Test
public void logClientHeader() throws Exception {
    long seq = 1;
    String authority = "authority";
    String methodName = "service/method";
    Duration timeout = Durations.fromMillis(1234);
    InetAddress address = InetAddress.getByName("127.0.0.1");
    int port = 12345;
    InetSocketAddress peerAddress = new InetSocketAddress(address, port);
    long callId = 1000;
    GrpcLogEntry.Builder builder = metadataToProtoTestHelper(EventType.EVENT_TYPE_CLIENT_HEADER, nonEmptyMetadata, 10).toBuilder().setTimestamp(timestamp).setSequenceIdWithinCall(seq).setLogger(Logger.LOGGER_CLIENT).setCallId(callId);
    builder.getClientHeaderBuilder().setMethodName("/" + methodName).setAuthority(authority).setTimeout(timeout);
    GrpcLogEntry base = builder.build();
    {
        sinkWriterImpl.logClientHeader(seq, methodName, authority, timeout, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
        null);
        verify(sink).write(base);
    }
    // logger is server
    {
        sinkWriterImpl.logClientHeader(seq, methodName, authority, timeout, nonEmptyMetadata, Logger.LOGGER_SERVER, callId, peerAddress);
        verify(sink).write(base.toBuilder().setPeer(BinlogHelper.socketToProto(peerAddress)).setLogger(Logger.LOGGER_SERVER).build());
    }
    // authority is null
    {
        sinkWriterImpl.logClientHeader(seq, methodName, /*authority=*/
        null, timeout, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().setClientHeader(builder.getClientHeader().toBuilder().clearAuthority().build()).build());
    }
    // timeout is null
    {
        sinkWriterImpl.logClientHeader(seq, methodName, authority, /*timeout=*/
        null, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().setClientHeader(builder.getClientHeader().toBuilder().clearTimeout().build()).build());
    }
    // peerAddress is non null (error for client side)
    try {
        sinkWriterImpl.logClientHeader(seq, methodName, authority, timeout, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress);
        fail();
    } catch (IllegalArgumentException expected) {
    // noop
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) GrpcLogEntry(io.grpc.binarylog.v1.GrpcLogEntry) Duration(com.google.protobuf.Duration) ByteString(com.google.protobuf.ByteString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with GrpcLogEntry

use of io.grpc.binarylog.v1.GrpcLogEntry in project grpc-java by grpc.

the class TempFileSinkTest method readMyWrite.

@Test
public void readMyWrite() throws Exception {
    TempFileSink sink = new TempFileSink();
    GrpcLogEntry e1 = GrpcLogEntry.newBuilder().setCallId(1234).build();
    GrpcLogEntry e2 = GrpcLogEntry.newBuilder().setCallId(5678).build();
    sink.write(e1);
    sink.write(e2);
    sink.close();
    DataInputStream input = new DataInputStream(new FileInputStream(sink.getPath()));
    try {
        GrpcLogEntry read1 = GrpcLogEntry.parseDelimitedFrom(input);
        GrpcLogEntry read2 = GrpcLogEntry.parseDelimitedFrom(input);
        assertEquals(e1, read1);
        assertEquals(e2, read2);
        assertEquals(-1, input.read());
    } finally {
        input.close();
    }
}
Also used : GrpcLogEntry(io.grpc.binarylog.v1.GrpcLogEntry) DataInputStream(java.io.DataInputStream) TempFileSink(io.grpc.protobuf.services.TempFileSink) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

GrpcLogEntry (io.grpc.binarylog.v1.GrpcLogEntry)4 Test (org.junit.Test)4 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 ByteString (com.google.protobuf.ByteString)1 Duration (com.google.protobuf.Duration)1 Status (io.grpc.Status)1 StatusException (io.grpc.StatusException)1 SinkWriterImpl (io.grpc.protobuf.services.BinlogHelper.SinkWriterImpl)1 TempFileSink (io.grpc.protobuf.services.TempFileSink)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1