@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, null); verify(sink).write(base); } { 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()); } { sinkWriterImpl.logClientHeader( seq, methodName, null, timeout, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, null); verify(sink).write( base.toBuilder() .setClientHeader(builder.getClientHeader().toBuilder().clearAuthority().build()) .build()); } { sinkWriterImpl.logClientHeader( seq, methodName, authority, null, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, null); verify(sink).write( base.toBuilder() .setClientHeader(builder.getClientHeader().toBuilder().clearTimeout().build()) .build()); } try { sinkWriterImpl.logClientHeader( seq, methodName, authority, timeout, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress); fail(); } catch (IllegalArgumentException expected) { } }
@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);  }