@Test public void neverLoggedMetadata_grpcStatusDetilsBin() throws Exception { Metadata.Key<byte[]> key = Metadata.Key.of("grpc-status-details-bin", Metadata.BINARY_BYTE_MARSHALLER); Metadata metadata = new Metadata(); metadata.put(key, new byte[1]); int unlimitedHeaderBytes = Integer.MAX_VALUE; MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> pair = createMetadataProto(metadata, unlimitedHeaderBytes); assertThat(pair.proto.getEntryBuilderList()).isEmpty(); assertFalse(pair.truncated); }
@VisibleForTesting static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto( Metadata metadata, int maxHeaderBytes) { checkNotNull(metadata, "metadata"); checkArgument(maxHeaderBytes >= 0, "maxHeaderBytes must be non negative"); io.grpc.binarylog.v1.Metadata.Builder metaBuilder = io.grpc.binarylog.v1.Metadata.newBuilder(); byte[][] serialized = InternalMetadata.serialize(metadata); boolean truncated = false; if (serialized != null) { int curBytes = 0; for (int i = 0; i < serialized.length; i += 2) { String key = new String(serialized[i], Charsets.UTF_8); byte[] value = serialized[i + 1]; if (NEVER_INCLUDED_METADATA.contains(key)) { continue; } boolean forceInclude = ALWAYS_INCLUDED_METADATA.contains(key); int bytesAfterAdd = curBytes + key.length() + value.length; if (!forceInclude && bytesAfterAdd > maxHeaderBytes) { truncated = true; continue; } metaBuilder.addEntryBuilder() .setKey(key) .setValue(ByteString.copyFrom(value)); if (!forceInclude) { curBytes = bytesAfterAdd; } } } return new MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder>(metaBuilder, truncated); }
BinlogHelper { @VisibleForTesting static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto( Metadata metadata, int maxHeaderBytes) { checkNotNull(metadata, "metadata"); checkArgument(maxHeaderBytes >= 0, "maxHeaderBytes must be non negative"); io.grpc.binarylog.v1.Metadata.Builder metaBuilder = io.grpc.binarylog.v1.Metadata.newBuilder(); byte[][] serialized = InternalMetadata.serialize(metadata); boolean truncated = false; if (serialized != null) { int curBytes = 0; for (int i = 0; i < serialized.length; i += 2) { String key = new String(serialized[i], Charsets.UTF_8); byte[] value = serialized[i + 1]; if (NEVER_INCLUDED_METADATA.contains(key)) { continue; } boolean forceInclude = ALWAYS_INCLUDED_METADATA.contains(key); int bytesAfterAdd = curBytes + key.length() + value.length; if (!forceInclude && bytesAfterAdd > maxHeaderBytes) { truncated = true; continue; } metaBuilder.addEntryBuilder() .setKey(key) .setValue(ByteString.copyFrom(value)); if (!forceInclude) { curBytes = bytesAfterAdd; } } } return new MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder>(metaBuilder, truncated); } }
BinlogHelper { @VisibleForTesting static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto( Metadata metadata, int maxHeaderBytes) { checkNotNull(metadata, "metadata"); checkArgument(maxHeaderBytes >= 0, "maxHeaderBytes must be non negative"); io.grpc.binarylog.v1.Metadata.Builder metaBuilder = io.grpc.binarylog.v1.Metadata.newBuilder(); byte[][] serialized = InternalMetadata.serialize(metadata); boolean truncated = false; if (serialized != null) { int curBytes = 0; for (int i = 0; i < serialized.length; i += 2) { String key = new String(serialized[i], Charsets.UTF_8); byte[] value = serialized[i + 1]; if (NEVER_INCLUDED_METADATA.contains(key)) { continue; } boolean forceInclude = ALWAYS_INCLUDED_METADATA.contains(key); int bytesAfterAdd = curBytes + key.length() + value.length; if (!forceInclude && bytesAfterAdd > maxHeaderBytes) { truncated = true; continue; } metaBuilder.addEntryBuilder() .setKey(key) .setValue(ByteString.copyFrom(value)); if (!forceInclude) { curBytes = bytesAfterAdd; } } } return new MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder>(metaBuilder, truncated); } @VisibleForTesting  BinlogHelper(SinkWriter writer); }
BinlogHelper { @VisibleForTesting static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto( Metadata metadata, int maxHeaderBytes) { checkNotNull(metadata, "metadata"); checkArgument(maxHeaderBytes >= 0, "maxHeaderBytes must be non negative"); io.grpc.binarylog.v1.Metadata.Builder metaBuilder = io.grpc.binarylog.v1.Metadata.newBuilder(); byte[][] serialized = InternalMetadata.serialize(metadata); boolean truncated = false; if (serialized != null) { int curBytes = 0; for (int i = 0; i < serialized.length; i += 2) { String key = new String(serialized[i], Charsets.UTF_8); byte[] value = serialized[i + 1]; if (NEVER_INCLUDED_METADATA.contains(key)) { continue; } boolean forceInclude = ALWAYS_INCLUDED_METADATA.contains(key); int bytesAfterAdd = curBytes + key.length() + value.length; if (!forceInclude && bytesAfterAdd > maxHeaderBytes) { truncated = true; continue; } metaBuilder.addEntryBuilder() .setKey(key) .setValue(ByteString.copyFrom(value)); if (!forceInclude) { curBytes = bytesAfterAdd; } } } return new MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder>(metaBuilder, truncated); } @VisibleForTesting  BinlogHelper(SinkWriter writer); ClientInterceptor getClientInterceptor(final long callId); ServerInterceptor getServerInterceptor(final long callId); }
BinlogHelper { @VisibleForTesting static MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder> createMetadataProto( Metadata metadata, int maxHeaderBytes) { checkNotNull(metadata, "metadata"); checkArgument(maxHeaderBytes >= 0, "maxHeaderBytes must be non negative"); io.grpc.binarylog.v1.Metadata.Builder metaBuilder = io.grpc.binarylog.v1.Metadata.newBuilder(); byte[][] serialized = InternalMetadata.serialize(metadata); boolean truncated = false; if (serialized != null) { int curBytes = 0; for (int i = 0; i < serialized.length; i += 2) { String key = new String(serialized[i], Charsets.UTF_8); byte[] value = serialized[i + 1]; if (NEVER_INCLUDED_METADATA.contains(key)) { continue; } boolean forceInclude = ALWAYS_INCLUDED_METADATA.contains(key); int bytesAfterAdd = curBytes + key.length() + value.length; if (!forceInclude && bytesAfterAdd > maxHeaderBytes) { truncated = true; continue; } metaBuilder.addEntryBuilder() .setKey(key) .setValue(ByteString.copyFrom(value)); if (!forceInclude) { curBytes = bytesAfterAdd; } } } return new MaybeTruncated<io.grpc.binarylog.v1.Metadata.Builder>(metaBuilder, truncated); } @VisibleForTesting  BinlogHelper(SinkWriter writer); ClientInterceptor getClientInterceptor(final long callId); ServerInterceptor getServerInterceptor(final long callId);  }