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(service: Service): List<Path> = emptyList()
override fun handle(type: Type): Path? {
val typeSpec = javaGenerator.generateType(type)
val typeSpec = javaGenerator.generateType(type, false)
val javaTypeName = javaGenerator.generatedTypeName(type)
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 {
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 {
val nameToJavaName = linkedMapOf<ProtoType, TypeName>()
for (protoFile in schema.protoFiles) {
@ -85,7 +91,7 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
putAll(nameToJavaName, javaPackage, null, protoFile.types)
}
return JavaGenerator(nameToJavaName)
return JavaGenerator(schema, nameToJavaName)
}
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) {
return generateMessage(type)
return generateMessage(type, nested)
}
if (type is EnumType) {
return generateEnum(type)
@ -126,21 +132,29 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
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 builder = TypeSpec.classBuilder(javaType.simpleName())
.addModifiers(PUBLIC, FINAL)
if (nested) {
builder.addModifiers(STATIC)
}
for (field in type.fieldsAndOneOfFields) {
builder.addField(
FieldSpec.builder(TypeName.INT, "${field.name.toUpperCase()}_FIELD_NUMBER", PUBLIC, STATIC, FINAL)
.initializer("\$L", field.tag)
FieldSpec.builder(PROTO_FIELD_INFO, field.name.toUpperCase(), PUBLIC, STATIC, FINAL)
.initializer("\$T.create(\$L, \$L, \"\$L\")",
PROTO_FIELD_INFO,
field.tag,
makeTag(field.tag, field.type as ProtoType, field.isRepeated),
field.jsonName)
.build())
field.type
}
for (nestedType in type.nestedTypes) {
builder.addType(generateType(nestedType))
builder.addType(generateType(nestedType, true))
}
return builder.build()
@ -161,5 +175,37 @@ class ProtoFieldsWireHandler : CustomHandlerBeta {
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 {
named("jmh") {
finalizedBy(named("jmhReport"))
outputs.cacheIf { false }
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -14,7 +14,7 @@ import java.io.IOException;
* at any time.
*/
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.resources.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
@ -39,190 +38,92 @@ final class MarshalerUtil {
return result;
}
static void marshalRepeatedFixed64(int fieldNumber, List<Long> values, CodedOutputStream output)
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 (long value : values) {
output.writeFixed64NoTag(value);
}
static int sizeRepeatedFixed64(ProtoFieldInfo field, List<Long> values) {
return sizeRepeatedFixed64(field, values.size());
}
static void marshalRepeatedDouble(int fieldNumber, List<Double> values, CodedOutputStream output)
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) {
private static int sizeRepeatedFixed64(ProtoFieldInfo field, int numValues) {
if (numValues == 0) {
return 0;
}
int dataSize = WireFormat.FIXED64_SIZE * numValues;
int size = 0;
size += CodedOutputStream.computeTagSize(fieldNumber);
size += field.getTagSize();
size += CodedOutputStream.computeLengthDelimitedFieldSize(dataSize);
return size;
}
static int sizeRepeatedDouble(int fieldNumber, List<Double> values) {
static int sizeRepeatedDouble(ProtoFieldInfo field, List<Double> values) {
// 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 fieldTagSize = CodedOutputStream.computeTagSize(fieldNumber);
int fieldTagSize = field.getTagSize();
for (Marshaler message : repeatedMessage) {
int fieldSize = message.getSerializedSize();
int fieldSize = message.getProtoSerializedSize();
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
}
return size;
}
static int sizeRepeatedMessage(int fieldNumber, List<? extends Marshaler> repeatedMessage) {
static int sizeRepeatedMessage(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) {
int size = 0;
int fieldTagSize = CodedOutputStream.computeTagSize(fieldNumber);
int fieldTagSize = field.getTagSize();
for (Marshaler message : repeatedMessage) {
int fieldSize = message.getSerializedSize();
int fieldSize = message.getProtoSerializedSize();
size += fieldTagSize + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
}
return size;
}
static int sizeMessage(int fieldNumber, Marshaler message) {
int fieldSize = message.getSerializedSize();
return CodedOutputStream.computeTagSize(fieldNumber)
+ CodedOutputStream.computeUInt32SizeNoTag(fieldSize)
+ fieldSize;
static int sizeMessage(ProtoFieldInfo field, Marshaler message) {
int fieldSize = message.getProtoSerializedSize();
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize;
}
static int sizeBool(int fieldNumber, boolean value) {
static int sizeBool(ProtoFieldInfo field, boolean value) {
if (!value) {
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) {
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) {
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) {
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) {
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.
static int sizeEnum(int fieldNumber, int value) {
static int sizeEnum(ProtoFieldInfo field, int value) {
if (value == 0) {
return 0;
}
return CodedOutputStream.computeEnumSize(fieldNumber, value);
return field.getTagSize() + CodedOutputStream.computeEnumSizeNoTag(value);
}
static byte[] toBytes(@Nullable String value) {

View File

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

View File

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

View File

@ -9,10 +9,10 @@ enum NoopMarshaler implements Marshaler {
INSTANCE;
@Override
public void writeTo(CodedOutputStream output) {}
public void writeTo(Serializer output) {}
@Override
public int getSerializedSize() {
public int getProtoSerializedSize() {
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}. */
public ProtoRequestBody(Marshaler marshaler) {
this.marshaler = marshaler;
contentLength = marshaler.getSerializedSize();
contentLength = marshaler.getProtoSerializedSize();
}
@Override
@ -42,7 +42,7 @@ public final class ProtoRequestBody extends RequestBody {
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
CodedOutputStream cos = CodedOutputStream.newInstance(bufferedSink.outputStream());
marshaler.writeTo(cos);
marshaler.writeTo(Serializer.createProtoSerializer(cos));
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) {
super(calculateSize(attributeMarshalers));
ByteArrayOutputStream bos = new ByteArrayOutputStream(getSerializedSize());
ByteArrayOutputStream bos = new ByteArrayOutputStream(getProtoSerializedSize());
CodedOutputStream output = CodedOutputStream.newInstance(bos);
ProtoSerializer serializer = new ProtoSerializer(output);
try {
MarshalerUtil.marshalRepeatedMessage(
Resource.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output);
serializer.serializeRepeatedMessage(Resource.ATTRIBUTES, attributeMarshalers);
output.flush();
} catch (IOException e) {
// Presized so can't happen (we would have already thrown OutOfMemoryError)
@ -45,11 +45,12 @@ final class ResourceMarshaler extends MarshalerWithSize {
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
output.writeRawBytes(serializedResource);
public void writeTo(Serializer output) throws IOException {
// TODO(anuraaga): Preserialize JSON as well.
output.writeSerializedMessage(serializedResource, MarshalerUtil.EMPTY_BYTES);
}
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) {
super(
MarshalerUtil.sizeRepeatedMessage(
ExportTraceServiceRequest.RESOURCE_SPANS_FIELD_NUMBER, resourceSpansMarshalers));
ExportTraceServiceRequest.RESOURCE_SPANS, resourceSpansMarshalers));
this.resourceSpansMarshalers = resourceSpansMarshalers;
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalRepeatedMessage(
ExportTraceServiceRequest.RESOURCE_SPANS_FIELD_NUMBER, resourceSpansMarshalers, output);
public void writeTo(Serializer output) throws IOException {
output.serializeRepeatedMessage(
ExportTraceServiceRequest.RESOURCE_SPANS, resourceSpansMarshalers);
}
private static final class ResourceSpansMarshaler extends MarshalerWithSize {
private final ResourceMarshaler resourceMarshaler;
private final byte[] schemaUrl;
private final byte[] schemaUrlUtf8;
private final InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers;
private ResourceSpansMarshaler(
ResourceMarshaler resourceMarshaler,
byte[] schemaUrl,
byte[] schemaUrlUtf8,
InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) {
super(calculateSize(resourceMarshaler, schemaUrl, instrumentationLibrarySpansMarshalers));
super(calculateSize(resourceMarshaler, schemaUrlUtf8, instrumentationLibrarySpansMarshalers));
this.resourceMarshaler = resourceMarshaler;
this.schemaUrl = schemaUrl;
this.schemaUrlUtf8 = schemaUrlUtf8;
this.instrumentationLibrarySpansMarshalers = instrumentationLibrarySpansMarshalers;
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalMessage(ResourceSpans.RESOURCE_FIELD_NUMBER, resourceMarshaler, output);
MarshalerUtil.marshalRepeatedMessage(
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS_FIELD_NUMBER,
instrumentationLibrarySpansMarshalers,
output);
MarshalerUtil.marshalBytes(ResourceSpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
public void writeTo(Serializer output) throws IOException {
output.serializeMessage(ResourceSpans.RESOURCE, resourceMarshaler);
output.serializeRepeatedMessage(
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS, instrumentationLibrarySpansMarshalers);
output.serializeString(ResourceSpans.SCHEMA_URL, schemaUrlUtf8);
}
private static int calculateSize(
ResourceMarshaler resourceMarshaler,
byte[] schemaUrl,
byte[] schemaUrlUtf8,
InstrumentationLibrarySpansMarshaler[] instrumentationLibrarySpansMarshalers) {
int size = 0;
size += MarshalerUtil.sizeMessage(ResourceSpans.RESOURCE_FIELD_NUMBER, resourceMarshaler);
size += MarshalerUtil.sizeBytes(ResourceSpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl);
size += MarshalerUtil.sizeMessage(ResourceSpans.RESOURCE, resourceMarshaler);
size += MarshalerUtil.sizeBytes(ResourceSpans.SCHEMA_URL, schemaUrlUtf8);
size +=
MarshalerUtil.sizeRepeatedMessage(
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS_FIELD_NUMBER,
instrumentationLibrarySpansMarshalers);
ResourceSpans.INSTRUMENTATION_LIBRARY_SPANS, instrumentationLibrarySpansMarshalers);
return size;
}
}
@ -130,44 +127,36 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private static final class InstrumentationLibrarySpansMarshaler extends MarshalerWithSize {
private final InstrumentationLibraryMarshaler instrumentationLibrary;
private final List<SpanMarshaler> spanMarshalers;
private final byte[] schemaUrl;
private final byte[] schemaUrlUtf8;
private InstrumentationLibrarySpansMarshaler(
InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl,
byte[] schemaUrlUtf8,
List<SpanMarshaler> spanMarshalers) {
super(calculateSize(instrumentationLibrary, schemaUrl, spanMarshalers));
super(calculateSize(instrumentationLibrary, schemaUrlUtf8, spanMarshalers));
this.instrumentationLibrary = instrumentationLibrary;
this.schemaUrl = schemaUrl;
this.schemaUrlUtf8 = schemaUrlUtf8;
this.spanMarshalers = spanMarshalers;
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalMessage(
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY_FIELD_NUMBER,
instrumentationLibrary,
output);
MarshalerUtil.marshalRepeatedMessage(
InstrumentationLibrarySpans.SPANS_FIELD_NUMBER, spanMarshalers, output);
MarshalerUtil.marshalBytes(
InstrumentationLibrarySpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl, output);
public void writeTo(Serializer output) throws IOException {
output.serializeMessage(
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
output.serializeRepeatedMessage(InstrumentationLibrarySpans.SPANS, spanMarshalers);
output.serializeString(InstrumentationLibrarySpans.SCHEMA_URL, schemaUrlUtf8);
}
private static int calculateSize(
InstrumentationLibraryMarshaler instrumentationLibrary,
byte[] schemaUrl,
byte[] schemaUrlUtf8,
List<SpanMarshaler> spanMarshalers) {
int size = 0;
size +=
MarshalerUtil.sizeMessage(
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY_FIELD_NUMBER,
instrumentationLibrary);
size +=
MarshalerUtil.sizeBytes(InstrumentationLibrarySpans.SCHEMA_URL_FIELD_NUMBER, schemaUrl);
size +=
MarshalerUtil.sizeRepeatedMessage(
InstrumentationLibrarySpans.SPANS_FIELD_NUMBER, spanMarshalers);
InstrumentationLibrarySpans.INSTRUMENTATION_LIBRARY, instrumentationLibrary);
size += MarshalerUtil.sizeBytes(InstrumentationLibrarySpans.SCHEMA_URL, schemaUrlUtf8);
size += MarshalerUtil.sizeRepeatedMessage(InstrumentationLibrarySpans.SPANS, spanMarshalers);
return size;
}
}
@ -176,7 +165,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private final byte[] traceId;
private final byte[] spanId;
private final byte[] parentSpanId;
private final byte[] name;
private final byte[] nameUtf8;
private final int spanKind;
private final long startEpochNanos;
private final long endEpochNanos;
@ -236,7 +225,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
byte[] traceId,
byte[] spanId,
byte[] parentSpanId,
byte[] name,
byte[] nameUtf8,
int spanKind,
long startEpochNanos,
long endEpochNanos,
@ -252,7 +241,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
traceId,
spanId,
parentSpanId,
name,
nameUtf8,
spanKind,
startEpochNanos,
endEpochNanos,
@ -266,7 +255,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
this.traceId = traceId;
this.spanId = spanId;
this.parentSpanId = parentSpanId;
this.name = name;
this.nameUtf8 = nameUtf8;
this.spanKind = spanKind;
this.startEpochNanos = startEpochNanos;
this.endEpochNanos = endEpochNanos;
@ -280,38 +269,35 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalBytes(Span.TRACE_ID_FIELD_NUMBER, traceId, output);
MarshalerUtil.marshalBytes(Span.SPAN_ID_FIELD_NUMBER, spanId, output);
public void writeTo(Serializer output) throws IOException {
output.serializeBytes(Span.TRACE_ID, traceId);
output.serializeBytes(Span.SPAN_ID, spanId);
// TODO: Set TraceState;
MarshalerUtil.marshalBytes(Span.PARENT_SPAN_ID_FIELD_NUMBER, parentSpanId, output);
MarshalerUtil.marshalBytes(Span.NAME_FIELD_NUMBER, name, output);
output.serializeBytes(Span.PARENT_SPAN_ID, parentSpanId);
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);
MarshalerUtil.marshalFixed64(Span.END_TIME_UNIX_NANO_FIELD_NUMBER, endEpochNanos, output);
output.serializeFixed64(Span.START_TIME_UNIX_NANO, startEpochNanos);
output.serializeFixed64(Span.END_TIME_UNIX_NANO, endEpochNanos);
MarshalerUtil.marshalRepeatedMessage(
Span.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output);
MarshalerUtil.marshalUInt32(
Span.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
output.serializeRepeatedMessage(Span.ATTRIBUTES, attributeMarshalers);
output.serializeUInt32(Span.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
MarshalerUtil.marshalRepeatedMessage(Span.EVENTS_FIELD_NUMBER, spanEventMarshalers, output);
MarshalerUtil.marshalUInt32(
Span.DROPPED_EVENTS_COUNT_FIELD_NUMBER, droppedEventsCount, output);
output.serializeRepeatedMessage(Span.EVENTS, spanEventMarshalers);
output.serializeUInt32(Span.DROPPED_EVENTS_COUNT, droppedEventsCount);
MarshalerUtil.marshalRepeatedMessage(Span.LINKS_FIELD_NUMBER, spanLinkMarshalers, output);
MarshalerUtil.marshalUInt32(Span.DROPPED_LINKS_COUNT_FIELD_NUMBER, droppedLinksCount, output);
output.serializeRepeatedMessage(Span.LINKS, spanLinkMarshalers);
output.serializeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);
MarshalerUtil.marshalMessage(Span.STATUS_FIELD_NUMBER, spanStatusMarshaler, output);
output.serializeMessage(Span.STATUS, spanStatusMarshaler);
}
private static int calculateSize(
byte[] traceId,
byte[] spanId,
byte[] parentSpanId,
byte[] name,
byte[] nameUtf8,
int spanKind,
long startEpochNanos,
long endEpochNanos,
@ -323,29 +309,27 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
int droppedLinksCount,
SpanStatusMarshaler spanStatusMarshaler) {
int size = 0;
size += MarshalerUtil.sizeBytes(Span.TRACE_ID_FIELD_NUMBER, traceId);
size += MarshalerUtil.sizeBytes(Span.SPAN_ID_FIELD_NUMBER, spanId);
size += MarshalerUtil.sizeBytes(Span.TRACE_ID, traceId);
size += MarshalerUtil.sizeBytes(Span.SPAN_ID, spanId);
// TODO: Set TraceState;
size += MarshalerUtil.sizeBytes(Span.PARENT_SPAN_ID_FIELD_NUMBER, parentSpanId);
size += MarshalerUtil.sizeBytes(Span.NAME_FIELD_NUMBER, name);
size += MarshalerUtil.sizeBytes(Span.PARENT_SPAN_ID, parentSpanId);
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.END_TIME_UNIX_NANO_FIELD_NUMBER, endEpochNanos);
size += MarshalerUtil.sizeFixed64(Span.START_TIME_UNIX_NANO, startEpochNanos);
size += MarshalerUtil.sizeFixed64(Span.END_TIME_UNIX_NANO, endEpochNanos);
size += MarshalerUtil.sizeRepeatedMessage(Span.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers);
size +=
MarshalerUtil.sizeUInt32(
Span.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.ATTRIBUTES, attributeMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.EVENTS_FIELD_NUMBER, spanEventMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_EVENTS_COUNT_FIELD_NUMBER, droppedEventsCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.EVENTS, spanEventMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_EVENTS_COUNT, droppedEventsCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.LINKS_FIELD_NUMBER, spanLinkMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT_FIELD_NUMBER, droppedLinksCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.LINKS, spanLinkMarshalers);
size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);
size += MarshalerUtil.sizeMessage(Span.STATUS_FIELD_NUMBER, spanStatusMarshaler);
size += MarshalerUtil.sizeMessage(Span.STATUS, spanStatusMarshaler);
return size;
}
}
@ -389,13 +373,11 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalFixed64(Span.Event.TIME_UNIX_NANO_FIELD_NUMBER, epochNanos, output);
MarshalerUtil.marshalBytes(Span.Event.NAME_FIELD_NUMBER, name, output);
MarshalerUtil.marshalRepeatedMessage(
Span.Event.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output);
MarshalerUtil.marshalUInt32(
Span.Event.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
public void writeTo(Serializer output) throws IOException {
output.serializeFixed64(Span.Event.TIME_UNIX_NANO, epochNanos);
output.serializeBytes(Span.Event.NAME, name);
output.serializeRepeatedMessage(Span.Event.ATTRIBUTES, attributeMarshalers);
output.serializeUInt32(Span.Event.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
}
private static int calculateSize(
@ -404,14 +386,10 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
AttributeMarshaler[] attributeMarshalers,
int droppedAttributesCount) {
int size = 0;
size += MarshalerUtil.sizeFixed64(Span.Event.TIME_UNIX_NANO_FIELD_NUMBER, epochNanos);
size += MarshalerUtil.sizeBytes(Span.Event.NAME_FIELD_NUMBER, name);
size +=
MarshalerUtil.sizeRepeatedMessage(
Span.Event.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers);
size +=
MarshalerUtil.sizeUInt32(
Span.Event.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
size += MarshalerUtil.sizeFixed64(Span.Event.TIME_UNIX_NANO, epochNanos);
size += MarshalerUtil.sizeBytes(Span.Event.NAME, name);
size += MarshalerUtil.sizeRepeatedMessage(Span.Event.ATTRIBUTES, attributeMarshalers);
size += MarshalerUtil.sizeUInt32(Span.Event.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
return size;
}
}
@ -460,14 +438,12 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
MarshalerUtil.marshalBytes(Span.Link.TRACE_ID_FIELD_NUMBER, traceId, output);
MarshalerUtil.marshalBytes(Span.Link.SPAN_ID_FIELD_NUMBER, spanId, output);
public void writeTo(Serializer output) throws IOException {
output.serializeBytes(Span.Link.TRACE_ID, traceId);
output.serializeBytes(Span.Link.SPAN_ID, spanId);
// TODO: Set TraceState;
MarshalerUtil.marshalRepeatedMessage(
Span.Link.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers, output);
MarshalerUtil.marshalUInt32(
Span.Link.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount, output);
output.serializeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
output.serializeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
}
private static int calculateSize(
@ -476,14 +452,11 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
AttributeMarshaler[] attributeMarshalers,
int droppedAttributesCount) {
int size = 0;
size += MarshalerUtil.sizeBytes(Span.Link.TRACE_ID_FIELD_NUMBER, traceId);
size += MarshalerUtil.sizeBytes(Span.Link.SPAN_ID_FIELD_NUMBER, spanId);
size += MarshalerUtil.sizeBytes(Span.Link.TRACE_ID, traceId);
size += MarshalerUtil.sizeBytes(Span.Link.SPAN_ID, spanId);
// TODO: Set TraceState;
size +=
MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES_FIELD_NUMBER, attributeMarshalers);
size +=
MarshalerUtil.sizeUInt32(
Span.Link.DROPPED_ATTRIBUTES_COUNT_FIELD_NUMBER, droppedAttributesCount);
size += MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
size += MarshalerUtil.sizeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
return size;
}
}
@ -491,7 +464,7 @@ public final class TraceRequestMarshaler extends MarshalerWithSize implements Ma
private static final class SpanStatusMarshaler extends MarshalerWithSize {
private final int protoStatusCode;
private final int deprecatedStatusCode;
private final byte[] description;
private final byte[] descriptionUtf8;
static SpanStatusMarshaler create(StatusData status) {
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);
}
private SpanStatusMarshaler(int protoStatusCode, int deprecatedStatusCode, byte[] description) {
super(computeSize(protoStatusCode, deprecatedStatusCode, description));
private SpanStatusMarshaler(
int protoStatusCode, int deprecatedStatusCode, byte[] descriptionUtf8) {
super(computeSize(protoStatusCode, deprecatedStatusCode, descriptionUtf8));
this.protoStatusCode = protoStatusCode;
this.deprecatedStatusCode = deprecatedStatusCode;
this.description = description;
this.descriptionUtf8 = descriptionUtf8;
}
@Override
public void writeTo(CodedOutputStream output) throws IOException {
if (deprecatedStatusCode != Status.DeprecatedStatusCode.DEPRECATED_STATUS_CODE_OK_VALUE) {
MarshalerUtil.marshalEnum(
Status.DEPRECATED_CODE_FIELD_NUMBER, deprecatedStatusCode, output);
}
MarshalerUtil.marshalBytes(Status.MESSAGE_FIELD_NUMBER, description, output);
if (protoStatusCode != Status.StatusCode.STATUS_CODE_UNSET_VALUE) {
MarshalerUtil.marshalEnum(Status.CODE_FIELD_NUMBER, protoStatusCode, output);
}
public void writeTo(Serializer output) throws IOException {
output.serializeEnum(Status.DEPRECATED_CODE, deprecatedStatusCode);
output.serializeString(Status.MESSAGE, descriptionUtf8);
output.serializeEnum(Status.CODE, protoStatusCode);
}
private static int computeSize(
int protoStatusCode, int deprecatedStatusCode, byte[] description) {
int protoStatusCode, int deprecatedStatusCode, byte[] descriptionUtf8) {
int size = 0;
size += MarshalerUtil.sizeEnum(Status.DEPRECATED_CODE_FIELD_NUMBER, deprecatedStatusCode);
size += MarshalerUtil.sizeBytes(Status.MESSAGE_FIELD_NUMBER, description);
size += MarshalerUtil.sizeEnum(Status.CODE_FIELD_NUMBER, protoStatusCode);
size += MarshalerUtil.sizeEnum(Status.DEPRECATED_CODE, deprecatedStatusCode);
size += MarshalerUtil.sizeBytes(Status.MESSAGE, descriptionUtf8);
size += MarshalerUtil.sizeEnum(Status.CODE, protoStatusCode);
return size;
}
}

View File

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

View File

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

View File

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

View File

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