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.AutoCloseable
Serializer 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 class
Serializer.RepeatedElementPairWriter<K,V>
private static class
Serializer.RepeatedElementWriter<T>
-
Field Summary
Fields Modifier and Type Field Description private static MarshalerContext.Key
ATTRIBUTES_WRITER_KEY
-
Constructor Summary
Constructors Constructor Description Serializer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
close()
void
serializeBool(ProtoFieldInfo field, boolean value)
Serializes a protobufbool
field.void
serializeByteAsFixed32(ProtoFieldInfo field, byte value)
Serializes a byte as a protobuffixed32
field.void
serializeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value)
Serializes a protobufbytes
field.void
serializeBytes(ProtoFieldInfo field, byte[] value)
Serializes a protobufbytes
field.void
serializeDouble(ProtoFieldInfo field, double value)
Serializes a proto bufdouble
field.void
serializeDoubleOptional(ProtoFieldInfo field, double value)
Serializes a proto bufdouble
field.void
serializeEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue)
Serializes a protobufenum
field.void
serializeFixed32(ProtoFieldInfo field, int value)
Serializes a protobuffixed32
field.void
serializeFixed64(ProtoFieldInfo field, long value)
Serializes a protobuffixed64
field.void
serializeFixed64Optional(ProtoFieldInfo field, long value)
Serializes a protobuffixed64
field.void
serializeInt32(ProtoFieldInfo field, int value)
Serializes a protobufuint32
field.void
serializeInt64(ProtoFieldInfo field, long value)
Serializes a protobufint64
field.void
serializeMessage(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> void
serializeMessageWithContext(ProtoFieldInfo field, T message, StatelessMarshaler<T> marshaler, MarshalerContext context)
Serializes a protobuf embeddedmessage
.void
serializeRepeatedDouble(ProtoFieldInfo field, java.util.List<java.lang.Double> values)
Serializes arepeated double
field.void
serializeRepeatedFixed64(ProtoFieldInfo field, long[] values)
Serializes arepeated fixed64
field.void
serializeRepeatedFixed64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)
Serializes arepeated fixed64
field.void
serializeRepeatedInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)
Serializes arepeated int64
field.abstract void
serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage)
Serializesrepeated message
field.abstract void
serializeRepeatedMessage(ProtoFieldInfo field, java.util.List<? extends Marshaler> repeatedMessage)
Serializesrepeated message
field.void
serializeRepeatedMessageWithContext(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>,java.lang.Object> marshaler, MarshalerContext context)
Serializesrepeated message
field.<T> void
serializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key)
Serializesrepeated message
field.abstract <T> void
serializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context)
Serializesrepeated message
field.<K,V>
voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, java.util.Map<K,V> messages, StatelessMarshaler2<K,V> marshaler, MarshalerContext context, MarshalerContext.Key key)
Serializesrepeated message
field.void
serializeRepeatedUInt64(ProtoFieldInfo field, long[] values)
Serializes arepeated uint64
field.void
serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values)
Serializes arepeated uint64
field.void
serializeRepeatedUInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values)
Serializes arepeated uint64
field.void
serializeSInt32(ProtoFieldInfo field, int value)
Serializes a protobufsint32
field.void
serializeSpanId(ProtoFieldInfo field, java.lang.String spanId)
Serializes a span ID field.void
serializeSpanId(ProtoFieldInfo field, java.lang.String spanId, MarshalerContext context)
void
serializeString(ProtoFieldInfo field, byte[] utf8Bytes)
Serializes a protobufstring
field.void
serializeStringWithContext(ProtoFieldInfo field, java.lang.String string, MarshalerContext context)
Serializes a protobufstring
field.void
serializeTraceId(ProtoFieldInfo field, java.lang.String traceId)
Serializes a trace ID field.void
serializeTraceId(ProtoFieldInfo field, java.lang.String traceId, MarshalerContext context)
void
serializeUInt32(ProtoFieldInfo field, int value)
Serializes a protobufuint32
field.void
serializeUInt64(ProtoFieldInfo field, long value)
Serializes a protobufuint64
field.abstract void
writeBool(ProtoFieldInfo field, boolean value)
Writes a protobufbool
field, even if it matches the default value.abstract void
writeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value)
abstract void
writeBytes(ProtoFieldInfo field, byte[] value)
abstract void
writeDouble(ProtoFieldInfo field, double value)
Writes a protobufdouble
field, even if it matches the default value.protected abstract void
writeDoubleValue(double value)
protected abstract void
writeEndMessage()
protected abstract void
writeEndRepeated()
Writes end of repeated messages.protected abstract void
writeEndRepeatedElement()
Writes end of a repeated message element.protected abstract void
writeEndRepeatedPrimitive()
protected abstract void
writeEndRepeatedVarint()
protected abstract void
writeEnum(ProtoFieldInfo field, ProtoEnumInfo enumValue)
protected abstract void
writeFixed32(ProtoFieldInfo field, int value)
protected abstract void
writeFixed64(ProtoFieldInfo field, long value)
protected abstract void
writeFixed64Value(long value)
protected abstract void
writeint32(ProtoFieldInfo field, int value)
abstract void
writeInt64(ProtoFieldInfo field, long value)
Writes a protobufint64
field, even if it matches the default value.abstract void
writeSerializedMessage(byte[] protoSerialized, java.lang.String jsonSerialized)
Writes the value for a message field that has been pre-serialized.protected abstract void
writeSInt32(ProtoFieldInfo info, int value)
protected abstract void
writeSpanId(ProtoFieldInfo field, java.lang.String spanId)
protected void
writeSpanId(ProtoFieldInfo field, java.lang.String spanId, MarshalerContext context)
protected abstract void
writeStartMessage(ProtoFieldInfo field, int protoMessageSize)
protected abstract void
writeStartRepeated(ProtoFieldInfo field)
Writes start of repeated messages.protected abstract void
writeStartRepeatedElement(ProtoFieldInfo field, int protoMessageSize)
Writes start of a repeated message element.protected abstract void
writeStartRepeatedPrimitive(ProtoFieldInfo field, int protoSizePerElement, int numElements)
protected abstract void
writeStartRepeatedVarint(ProtoFieldInfo field, int payloadSize)
abstract void
writeString(ProtoFieldInfo field, byte[] utf8Bytes)
Writes a protobufstring
field, even if it matches the default value.abstract void
writeString(ProtoFieldInfo field, java.lang.String string, int utf8Length, MarshalerContext context)
protected abstract void
writeTraceId(ProtoFieldInfo field, java.lang.String traceId)
protected void
writeTraceId(ProtoFieldInfo field, java.lang.String traceId, MarshalerContext context)
protected abstract void
writeUint32(ProtoFieldInfo field, int value)
abstract void
writeUInt64(ProtoFieldInfo field, long value)
Writes a protobufuint64
field, even if it matches the default value.protected abstract void
writeUInt64Value(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 protobufbool
field.- Throws:
java.io.IOException
-
writeBool
public abstract void writeBool(ProtoFieldInfo field, boolean value) throws java.io.IOException
Writes a protobufbool
field, 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 protobufenum
field.- 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 protobufuint32
field.- 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 protobufsint32
field.- 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 protobufuint32
field.- 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 protobufint64
field.- Throws:
java.io.IOException
-
serializeUInt64
public void serializeUInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobufuint64
field.- Throws:
java.io.IOException
-
writeInt64
public abstract void writeInt64(ProtoFieldInfo field, long value) throws java.io.IOException
Writes a protobufint64
field, 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 protobufuint64
field, 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 protobuffixed64
field.- Throws:
java.io.IOException
-
serializeFixed64Optional
public void serializeFixed64Optional(ProtoFieldInfo field, long value) throws java.io.IOException
Serializes a protobuffixed64
field.- 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 protobuffixed32
field. 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 protobuffixed32
field.- 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 bufdouble
field.- Throws:
java.io.IOException
-
serializeDoubleOptional
public void serializeDoubleOptional(ProtoFieldInfo field, double value) throws java.io.IOException
Serializes a proto bufdouble
field.- Throws:
java.io.IOException
-
writeDouble
public abstract void writeDouble(ProtoFieldInfo field, double value) throws java.io.IOException
Writes a protobufdouble
field, 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 protobufstring
field.utf8Bytes
is 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 protobufstring
field.string
is the value to be serialized andutf8Length
is 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 protobufstring
field, 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 protobufbytes
field.- Throws:
java.io.IOException
-
serializeByteBuffer
public void serializeByteBuffer(ProtoFieldInfo field, java.nio.ByteBuffer value) throws java.io.IOException
Serializes a protobufbytes
field. 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 fixed64
field.- Throws:
java.io.IOException
-
serializeRepeatedFixed64
public void serializeRepeatedFixed64(ProtoFieldInfo field, long[] values) throws java.io.IOException
Serializes arepeated fixed64
field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, long[] values) throws java.io.IOException
Serializes arepeated uint64
field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values) throws java.io.IOException
Serializes arepeated uint64
field.- Throws:
java.io.IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values) throws java.io.IOException
Serializes arepeated uint64
field.NOTE: This is the same as
serializeRepeatedUInt64(ProtoFieldInfo, long[])
but instead of taking a primitive array it takes aDynamicPrimitiveLongList
as input.- Throws:
java.io.IOException
-
serializeRepeatedInt64
public void serializeRepeatedInt64(ProtoFieldInfo field, java.util.List<java.lang.Long> values) throws java.io.IOException
Serializes arepeated int64
field.- Throws:
java.io.IOException
-
serializeRepeatedDouble
public void serializeRepeatedDouble(ProtoFieldInfo field, java.util.List<java.lang.Double> values) throws java.io.IOException
Serializes arepeated double
field.- Throws:
java.io.IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage) throws java.io.IOException
Serializesrepeated message
field.- Throws:
java.io.IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, java.util.List<? extends Marshaler> repeatedMessage) throws java.io.IOException
Serializesrepeated message
field.- 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 message
field. 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 message
field. 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 message
field. 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 message
field. 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.IOException
Writes 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.IOException
Writes end of a repeated message element.- Throws:
java.io.IOException
-
writeSerializedMessage
public abstract void writeSerializedMessage(byte[] protoSerialized, java.lang.String jsonSerialized) throws java.io.IOException
Writes 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:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.io.IOException
-
-