@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); } { sinkWriterImpl.logTrailer( seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_SERVER, callId, null); verify(sink).write( base.toBuilder() .clearPeer() .setLogger(Logger.LOGGER_SERVER) .build()); } { sinkWriterImpl.logTrailer( seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, null); verify(sink).write( base.toBuilder() .clearPeer() .build()); } { sinkWriterImpl.logTrailer( seq, statusDescription.getCode().toStatus(), nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress); verify(sink).write( base.toBuilder() .setTrailer(base.getTrailer().toBuilder().clearStatusMessage()) .build()); } { 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()); } }
@VisibleForTesting static Address socketToProto(SocketAddress address) { checkNotNull(address, "address"); Address.Builder builder = Address.newBuilder(); if (address instanceof InetSocketAddress) { InetAddress inetAddress = ((InetSocketAddress) address).getAddress(); if (inetAddress instanceof Inet4Address) { builder.setType(Type.TYPE_IPV4) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else if (inetAddress instanceof Inet6Address) { builder.setType(Type.TYPE_IPV6) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else { logger.log(Level.SEVERE, "unknown type of InetSocketAddress: {}", address); builder.setAddress(address.toString()); } builder.setIpPort(((InetSocketAddress) address).getPort()); } else if (address.getClass().getName().equals("io.netty.channel.unix.DomainSocketAddress")) { builder.setType(Type.TYPE_UNIX) .setAddress(address.toString()); } else { builder.setType(Type.TYPE_UNKNOWN).setAddress(address.toString()); } return builder.build(); }
BinlogHelper { @VisibleForTesting static Address socketToProto(SocketAddress address) { checkNotNull(address, "address"); Address.Builder builder = Address.newBuilder(); if (address instanceof InetSocketAddress) { InetAddress inetAddress = ((InetSocketAddress) address).getAddress(); if (inetAddress instanceof Inet4Address) { builder.setType(Type.TYPE_IPV4) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else if (inetAddress instanceof Inet6Address) { builder.setType(Type.TYPE_IPV6) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else { logger.log(Level.SEVERE, "unknown type of InetSocketAddress: {}", address); builder.setAddress(address.toString()); } builder.setIpPort(((InetSocketAddress) address).getPort()); } else if (address.getClass().getName().equals("io.netty.channel.unix.DomainSocketAddress")) { builder.setType(Type.TYPE_UNIX) .setAddress(address.toString()); } else { builder.setType(Type.TYPE_UNKNOWN).setAddress(address.toString()); } return builder.build(); } }
BinlogHelper { @VisibleForTesting static Address socketToProto(SocketAddress address) { checkNotNull(address, "address"); Address.Builder builder = Address.newBuilder(); if (address instanceof InetSocketAddress) { InetAddress inetAddress = ((InetSocketAddress) address).getAddress(); if (inetAddress instanceof Inet4Address) { builder.setType(Type.TYPE_IPV4) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else if (inetAddress instanceof Inet6Address) { builder.setType(Type.TYPE_IPV6) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else { logger.log(Level.SEVERE, "unknown type of InetSocketAddress: {}", address); builder.setAddress(address.toString()); } builder.setIpPort(((InetSocketAddress) address).getPort()); } else if (address.getClass().getName().equals("io.netty.channel.unix.DomainSocketAddress")) { builder.setType(Type.TYPE_UNIX) .setAddress(address.toString()); } else { builder.setType(Type.TYPE_UNKNOWN).setAddress(address.toString()); } return builder.build(); } @VisibleForTesting  BinlogHelper(SinkWriter writer); }
BinlogHelper { @VisibleForTesting static Address socketToProto(SocketAddress address) { checkNotNull(address, "address"); Address.Builder builder = Address.newBuilder(); if (address instanceof InetSocketAddress) { InetAddress inetAddress = ((InetSocketAddress) address).getAddress(); if (inetAddress instanceof Inet4Address) { builder.setType(Type.TYPE_IPV4) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else if (inetAddress instanceof Inet6Address) { builder.setType(Type.TYPE_IPV6) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else { logger.log(Level.SEVERE, "unknown type of InetSocketAddress: {}", address); builder.setAddress(address.toString()); } builder.setIpPort(((InetSocketAddress) address).getPort()); } else if (address.getClass().getName().equals("io.netty.channel.unix.DomainSocketAddress")) { builder.setType(Type.TYPE_UNIX) .setAddress(address.toString()); } else { builder.setType(Type.TYPE_UNKNOWN).setAddress(address.toString()); } return builder.build(); } @VisibleForTesting  BinlogHelper(SinkWriter writer); ClientInterceptor getClientInterceptor(final long callId); ServerInterceptor getServerInterceptor(final long callId); }
BinlogHelper { @VisibleForTesting static Address socketToProto(SocketAddress address) { checkNotNull(address, "address"); Address.Builder builder = Address.newBuilder(); if (address instanceof InetSocketAddress) { InetAddress inetAddress = ((InetSocketAddress) address).getAddress(); if (inetAddress instanceof Inet4Address) { builder.setType(Type.TYPE_IPV4) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else if (inetAddress instanceof Inet6Address) { builder.setType(Type.TYPE_IPV6) .setAddress(InetAddressUtil.toAddrString(inetAddress)); } else { logger.log(Level.SEVERE, "unknown type of InetSocketAddress: {}", address); builder.setAddress(address.toString()); } builder.setIpPort(((InetSocketAddress) address).getPort()); } else if (address.getClass().getName().equals("io.netty.channel.unix.DomainSocketAddress")) { builder.setType(Type.TYPE_UNIX) .setAddress(address.toString()); } else { builder.setType(Type.TYPE_UNKNOWN).setAddress(address.toString()); } return builder.build(); } @VisibleForTesting  BinlogHelper(SinkWriter writer); ClientInterceptor getClientInterceptor(final long callId); ServerInterceptor getServerInterceptor(final long callId);  }