Class Serializer
- java.lang.Object
-
- io.opentelemetry.exporter.internal.marshal.Serializer
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
JsonSerializer,ProtoSerializer
public abstract class Serializer extends java.lang.Object implements java.lang.AutoCloseableSerializer to use when converting from an SDK data object into a protobuf output format. UnlikeCodedOutputStream, which strictly encodes data into the protobuf binary format, this- Handles proto3 semantics of not outputting the value when it matches the default of a field
- Can be implemented to serialize into protobuf JSON format (not binary)
This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSerializer.RepeatedElementPairWriter<K,V>private static classSerializer.RepeatedElementWriter<T>
-
Field Summary
Fields Modifier and Type Field Description private static MarshalerContext.KeyATTRIBUTES_WRITER_KEY
-
Constructor Summary
Constructors Constructor Description Serializer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidclose()voidserializeBool(ProtoFieldInfo field, boolean value)Serializes a protobufboolfield.voidserializeByteAsFixed32(ProtoFieldInfo field, byte value)Serializes a byte as a protobuffixed32field.voidserializeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value)Serializes a protobufbytesfield.voidserializeBytes(ProtoFieldInfo field, byte[] value)Serializes a protobufbytesfield.voidserializeDouble(ProtoFieldInfo field, double value)Serializes a proto bufdoublefield.voidserializeDoubleOptional(ProtoFieldInfo field, double value)Serializes a proto bufdoublefield.voidserializeEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue)Serializes a protobufenumfield.voidserializeFixed32(ProtoFieldInfo field, int value)Serializes a protobuffixed32field.voidserializeFixed64(ProtoFieldInfo field, long value)Serializes a protobuffixed64field.voidserializeFixed64Optional(ProtoFieldInfo field, long value)Serializes a protobuffixed64field.voidserializeInt32(ProtoFieldInfo field, int value)Serializes a protobufuint32field.voidserializeInt64(ProtoFieldInfo field, long value)Serializes a protobufint64field.voidserializeMessage(ProtoFieldInfo field, Marshaler message)Serializes a protobuf embeddedmessage.<K,V>
voidserializeMessageWithContext(ProtoFieldInfo field, K key, V value, StatelessMarshaler2<K,V> marshaler, MarshalerContext context)Serializes a protobuf embeddedmessage.<T> voidserializeMessageWithContext(ProtoFieldInfo field, T message, StatelessMarshaler<T> marshaler, MarshalerContext context)Serializes a protobuf embeddedmessage.voidserializeRepeatedDouble(ProtoFieldInfo field, java.util.List<java.lang.Double> values)Serializes arepeated doublefield.voidserializeRepeatedFixed64(ProtoFieldInfo field, long[] values)Serializes arepeated fixed64field.voidserializeRepeatedFixed64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)Serializes arepeated fixed64field.voidserializeRepeatedInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)Serializes arepeated int64field.abstract voidserializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage)Serializesrepeated messagefield.abstract voidserializeRepeatedMessage(ProtoFieldInfo field, java.util.List<? extends Marshaler> repeatedMessage)Serializesrepeated messagefield.voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>,java.lang.Object> marshaler, MarshalerContext context)Serializesrepeated messagefield.<T> voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key)Serializesrepeated messagefield.abstract <T> voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context)Serializesrepeated messagefield.<K,V>
voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Map<K,V> messages, StatelessMarshaler2<K,V> marshaler, MarshalerContext context, MarshalerContext.Key key)Serializesrepeated messagefield.voidserializeRepeatedUInt64(ProtoFieldInfo field, long[] values)Serializes arepeated uint64field.voidserializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values)Serializes arepeated uint64field.voidserializeRepeatedUInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)Serializes arepeated uint64field.voidserializeSInt32(ProtoFieldInfo field, int value)Serializes a protobufsint32field.voidserializeSpanId(ProtoFieldInfo field, java.lang.String spanId)Serializes a span ID field.voidserializeSpanId(ProtoFieldInfo field, java.lang.String spanId, MarshalerContext context)voidserializeString(ProtoFieldInfo field, byte[] utf8Bytes)Serializes a protobufstringfield.voidserializeStringWithContext(ProtoFieldInfo field, java.lang.String string, MarshalerContext context)Serializes a protobufstringfield.voidserializeTraceId(ProtoFieldInfo field, java.lang.String traceId)Serializes a trace ID field.voidserializeTraceId(ProtoFieldInfo field, java.lang.String traceId, MarshalerContext context)voidserializeUInt32(ProtoFieldInfo field, int value)Serializes a protobufuint32field.voidserializeUInt64(ProtoFieldInfo field, long value)Serializes a protobufuint64field.abstract voidwriteBool(ProtoFieldInfo field, boolean value)Writes a protobufboolfield, even if it matches the default value.abstract voidwriteByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value)abstract voidwriteBytes(ProtoFieldInfo field, byte[] value)abstract voidwriteDouble(ProtoFieldInfo field, double value)Writes a protobufdoublefield, even if it matches the default value.protected abstract voidwriteDoubleValue(double value)protected abstract voidwriteEndMessage()protected abstract voidwriteEndRepeated()Writes end of repeated messages.protected abstract voidwriteEndRepeatedElement()Writes end of a repeated message element.protected abstract voidwriteEndRepeatedPrimitive()protected abstract voidwriteEndRepeatedVarint()protected abstract voidwriteEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue)protected abstract voidwriteFixed32(ProtoFieldInfo field, int value)protected abstract voidwriteFixed64(ProtoFieldInfo field, long value)protected abstract voidwriteFixed64Value(long value)protected abstract voidwriteint32(ProtoFieldInfo field, int value)abstract voidwriteInt64(ProtoFieldInfo field, long value)Writes a protobufint64field, even if it matches the default value.abstract voidwriteSerializedMessage(byte[] protoSerialized, java.lang.String jsonSerialized)Writes the value for a message field that has been pre-serialized.protected abstract voidwriteSInt32(ProtoFieldInfo info, int value)protected abstract voidwriteSpanId(ProtoFieldInfo field, java.lang.String spanId)protected voidwriteSpanId(ProtoFieldInfo field, java.lang.String spanId, MarshalerContext context)protected abstract voidwriteStartMessage(ProtoFieldInfo field, int protoMessageSize)protected abstract voidwriteStartRepeated(ProtoFieldInfo field)Writes start of repeated messages.protected abstract voidwriteStartRepeatedElement(ProtoFieldInfo field, int protoMessageSize)Writes start of a repeated message element.protected abstract voidwriteStartRepeatedPrimitive(ProtoFieldInfo field, int protoSizePerElement, int numElements)protected abstract voidwriteStartRepeatedVarint(ProtoFieldInfo field, int payloadSize)abstract voidwriteString(ProtoFieldInfo field, byte[] utf8Bytes)Writes a protobufstringfield, even if it matches the default value.abstract voidwriteString(ProtoFieldInfo field, java.lang.String string, int utf8Length, MarshalerContext context)protected abstract voidwriteTraceId(ProtoFieldInfo field, java.lang.String traceId)protected voidwriteTraceId(ProtoFieldInfo field, java.lang.String traceId, MarshalerContext context)protected abstract voidwriteUint32(ProtoFieldInfo field, int value)abstract voidwriteUInt64(ProtoFieldInfo field, long value)Writes a protobufuint64field, even if it matches the default value.protected abstract voidwriteUInt64Value(long value)
-
-
-
Field Detail
-
ATTRIBUTES_WRITER_KEY
private static final MarshalerContext.Key ATTRIBUTES_WRITER_KEY
-
-
Method Detail
-
serializeTraceId
public void serializeTraceId(ProtoFieldInfo field, @Nullable java.lang.String traceId) throws java.io.IOException
Serializes a trace ID field.- Throws:
java.io.IOException
-
serializeTraceId
public void serializeTraceId(ProtoFieldInfo field, @Nullable java.lang.String traceId, MarshalerContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTraceId
protected abstract void writeTraceId(ProtoFieldInfo field, java.lang.String traceId) throws java.io.IOException
- Throws:
java.io.IOException
-
writeTraceId
protected void writeTraceId(ProtoFieldInfo field, java.lang.String traceId, MarshalerContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeSpanId
public void serializeSpanId(ProtoFieldInfo field, @Nullable java.lang.String spanId) throws java.io.IOException
Serializes a span ID field.- Throws:
java.io.IOException
-
serializeSpanId
public void serializeSpanId(ProtoFieldInfo field, @Nullable java.lang.String spanId, MarshalerContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSpanId
protected abstract void writeSpanId(ProtoFieldInfo field, java.lang.String spanId) throws java.io.IOException
- Throws:
java.io.IOException
-
writeSpanId
protected void writeSpanId(ProtoFieldInfo field, java.lang.String spanId, MarshalerContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeBool
public void serializeBool(ProtoFieldInfo field, boolean value) throws java.io.IOException
Serializes a protobufboolfield.- Throws:
java.io.IOException
-
writeBool
public abstract void writeBool(ProtoFieldInfo field, boolean value) throws java.io.IOException
Writes a protobufboolfield, even if it matches the default value.- Throws:
java.io.IOException
-
serializeEnum
public void serializeEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue) throws java.io.IOException
Serializes a protobufenumfield.- Throws:
java.io.IOException
-
writeEnum
protected abstract void writeEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeUInt32
public void serializeUInt32(ProtoFieldInfo field, int value) throws java.io.IOException
Serializes a protobufuint32field.- Throws:
java.io.IOException
-
writeUint32
protected abstract void writeUint32(ProtoFieldInfo field, int value) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeSInt32
public void serializeSInt32(ProtoFieldInfo field, int value) throws java.io.IOException
Serializes a protobufsint32field.- Throws:
java.io.IOException
-
writeSInt32
protected abstract void writeSInt32(ProtoFieldInfo info, int value) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeInt32
public void serializeInt32(ProtoFieldInfo field, int value) throws java.io.IOException
Serializes a protobufuint32field.- Throws:
java.io.IOException
-
writeint32
protected abstract void writeint32(ProtoFieldInfo field, int value) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeInt64
public void serializeInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobufint64field.- Throws:
java.io.IOException
-
serializeUInt64
public void serializeUInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobufuint64field.- Throws:
java.io.IOException
-
writeInt64
public abstract void writeInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Writes a protobufint64field, even if it matches the default value.- Throws:
java.io.IOException
-
writeUInt64
public abstract void writeUInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Writes a protobufuint64field, even if it matches the default value.- Throws:
java.io.IOException
-
serializeFixed64
public void serializeFixed64(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobuffixed64field.- Throws:
java.io.IOException
-
serializeFixed64Optional
public void serializeFixed64Optional(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobuffixed64field.- Throws:
java.io.IOException
-
writeFixed64
protected abstract void writeFixed64(ProtoFieldInfo field, long value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeFixed64Value
protected abstract void writeFixed64Value(long value) throws java.io.IOException- Throws:
java.io.IOException
-
writeUInt64Value
protected abstract void writeUInt64Value(long value) throws java.io.IOException- Throws:
java.io.IOException
-
serializeByteAsFixed32
public void serializeByteAsFixed32(ProtoFieldInfo field, byte value) throws java.io.IOException
Serializes a byte as a protobuffixed32field. Ensures that there is no sign propagation if the high bit in the byte is set.- Throws:
java.io.IOException
-
serializeFixed32
public void serializeFixed32(ProtoFieldInfo field, int value) throws java.io.IOException
Serializes a protobuffixed32field.- Throws:
java.io.IOException
-
writeFixed32
protected abstract void writeFixed32(ProtoFieldInfo field, int value) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeDouble
public void serializeDouble(ProtoFieldInfo field, double value) throws java.io.IOException
Serializes a proto bufdoublefield.- Throws:
java.io.IOException
-
serializeDoubleOptional
public void serializeDoubleOptional(ProtoFieldInfo field, double value) throws java.io.IOException
Serializes a proto bufdoublefield.- Throws:
java.io.IOException
-
writeDouble
public abstract void writeDouble(ProtoFieldInfo field, double value) throws java.io.IOException
Writes a protobufdoublefield, even if it matches the default value.- Throws:
java.io.IOException
-
writeDoubleValue
protected abstract void writeDoubleValue(double value) throws java.io.IOException- Throws:
java.io.IOException
-
serializeString
public void serializeString(ProtoFieldInfo field, byte[] utf8Bytes) throws java.io.IOException
Serializes a protobufstringfield.utf8Bytesis the UTF8 encoded bytes of the string to serialize.- Throws:
java.io.IOException
-
serializeStringWithContext
public void serializeStringWithContext(ProtoFieldInfo field, @Nullable java.lang.String string, MarshalerContext context) throws java.io.IOException
Serializes a protobufstringfield.stringis the value to be serialized andutf8Lengthis the length of the string after it is encoded in UTF8. This method reads elements from context, use together withStatelessMarshalerUtil.sizeStringWithContext(ProtoFieldInfo, String, MarshalerContext).- Throws:
java.io.IOException
-
writeString
public abstract void writeString(ProtoFieldInfo field, byte[] utf8Bytes) throws java.io.IOException
Writes a protobufstringfield, even if it matches the default value.- Throws:
java.io.IOException
-
writeString
public abstract void writeString(ProtoFieldInfo field, java.lang.String string, int utf8Length, MarshalerContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
serializeBytes
public void serializeBytes(ProtoFieldInfo field, byte[] value) throws java.io.IOException
Serializes a protobufbytesfield.- Throws:
java.io.IOException
-
serializeByteBuffer
public void serializeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value) throws java.io.IOException
Serializes a protobufbytesfield. Writes all content of the ByteBuffer regardless of the current position and limit. Does not alter the position or limit of the provided ByteBuffer.- Throws:
java.io.IOException
-
writeBytes
public abstract void writeBytes(ProtoFieldInfo field, byte[] value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeByteBuffer
public abstract void writeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeStartMessage
protected abstract void writeStartMessage(ProtoFieldInfo field, int protoMessageSize) throws java.io.IOException
- Throws:
java.io.IOException
-
writeEndMessage
protected abstract void writeEndMessage() throws java.io.IOException- Throws:
java.io.IOException
-
serializeMessage
public void serializeMessage(ProtoFieldInfo field, Marshaler message) throws java.io.IOException
Serializes a protobuf embeddedmessage.- Throws:
java.io.IOException
-
serializeMessageWithContext
public <T> void serializeMessageWithContext(ProtoFieldInfo field, T message, StatelessMarshaler<T> marshaler, MarshalerContext context) throws java.io.IOException
Serializes a protobuf embeddedmessage. This method adds elements to context, use together withStatelessMarshalerUtil.sizeMessageWithContext(ProtoFieldInfo, Object, StatelessMarshaler, MarshalerContext).- Throws:
java.io.IOException
-
serializeMessageWithContext
public <K,V> void serializeMessageWithContext(ProtoFieldInfo field, K key, V value, StatelessMarshaler2<K,V> marshaler, MarshalerContext context) throws java.io.IOException
Serializes a protobuf embeddedmessage. This method adds elements to context, use together withStatelessMarshalerUtil.sizeMessageWithContext(ProtoFieldInfo, Object, Object, StatelessMarshaler2, MarshalerContext).- Throws:
java.io.IOException
-
writeStartRepeatedPrimitive
protected abstract void writeStartRepeatedPrimitive(ProtoFieldInfo field, int protoSizePerElement, int numElements) throws java.io.IOException
- Throws:
java.io.IOException
-
writeEndRepeatedPrimitive
protected abstract void writeEndRepeatedPrimitive() throws java.io.IOException- Throws:
java.io.IOException
-
writeStartRepeatedVarint
protected abstract void writeStartRepeatedVarint(ProtoFieldInfo field, int payloadSize) throws java.io.IOException
- Throws:
java.io.IOException
-
writeEndRepeatedVarint
protected abstract void writeEndRepeatedVarint() throws java.io.IOException- Throws:
java.io.IOException
-
serializeRepeatedFixed64
public void serializeRepeatedFixed64(ProtoFieldInfo field, java.util.List<java.lang.Long> values) throws java.io.IOException
Serializes arepeated fixed64field.- Throws:
java.io.IOException
-
serializeRepeatedFixed64
public void serializeRepeatedFixed64(ProtoFieldInfo field, long[] values) throws java.io.IOException
Serializes arepeated fixed64field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, long[] values) throws java.io.IOException
Serializes arepeated uint64field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values) throws java.io.IOException
Serializes arepeated uint64field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values) throws java.io.IOException
Serializes arepeated uint64field.NOTE: This is the same as
serializeRepeatedUInt64(ProtoFieldInfo, long[])but instead of taking a primitive array it takes aDynamicPrimitiveLongListas input.- Throws:
java.io.IOException
-
serializeRepeatedInt64
public void serializeRepeatedInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values) throws java.io.IOException
Serializes arepeated int64field.- Throws:
java.io.IOException
-
serializeRepeatedDouble
public void serializeRepeatedDouble(ProtoFieldInfo field, java.util.List<java.lang.Double> values) throws java.io.IOException
Serializes arepeated doublefield.- Throws:
java.io.IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage) throws java.io.IOException
Serializesrepeated messagefield.- Throws:
java.io.IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, java.util.List<? extends Marshaler> repeatedMessage) throws java.io.IOException
Serializesrepeated messagefield.- Throws:
java.io.IOException
-
serializeRepeatedMessageWithContext
public abstract <T> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context) throws java.io.IOException
Serializesrepeated messagefield. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, List, StatelessMarshaler, MarshalerContext).- Throws:
java.io.IOException
-
serializeRepeatedMessageWithContext
public <T> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key) throws java.io.IOException
Serializesrepeated messagefield. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Collection, StatelessMarshaler, MarshalerContext, MarshalerContext.Key).- Throws:
java.io.IOException
-
serializeRepeatedMessageWithContext
public <K,V> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Map<K,V> messages, StatelessMarshaler2<K,V> marshaler, MarshalerContext context, MarshalerContext.Key key) throws java.io.IOException
Serializesrepeated messagefield. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Map, StatelessMarshaler2, MarshalerContext, MarshalerContext.Key).- Throws:
java.io.IOException
-
serializeRepeatedMessageWithContext
public void serializeRepeatedMessageWithContext(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>,java.lang.Object> marshaler, MarshalerContext context) throws java.io.IOException
Serializesrepeated messagefield. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Attributes, StatelessMarshaler2, MarshalerContext).- Throws:
java.io.IOException
-
writeStartRepeated
protected abstract void writeStartRepeated(ProtoFieldInfo field) throws java.io.IOException
Writes start of repeated messages.- Throws:
java.io.IOException
-
writeEndRepeated
protected abstract void writeEndRepeated() throws java.io.IOExceptionWrites end of repeated messages.- Throws:
java.io.IOException
-
writeStartRepeatedElement
protected abstract void writeStartRepeatedElement(ProtoFieldInfo field, int protoMessageSize) throws java.io.IOException
Writes start of a repeated message element.- Throws:
java.io.IOException
-
writeEndRepeatedElement
protected abstract void writeEndRepeatedElement() throws java.io.IOExceptionWrites end of a repeated message element.- Throws:
java.io.IOException
-
writeSerializedMessage
public abstract void writeSerializedMessage(byte[] protoSerialized, java.lang.String jsonSerialized) throws java.io.IOExceptionWrites the value for a message field that has been pre-serialized.- Throws:
java.io.IOException
-
close
public abstract void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
-