Class Serializer
java.lang.Object
io.opentelemetry.exporter.internal.marshal.Serializer
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
JsonSerializer
,ProtoSerializer
Serializer to use when converting from an SDK data object into a protobuf output format. Unlike
CodedOutputStream
, 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 ClassesModifier and TypeClassDescriptionprivate static class
private static class
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract 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, 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> void serializeMessageWithContext
(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, List<Double> values) Serializes arepeated double
field.void
serializeRepeatedFixed64
(ProtoFieldInfo field, long[] values) Serializes arepeated fixed64
field.void
serializeRepeatedFixed64
(ProtoFieldInfo field, List<Long> values) Serializes arepeated fixed64
field.void
serializeRepeatedInt64
(ProtoFieldInfo field, List<Long> values) Serializes arepeated int64
field.abstract void
serializeRepeatedMessage
(ProtoFieldInfo field, Marshaler[] repeatedMessage) Serializesrepeated message
field.abstract void
serializeRepeatedMessage
(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) Serializesrepeated message
field.void
serializeRepeatedMessageWithContext
(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>, Object> marshaler, MarshalerContext context) Serializesrepeated message
field.<T> void
serializeRepeatedMessageWithContext
(ProtoFieldInfo field, Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key) Serializesrepeated message
field.abstract <T> void
serializeRepeatedMessageWithContext
(ProtoFieldInfo field, List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context) Serializesrepeated message
field.<K,
V> void serializeRepeatedMessageWithContext
(ProtoFieldInfo field, 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, List<Long> values) Serializes arepeated uint64
field.void
serializeSInt32
(ProtoFieldInfo field, int value) Serializes a protobufsint32
field.void
serializeSpanId
(ProtoFieldInfo field, String spanId) Serializes a span ID field.void
serializeSpanId
(ProtoFieldInfo field, String spanId, MarshalerContext context) void
serializeString
(ProtoFieldInfo field, byte[] utf8Bytes) Serializes a protobufstring
field.void
serializeStringWithContext
(ProtoFieldInfo field, String string, MarshalerContext context) Serializes a protobufstring
field.void
serializeTraceId
(ProtoFieldInfo field, String traceId) Serializes a trace ID field.void
serializeTraceId
(ProtoFieldInfo field, 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, 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
protected abstract void
Writes end of repeated messages.protected abstract void
Writes end of a repeated message element.protected abstract void
protected abstract void
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, 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, String spanId) protected void
writeSpanId
(ProtoFieldInfo field, 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, String string, int utf8Length, MarshalerContext context) protected abstract void
writeTraceId
(ProtoFieldInfo field, String traceId) protected void
writeTraceId
(ProtoFieldInfo field, 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 Details
-
ATTRIBUTES_WRITER_KEY
-
-
Constructor Details
-
Serializer
Serializer()
-
-
Method Details
-
serializeTraceId
Serializes a trace ID field.- Throws:
IOException
-
serializeTraceId
public void serializeTraceId(ProtoFieldInfo field, @Nullable String traceId, MarshalerContext context) throws IOException - Throws:
IOException
-
writeTraceId
- Throws:
IOException
-
writeTraceId
protected void writeTraceId(ProtoFieldInfo field, String traceId, MarshalerContext context) throws IOException - Throws:
IOException
-
serializeSpanId
Serializes a span ID field.- Throws:
IOException
-
serializeSpanId
public void serializeSpanId(ProtoFieldInfo field, @Nullable String spanId, MarshalerContext context) throws IOException - Throws:
IOException
-
writeSpanId
- Throws:
IOException
-
writeSpanId
protected void writeSpanId(ProtoFieldInfo field, String spanId, MarshalerContext context) throws IOException - Throws:
IOException
-
serializeBool
Serializes a protobufbool
field.- Throws:
IOException
-
writeBool
Writes a protobufbool
field, even if it matches the default value.- Throws:
IOException
-
serializeEnum
Serializes a protobufenum
field.- Throws:
IOException
-
writeEnum
- Throws:
IOException
-
serializeUInt32
Serializes a protobufuint32
field.- Throws:
IOException
-
writeUint32
- Throws:
IOException
-
serializeSInt32
Serializes a protobufsint32
field.- Throws:
IOException
-
writeSInt32
- Throws:
IOException
-
serializeInt32
Serializes a protobufuint32
field.- Throws:
IOException
-
writeint32
- Throws:
IOException
-
serializeInt64
Serializes a protobufint64
field.- Throws:
IOException
-
serializeUInt64
Serializes a protobufuint64
field.- Throws:
IOException
-
writeInt64
Writes a protobufint64
field, even if it matches the default value.- Throws:
IOException
-
writeUInt64
Writes a protobufuint64
field, even if it matches the default value.- Throws:
IOException
-
serializeFixed64
Serializes a protobuffixed64
field.- Throws:
IOException
-
serializeFixed64Optional
Serializes a protobuffixed64
field.- Throws:
IOException
-
writeFixed64
- Throws:
IOException
-
writeFixed64Value
- Throws:
IOException
-
writeUInt64Value
- Throws:
IOException
-
serializeByteAsFixed32
Serializes a byte as a protobuffixed32
field. Ensures that there is no sign propagation if the high bit in the byte is set.- Throws:
IOException
-
serializeFixed32
Serializes a protobuffixed32
field.- Throws:
IOException
-
writeFixed32
- Throws:
IOException
-
serializeDouble
Serializes a proto bufdouble
field.- Throws:
IOException
-
serializeDoubleOptional
Serializes a proto bufdouble
field.- Throws:
IOException
-
writeDouble
Writes a protobufdouble
field, even if it matches the default value.- Throws:
IOException
-
writeDoubleValue
- Throws:
IOException
-
serializeString
Serializes a protobufstring
field.utf8Bytes
is the UTF8 encoded bytes of the string to serialize.- Throws:
IOException
-
serializeStringWithContext
public void serializeStringWithContext(ProtoFieldInfo field, @Nullable String string, MarshalerContext context) throws 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:
IOException
-
writeString
Writes a protobufstring
field, even if it matches the default value.- Throws:
IOException
-
writeString
public abstract void writeString(ProtoFieldInfo field, String string, int utf8Length, MarshalerContext context) throws IOException - Throws:
IOException
-
serializeBytes
Serializes a protobufbytes
field.- Throws:
IOException
-
serializeByteBuffer
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:
IOException
-
writeBytes
- Throws:
IOException
-
writeByteBuffer
- Throws:
IOException
-
writeStartMessage
protected abstract void writeStartMessage(ProtoFieldInfo field, int protoMessageSize) throws IOException - Throws:
IOException
-
writeEndMessage
- Throws:
IOException
-
serializeMessage
Serializes a protobuf embeddedmessage
.- Throws:
IOException
-
serializeMessageWithContext
public <T> void serializeMessageWithContext(ProtoFieldInfo field, T message, StatelessMarshaler<T> marshaler, MarshalerContext context) throws IOException Serializes a protobuf embeddedmessage
. This method adds elements to context, use together withStatelessMarshalerUtil.sizeMessageWithContext(ProtoFieldInfo, Object, StatelessMarshaler, MarshalerContext)
.- Throws:
IOException
-
serializeMessageWithContext
public <K,V> void serializeMessageWithContext(ProtoFieldInfo field, K key, V value, StatelessMarshaler2<K, V> marshaler, MarshalerContext context) throws IOExceptionSerializes a protobuf embeddedmessage
. This method adds elements to context, use together withStatelessMarshalerUtil.sizeMessageWithContext(ProtoFieldInfo, Object, Object, StatelessMarshaler2, MarshalerContext)
.- Throws:
IOException
-
writeStartRepeatedPrimitive
protected abstract void writeStartRepeatedPrimitive(ProtoFieldInfo field, int protoSizePerElement, int numElements) throws IOException - Throws:
IOException
-
writeEndRepeatedPrimitive
- Throws:
IOException
-
writeStartRepeatedVarint
protected abstract void writeStartRepeatedVarint(ProtoFieldInfo field, int payloadSize) throws IOException - Throws:
IOException
-
writeEndRepeatedVarint
- Throws:
IOException
-
serializeRepeatedFixed64
Serializes arepeated fixed64
field.- Throws:
IOException
-
serializeRepeatedFixed64
Serializes arepeated fixed64
field.- Throws:
IOException
-
serializeRepeatedUInt64
Serializes arepeated uint64
field.- Throws:
IOException
-
serializeRepeatedUInt64
Serializes arepeated uint64
field.- Throws:
IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values) throws 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:
IOException
-
serializeRepeatedInt64
Serializes arepeated int64
field.- Throws:
IOException
-
serializeRepeatedDouble
Serializes arepeated double
field.- Throws:
IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage) throws IOException Serializesrepeated message
field.- Throws:
IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) throws IOException Serializesrepeated message
field.- Throws:
IOException
-
serializeRepeatedMessageWithContext
public abstract <T> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context) throws IOException Serializesrepeated message
field. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, List, StatelessMarshaler, MarshalerContext)
.- Throws:
IOException
-
serializeRepeatedMessageWithContext
public <T> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key) throws IOException Serializesrepeated message
field. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Collection, StatelessMarshaler, MarshalerContext, MarshalerContext.Key)
.- Throws:
IOException
-
serializeRepeatedMessageWithContext
public <K,V> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, Map<K, V> messages, StatelessMarshaler2<K, throws IOExceptionV> marshaler, MarshalerContext context, MarshalerContext.Key key) Serializesrepeated message
field. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Map, StatelessMarshaler2, MarshalerContext, MarshalerContext.Key)
.- Throws:
IOException
-
serializeRepeatedMessageWithContext
public void serializeRepeatedMessageWithContext(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>, Object> marshaler, MarshalerContext context) throws IOExceptionSerializesrepeated message
field. This method reads elements from context, use together withStatelessMarshalerUtil.sizeRepeatedMessageWithContext(ProtoFieldInfo, Attributes, StatelessMarshaler2, MarshalerContext)
.- Throws:
IOException
-
writeStartRepeated
Writes start of repeated messages.- Throws:
IOException
-
writeEndRepeated
Writes end of repeated messages.- Throws:
IOException
-
writeStartRepeatedElement
protected abstract void writeStartRepeatedElement(ProtoFieldInfo field, int protoMessageSize) throws IOException Writes start of a repeated message element.- Throws:
IOException
-
writeEndRepeatedElement
Writes end of a repeated message element.- Throws:
IOException
-
writeSerializedMessage
public abstract void writeSerializedMessage(byte[] protoSerialized, String jsonSerialized) throws IOException Writes the value for a message field that has been pre-serialized.- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-