Migrate MarshalerUtil to an abstract class (#3573)

* Replace MarshalerUtil with Serializer abstraction to also implement a JSON version.

* Continue

* Finish

* Finish

* Clean

* ProtoFieldInfo

* Precompute tags

* Constants

* More pretag
This commit is contained in:
Anuraag Agrawal 2021-09-06 13:08:42 +09:00 committed by GitHub
parent f918674707
commit 083d91a498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 821 additions and 609 deletions

View File

@ -49,7 +49,7 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
override fun handle(extend: Extend, field: Field) = null override fun handle(extend: Extend, field: Field) = null
override fun handle(service: Service): List<Path> = emptyList() override fun handle(service: Service): List<Path> = emptyList()
override fun handle(type: Type): Path? { override fun handle(type: Type): Path? {
val typeSpec = javaGenerator.generateType(type) val typeSpec = javaGenerator.generateType(type, false)
val javaTypeName = javaGenerator.generatedTypeName(type) val javaTypeName = javaGenerator.generatedTypeName(type)
if (typeSpec == null) { if (typeSpec == null) {
@ -75,9 +75,15 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
} }
} }
private class JavaGenerator(private val typeToJavaName: Map<ProtoType, TypeName>) { private class JavaGenerator(private val schema: Schema, private val typeToJavaName: Map<ProtoType, TypeName>) {
companion object { companion object {
private val PROTO_FIELD_INFO = ClassName.get("io.opentelemetry.exporter.otlp.internal", "ProtoFieldInfo")
private val WIRETYPE_VARINT = 0
private val WIRETYPE_FIXED64 = 1
private val WIRETYPE_LENGTH_DELIMITED = 2
private val WIRETYPE_FIXED32 = 5
fun get(schema: Schema): JavaGenerator { fun get(schema: Schema): JavaGenerator {
val nameToJavaName = linkedMapOf<ProtoType, TypeName>() val nameToJavaName = linkedMapOf<ProtoType, TypeName>()
for (protoFile in schema.protoFiles) { for (protoFile in schema.protoFiles) {
@ -85,7 +91,7 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
putAll(nameToJavaName, javaPackage, null, protoFile.types) putAll(nameToJavaName, javaPackage, null, protoFile.types)
} }
return JavaGenerator(nameToJavaName) return JavaGenerator(schema, nameToJavaName)
} }
private fun putAll( private fun putAll(
@ -112,9 +118,9 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
} }
} }
fun generateType(type: Type): TypeSpec? { fun generateType(type: Type, nested: Boolean): TypeSpec? {
if (type is MessageType) { if (type is MessageType) {
return generateMessage(type) return generateMessage(type, nested)
} }
if (type is EnumType) { if (type is EnumType) {
return generateEnum(type) return generateEnum(type)
@ -126,21 +132,29 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
return typeToJavaName[type.type] as ClassName return typeToJavaName[type.type] as ClassName
} }
private fun generateMessage(type: MessageType): TypeSpec { private fun generateMessage(type: MessageType, nested: Boolean): TypeSpec {
val javaType = typeToJavaName[type.type] as ClassName val javaType = typeToJavaName[type.type] as ClassName
val builder = TypeSpec.classBuilder(javaType.simpleName()) val builder = TypeSpec.classBuilder(javaType.simpleName())
.addModifiers(PUBLIC, FINAL) .addModifiers(PUBLIC, FINAL)
if (nested) {
builder.addModifiers(STATIC)
}
for (field in type.fieldsAndOneOfFields) { for (field in type.fieldsAndOneOfFields) {
builder.addField( builder.addField(
FieldSpec.builder(TypeName.INT, "${field.name.toUpperCase()}_FIELD_NUMBER", PUBLIC, STATIC, FINAL) FieldSpec.builder(PROTO_FIELD_INFO, field.name.toUpperCase(), PUBLIC, STATIC, FINAL)
.initializer("\$L", field.tag) .initializer("\$T.create(\$L, \$L, \"\$L\")",
PROTO_FIELD_INFO,
field.tag,
makeTag(field.tag, field.type as ProtoType, field.isRepeated),
field.jsonName)
.build()) .build())
field.type
} }
for (nestedType in type.nestedTypes) { for (nestedType in type.nestedTypes) {
builder.addType(generateType(nestedType)) builder.addType(generateType(nestedType, true))
} }
return builder.build() return builder.build()
@ -161,5 +175,37 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
return builder.build() return builder.build()
} }
private fun fieldEncoding(type: ProtoType, isRepeated: Boolean): Int {
if (isRepeated) {
// Repeated fields are always length delimited in proto3
return WIRETYPE_LENGTH_DELIMITED
}
if (schema.getType(type) is EnumType) {
return WIRETYPE_VARINT
}
if (!type.isScalar) {
// Non-scalar and not enum is a message
return WIRETYPE_LENGTH_DELIMITED
}
return when(type) {
ProtoType.FIXED32,
ProtoType.SFIXED32,
ProtoType.FLOAT-> WIRETYPE_FIXED32
ProtoType.FIXED64,
ProtoType.SFIXED64,
ProtoType.DOUBLE -> WIRETYPE_FIXED64
ProtoType.BYTES,
ProtoType.STRING -> WIRETYPE_LENGTH_DELIMITED
else -> WIRETYPE_VARINT
}
}
private fun makeTag(fieldNumber: Int, type: ProtoType, isRepeated: Boolean): Int {
return (fieldNumber shl 3) or fieldEncoding(type, isRepeated)
}
} }
} }

View File

@ -32,5 +32,7 @@ jmhReport {
tasks { tasks {
named("jmh") { named("jmh") {
finalizedBy(named("jmhReport")) finalizedBy(named("jmhReport"))
outputs.cacheIf { false }
} }
} }

View File

@ -7,4 +7,5 @@
<!-- Suppress Javadoc-related checks in non-public code. --> <!-- Suppress Javadoc-related checks in non-public code. -->
<suppress checks="JavadocParagraph|JavadocMethod" files="[\\/](jmh|test)[\\/]" /> <suppress checks="JavadocParagraph|JavadocMethod" files="[\\/](jmh|test)[\\/]" />
<suppress checks="SummaryJavadoc" files="SemanticAttributes|ResourceAttributes"/> <suppress checks="SummaryJavadoc" files="SemanticAttributes|ResourceAttributes"/>
<suppress checks="RedundantImport" files="[\\/]build[\\/]"/>
</suppressions> </suppressions>

View File

@ -31,6 +31,8 @@ dependencies {
compileOnly("io.grpc:grpc-okhttp") compileOnly("io.grpc:grpc-okhttp")
compileOnly("io.grpc:grpc-stub") compileOnly("io.grpc:grpc-stub")
annotationProcessor("com.google.auto.value:auto-value")
testImplementation(project(":proto")) testImplementation(project(":proto"))
testImplementation(project(":sdk:testing")) testImplementation(project(":sdk:testing"))

View File

@ -129,7 +129,7 @@ public class MetricsRequestMarshalerBenchmark {
MetricsRequestMarshaler marshaler = MetricsRequestMarshaler.create(METRICS); MetricsRequestMarshaler marshaler = MetricsRequestMarshaler.create(METRICS);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
CodedOutputStream cos = CodedOutputStream.newInstance(bos); CodedOutputStream cos = CodedOutputStream.newInstance(bos);
marshaler.writeTo(cos); marshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
return bos; return bos;
} }

View File

@ -11,12 +11,13 @@ import io.opentelemetry.proto.common.v1.internal.AnyValue;
import io.opentelemetry.proto.common.v1.internal.ArrayValue; import io.opentelemetry.proto.common.v1.internal.ArrayValue;
import io.opentelemetry.proto.common.v1.internal.KeyValue; import io.opentelemetry.proto.common.v1.internal.KeyValue;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
abstract class AttributeMarshaler extends MarshalerWithSize { abstract class AttributeMarshaler extends MarshalerWithSize {
private static final AttributeMarshaler[] EMPTY_REPEATED = new AttributeMarshaler[0]; private static final AttributeMarshaler[] EMPTY_REPEATED = new AttributeMarshaler[0];
private final byte[] key; private final byte[] keyUtf8;
private final int valueSize; private final int valueSize;
static AttributeMarshaler[] createRepeated(Attributes attributes) { static AttributeMarshaler[] createRepeated(Attributes attributes) {
@ -39,128 +40,144 @@ abstract class AttributeMarshaler extends MarshalerWithSize {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static AttributeMarshaler create(AttributeKey<?> attributeKey, Object value) { static AttributeMarshaler create(AttributeKey<?> attributeKey, Object value) {
byte[] key = MarshalerUtil.toBytes(attributeKey.getKey()); byte[] keyUtf8Utf8 = MarshalerUtil.toBytes(attributeKey.getKey());
if (value == null) { if (value == null) {
return new KeyValueNullMarshaler(key); return new KeyValueNullMarshaler(keyUtf8Utf8);
} }
switch (attributeKey.getType()) { switch (attributeKey.getType()) {
case STRING: case STRING:
return new KeyValueStringMarshaler(key, MarshalerUtil.toBytes((String) value)); return new KeyValueStringMarshaler(keyUtf8Utf8, MarshalerUtil.toBytes((String) value));
case LONG: case LONG:
return new KeyValueLongMarshaler(key, (Long) value); return new KeyValueLongMarshaler(keyUtf8Utf8, (Long) value);
case BOOLEAN: case BOOLEAN:
return new KeyValueBooleanMarshaler(key, (Boolean) value); return new KeyValueBooleanMarshaler(keyUtf8Utf8, (Boolean) value);
case DOUBLE: case DOUBLE:
return new KeyValueDoubleMarshaler(key, (Double) value); return new KeyValueDoubleMarshaler(keyUtf8Utf8, (Double) value);
case STRING_ARRAY: case STRING_ARRAY:
return new KeyValueArrayStringMarshaler(key, (List<String>) value); return new KeyValueArrayStringMarshaler(keyUtf8Utf8, (List<String>) value);
case LONG_ARRAY: case LONG_ARRAY:
return new KeyValueArrayLongMarshaler(key, (List<Long>) value); return new KeyValueArrayLongMarshaler(keyUtf8Utf8, (List<Long>) value);
case BOOLEAN_ARRAY: case BOOLEAN_ARRAY:
return new KeyValueArrayBooleanMarshaler(key, (List<Boolean>) value); return new KeyValueArrayBooleanMarshaler(keyUtf8Utf8, (List<Boolean>) value);
case DOUBLE_ARRAY: case DOUBLE_ARRAY:
return new KeyValueArrayDoubleMarshaler(key, (List<Double>) value); return new KeyValueArrayDoubleMarshaler(keyUtf8Utf8, (List<Double>) value);
} }
throw new IllegalArgumentException("Unsupported attribute type."); throw new IllegalArgumentException("Unsupported attribute type.");
} }
private AttributeMarshaler(byte[] key, int valueSize) { private AttributeMarshaler(byte[] keyUtf8, int valueSize) {
super(calculateSize(key, valueSize)); super(calculateSize(keyUtf8, valueSize));
this.key = key; this.keyUtf8 = keyUtf8;
this.valueSize = valueSize; this.valueSize = valueSize;
} }
@Override @Override
public final void writeTo(CodedOutputStream output) throws IOException { public final void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalBytes(KeyValue.KEY_FIELD_NUMBER, key, output); output.serializeString(KeyValue.KEY, keyUtf8);
if (valueSize > 0) { if (valueSize > 0) {
output.writeTag(KeyValue.VALUE_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED); // TODO(anuraaga): Replace this hack with directly serializing Value within Serializer. The
output.writeUInt32NoTag(valueSize); // proto and JSON representations of Value differ too much to use Marshaler.
CodedOutputStream cos = ((ProtoSerializer) output).getCodedOutputStream();
cos.writeUInt32NoTag(KeyValue.VALUE.getTag());
cos.writeUInt32NoTag(valueSize);
writeValueTo(output); writeValueTo(output);
} }
} }
abstract void writeValueTo(CodedOutputStream output) throws IOException; abstract void writeValueTo(Serializer output) throws IOException;
private static int calculateSize(byte[] key, int valueSize) { private static int calculateSize(byte[] keyUtf8, int valueSize) {
return MarshalerUtil.sizeBytes(KeyValue.KEY_FIELD_NUMBER, key) return MarshalerUtil.sizeBytes(KeyValue.KEY, keyUtf8)
+ CodedOutputStream.computeTagSize(KeyValue.VALUE_FIELD_NUMBER) + KeyValue.VALUE.getTagSize()
+ CodedOutputStream.computeUInt32SizeNoTag(valueSize) + CodedOutputStream.computeUInt32SizeNoTag(valueSize)
+ valueSize; + valueSize;
} }
private static final class KeyValueNullMarshaler extends AttributeMarshaler { private static final class KeyValueNullMarshaler extends AttributeMarshaler {
private KeyValueNullMarshaler(byte[] key) { private KeyValueNullMarshaler(byte[] keyUtf8) {
super(key, 0); super(keyUtf8, 0);
} }
@Override @Override
void writeValueTo(CodedOutputStream output) {} void writeValueTo(Serializer output) {}
} }
private static final class KeyValueStringMarshaler extends AttributeMarshaler { private static final class KeyValueStringMarshaler extends AttributeMarshaler {
private final byte[] value; private final byte[] value;
private KeyValueStringMarshaler(byte[] key, byte[] value) { private KeyValueStringMarshaler(byte[] keyUtf8, byte[] value) {
super(key, CodedOutputStream.computeByteArraySize(AnyValue.STRING_VALUE_FIELD_NUMBER, value)); super(
keyUtf8,
AnyValue.STRING_VALUE.getTagSize() + CodedOutputStream.computeByteArraySizeNoTag(value));
this.value = value; this.value = value;
} }
@Override @Override
public void writeValueTo(CodedOutputStream output) throws IOException { public void writeValueTo(Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeByteArray(AnyValue.STRING_VALUE_FIELD_NUMBER, value); output.writeString(AnyValue.STRING_VALUE, value);
} }
} }
private static final class KeyValueLongMarshaler extends AttributeMarshaler { private static final class KeyValueLongMarshaler extends AttributeMarshaler {
private final long value; private final long value;
private KeyValueLongMarshaler(byte[] key, long value) { private KeyValueLongMarshaler(byte[] keyUtf8, long value) {
super(key, CodedOutputStream.computeInt64Size(AnyValue.INT_VALUE_FIELD_NUMBER, value)); super(
keyUtf8,
AnyValue.INT_VALUE.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(value));
this.value = value; this.value = value;
} }
@Override @Override
public void writeValueTo(CodedOutputStream output) throws IOException { public void writeValueTo(Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeInt64(AnyValue.INT_VALUE_FIELD_NUMBER, value); output.writeInt64(AnyValue.INT_VALUE, value);
} }
} }
private static final class KeyValueBooleanMarshaler extends AttributeMarshaler { private static final class KeyValueBooleanMarshaler extends AttributeMarshaler {
private final boolean value; private final boolean value;
private KeyValueBooleanMarshaler(byte[] key, boolean value) { private KeyValueBooleanMarshaler(byte[] keyUtf8, boolean value) {
super(key, CodedOutputStream.computeBoolSize(AnyValue.BOOL_VALUE_FIELD_NUMBER, value)); super(
keyUtf8,
AnyValue.BOOL_VALUE.getTagSize() + CodedOutputStream.computeBoolSizeNoTag(value));
this.value = value; this.value = value;
} }
@Override @Override
public void writeValueTo(CodedOutputStream output) throws IOException { public void writeValueTo(Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeBool(AnyValue.BOOL_VALUE_FIELD_NUMBER, value); output.writeBool(AnyValue.BOOL_VALUE, value);
} }
} }
private static final class KeyValueDoubleMarshaler extends AttributeMarshaler { private static final class KeyValueDoubleMarshaler extends AttributeMarshaler {
private final double value; private final double value;
private KeyValueDoubleMarshaler(byte[] key, double value) { private KeyValueDoubleMarshaler(byte[] keyUtf8, double value) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
super(key, CodedOutputStream.computeDoubleSize(AnyValue.DOUBLE_VALUE_FIELD_NUMBER, value)); super(
keyUtf8,
AnyValue.DOUBLE_VALUE.getTagSize() + CodedOutputStream.computeDoubleSizeNoTag(value));
this.value = value; this.value = value;
} }
@Override @Override
public void writeValueTo(CodedOutputStream output) throws IOException { public void writeValueTo(Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeDouble(AnyValue.DOUBLE_VALUE_FIELD_NUMBER, value); output.writeDouble(AnyValue.DOUBLE_VALUE, value);
} }
} }
@ -168,60 +185,66 @@ abstract class AttributeMarshaler extends MarshalerWithSize {
private final List<T> values; private final List<T> values;
private final int valuesSize; private final int valuesSize;
private KeyValueArrayMarshaler(byte[] key, List<T> values, int valuesSize) { private KeyValueArrayMarshaler(byte[] keyUtf8, List<T> values, int valuesSize) {
super(key, calculateWrapperSize(valuesSize) + valuesSize); super(keyUtf8, calculateWrapperSize(valuesSize) + valuesSize);
this.values = values; this.values = values;
this.valuesSize = valuesSize; this.valuesSize = valuesSize;
} }
@Override @Override
public final void writeValueTo(CodedOutputStream output) throws IOException { public final void writeValueTo(Serializer output) throws IOException {
output.writeTag(AnyValue.ARRAY_VALUE_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED); // TODO(anuraaga): Replace this hack with directly serializing Value within Serializer. The
output.writeUInt32NoTag(valuesSize); // proto and JSON representations of Value differ too much to use Marshaler.
CodedOutputStream cos = ((ProtoSerializer) output).getCodedOutputStream();
cos.writeUInt32NoTag(AnyValue.ARRAY_VALUE.getTag());
cos.writeUInt32NoTag(valuesSize);
for (T value : values) { for (T value : values) {
output.writeTag(ArrayValue.VALUES_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED); cos.writeUInt32NoTag(ArrayValue.VALUES.getTag());
output.writeUInt32NoTag(getArrayElementSerializedSize(value)); cos.writeUInt32NoTag(getArrayElementSerializedSize(value));
writeArrayElementTo(value, output); writeArrayElementTo(value, output);
} }
} }
abstract void writeArrayElementTo(T value, CodedOutputStream output) throws IOException; abstract void writeArrayElementTo(T value, Serializer output) throws IOException;
abstract int getArrayElementSerializedSize(T value); abstract int getArrayElementSerializedSize(T value);
private static int calculateWrapperSize(int valuesSize) { private static int calculateWrapperSize(int valuesSize) {
return CodedOutputStream.computeTagSize(AnyValue.ARRAY_VALUE_FIELD_NUMBER) return AnyValue.ARRAY_VALUE.getTagSize()
+ CodedOutputStream.computeUInt32SizeNoTag(valuesSize); + CodedOutputStream.computeUInt32SizeNoTag(valuesSize);
} }
} }
private static final class KeyValueArrayStringMarshaler extends KeyValueArrayMarshaler<String> { private static final class KeyValueArrayStringMarshaler extends KeyValueArrayMarshaler<String> {
private KeyValueArrayStringMarshaler(byte[] key, List<String> values) { private KeyValueArrayStringMarshaler(byte[] keyUtf8, List<String> values) {
super(key, values, calculateValuesSize(values)); super(keyUtf8, values, calculateValuesSize(values));
} }
@Override @Override
void writeArrayElementTo(String value, CodedOutputStream output) throws IOException { void writeArrayElementTo(String value, Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeString(AnyValue.STRING_VALUE_FIELD_NUMBER, value); output.writeString(AnyValue.STRING_VALUE, value.getBytes(StandardCharsets.UTF_8));
} }
@Override @Override
int getArrayElementSerializedSize(String value) { int getArrayElementSerializedSize(String value) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
return CodedOutputStream.computeStringSize(AnyValue.STRING_VALUE_FIELD_NUMBER, value); return AnyValue.STRING_VALUE.getTagSize() + CodedOutputStream.computeStringSizeNoTag(value);
} }
static int calculateValuesSize(List<String> values) { static int calculateValuesSize(List<String> values) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(ArrayValue.VALUES_FIELD_NUMBER); int fieldTagSize = ArrayValue.VALUES.getTagSize();
for (String value : values) { for (String value : values) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
int fieldSize = int fieldSize =
CodedOutputStream.computeStringSize(AnyValue.STRING_VALUE_FIELD_NUMBER, value); AnyValue.STRING_VALUE.getTagSize() + CodedOutputStream.computeStringSizeNoTag(value);
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;
@ -229,31 +252,35 @@ abstract class AttributeMarshaler extends MarshalerWithSize {
} }
private static final class KeyValueArrayLongMarshaler extends KeyValueArrayMarshaler<Long> { private static final class KeyValueArrayLongMarshaler extends KeyValueArrayMarshaler<Long> {
private KeyValueArrayLongMarshaler(byte[] key, List<Long> values) { private KeyValueArrayLongMarshaler(byte[] keyUtf8, List<Long> values) {
super(key, values, calculateValuesSize(values)); super(keyUtf8, values, calculateValuesSize(values));
} }
@Override @Override
void writeArrayElementTo(Long value, CodedOutputStream output) throws IOException { void writeArrayElementTo(Long value, Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeInt64(AnyValue.INT_VALUE_FIELD_NUMBER, value); output.writeInt64(AnyValue.INT_VALUE, value);
} }
@Override @Override
int getArrayElementSerializedSize(Long value) { int getArrayElementSerializedSize(Long value) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
return CodedOutputStream.computeInt64Size(AnyValue.INT_VALUE_FIELD_NUMBER, value); return AnyValue.INT_VALUE.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(value);
} }
static int calculateValuesSize(List<Long> values) { static int calculateValuesSize(List<Long> values) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(ArrayValue.VALUES_FIELD_NUMBER); int fieldTagSize = ArrayValue.VALUES.getTagSize();
for (Long value : values) { for (Long value : values) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
int fieldSize = CodedOutputStream.computeInt64Size(AnyValue.INT_VALUE_FIELD_NUMBER, value); int fieldSize =
AnyValue.INT_VALUE.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(value);
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;
@ -261,31 +288,35 @@ abstract class AttributeMarshaler extends MarshalerWithSize {
} }
private static final class KeyValueArrayBooleanMarshaler extends KeyValueArrayMarshaler<Boolean> { private static final class KeyValueArrayBooleanMarshaler extends KeyValueArrayMarshaler<Boolean> {
private KeyValueArrayBooleanMarshaler(byte[] key, List<Boolean> values) { private KeyValueArrayBooleanMarshaler(byte[] keyUtf8, List<Boolean> values) {
super(key, values, calculateValuesSize(values)); super(keyUtf8, values, calculateValuesSize(values));
} }
@Override @Override
void writeArrayElementTo(Boolean value, CodedOutputStream output) throws IOException { void writeArrayElementTo(Boolean value, Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeBool(AnyValue.BOOL_VALUE_FIELD_NUMBER, value); output.writeBool(AnyValue.BOOL_VALUE, value);
} }
@Override @Override
int getArrayElementSerializedSize(Boolean value) { int getArrayElementSerializedSize(Boolean value) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
return CodedOutputStream.computeBoolSize(AnyValue.BOOL_VALUE_FIELD_NUMBER, value); return AnyValue.BOOL_VALUE.getTagSize() + CodedOutputStream.computeBoolSizeNoTag(value);
} }
static int calculateValuesSize(List<Boolean> values) { static int calculateValuesSize(List<Boolean> values) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(ArrayValue.VALUES_FIELD_NUMBER); int fieldTagSize = ArrayValue.VALUES.getTagSize();
for (Boolean value : values) { for (Boolean value : values) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
int fieldSize = CodedOutputStream.computeBoolSize(AnyValue.BOOL_VALUE_FIELD_NUMBER, value); int fieldSize =
AnyValue.BOOL_VALUE.getTagSize() + CodedOutputStream.computeBoolSizeNoTag(value);
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;
@ -293,32 +324,35 @@ abstract class AttributeMarshaler extends MarshalerWithSize {
} }
private static final class KeyValueArrayDoubleMarshaler extends KeyValueArrayMarshaler<Double> { private static final class KeyValueArrayDoubleMarshaler extends KeyValueArrayMarshaler<Double> {
private KeyValueArrayDoubleMarshaler(byte[] key, List<Double> values) { private KeyValueArrayDoubleMarshaler(byte[] keyUtf8, List<Double> values) {
super(key, values, calculateValuesSize(values)); super(keyUtf8, values, calculateValuesSize(values));
} }
@Override @Override
void writeArrayElementTo(Double value, CodedOutputStream output) throws IOException { void writeArrayElementTo(Double value, Serializer output) throws IOException {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
output.writeDouble(AnyValue.DOUBLE_VALUE_FIELD_NUMBER, value); output.writeDouble(AnyValue.DOUBLE_VALUE, value);
} }
@Override @Override
int getArrayElementSerializedSize(Double value) { int getArrayElementSerializedSize(Double value) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
return CodedOutputStream.computeDoubleSize(AnyValue.DOUBLE_VALUE_FIELD_NUMBER, value); return AnyValue.DOUBLE_VALUE.getTagSize() + CodedOutputStream.computeDoubleSizeNoTag(value);
} }
static int calculateValuesSize(List<Double> values) { static int calculateValuesSize(List<Double> values) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(ArrayValue.VALUES_FIELD_NUMBER); int fieldTagSize = ArrayValue.VALUES.getTagSize();
for (Double value : values) { for (Double value : values) {
// Do not call MarshalUtil because we always have to write the message tag even if the value // Do not call serialize* method because we always have to write the message tag even if the
// value
// is empty. // is empty.
int fieldSize = int fieldSize =
CodedOutputStream.computeDoubleSize(AnyValue.DOUBLE_VALUE_FIELD_NUMBER, value); AnyValue.DOUBLE_VALUE.getTagSize() + CodedOutputStream.computeDoubleSizeNoTag(value);
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;

View File

@ -35,11 +35,12 @@ final class InstrumentationLibraryMarshaler extends MarshalerWithSize {
private InstrumentationLibraryMarshaler(byte[] name, byte[] version) { private InstrumentationLibraryMarshaler(byte[] name, byte[] version) {
super(computeSize(name, version)); super(computeSize(name, version));
ByteArrayOutputStream bos = new ByteArrayOutputStream(getSerializedSize()); ByteArrayOutputStream bos = new ByteArrayOutputStream(getProtoSerializedSize());
CodedOutputStream output = CodedOutputStream.newInstance(bos); CodedOutputStream output = CodedOutputStream.newInstance(bos);
Serializer serializer = Serializer.createProtoSerializer(output);
try { try {
MarshalerUtil.marshalBytes(InstrumentationLibrary.NAME_FIELD_NUMBER, name, output); serializer.serializeString(InstrumentationLibrary.NAME, name);
MarshalerUtil.marshalBytes(InstrumentationLibrary.VERSION_FIELD_NUMBER, version, output); serializer.serializeString(InstrumentationLibrary.VERSION, version);
output.flush(); output.flush();
} catch (IOException e) { } catch (IOException e) {
// Presized so can't happen (we would have already thrown OutOfMemoryError) // Presized so can't happen (we would have already thrown OutOfMemoryError)
@ -49,12 +50,13 @@ final class InstrumentationLibraryMarshaler extends MarshalerWithSize {
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
output.writeRawBytes(serializedInfo); // TODO(anuraaga): Preserialize JSON as well.
output.writeSerializedMessage(serializedInfo, MarshalerUtil.EMPTY_BYTES);
} }
private static int computeSize(byte[] name, byte[] version) { private static int computeSize(byte[] name, byte[] version) {
return MarshalerUtil.sizeBytes(InstrumentationLibrary.NAME_FIELD_NUMBER, name) return MarshalerUtil.sizeBytes(InstrumentationLibrary.NAME, name)
+ MarshalerUtil.sizeBytes(InstrumentationLibrary.VERSION_FIELD_NUMBER, version); + MarshalerUtil.sizeBytes(InstrumentationLibrary.VERSION, version);
} }
} }

View File

@ -14,7 +14,7 @@ import java.io.IOException;
* at any time. * at any time.
*/ */
public interface Marshaler { public interface Marshaler {
void writeTo(CodedOutputStream output) throws IOException; void writeTo(Serializer output) throws IOException;
int getSerializedSize(); int getProtoSerializedSize();
} }

View File

@ -7,7 +7,6 @@ package io.opentelemetry.exporter.otlp.internal;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -39,190 +38,92 @@ final class MarshalerUtil {
return result; return result;
} }
static void marshalRepeatedFixed64(int fieldNumber, List<Long> values, CodedOutputStream output) static int sizeRepeatedFixed64(ProtoFieldInfo field, List<Long> values) {
throws IOException { return sizeRepeatedFixed64(field, values.size());
if (values.isEmpty()) {
return;
}
output.writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
// TODO(anuraaga): Consider passing in from calculateSize to avoid recomputing.
output.writeUInt32NoTag(WireFormat.FIXED64_SIZE * values.size());
for (long value : values) {
output.writeFixed64NoTag(value);
}
} }
static void marshalRepeatedDouble(int fieldNumber, List<Double> values, CodedOutputStream output) private static int sizeRepeatedFixed64(ProtoFieldInfo field, int numValues) {
throws IOException {
if (values.isEmpty()) {
return;
}
output.writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
// TODO(anuraaga): Consider passing in from calculateSize to avoid recomputing.
output.writeUInt32NoTag(WireFormat.FIXED64_SIZE * values.size());
for (double value : values) {
output.writeDoubleNoTag(value);
}
}
static <T extends Marshaler> void marshalRepeatedMessage(
int fieldNumber, T[] repeatedMessage, CodedOutputStream output) throws IOException {
for (Marshaler message : repeatedMessage) {
marshalMessage(fieldNumber, message, output);
}
}
static void marshalRepeatedMessage(
int fieldNumber, List<? extends Marshaler> repeatedMessage, CodedOutputStream output)
throws IOException {
for (Marshaler message : repeatedMessage) {
marshalMessage(fieldNumber, message, output);
}
}
static void marshalMessage(int fieldNumber, Marshaler message, CodedOutputStream output)
throws IOException {
output.writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
output.writeUInt32NoTag(message.getSerializedSize());
message.writeTo(output);
}
static void marshalBool(int fieldNumber, boolean value, CodedOutputStream output)
throws IOException {
if (!value) {
return;
}
output.writeBool(fieldNumber, value);
}
static void marshalUInt32(int fieldNumber, int message, CodedOutputStream output)
throws IOException {
if (message == 0) {
return;
}
output.writeUInt32(fieldNumber, message);
}
static void marshalFixed64(int fieldNumber, long message, CodedOutputStream output)
throws IOException {
if (message == 0L) {
return;
}
output.writeFixed64(fieldNumber, message);
}
static void marshalDouble(int fieldNumber, double message, CodedOutputStream output)
throws IOException {
if (message == 0D) {
return;
}
output.writeDouble(fieldNumber, message);
}
static void marshalBytes(int fieldNumber, byte[] message, CodedOutputStream output)
throws IOException {
if (message.length == 0) {
return;
}
output.writeByteArray(fieldNumber, message);
}
// Assumes OTLP always defines the first item in an enum with number 0, which it does and will.
static void marshalEnum(int fieldNumber, int value, CodedOutputStream output) throws IOException {
if (value == 0) {
return;
}
output.writeEnum(fieldNumber, value);
}
static int sizeRepeatedFixed64(int fieldNumber, List<Long> values) {
return sizeRepeatedFixed64(fieldNumber, values.size());
}
private static int sizeRepeatedFixed64(int fieldNumber, int numValues) {
if (numValues == 0) { if (numValues == 0) {
return 0; return 0;
} }
int dataSize = WireFormat.FIXED64_SIZE * numValues; int dataSize = WireFormat.FIXED64_SIZE * numValues;
int size = 0; int size = 0;
size += CodedOutputStream.computeTagSize(fieldNumber); size += field.getTagSize();
size += CodedOutputStream.computeLengthDelimitedFieldSize(dataSize); size += CodedOutputStream.computeLengthDelimitedFieldSize(dataSize);
return size; return size;
} }
static int sizeRepeatedDouble(int fieldNumber, List<Double> values) { static int sizeRepeatedDouble(ProtoFieldInfo field, List<Double> values) {
// Same as fixed64. // Same as fixed64.
return sizeRepeatedFixed64(fieldNumber, values.size()); return sizeRepeatedFixed64(field, values.size());
} }
static <T extends Marshaler> int sizeRepeatedMessage(int fieldNumber, T[] repeatedMessage) { static <T extends Marshaler> int sizeRepeatedMessage(ProtoFieldInfo field, T[] repeatedMessage) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(fieldNumber); int fieldTagSize = field.getTagSize();
for (Marshaler message : repeatedMessage) { for (Marshaler message : repeatedMessage) {
int fieldSize = message.getSerializedSize(); int fieldSize = message.getProtoSerializedSize();
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;
} }
static int sizeRepeatedMessage(int fieldNumber, List<? extends Marshaler> repeatedMessage) { static int sizeRepeatedMessage(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) {
int size = 0; int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(fieldNumber); int fieldTagSize = field.getTagSize();
for (Marshaler message : repeatedMessage) { for (Marshaler message : repeatedMessage) {
int fieldSize = message.getSerializedSize(); int fieldSize = message.getProtoSerializedSize();
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
} }
return size; return size;
} }
static int sizeMessage(int fieldNumber, Marshaler message) { static int sizeMessage(ProtoFieldInfo field, Marshaler message) {
int fieldSize = message.getSerializedSize(); int fieldSize = message.getProtoSerializedSize();
return CodedOutputStream.computeTagSize(fieldNumber) return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
+ CodedOutputStream.computeUInt32SizeNoTag(fieldSize)
+ fieldSize;
} }
static int sizeBool(int fieldNumber, boolean value) { static int sizeBool(ProtoFieldInfo field, boolean value) {
if (!value) { if (!value) {
return 0; return 0;
} }
return CodedOutputStream.computeBoolSize(fieldNumber, value); return field.getTagSize() + CodedOutputStream.computeBoolSizeNoTag(value);
} }
static int sizeUInt32(int fieldNumber, int message) { static int sizeUInt32(ProtoFieldInfo field, int message) {
if (message == 0) { if (message == 0) {
return 0; return 0;
} }
return CodedOutputStream.computeUInt32Size(fieldNumber, message); return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(message);
} }
static int sizeDouble(int fieldNumber, double value) { static int sizeDouble(ProtoFieldInfo field, double value) {
if (value == 0D) { if (value == 0D) {
return 0; return 0;
} }
return CodedOutputStream.computeDoubleSize(fieldNumber, value); return field.getTagSize() + CodedOutputStream.computeDoubleSizeNoTag(value);
} }
static int sizeFixed64(int fieldNumber, long message) { static int sizeFixed64(ProtoFieldInfo field, long message) {
if (message == 0L) { if (message == 0L) {
return 0; return 0;
} }
return CodedOutputStream.computeFixed64Size(fieldNumber, message); return field.getTagSize() + CodedOutputStream.computeFixed64SizeNoTag(message);
} }
static int sizeBytes(int fieldNumber, byte[] message) { static int sizeBytes(ProtoFieldInfo field, byte[] message) {
if (message.length == 0) { if (message.length == 0) {
return 0; return 0;
} }
return CodedOutputStream.computeByteArraySize(fieldNumber, message); return field.getTagSize() + CodedOutputStream.computeByteArraySizeNoTag(message);
} }
// Assumes OTLP always defines the first item in an enum with number 0, which it does and will. // Assumes OTLP always defines the first item in an enum with number 0, which it does and will.
static int sizeEnum(int fieldNumber, int value) { static int sizeEnum(ProtoFieldInfo field, int value) {
if (value == 0) { if (value == 0) {
return 0; return 0;
} }
return CodedOutputStream.computeEnumSize(fieldNumber, value); return field.getTagSize() + CodedOutputStream.computeEnumSizeNoTag(value);
} }
static byte[] toBytes(@Nullable String value) { static byte[] toBytes(@Nullable String value) {

View File

@ -13,7 +13,7 @@ abstract class MarshalerWithSize implements Marshaler {
} }
@Override @Override
public final int getSerializedSize() { public final int getProtoSerializedSize() {
return size; return size;
} }
} }

View File

@ -91,18 +91,16 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(
ExportMetricsServiceRequest.RESOURCE_METRICS_FIELD_NUMBER, ExportMetricsServiceRequest.RESOURCE_METRICS, resourceMetricsMarshalers);
resourceMetricsMarshalers,
output);
} }
private static int calculateSize(ResourceMetricsMarshaler[] resourceMetricsMarshalers) { private static int calculateSize(ResourceMetricsMarshaler[] resourceMetricsMarshalers) {
int size = 0; int size = 0;
size += size +=
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
ExportMetricsServiceRequest.RESOURCE_METRICS_FIELD_NUMBER, resourceMetricsMarshalers); ExportMetricsServiceRequest.RESOURCE_METRICS, resourceMetricsMarshalers);
return size; return size;
} }
@ -133,14 +131,11 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalMessage( output.serializeMessage(ResourceMetrics.RESOURCE, resourceMarshaler);
ResourceMetrics.RESOURCE_FIELD_NUMBER, resourceMarshaler, output); output.serializeRepeatedMessage(
MarshalerUtil.marshalRepeatedMessage( ResourceMetrics.INSTRUMENTATION_LIBRARY_METRICS, instrumentationLibraryMetricsMarshalers);
ResourceMetrics.INSTRUMENTATION_LIBRARY_METRICS_FIELD_NUMBER, output.serializeString(ResourceMetrics.SCHEMA_URL, schemaUrl);
instrumentationLibraryMetricsMarshalers,
output);
MarshalerUtil.marshalBytes(ResourceMetrics.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
} }
private static int calculateSize( private static int calculateSize(
@ -148,11 +143,11 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
byte[] schemaUrl, byte[] schemaUrl,
InstrumentationLibraryMetricsMarshaler[] instrumentationLibraryMetricsMarshalers) { InstrumentationLibraryMetricsMarshaler[] instrumentationLibraryMetricsMarshalers) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeMessage(ResourceMetrics.RESOURCE_FIELD_NUMBER, resourceMarshaler); size += MarshalerUtil.sizeMessage(ResourceMetrics.RESOURCE, resourceMarshaler);
size += MarshalerUtil.sizeBytes(ResourceMetrics.SCHEMA_URL_FIELD_NUMBER, schemaUrl); size += MarshalerUtil.sizeBytes(ResourceMetrics.SCHEMA_URL, schemaUrl);
size += size +=
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
ResourceMetrics.INSTRUMENTATION_LIBRARY_METRICS_FIELD_NUMBER, ResourceMetrics.INSTRUMENTATION_LIBRARY_METRICS,
instrumentationLibraryMetricsMarshalers); instrumentationLibraryMetricsMarshalers);
return size; return size;
} }
@ -161,55 +156,49 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
private static final class InstrumentationLibraryMetricsMarshaler extends MarshalerWithSize { private static final class InstrumentationLibraryMetricsMarshaler extends MarshalerWithSize {
private final InstrumentationLibraryMarshaler instrumentationLibrary; private final InstrumentationLibraryMarshaler instrumentationLibrary;
private final List<Marshaler> metricMarshalers; private final List<Marshaler> metricMarshalers;
private final byte[] schemaUrl; private final byte[] schemaUrlUtf8;
private InstrumentationLibraryMetricsMarshaler( private InstrumentationLibraryMetricsMarshaler(
InstrumentationLibraryMarshaler instrumentationLibrary, InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl, byte[] schemaUrlUtf8,
List<Marshaler> metricMarshalers) { List<Marshaler> metricMarshalers) {
super(calculateSize(instrumentationLibrary, schemaUrl, metricMarshalers)); super(calculateSize(instrumentationLibrary, schemaUrlUtf8, metricMarshalers));
this.instrumentationLibrary = instrumentationLibrary; this.instrumentationLibrary = instrumentationLibrary;
this.schemaUrl = schemaUrl; this.schemaUrlUtf8 = schemaUrlUtf8;
this.metricMarshalers = metricMarshalers; this.metricMarshalers = metricMarshalers;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalMessage( output.serializeMessage(
InstrumentationLibraryMetrics.INSTRUMENTATION_LIBRARY_FIELD_NUMBER, InstrumentationLibraryMetrics.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
instrumentationLibrary, output.serializeRepeatedMessage(InstrumentationLibraryMetrics.METRICS, metricMarshalers);
output); output.serializeString(InstrumentationLibraryMetrics.SCHEMA_URL, schemaUrlUtf8);
MarshalerUtil.marshalRepeatedMessage(
InstrumentationLibraryMetrics.METRICS_FIELD_NUMBER, metricMarshalers, output);
MarshalerUtil.marshalBytes(
InstrumentationLibraryMetrics.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
} }
private static int calculateSize( private static int calculateSize(
InstrumentationLibraryMarshaler instrumentationLibrary, InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl, byte[] schemaUrlUtf8,
List<Marshaler> metricMarshalers) { List<Marshaler> metricMarshalers) {
int size = 0; int size = 0;
size += size +=
MarshalerUtil.sizeMessage( MarshalerUtil.sizeMessage(
InstrumentationLibraryMetrics.INSTRUMENTATION_LIBRARY_FIELD_NUMBER, InstrumentationLibraryMetrics.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
instrumentationLibrary); size += MarshalerUtil.sizeBytes(InstrumentationLibraryMetrics.SCHEMA_URL, schemaUrlUtf8);
size +=
MarshalerUtil.sizeBytes(InstrumentationLibraryMetrics.SCHEMA_URL_FIELD_NUMBER, schemaUrl);
size += size +=
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
InstrumentationLibraryMetrics.METRICS_FIELD_NUMBER, metricMarshalers); InstrumentationLibraryMetrics.METRICS, metricMarshalers);
return size; return size;
} }
} }
static final class MetricMarshaler extends MarshalerWithSize { static final class MetricMarshaler extends MarshalerWithSize {
private final byte[] name; private final byte[] nameUtf8;
private final byte[] description; private final byte[] descriptionUtf8;
private final byte[] unit; private final byte[] unitUtf8;
private final Marshaler dataMarshaler; private final Marshaler dataMarshaler;
private final int dataFieldNumber; private final ProtoFieldInfo dataField;
static Marshaler create(MetricData metric) { static Marshaler create(MetricData metric) {
// TODO(anuraaga): Cache these as they should be effectively singleton. // TODO(anuraaga): Cache these as they should be effectively singleton.
@ -218,31 +207,31 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
byte[] unit = MarshalerUtil.toBytes(metric.getUnit()); byte[] unit = MarshalerUtil.toBytes(metric.getUnit());
Marshaler dataMarshaler = null; Marshaler dataMarshaler = null;
int dataFieldNumber = -1; ProtoFieldInfo dataFIeld = null;
switch (metric.getType()) { switch (metric.getType()) {
case LONG_GAUGE: case LONG_GAUGE:
dataMarshaler = GaugeMarshaler.create(metric.getLongGaugeData()); dataMarshaler = GaugeMarshaler.create(metric.getLongGaugeData());
dataFieldNumber = Metric.GAUGE_FIELD_NUMBER; dataFIeld = Metric.GAUGE;
break; break;
case DOUBLE_GAUGE: case DOUBLE_GAUGE:
dataMarshaler = GaugeMarshaler.create(metric.getDoubleGaugeData()); dataMarshaler = GaugeMarshaler.create(metric.getDoubleGaugeData());
dataFieldNumber = Metric.GAUGE_FIELD_NUMBER; dataFIeld = Metric.GAUGE;
break; break;
case LONG_SUM: case LONG_SUM:
dataMarshaler = SumMarshaler.create(metric.getLongSumData()); dataMarshaler = SumMarshaler.create(metric.getLongSumData());
dataFieldNumber = Metric.SUM_FIELD_NUMBER; dataFIeld = Metric.SUM;
break; break;
case DOUBLE_SUM: case DOUBLE_SUM:
dataMarshaler = SumMarshaler.create(metric.getDoubleSumData()); dataMarshaler = SumMarshaler.create(metric.getDoubleSumData());
dataFieldNumber = Metric.SUM_FIELD_NUMBER; dataFIeld = Metric.SUM;
break; break;
case SUMMARY: case SUMMARY:
dataMarshaler = SummaryMarshaler.create(metric.getDoubleSummaryData()); dataMarshaler = SummaryMarshaler.create(metric.getDoubleSummaryData());
dataFieldNumber = Metric.SUMMARY_FIELD_NUMBER; dataFIeld = Metric.SUMMARY;
break; break;
case HISTOGRAM: case HISTOGRAM:
dataMarshaler = HistogramMarshaler.create(metric.getDoubleHistogramData()); dataMarshaler = HistogramMarshaler.create(metric.getDoubleHistogramData());
dataFieldNumber = Metric.HISTOGRAM_FIELD_NUMBER; dataFIeld = Metric.HISTOGRAM;
break; break;
} }
@ -251,42 +240,42 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
return NoopMarshaler.INSTANCE; return NoopMarshaler.INSTANCE;
} }
return new MetricMarshaler(name, description, unit, dataMarshaler, dataFieldNumber); return new MetricMarshaler(name, description, unit, dataMarshaler, dataFIeld);
} }
private MetricMarshaler( private MetricMarshaler(
byte[] name, byte[] nameUtf8,
byte[] description, byte[] descriptionUtf8,
byte[] unit, byte[] unitUtf8,
Marshaler dataMarshaler, Marshaler dataMarshaler,
int dataFieldNumber) { ProtoFieldInfo dataField) {
super(calculateSize(name, description, unit, dataMarshaler, dataFieldNumber)); super(calculateSize(nameUtf8, descriptionUtf8, unitUtf8, dataMarshaler, dataField));
this.name = name; this.nameUtf8 = nameUtf8;
this.description = description; this.descriptionUtf8 = descriptionUtf8;
this.unit = unit; this.unitUtf8 = unitUtf8;
this.dataMarshaler = dataMarshaler; this.dataMarshaler = dataMarshaler;
this.dataFieldNumber = dataFieldNumber; this.dataField = dataField;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalBytes(Metric.NAME_FIELD_NUMBER, name, output); output.serializeString(Metric.NAME, nameUtf8);
MarshalerUtil.marshalBytes(Metric.DESCRIPTION_FIELD_NUMBER, description, output); output.serializeString(Metric.DESCRIPTION, descriptionUtf8);
MarshalerUtil.marshalBytes(Metric.UNIT_FIELD_NUMBER, unit, output); output.serializeString(Metric.UNIT, unitUtf8);
MarshalerUtil.marshalMessage(dataFieldNumber, dataMarshaler, output); output.serializeMessage(dataField, dataMarshaler);
} }
private static int calculateSize( private static int calculateSize(
byte[] name, byte[] nameUtf8,
byte[] description, byte[] descriptionUtf8,
byte[] unit, byte[] unitUtf8,
Marshaler dataMarshaler, Marshaler dataMarshaler,
int dataFieldNumber) { ProtoFieldInfo dataField) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeBytes(Metric.NAME_FIELD_NUMBER, name); size += MarshalerUtil.sizeBytes(Metric.NAME, nameUtf8);
size += MarshalerUtil.sizeBytes(Metric.DESCRIPTION_FIELD_NUMBER, description); size += MarshalerUtil.sizeBytes(Metric.DESCRIPTION, descriptionUtf8);
size += MarshalerUtil.sizeBytes(Metric.UNIT_FIELD_NUMBER, unit); size += MarshalerUtil.sizeBytes(Metric.UNIT, unitUtf8);
size += MarshalerUtil.sizeMessage(dataFieldNumber, dataMarshaler); size += MarshalerUtil.sizeMessage(dataField, dataMarshaler);
return size; return size;
} }
} }
@ -307,13 +296,13 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage(Gauge.DATA_POINTS_FIELD_NUMBER, dataPoints, output); output.serializeRepeatedMessage(Gauge.DATA_POINTS, dataPoints);
} }
private static int calculateSize(NumberDataPointMarshaler[] dataPoints) { private static int calculateSize(NumberDataPointMarshaler[] dataPoints) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeRepeatedMessage(Gauge.DATA_POINTS_FIELD_NUMBER, dataPoints); size += MarshalerUtil.sizeRepeatedMessage(Gauge.DATA_POINTS, dataPoints);
return size; return size;
} }
} }
@ -337,19 +326,16 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage(Histogram.DATA_POINTS_FIELD_NUMBER, dataPoints, output); output.serializeRepeatedMessage(Histogram.DATA_POINTS, dataPoints);
MarshalerUtil.marshalEnum( output.serializeEnum(Histogram.AGGREGATION_TEMPORALITY, aggregationTemporality);
Histogram.AGGREGATION_TEMPORALITY_FIELD_NUMBER, aggregationTemporality, output);
} }
private static int calculateSize( private static int calculateSize(
HistogramDataPointMarshaler[] dataPoints, int aggregationTemporality) { HistogramDataPointMarshaler[] dataPoints, int aggregationTemporality) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeRepeatedMessage(Histogram.DATA_POINTS_FIELD_NUMBER, dataPoints); size += MarshalerUtil.sizeRepeatedMessage(Histogram.DATA_POINTS, dataPoints);
size += size += MarshalerUtil.sizeEnum(Histogram.AGGREGATION_TEMPORALITY, aggregationTemporality);
MarshalerUtil.sizeEnum(
Histogram.AGGREGATION_TEMPORALITY_FIELD_NUMBER, aggregationTemporality);
return size; return size;
} }
} }
@ -421,21 +407,15 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalFixed64( output.serializeFixed64(HistogramDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
HistogramDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano, output); output.serializeFixed64(HistogramDataPoint.TIME_UNIX_NANO, timeUnixNano);
MarshalerUtil.marshalFixed64( output.serializeFixed64(HistogramDataPoint.COUNT, count);
HistogramDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano, output); output.serializeDouble(HistogramDataPoint.SUM, sum);
MarshalerUtil.marshalFixed64(HistogramDataPoint.COUNT_FIELD_NUMBER, count, output); output.serializeRepeatedFixed64(HistogramDataPoint.BUCKET_COUNTS, bucketCounts);
MarshalerUtil.marshalDouble(HistogramDataPoint.SUM_FIELD_NUMBER, sum, output); output.serializeRepeatedDouble(HistogramDataPoint.EXPLICIT_BOUNDS, explicitBounds);
MarshalerUtil.marshalRepeatedFixed64( output.serializeRepeatedMessage(HistogramDataPoint.EXEMPLARS, exemplars);
HistogramDataPoint.BUCKET_COUNTS_FIELD_NUMBER, bucketCounts, output); output.serializeRepeatedMessage(HistogramDataPoint.ATTRIBUTES, attributes);
MarshalerUtil.marshalRepeatedDouble(
HistogramDataPoint.EXPLICIT_BOUNDS_FIELD_NUMBER, explicitBounds, output);
MarshalerUtil.marshalRepeatedMessage(
HistogramDataPoint.EXEMPLARS_FIELD_NUMBER, exemplars, output);
MarshalerUtil.marshalRepeatedMessage(
HistogramDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes, output);
} }
private static int calculateSize( private static int calculateSize(
@ -448,23 +428,14 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
ExemplarMarshaler[] exemplars, ExemplarMarshaler[] exemplars,
AttributeMarshaler[] attributes) { AttributeMarshaler[] attributes) {
int size = 0; int size = 0;
size += size += MarshalerUtil.sizeFixed64(HistogramDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
MarshalerUtil.sizeFixed64( size += MarshalerUtil.sizeFixed64(HistogramDataPoint.TIME_UNIX_NANO, timeUnixNano);
HistogramDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano); size += MarshalerUtil.sizeFixed64(HistogramDataPoint.COUNT, count);
size += size += MarshalerUtil.sizeDouble(HistogramDataPoint.SUM, sum);
MarshalerUtil.sizeFixed64(HistogramDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano); size += MarshalerUtil.sizeRepeatedFixed64(HistogramDataPoint.BUCKET_COUNTS, bucketCounts);
size += MarshalerUtil.sizeFixed64(HistogramDataPoint.COUNT_FIELD_NUMBER, count); size += MarshalerUtil.sizeRepeatedDouble(HistogramDataPoint.EXPLICIT_BOUNDS, explicitBounds);
size += MarshalerUtil.sizeDouble(HistogramDataPoint.SUM_FIELD_NUMBER, sum); size += MarshalerUtil.sizeRepeatedMessage(HistogramDataPoint.EXEMPLARS, exemplars);
size += size += MarshalerUtil.sizeRepeatedMessage(HistogramDataPoint.ATTRIBUTES, attributes);
MarshalerUtil.sizeRepeatedFixed64(
HistogramDataPoint.BUCKET_COUNTS_FIELD_NUMBER, bucketCounts);
size +=
MarshalerUtil.sizeRepeatedDouble(
HistogramDataPoint.EXPLICIT_BOUNDS_FIELD_NUMBER, explicitBounds);
size +=
MarshalerUtil.sizeRepeatedMessage(HistogramDataPoint.EXEMPLARS_FIELD_NUMBER, exemplars);
size +=
MarshalerUtil.sizeRepeatedMessage(HistogramDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes);
return size; return size;
} }
} }
@ -493,20 +464,18 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage(Sum.DATA_POINTS_FIELD_NUMBER, dataPoints, output); output.serializeRepeatedMessage(Sum.DATA_POINTS, dataPoints);
MarshalerUtil.marshalEnum( output.serializeEnum(Sum.AGGREGATION_TEMPORALITY, aggregationTemporality);
Sum.AGGREGATION_TEMPORALITY_FIELD_NUMBER, aggregationTemporality, output); output.serializeBool(Sum.IS_MONOTONIC, isMonotonic);
MarshalerUtil.marshalBool(Sum.IS_MONOTONIC_FIELD_NUMBER, isMonotonic, output);
} }
private static int calculateSize( private static int calculateSize(
NumberDataPointMarshaler[] dataPoints, int aggregationTemporality, boolean isMonotonic) { NumberDataPointMarshaler[] dataPoints, int aggregationTemporality, boolean isMonotonic) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeRepeatedMessage(Sum.DATA_POINTS_FIELD_NUMBER, dataPoints); size += MarshalerUtil.sizeRepeatedMessage(Sum.DATA_POINTS, dataPoints);
size += size += MarshalerUtil.sizeEnum(Sum.AGGREGATION_TEMPORALITY, aggregationTemporality);
MarshalerUtil.sizeEnum(Sum.AGGREGATION_TEMPORALITY_FIELD_NUMBER, aggregationTemporality); size += MarshalerUtil.sizeBool(Sum.IS_MONOTONIC, isMonotonic);
size += MarshalerUtil.sizeBool(Sum.IS_MONOTONIC_FIELD_NUMBER, isMonotonic);
return size; return size;
} }
} }
@ -526,13 +495,13 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage(Summary.DATA_POINTS_FIELD_NUMBER, dataPoints, output); output.serializeRepeatedMessage(Summary.DATA_POINTS, dataPoints);
} }
private static int calculateSize(SummaryDataPointMarshaler[] dataPoints) { private static int calculateSize(SummaryDataPointMarshaler[] dataPoints) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeRepeatedMessage(Summary.DATA_POINTS_FIELD_NUMBER, dataPoints); size += MarshalerUtil.sizeRepeatedMessage(Summary.DATA_POINTS, dataPoints);
return size; return size;
} }
} }
@ -586,17 +555,13 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalFixed64( output.serializeFixed64(SummaryDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
SummaryDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano, output); output.serializeFixed64(SummaryDataPoint.TIME_UNIX_NANO, timeUnixNano);
MarshalerUtil.marshalFixed64( output.serializeFixed64(SummaryDataPoint.COUNT, count);
SummaryDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano, output); output.serializeDouble(SummaryDataPoint.SUM, sum);
MarshalerUtil.marshalFixed64(SummaryDataPoint.COUNT_FIELD_NUMBER, count, output); output.serializeRepeatedMessage(SummaryDataPoint.QUANTILE_VALUES, quantileValues);
MarshalerUtil.marshalDouble(SummaryDataPoint.SUM_FIELD_NUMBER, sum, output); output.serializeRepeatedMessage(SummaryDataPoint.ATTRIBUTES, attributes);
MarshalerUtil.marshalRepeatedMessage(
SummaryDataPoint.QUANTILE_VALUES_FIELD_NUMBER, quantileValues, output);
MarshalerUtil.marshalRepeatedMessage(
SummaryDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes, output);
} }
private static int calculateSize( private static int calculateSize(
@ -607,17 +572,12 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
ValueAtQuantileMarshaler[] quantileValues, ValueAtQuantileMarshaler[] quantileValues,
AttributeMarshaler[] attributes) { AttributeMarshaler[] attributes) {
int size = 0; int size = 0;
size += size += MarshalerUtil.sizeFixed64(SummaryDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
MarshalerUtil.sizeFixed64( size += MarshalerUtil.sizeFixed64(SummaryDataPoint.TIME_UNIX_NANO, timeUnixNano);
SummaryDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano); size += MarshalerUtil.sizeFixed64(SummaryDataPoint.COUNT, count);
size += MarshalerUtil.sizeFixed64(SummaryDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano); size += MarshalerUtil.sizeDouble(SummaryDataPoint.SUM, sum);
size += MarshalerUtil.sizeFixed64(SummaryDataPoint.COUNT_FIELD_NUMBER, count); size += MarshalerUtil.sizeRepeatedMessage(SummaryDataPoint.QUANTILE_VALUES, quantileValues);
size += MarshalerUtil.sizeDouble(SummaryDataPoint.SUM_FIELD_NUMBER, sum); size += MarshalerUtil.sizeRepeatedMessage(SummaryDataPoint.ATTRIBUTES, attributes);
size +=
MarshalerUtil.sizeRepeatedMessage(
SummaryDataPoint.QUANTILE_VALUES_FIELD_NUMBER, quantileValues);
size +=
MarshalerUtil.sizeRepeatedMessage(SummaryDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes);
return size; return size;
} }
} }
@ -646,19 +606,15 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalDouble( output.serializeDouble(SummaryDataPoint.ValueAtQuantile.QUANTILE, quantile);
SummaryDataPoint.ValueAtQuantile.QUANTILE_FIELD_NUMBER, quantile, output); output.serializeDouble(SummaryDataPoint.ValueAtQuantile.VALUE, value);
MarshalerUtil.marshalDouble(
SummaryDataPoint.ValueAtQuantile.VALUE_FIELD_NUMBER, value, output);
} }
private static int calculateSize(double quantile, double value) { private static int calculateSize(double quantile, double value) {
int size = 0; int size = 0;
size += size += MarshalerUtil.sizeDouble(SummaryDataPoint.ValueAtQuantile.QUANTILE, quantile);
MarshalerUtil.sizeDouble( size += MarshalerUtil.sizeDouble(SummaryDataPoint.ValueAtQuantile.VALUE, value);
SummaryDataPoint.ValueAtQuantile.QUANTILE_FIELD_NUMBER, quantile);
size += MarshalerUtil.sizeDouble(SummaryDataPoint.ValueAtQuantile.VALUE_FIELD_NUMBER, value);
return size; return size;
} }
} }
@ -669,7 +625,7 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
// Always fixed64, for a double it's the bits themselves. // Always fixed64, for a double it's the bits themselves.
private final long value; private final long value;
private final int valueFieldNumber; private final ProtoFieldInfo valueField;
private final ExemplarMarshaler[] exemplars; private final ExemplarMarshaler[] exemplars;
private final AttributeMarshaler[] attributes; private final AttributeMarshaler[] attributes;
@ -691,21 +647,21 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
AttributeMarshaler.createRepeated(point.getAttributes()); AttributeMarshaler.createRepeated(point.getAttributes());
final long value; final long value;
final int valueFieldNumber; final ProtoFieldInfo valueField;
if (point instanceof LongPointData) { if (point instanceof LongPointData) {
value = ((LongPointData) point).getValue(); value = ((LongPointData) point).getValue();
valueFieldNumber = NumberDataPoint.AS_INT_FIELD_NUMBER; valueField = NumberDataPoint.AS_INT;
} else { } else {
assert point instanceof DoublePointData; assert point instanceof DoublePointData;
value = Double.doubleToRawLongBits(((DoublePointData) point).getValue()); value = Double.doubleToRawLongBits(((DoublePointData) point).getValue());
valueFieldNumber = NumberDataPoint.AS_DOUBLE_FIELD_NUMBER; valueField = NumberDataPoint.AS_DOUBLE;
} }
return new NumberDataPointMarshaler( return new NumberDataPointMarshaler(
point.getStartEpochNanos(), point.getStartEpochNanos(),
point.getEpochNanos(), point.getEpochNanos(),
value, value,
valueFieldNumber, valueField,
exemplarMarshalers, exemplarMarshalers,
attributeMarshalers); attributeMarshalers);
} }
@ -714,49 +670,41 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
long startTimeUnixNano, long startTimeUnixNano,
long timeUnixNano, long timeUnixNano,
long value, long value,
int valueFieldNumber, ProtoFieldInfo valueField,
ExemplarMarshaler[] exemplars, ExemplarMarshaler[] exemplars,
AttributeMarshaler[] attributes) { AttributeMarshaler[] attributes) {
super( super(
calculateSize( calculateSize(startTimeUnixNano, timeUnixNano, value, valueField, exemplars, attributes));
startTimeUnixNano, timeUnixNano, value, valueFieldNumber, exemplars, attributes));
this.startTimeUnixNano = startTimeUnixNano; this.startTimeUnixNano = startTimeUnixNano;
this.timeUnixNano = timeUnixNano; this.timeUnixNano = timeUnixNano;
this.value = value; this.value = value;
this.valueFieldNumber = valueFieldNumber; this.valueField = valueField;
this.exemplars = exemplars; this.exemplars = exemplars;
this.attributes = attributes; this.attributes = attributes;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalFixed64( output.serializeFixed64(NumberDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
NumberDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano, output); output.serializeFixed64(NumberDataPoint.TIME_UNIX_NANO, timeUnixNano);
MarshalerUtil.marshalFixed64( output.serializeFixed64(valueField, value);
NumberDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano, output); output.serializeRepeatedMessage(NumberDataPoint.EXEMPLARS, exemplars);
MarshalerUtil.marshalFixed64(valueFieldNumber, value, output); output.serializeRepeatedMessage(NumberDataPoint.ATTRIBUTES, attributes);
MarshalerUtil.marshalRepeatedMessage(
NumberDataPoint.EXEMPLARS_FIELD_NUMBER, exemplars, output);
MarshalerUtil.marshalRepeatedMessage(
NumberDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes, output);
} }
private static int calculateSize( private static int calculateSize(
long startTimeUnixNano, long startTimeUnixNano,
long timeUnixNano, long timeUnixNano,
long value, long value,
int valueFieldNumber, ProtoFieldInfo valueField,
ExemplarMarshaler[] exemplars, ExemplarMarshaler[] exemplars,
AttributeMarshaler[] attributes) { AttributeMarshaler[] attributes) {
int size = 0; int size = 0;
size += size += MarshalerUtil.sizeFixed64(NumberDataPoint.START_TIME_UNIX_NANO, startTimeUnixNano);
MarshalerUtil.sizeFixed64( size += MarshalerUtil.sizeFixed64(NumberDataPoint.TIME_UNIX_NANO, timeUnixNano);
NumberDataPoint.START_TIME_UNIX_NANO_FIELD_NUMBER, startTimeUnixNano); size += MarshalerUtil.sizeFixed64(valueField, value);
size += MarshalerUtil.sizeFixed64(NumberDataPoint.TIME_UNIX_NANO_FIELD_NUMBER, timeUnixNano); size += MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.EXEMPLARS, exemplars);
size += MarshalerUtil.sizeFixed64(valueFieldNumber, value); size += MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.ATTRIBUTES, attributes);
size += MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.EXEMPLARS_FIELD_NUMBER, exemplars);
size +=
MarshalerUtil.sizeRepeatedMessage(NumberDataPoint.ATTRIBUTES_FIELD_NUMBER, attributes);
return size; return size;
} }
} }
@ -767,7 +715,7 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
// Always fixed64, for a double it's the bits themselves. // Always fixed64, for a double it's the bits themselves.
private final long value; private final long value;
private final int valueFieldNumber; private final ProtoFieldInfo valueField;
private final byte[] spanId; private final byte[] spanId;
private final byte[] traceId; private final byte[] traceId;
@ -788,15 +736,14 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
AttributeMarshaler.createRepeated(exemplar.getFilteredAttributes()); AttributeMarshaler.createRepeated(exemplar.getFilteredAttributes());
final long value; final long value;
final int valueFieldNumber; final ProtoFieldInfo valueField;
if (exemplar instanceof LongExemplar) { if (exemplar instanceof LongExemplar) {
value = ((LongExemplar) exemplar).getValue(); value = ((LongExemplar) exemplar).getValue();
valueFieldNumber = io.opentelemetry.proto.metrics.v1.internal.Exemplar.AS_INT_FIELD_NUMBER; valueField = io.opentelemetry.proto.metrics.v1.internal.Exemplar.AS_INT;
} else { } else {
assert exemplar instanceof DoubleExemplar; assert exemplar instanceof DoubleExemplar;
value = Double.doubleToRawLongBits(((DoubleExemplar) exemplar).getValue()); value = Double.doubleToRawLongBits(((DoubleExemplar) exemplar).getValue());
valueFieldNumber = valueField = io.opentelemetry.proto.metrics.v1.internal.Exemplar.AS_DOUBLE;
io.opentelemetry.proto.metrics.v1.internal.Exemplar.AS_DOUBLE_FIELD_NUMBER;
} }
byte[] spanId = MarshalerUtil.EMPTY_BYTES; byte[] spanId = MarshalerUtil.EMPTY_BYTES;
@ -809,68 +756,60 @@ public final class MetricsRequestMarshaler extends MarshalerWithSize implements
} }
return new ExemplarMarshaler( return new ExemplarMarshaler(
exemplar.getEpochNanos(), value, valueFieldNumber, spanId, traceId, attributeMarshalers); exemplar.getEpochNanos(), value, valueField, spanId, traceId, attributeMarshalers);
} }
private ExemplarMarshaler( private ExemplarMarshaler(
long timeUnixNano, long timeUnixNano,
long value, long value,
int valueFieldNumber, ProtoFieldInfo valueField,
byte[] spanId, byte[] spanId,
byte[] traceId, byte[] traceId,
AttributeMarshaler[] filteredAttributeMarshalers) { AttributeMarshaler[] filteredAttributeMarshalers) {
super( super(
calculateSize( calculateSize(
timeUnixNano, value, valueFieldNumber, spanId, traceId, filteredAttributeMarshalers)); timeUnixNano, value, valueField, spanId, traceId, filteredAttributeMarshalers));
this.timeUnixNano = timeUnixNano; this.timeUnixNano = timeUnixNano;
this.value = value; this.value = value;
this.valueFieldNumber = valueFieldNumber; this.valueField = valueField;
this.spanId = spanId; this.spanId = spanId;
this.traceId = traceId; this.traceId = traceId;
this.filteredAttributeMarshalers = filteredAttributeMarshalers; this.filteredAttributeMarshalers = filteredAttributeMarshalers;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalFixed64( output.serializeFixed64(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TIME_UNIX_NANO_FIELD_NUMBER, io.opentelemetry.proto.metrics.v1.internal.Exemplar.TIME_UNIX_NANO, timeUnixNano);
timeUnixNano, output.serializeFixed64(valueField, value);
output); output.serializeBytes(io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanId);
MarshalerUtil.marshalFixed64(valueFieldNumber, value, output); output.serializeBytes(io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID, traceId);
MarshalerUtil.marshalBytes( output.serializeRepeatedMessage(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID_FIELD_NUMBER, spanId, output); io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES,
MarshalerUtil.marshalBytes( filteredAttributeMarshalers);
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID_FIELD_NUMBER,
traceId,
output);
MarshalerUtil.marshalRepeatedMessage(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES_FIELD_NUMBER,
filteredAttributeMarshalers,
output);
} }
private static int calculateSize( private static int calculateSize(
long timeUnixNano, long timeUnixNano,
long value, long value,
int valueFieldNumber, ProtoFieldInfo valueField,
byte[] spanId, byte[] spanId,
byte[] traceId, byte[] traceId,
AttributeMarshaler[] filteredAttributeMarshalers) { AttributeMarshaler[] filteredAttributeMarshalers) {
int size = 0; int size = 0;
size += size +=
MarshalerUtil.sizeFixed64( MarshalerUtil.sizeFixed64(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TIME_UNIX_NANO_FIELD_NUMBER, io.opentelemetry.proto.metrics.v1.internal.Exemplar.TIME_UNIX_NANO, timeUnixNano);
timeUnixNano); size += MarshalerUtil.sizeFixed64(valueField, value);
size += MarshalerUtil.sizeFixed64(valueFieldNumber, value);
size += size +=
MarshalerUtil.sizeBytes( MarshalerUtil.sizeBytes(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID_FIELD_NUMBER, spanId); io.opentelemetry.proto.metrics.v1.internal.Exemplar.SPAN_ID, spanId);
size += size +=
MarshalerUtil.sizeBytes( MarshalerUtil.sizeBytes(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID_FIELD_NUMBER, traceId); io.opentelemetry.proto.metrics.v1.internal.Exemplar.TRACE_ID, traceId);
size += size +=
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES_FIELD_NUMBER, io.opentelemetry.proto.metrics.v1.internal.Exemplar.FILTERED_ATTRIBUTES,
filteredAttributeMarshalers); filteredAttributeMarshalers);
return size; return size;
} }

View File

@ -9,10 +9,10 @@ enum NoopMarshaler implements Marshaler {
INSTANCE; INSTANCE;
@Override @Override
public void writeTo(CodedOutputStream output) {} public void writeTo(Serializer output) {}
@Override @Override
public int getSerializedSize() { public int getProtoSerializedSize() {
return 0; return 0;
} }
} }

View File

@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.otlp.internal;
import com.google.auto.value.AutoValue;
@AutoValue
public abstract class ProtoFieldInfo {
public static ProtoFieldInfo create(int fieldNumber, int tag, String jsonName) {
return new AutoValue_ProtoFieldInfo(
fieldNumber, tag, CodedOutputStream.computeTagSize(fieldNumber), jsonName);
}
public abstract int getFieldNumber();
public abstract int getTag();
public abstract int getTagSize();
public abstract String getJsonName();
}

View File

@ -26,7 +26,7 @@ public final class ProtoRequestBody extends RequestBody {
/** Creates a new {@link ProtoRequestBody}. */ /** Creates a new {@link ProtoRequestBody}. */
public ProtoRequestBody(Marshaler marshaler) { public ProtoRequestBody(Marshaler marshaler) {
this.marshaler = marshaler; this.marshaler = marshaler;
contentLength = marshaler.getSerializedSize(); contentLength = marshaler.getProtoSerializedSize();
} }
@Override @Override
@ -42,7 +42,7 @@ public final class ProtoRequestBody extends RequestBody {
@Override @Override
public void writeTo(BufferedSink bufferedSink) throws IOException { public void writeTo(BufferedSink bufferedSink) throws IOException {
CodedOutputStream cos = CodedOutputStream.newInstance(bufferedSink.outputStream()); CodedOutputStream cos = CodedOutputStream.newInstance(bufferedSink.outputStream());
marshaler.writeTo(cos); marshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
} }
} }

View File

@ -0,0 +1,127 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.otlp.internal;
import java.io.IOException;
import java.util.List;
/** Serializer for the protobuf binary wire format. */
final class ProtoSerializer extends Serializer {
private final CodedOutputStream output;
ProtoSerializer(CodedOutputStream output) {
this.output = output;
}
@Override
protected void writeBool(ProtoFieldInfo field, boolean value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeBoolNoTag(value);
}
@Override
protected void writeEnum(ProtoFieldInfo field, int enumNumber) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeEnumNoTag(enumNumber);
}
@Override
protected void writeUint32(ProtoFieldInfo field, int value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeUInt32NoTag(value);
}
@Override
protected void writeInt64(ProtoFieldInfo field, long value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeInt64NoTag(value);
}
@Override
protected void writeFixed64(ProtoFieldInfo field, long value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeFixed64NoTag(value);
}
@Override
protected void writeFixed64Value(long value) throws IOException {
output.writeFixed64NoTag(value);
}
@Override
protected void writeDouble(ProtoFieldInfo field, double value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeDoubleNoTag(value);
}
@Override
protected void writeDoubleValue(double value) throws IOException {
output.writeDoubleNoTag(value);
}
@Override
protected void writeString(ProtoFieldInfo field, byte[] utf8Bytes) throws IOException {
writeBytes(field, utf8Bytes);
}
@Override
protected void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeByteArrayNoTag(value);
}
@Override
protected void writeStartMessage(ProtoFieldInfo field, int protoMessageSize) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeUInt32NoTag(protoMessageSize);
}
@Override
protected void writeEndMessage() {
// Do nothing
}
@Override
protected void writeStartRepeatedPrimitive(
ProtoFieldInfo field, int protoSizePerElement, int numElements) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeUInt32NoTag(protoSizePerElement * numElements);
}
@Override
protected void writeEndRepeatedPrimitive() {
// Do nothing
}
@Override
public void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage)
throws IOException {
for (Marshaler message : repeatedMessage) {
serializeMessage(field, message);
}
}
@Override
public void serializeRepeatedMessage(
ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) throws IOException {
for (Marshaler message : repeatedMessage) {
serializeMessage(field, message);
}
}
@Override
public void writeSerializedMessage(byte[] protoSerialized, byte[] jsonSerialized)
throws IOException {
output.writeRawBytes(protoSerialized);
}
// TODO(anuraaga): Remove after moving protobuf Value serialization from AttributeMarshaler to
// here.
CodedOutputStream getCodedOutputStream() {
return output;
}
}

View File

@ -31,11 +31,11 @@ final class ResourceMarshaler extends MarshalerWithSize {
private ResourceMarshaler(AttributeMarshaler[] attributeMarshalers) { private ResourceMarshaler(AttributeMarshaler[] attributeMarshalers) {
super(calculateSize(attributeMarshalers)); super(calculateSize(attributeMarshalers));
ByteArrayOutputStream bos = new ByteArrayOutputStream(getSerializedSize()); ByteArrayOutputStream bos = new ByteArrayOutputStream(getProtoSerializedSize());
CodedOutputStream output = CodedOutputStream.newInstance(bos); CodedOutputStream output = CodedOutputStream.newInstance(bos);
ProtoSerializer serializer = new ProtoSerializer(output);
try { try {
MarshalerUtil.marshalRepeatedMessage( serializer.serializeRepeatedMessage(Resource.ATTRIBUTES, attributeMarshalers);
Resource.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output);
output.flush(); output.flush();
} catch (IOException e) { } catch (IOException e) {
// Presized so can't happen (we would have already thrown OutOfMemoryError) // Presized so can't happen (we would have already thrown OutOfMemoryError)
@ -45,11 +45,12 @@ final class ResourceMarshaler extends MarshalerWithSize {
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
output.writeRawBytes(serializedResource); // TODO(anuraaga): Preserialize JSON as well.
output.writeSerializedMessage(serializedResource, MarshalerUtil.EMPTY_BYTES);
} }
private static int calculateSize(AttributeMarshaler[] attributeMarshalers) { private static int calculateSize(AttributeMarshaler[] attributeMarshalers) {
return MarshalerUtil.sizeRepeatedMessage(Resource.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers); return MarshalerUtil.sizeRepeatedMessage(Resource.ATTRIBUTES, attributeMarshalers);
} }
} }

View File

@ -0,0 +1,161 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.otlp.internal;
import java.io.IOException;
import java.util.List;
/**
* Serializer to use when converting from an SDK data object into a protobuf output format. Unlike
* {@link CodedOutputStream}, which strictly encodes data into the protobuf binary format, this
*
* <ul>
* <li>Handles proto3 semantics of not outputting the value when it matches the default of a field
* <li>Can be implemented to serialize into protobuf JSON format (not binary)
* </ul>
*/
public abstract class Serializer {
/** Returns a {@link Serializer} to serialize into protobuf binary format. */
public static Serializer createProtoSerializer(CodedOutputStream output) {
return new ProtoSerializer(output);
}
Serializer() {}
/** Serializes a protobuf {@code bool} field. */
public void serializeBool(ProtoFieldInfo field, boolean value) throws IOException {
if (!value) {
return;
}
writeBool(field, value);
}
protected abstract void writeBool(ProtoFieldInfo field, boolean value) throws IOException;
/** Serializes a protobuf {@code enum} field. */
public void serializeEnum(ProtoFieldInfo field, int enumNumber) throws IOException {
if (enumNumber == 0) {
return;
}
writeEnum(field, enumNumber);
}
protected abstract void writeEnum(ProtoFieldInfo field, int enumNumber) throws IOException;
/** Serializes a protobuf {@code uint32} field. */
public void serializeUInt32(ProtoFieldInfo field, int value) throws IOException {
if (value == 0) {
return;
}
writeUint32(field, value);
}
protected abstract void writeUint32(ProtoFieldInfo field, int value) throws IOException;
protected abstract void writeInt64(ProtoFieldInfo field, long value) throws IOException;
/** Serializes a protobuf {@code fixed64} field. */
public void serializeFixed64(ProtoFieldInfo field, long value) throws IOException {
if (value == 0) {
return;
}
writeFixed64(field, value);
}
protected abstract void writeFixed64(ProtoFieldInfo field, long value) throws IOException;
protected abstract void writeFixed64Value(long value) throws IOException;
/** Serializes a proto buf {@code double} field. */
public void serializeDouble(ProtoFieldInfo field, double value) throws IOException {
if (value == 0D) {
return;
}
writeDouble(field, value);
}
protected abstract void writeDouble(ProtoFieldInfo field, double value) throws IOException;
protected abstract void writeDoubleValue(double value) throws IOException;
/**
* Serializes a protobuf {@code string} field. {@code utf8Bytes} is the UTF8 encoded bytes of the
* string to serialize.
*/
public void serializeString(ProtoFieldInfo field, byte[] utf8Bytes) throws IOException {
if (utf8Bytes.length == 0) {
return;
}
writeString(field, utf8Bytes);
}
protected abstract void writeString(ProtoFieldInfo field, byte[] utf8Bytes) throws IOException;
/** Serializes a protobuf {@code bytes} field. */
public void serializeBytes(ProtoFieldInfo field, byte[] value) throws IOException {
if (value.length == 0) {
return;
}
writeBytes(field, value);
}
protected abstract void writeBytes(ProtoFieldInfo field, byte[] value) throws IOException;
protected abstract void writeStartMessage(ProtoFieldInfo field, int protoMessageSize)
throws IOException;
protected abstract void writeEndMessage() throws IOException;
/** Serializes a protobuf embedded {@code message}. */
public void serializeMessage(ProtoFieldInfo field, Marshaler message) throws IOException {
writeStartMessage(field, message.getProtoSerializedSize());
message.writeTo(this);
writeEndMessage();
}
protected abstract void writeStartRepeatedPrimitive(
ProtoFieldInfo field, int protoSizePerElement, int numElements) throws IOException;
protected abstract void writeEndRepeatedPrimitive() throws IOException;
/** Serializes a {@code repeated fixed64} field. */
public void serializeRepeatedFixed64(ProtoFieldInfo field, List<Long> values) throws IOException {
if (values.isEmpty()) {
return;
}
writeStartRepeatedPrimitive(field, WireFormat.FIXED64_SIZE, values.size());
for (long value : values) {
writeFixed64Value(value);
}
writeEndRepeatedPrimitive();
}
/** Serializes a {@code repeated double} field. */
public void serializeRepeatedDouble(ProtoFieldInfo field, List<Double> values)
throws IOException {
if (values.isEmpty()) {
return;
}
writeStartRepeatedPrimitive(field, WireFormat.FIXED64_SIZE, values.size());
for (double value : values) {
writeDoubleValue(value);
}
writeEndRepeatedPrimitive();
}
/** Serializes {@code repeated message} field. */
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage)
throws IOException;
/** Serializes {@code repeated message} field. */
public abstract void serializeRepeatedMessage(
ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) throws IOException;
/** Writes the value for a message field that has been pre-serialized. */
public abstract void writeSerializedMessage(byte[] protoSerialized, byte[] jsonSerialized)
throws IOException;
}

View File

@ -77,52 +77,49 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private TraceRequestMarshaler(ResourceSpansMarshaler[] resourceSpansMarshalers) { private TraceRequestMarshaler(ResourceSpansMarshaler[] resourceSpansMarshalers) {
super( super(
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
ExportTraceServiceRequest.RESOURCE_SPANS_FIELD_NUMBER, resourceSpansMarshalers)); ExportTraceServiceRequest.RESOURCE_SPANS, resourceSpansMarshalers));
this.resourceSpansMarshalers = resourceSpansMarshalers; this.resourceSpansMarshalers = resourceSpansMarshalers;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(
ExportTraceServiceRequest.RESOURCE_SPANS_FIELD_NUMBER, resourceSpansMarshalers, output); ExportTraceServiceRequest.RESOURCE_SPANS, resourceSpansMarshalers);
} }
private static final class ResourceSpansMarshaler extends MarshalerWithSize { private static final class ResourceSpansMarshaler extends MarshalerWithSize {
private final ResourceMarshaler resourceMarshaler; private final ResourceMarshaler resourceMarshaler;
private final byte[] schemaUrl; private final byte[] schemaUrlUtf8;
private final InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers; private final InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers;
private ResourceSpansMarshaler( private ResourceSpansMarshaler(
ResourceMarshaler resourceMarshaler, ResourceMarshaler resourceMarshaler,
byte[] schemaUrl, byte[] schemaUrlUtf8,
InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) { InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) {
super(calculateSize(resourceMarshaler, schemaUrl, instrumentationLibrarySpansMarshalers)); super(calculateSize(resourceMarshaler, schemaUrlUtf8, instrumentationLibrarySpansMarshalers));
this.resourceMarshaler = resourceMarshaler; this.resourceMarshaler = resourceMarshaler;
this.schemaUrl = schemaUrl; this.schemaUrlUtf8 = schemaUrlUtf8;
this.instrumentationLibrarySpansMarshalers = instrumentationLibrarySpansMarshalers; this.instrumentationLibrarySpansMarshalers = instrumentationLibrarySpansMarshalers;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalMessage(ResourceSpans.RESOURCE_FIELD_NUMBER, resourceMarshaler, output); output.serializeMessage(ResourceSpans.RESOURCE, resourceMarshaler);
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS_FIELD_NUMBER, ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS, instrumentationLibrarySpansMarshalers);
instrumentationLibrarySpansMarshalers, output.serializeString(ResourceSpans.SCHEMA_URL, schemaUrlUtf8);
output);
MarshalerUtil.marshalBytes(ResourceSpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
} }
private static int calculateSize( private static int calculateSize(
ResourceMarshaler resourceMarshaler, ResourceMarshaler resourceMarshaler,
byte[] schemaUrl, byte[] schemaUrlUtf8,
InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) { InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeMessage(ResourceSpans.RESOURCE_FIELD_NUMBER, resourceMarshaler); size += MarshalerUtil.sizeMessage(ResourceSpans.RESOURCE, resourceMarshaler);
size += MarshalerUtil.sizeBytes(ResourceSpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl); size += MarshalerUtil.sizeBytes(ResourceSpans.SCHEMA_URL, schemaUrlUtf8);
size += size +=
MarshalerUtil.sizeRepeatedMessage( MarshalerUtil.sizeRepeatedMessage(
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS_FIELD_NUMBER, ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS, instrumentationLibrarySpansMarshalers);
instrumentationLibrarySpansMarshalers);
return size; return size;
} }
} }
@ -130,44 +127,36 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private static final class InstrumentationLibrarySpansMarshaler extends MarshalerWithSize { private static final class InstrumentationLibrarySpansMarshaler extends MarshalerWithSize {
private final InstrumentationLibraryMarshaler instrumentationLibrary; private final InstrumentationLibraryMarshaler instrumentationLibrary;
private final List<SpanMarshaler> spanMarshalers; private final List<SpanMarshaler> spanMarshalers;
private final byte[] schemaUrl; private final byte[] schemaUrlUtf8;
private InstrumentationLibrarySpansMarshaler( private InstrumentationLibrarySpansMarshaler(
InstrumentationLibraryMarshaler instrumentationLibrary, InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl, byte[] schemaUrlUtf8,
List<SpanMarshaler> spanMarshalers) { List<SpanMarshaler> spanMarshalers) {
super(calculateSize(instrumentationLibrary, schemaUrl, spanMarshalers)); super(calculateSize(instrumentationLibrary, schemaUrlUtf8, spanMarshalers));
this.instrumentationLibrary = instrumentationLibrary; this.instrumentationLibrary = instrumentationLibrary;
this.schemaUrl = schemaUrl; this.schemaUrlUtf8 = schemaUrlUtf8;
this.spanMarshalers = spanMarshalers; this.spanMarshalers = spanMarshalers;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalMessage( output.serializeMessage(
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY_FIELD_NUMBER, InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
instrumentationLibrary, output.serializeRepeatedMessage(InstrumentationLibrarySpans.SPANS, spanMarshalers);
output); output.serializeString(InstrumentationLibrarySpans.SCHEMA_URL, schemaUrlUtf8);
MarshalerUtil.marshalRepeatedMessage(
InstrumentationLibrarySpans.SPANS_FIELD_NUMBER, spanMarshalers, output);
MarshalerUtil.marshalBytes(
InstrumentationLibrarySpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
} }
private static int calculateSize( private static int calculateSize(
InstrumentationLibraryMarshaler instrumentationLibrary, InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl, byte[] schemaUrlUtf8,
List<SpanMarshaler> spanMarshalers) { List<SpanMarshaler> spanMarshalers) {
int size = 0; int size = 0;
size += size +=
MarshalerUtil.sizeMessage( MarshalerUtil.sizeMessage(
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY_FIELD_NUMBER, InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
instrumentationLibrary); size += MarshalerUtil.sizeBytes(InstrumentationLibrarySpans.SCHEMA_URL, schemaUrlUtf8);
size += size += MarshalerUtil.sizeRepeatedMessage(InstrumentationLibrarySpans.SPANS, spanMarshalers);
MarshalerUtil.sizeBytes(InstrumentationLibrarySpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl);
size +=
MarshalerUtil.sizeRepeatedMessage(
InstrumentationLibrarySpans.SPANS_FIELD_NUMBER, spanMarshalers);
return size; return size;
} }
} }
@ -176,7 +165,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private final byte[] traceId; private final byte[] traceId;
private final byte[] spanId; private final byte[] spanId;
private final byte[] parentSpanId; private final byte[] parentSpanId;
private final byte[] name; private final byte[] nameUtf8;
private final int spanKind; private final int spanKind;
private final long startEpochNanos; private final long startEpochNanos;
private final long endEpochNanos; private final long endEpochNanos;
@ -236,7 +225,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
byte[] traceId, byte[] traceId,
byte[] spanId, byte[] spanId,
byte[] parentSpanId, byte[] parentSpanId,
byte[] name, byte[] nameUtf8,
int spanKind, int spanKind,
long startEpochNanos, long startEpochNanos,
long endEpochNanos, long endEpochNanos,
@ -252,7 +241,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
traceId, traceId,
spanId, spanId,
parentSpanId, parentSpanId,
name, nameUtf8,
spanKind, spanKind,
startEpochNanos, startEpochNanos,
endEpochNanos, endEpochNanos,
@ -266,7 +255,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
this.traceId = traceId; this.traceId = traceId;
this.spanId = spanId; this.spanId = spanId;
this.parentSpanId = parentSpanId; this.parentSpanId = parentSpanId;
this.name = name; this.nameUtf8 = nameUtf8;
this.spanKind = spanKind; this.spanKind = spanKind;
this.startEpochNanos = startEpochNanos; this.startEpochNanos = startEpochNanos;
this.endEpochNanos = endEpochNanos; this.endEpochNanos = endEpochNanos;
@ -280,38 +269,35 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalBytes(Span.TRACE_ID_FIELD_NUMBER, traceId, output); output.serializeBytes(Span.TRACE_ID, traceId);
MarshalerUtil.marshalBytes(Span.SPAN_ID_FIELD_NUMBER, spanId, output); output.serializeBytes(Span.SPAN_ID, spanId);
// TODO: Set TraceState; // TODO: Set TraceState;
MarshalerUtil.marshalBytes(Span.PARENT_SPAN_ID_FIELD_NUMBER, parentSpanId, output); output.serializeBytes(Span.PARENT_SPAN_ID, parentSpanId);
MarshalerUtil.marshalBytes(Span.NAME_FIELD_NUMBER, name, output); output.serializeString(Span.NAME, nameUtf8);
MarshalerUtil.marshalEnum(Span.KIND_FIELD_NUMBER, spanKind, output); output.serializeEnum(Span.KIND, spanKind);
MarshalerUtil.marshalFixed64(Span.START_TIME_UNIX_NANO_FIELD_NUMBER, startEpochNanos, output); output.serializeFixed64(Span.START_TIME_UNIX_NANO, startEpochNanos);
MarshalerUtil.marshalFixed64(Span.END_TIME_UNIX_NANO_FIELD_NUMBER, endEpochNanos, output); output.serializeFixed64(Span.END_TIME_UNIX_NANO, endEpochNanos);
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(Span.ATTRIBUTES, attributeMarshalers);
Span.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output); output.serializeUInt32(Span.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
MarshalerUtil.marshalUInt32(
Span.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
MarshalerUtil.marshalRepeatedMessage(Span.EVENTS_FIELD_NUMBER, spanEventMarshalers, output); output.serializeRepeatedMessage(Span.EVENTS, spanEventMarshalers);
MarshalerUtil.marshalUInt32( output.serializeUInt32(Span.DROPPED_EVENTS_COUNT, droppedEventsCount);
Span.DROPPED_EVENTS_COUNT_FIELD_NUMBER, droppedEventsCount, output);
MarshalerUtil.marshalRepeatedMessage(Span.LINKS_FIELD_NUMBER, spanLinkMarshalers, output); output.serializeRepeatedMessage(Span.LINKS, spanLinkMarshalers);
MarshalerUtil.marshalUInt32(Span.DROPPED_LINKS_COUNT_FIELD_NUMBER, droppedLinksCount, output); output.serializeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);
MarshalerUtil.marshalMessage(Span.STATUS_FIELD_NUMBER, spanStatusMarshaler, output); output.serializeMessage(Span.STATUS, spanStatusMarshaler);
} }
private static int calculateSize( private static int calculateSize(
byte[] traceId, byte[] traceId,
byte[] spanId, byte[] spanId,
byte[] parentSpanId, byte[] parentSpanId,
byte[] name, byte[] nameUtf8,
int spanKind, int spanKind,
long startEpochNanos, long startEpochNanos,
long endEpochNanos, long endEpochNanos,
@ -323,29 +309,27 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
int droppedLinksCount, int droppedLinksCount,
SpanStatusMarshaler spanStatusMarshaler) { SpanStatusMarshaler spanStatusMarshaler) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeBytes(Span.TRACE_ID_FIELD_NUMBER, traceId); size += MarshalerUtil.sizeBytes(Span.TRACE_ID, traceId);
size += MarshalerUtil.sizeBytes(Span.SPAN_ID_FIELD_NUMBER, spanId); size += MarshalerUtil.sizeBytes(Span.SPAN_ID, spanId);
// TODO: Set TraceState; // TODO: Set TraceState;
size += MarshalerUtil.sizeBytes(Span.PARENT_SPAN_ID_FIELD_NUMBER, parentSpanId); size += MarshalerUtil.sizeBytes(Span.PARENT_SPAN_ID, parentSpanId);
size += MarshalerUtil.sizeBytes(Span.NAME_FIELD_NUMBER, name); size += MarshalerUtil.sizeBytes(Span.NAME, nameUtf8);
size += MarshalerUtil.sizeEnum(Span.KIND_FIELD_NUMBER, spanKind); size += MarshalerUtil.sizeEnum(Span.KIND, spanKind);
size += MarshalerUtil.sizeFixed64(Span.START_TIME_UNIX_NANO_FIELD_NUMBER, startEpochNanos); size += MarshalerUtil.sizeFixed64(Span.START_TIME_UNIX_NANO, startEpochNanos);
size += MarshalerUtil.sizeFixed64(Span.END_TIME_UNIX_NANO_FIELD_NUMBER, endEpochNanos); size += MarshalerUtil.sizeFixed64(Span.END_TIME_UNIX_NANO, endEpochNanos);
size += MarshalerUtil.sizeRepeatedMessage(Span.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers); size += MarshalerUtil.sizeRepeatedMessage(Span.ATTRIBUTES, attributeMarshalers);
size += size += MarshalerUtil.sizeUInt32(Span.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
MarshalerUtil.sizeUInt32(
Span.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.EVENTS_FIELD_NUMBER, spanEventMarshalers); size += MarshalerUtil.sizeRepeatedMessage(Span.EVENTS, spanEventMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_EVENTS_COUNT_FIELD_NUMBER, droppedEventsCount); size += MarshalerUtil.sizeUInt32(Span.DROPPED_EVENTS_COUNT, droppedEventsCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.LINKS_FIELD_NUMBER, spanLinkMarshalers); size += MarshalerUtil.sizeRepeatedMessage(Span.LINKS, spanLinkMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT_FIELD_NUMBER, droppedLinksCount); size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);
size += MarshalerUtil.sizeMessage(Span.STATUS_FIELD_NUMBER, spanStatusMarshaler); size += MarshalerUtil.sizeMessage(Span.STATUS, spanStatusMarshaler);
return size; return size;
} }
} }
@ -389,13 +373,11 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalFixed64(Span.Event.TIME_UNIX_NANO_FIELD_NUMBER, epochNanos, output); output.serializeFixed64(Span.Event.TIME_UNIX_NANO, epochNanos);
MarshalerUtil.marshalBytes(Span.Event.NAME_FIELD_NUMBER, name, output); output.serializeBytes(Span.Event.NAME, name);
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(Span.Event.ATTRIBUTES, attributeMarshalers);
Span.Event.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output); output.serializeUInt32(Span.Event.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
MarshalerUtil.marshalUInt32(
Span.Event.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
} }
private static int calculateSize( private static int calculateSize(
@ -404,14 +386,10 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
AttributeMarshaler[] attributeMarshalers, AttributeMarshaler[] attributeMarshalers,
int droppedAttributesCount) { int droppedAttributesCount) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeFixed64(Span.Event.TIME_UNIX_NANO_FIELD_NUMBER, epochNanos); size += MarshalerUtil.sizeFixed64(Span.Event.TIME_UNIX_NANO, epochNanos);
size += MarshalerUtil.sizeBytes(Span.Event.NAME_FIELD_NUMBER, name); size += MarshalerUtil.sizeBytes(Span.Event.NAME, name);
size += size += MarshalerUtil.sizeRepeatedMessage(Span.Event.ATTRIBUTES, attributeMarshalers);
MarshalerUtil.sizeRepeatedMessage( size += MarshalerUtil.sizeUInt32(Span.Event.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
Span.Event.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers);
size +=
MarshalerUtil.sizeUInt32(
Span.Event.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
return size; return size;
} }
} }
@ -460,14 +438,12 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
MarshalerUtil.marshalBytes(Span.Link.TRACE_ID_FIELD_NUMBER, traceId, output); output.serializeBytes(Span.Link.TRACE_ID, traceId);
MarshalerUtil.marshalBytes(Span.Link.SPAN_ID_FIELD_NUMBER, spanId, output); output.serializeBytes(Span.Link.SPAN_ID, spanId);
// TODO: Set TraceState; // TODO: Set TraceState;
MarshalerUtil.marshalRepeatedMessage( output.serializeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
Span.Link.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output); output.serializeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
MarshalerUtil.marshalUInt32(
Span.Link.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
} }
private static int calculateSize( private static int calculateSize(
@ -476,14 +452,11 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
AttributeMarshaler[] attributeMarshalers, AttributeMarshaler[] attributeMarshalers,
int droppedAttributesCount) { int droppedAttributesCount) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeBytes(Span.Link.TRACE_ID_FIELD_NUMBER, traceId); size += MarshalerUtil.sizeBytes(Span.Link.TRACE_ID, traceId);
size += MarshalerUtil.sizeBytes(Span.Link.SPAN_ID_FIELD_NUMBER, spanId); size += MarshalerUtil.sizeBytes(Span.Link.SPAN_ID, spanId);
// TODO: Set TraceState; // TODO: Set TraceState;
size += size += MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers); size += MarshalerUtil.sizeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
size +=
MarshalerUtil.sizeUInt32(
Span.Link.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
return size; return size;
} }
} }
@ -491,7 +464,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private static final class SpanStatusMarshaler extends MarshalerWithSize { private static final class SpanStatusMarshaler extends MarshalerWithSize {
private final int protoStatusCode; private final int protoStatusCode;
private final int deprecatedStatusCode; private final int deprecatedStatusCode;
private final byte[] description; private final byte[] descriptionUtf8;
static SpanStatusMarshaler create(StatusData status) { static SpanStatusMarshaler create(StatusData status) {
int protoStatusCode = Status.StatusCode.STATUS_CODE_UNSET_VALUE; int protoStatusCode = Status.StatusCode.STATUS_CODE_UNSET_VALUE;
@ -507,31 +480,27 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
return new SpanStatusMarshaler(protoStatusCode, deprecatedStatusCode, description); return new SpanStatusMarshaler(protoStatusCode, deprecatedStatusCode, description);
} }
private SpanStatusMarshaler(int protoStatusCode, int deprecatedStatusCode, byte[] description) { private SpanStatusMarshaler(
super(computeSize(protoStatusCode, deprecatedStatusCode, description)); int protoStatusCode, int deprecatedStatusCode, byte[] descriptionUtf8) {
super(computeSize(protoStatusCode, deprecatedStatusCode, descriptionUtf8));
this.protoStatusCode = protoStatusCode; this.protoStatusCode = protoStatusCode;
this.deprecatedStatusCode = deprecatedStatusCode; this.deprecatedStatusCode = deprecatedStatusCode;
this.description = description; this.descriptionUtf8 = descriptionUtf8;
} }
@Override @Override
public void writeTo(CodedOutputStream output) throws IOException { public void writeTo(Serializer output) throws IOException {
if (deprecatedStatusCode != Status.DeprecatedStatusCode.DEPRECATED_STATUS_CODE_OK_VALUE) { output.serializeEnum(Status.DEPRECATED_CODE, deprecatedStatusCode);
MarshalerUtil.marshalEnum( output.serializeString(Status.MESSAGE, descriptionUtf8);
Status.DEPRECATED_CODE_FIELD_NUMBER, deprecatedStatusCode, output); output.serializeEnum(Status.CODE, protoStatusCode);
}
MarshalerUtil.marshalBytes(Status.MESSAGE_FIELD_NUMBER, description, output);
if (protoStatusCode != Status.StatusCode.STATUS_CODE_UNSET_VALUE) {
MarshalerUtil.marshalEnum(Status.CODE_FIELD_NUMBER, protoStatusCode, output);
}
} }
private static int computeSize( private static int computeSize(
int protoStatusCode, int deprecatedStatusCode, byte[] description) { int protoStatusCode, int deprecatedStatusCode, byte[] descriptionUtf8) {
int size = 0; int size = 0;
size += MarshalerUtil.sizeEnum(Status.DEPRECATED_CODE_FIELD_NUMBER, deprecatedStatusCode); size += MarshalerUtil.sizeEnum(Status.DEPRECATED_CODE, deprecatedStatusCode);
size += MarshalerUtil.sizeBytes(Status.MESSAGE_FIELD_NUMBER, description); size += MarshalerUtil.sizeBytes(Status.MESSAGE, descriptionUtf8);
size += MarshalerUtil.sizeEnum(Status.CODE_FIELD_NUMBER, protoStatusCode); size += MarshalerUtil.sizeEnum(Status.CODE, protoStatusCode);
return size; return size;
} }
} }

View File

@ -27,6 +27,7 @@ import io.grpc.Drainable;
import io.grpc.KnownLength; import io.grpc.KnownLength;
import io.opentelemetry.exporter.otlp.internal.CodedOutputStream; import io.opentelemetry.exporter.otlp.internal.CodedOutputStream;
import io.opentelemetry.exporter.otlp.internal.Marshaler; import io.opentelemetry.exporter.otlp.internal.Marshaler;
import io.opentelemetry.exporter.otlp.internal.Serializer;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -56,9 +57,9 @@ public final class MarshalerInputStream extends InputStream implements Drainable
public int drainTo(OutputStream target) throws IOException { public int drainTo(OutputStream target) throws IOException {
int written; int written;
if (message != null) { if (message != null) {
written = message.getSerializedSize(); written = message.getProtoSerializedSize();
CodedOutputStream cos = CodedOutputStream.newInstance(target); CodedOutputStream cos = CodedOutputStream.newInstance(target);
message.writeTo(cos); message.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
message = null; message = null;
} else if (partial != null) { } else if (partial != null) {
@ -85,7 +86,7 @@ public final class MarshalerInputStream extends InputStream implements Drainable
@Override @Override
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
if (message != null) { if (message != null) {
int size = message.getSerializedSize(); int size = message.getProtoSerializedSize();
if (size == 0) { if (size == 0) {
message = null; message = null;
partial = null; partial = null;
@ -107,9 +108,9 @@ public final class MarshalerInputStream extends InputStream implements Drainable
} }
private static byte[] toByteArray(Marshaler message) throws IOException { private static byte[] toByteArray(Marshaler message) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(message.getSerializedSize()); ByteArrayOutputStream bos = new ByteArrayOutputStream(message.getProtoSerializedSize());
CodedOutputStream cos = CodedOutputStream.newInstance(bos); CodedOutputStream cos = CodedOutputStream.newInstance(bos);
message.writeTo(cos); message.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
return bos.toByteArray(); return bos.toByteArray();
} }
@ -117,7 +118,7 @@ public final class MarshalerInputStream extends InputStream implements Drainable
@Override @Override
public int available() { public int available() {
if (message != null) { if (message != null) {
return message.getSerializedSize(); return message.getProtoSerializedSize();
} else if (partial != null) { } else if (partial != null) {
return partial.available(); return partial.available();
} }

View File

@ -788,7 +788,7 @@ class MetricsRequestMarshalerTest {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
CodedOutputStream cos = CodedOutputStream.newInstance(bos); CodedOutputStream cos = CodedOutputStream.newInstance(bos);
try { try {
marshaler.writeTo(cos); marshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);

View File

@ -153,9 +153,9 @@ class TraceRequestMarshalerTest {
byte[] protoOutput = protoRequest.toByteArray(); byte[] protoOutput = protoRequest.toByteArray();
ByteArrayOutputStream customOutput = ByteArrayOutputStream customOutput =
new ByteArrayOutputStream(requestMarshaler.getSerializedSize()); new ByteArrayOutputStream(requestMarshaler.getProtoSerializedSize());
CodedOutputStream cos = CodedOutputStream.newInstance(customOutput); CodedOutputStream cos = CodedOutputStream.newInstance(customOutput);
requestMarshaler.writeTo(cos); requestMarshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
byte[] customOutputBytes = customOutput.toByteArray(); byte[] customOutputBytes = customOutput.toByteArray();
if (!Arrays.equals(customOutputBytes, protoOutput)) { if (!Arrays.equals(customOutputBytes, protoOutput)) {
@ -163,7 +163,7 @@ class TraceRequestMarshalerTest {
try { try {
reverse = ExportTraceServiceRequest.parseFrom(customOutputBytes).toString(); reverse = ExportTraceServiceRequest.parseFrom(customOutputBytes).toString();
} catch (IOException e) { } catch (IOException e) {
// Leave <invalid> reverse += " " + e.getMessage();
} }
throw new AssertionError( throw new AssertionError(
"Serialization through TraceMarshaller does not match serialization through " "Serialization through TraceMarshaller does not match serialization through "
@ -197,15 +197,15 @@ class TraceRequestMarshalerTest {
.build(); .build();
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(spanDataList); TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(spanDataList);
int protoSize = protoRequest.getSerializedSize(); int protoSize = protoRequest.getSerializedSize();
assertThat(requestMarshaler.getSerializedSize()).isEqualTo(protoSize); assertThat(requestMarshaler.getProtoSerializedSize()).isEqualTo(protoSize);
ByteArrayOutputStream protoOutput = new ByteArrayOutputStream(protoRequest.getSerializedSize()); ByteArrayOutputStream protoOutput = new ByteArrayOutputStream(protoRequest.getSerializedSize());
protoRequest.writeTo(protoOutput); protoRequest.writeTo(protoOutput);
ByteArrayOutputStream customOutput = ByteArrayOutputStream customOutput =
new ByteArrayOutputStream(requestMarshaler.getSerializedSize()); new ByteArrayOutputStream(requestMarshaler.getProtoSerializedSize());
CodedOutputStream cos = CodedOutputStream.newInstance(customOutput); CodedOutputStream cos = CodedOutputStream.newInstance(customOutput);
requestMarshaler.writeTo(cos); requestMarshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
assertThat(customOutput.toByteArray()).isEqualTo(protoOutput.toByteArray()); assertThat(customOutput.toByteArray()).isEqualTo(protoOutput.toByteArray());
} }

View File

@ -6,6 +6,7 @@
package io.opentelemetry.exporter.otlp.trace; package io.opentelemetry.exporter.otlp.trace;
import io.opentelemetry.exporter.otlp.internal.CodedOutputStream; import io.opentelemetry.exporter.otlp.internal.CodedOutputStream;
import io.opentelemetry.exporter.otlp.internal.Serializer;
import io.opentelemetry.exporter.otlp.internal.SpanAdapter; import io.opentelemetry.exporter.otlp.internal.SpanAdapter;
import io.opentelemetry.exporter.otlp.internal.TraceRequestMarshaler; import io.opentelemetry.exporter.otlp.internal.TraceRequestMarshaler;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest; import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
@ -54,7 +55,7 @@ public class RequestMarshalBenchmarks {
@Threads(1) @Threads(1)
public ByteArrayOutputStream createCustomMarshal(RequestMarshalState state) { public ByteArrayOutputStream createCustomMarshal(RequestMarshalState state) {
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList); TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList);
return new ByteArrayOutputStream(requestMarshaler.getSerializedSize()); return new ByteArrayOutputStream(requestMarshaler.getProtoSerializedSize());
} }
@Benchmark @Benchmark
@ -62,9 +63,9 @@ public class RequestMarshalBenchmarks {
public ByteArrayOutputStream marshalCustom(RequestMarshalState state) throws IOException { public ByteArrayOutputStream marshalCustom(RequestMarshalState state) throws IOException {
TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList); TraceRequestMarshaler requestMarshaler = TraceRequestMarshaler.create(state.spanDataList);
ByteArrayOutputStream customOutput = ByteArrayOutputStream customOutput =
new ByteArrayOutputStream(requestMarshaler.getSerializedSize()); new ByteArrayOutputStream(requestMarshaler.getProtoSerializedSize());
CodedOutputStream cos = CodedOutputStream.newInstance(customOutput); CodedOutputStream cos = CodedOutputStream.newInstance(customOutput);
requestMarshaler.writeTo(cos); requestMarshaler.writeTo(Serializer.createProtoSerializer(cos));
cos.flush(); cos.flush();
return customOutput; return customOutput;
} }