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 classprivate static class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()voidserializeBool(ProtoFieldInfo field, boolean value) Serializes a protobufboolfield.voidserializeByteAsFixed32(ProtoFieldInfo field, byte value) Serializes a byte as a protobuffixed32field.voidserializeByteBuffer(ProtoFieldInfo field, 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> void serializeMessageWithContext(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, List<Double> values) Serializes arepeated doublefield.voidserializeRepeatedFixed64(ProtoFieldInfo field, long[] values) Serializes arepeated fixed64field.voidserializeRepeatedFixed64(ProtoFieldInfo field, List<Long> values) Serializes arepeated fixed64field.voidserializeRepeatedInt64(ProtoFieldInfo field, List<Long> values) Serializes arepeated int64field.abstract voidserializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage) Serializesrepeated messagefield.abstract voidserializeRepeatedMessage(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) Serializesrepeated messagefield.voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, Attributes attributes, StatelessMarshaler2<AttributeKey<?>, Object> marshaler, MarshalerContext context) Serializesrepeated messagefield.<T> voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, Collection<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context, MarshalerContext.Key key) Serializesrepeated messagefield.abstract <T> voidserializeRepeatedMessageWithContext(ProtoFieldInfo field, List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context) Serializesrepeated messagefield.<K,V> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, 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, List<Long> values) Serializes arepeated uint64field.voidserializeSInt32(ProtoFieldInfo field, int value) Serializes a protobufsint32field.voidserializeSpanId(ProtoFieldInfo field, String spanId) Serializes a span ID field.voidserializeSpanId(ProtoFieldInfo field, String spanId, MarshalerContext context) voidserializeString(ProtoFieldInfo field, byte[] utf8Bytes) Serializes a protobufstringfield.voidserializeStringWithContext(ProtoFieldInfo field, String string, MarshalerContext context) Serializes a protobufstringfield.voidserializeTraceId(ProtoFieldInfo field, String traceId) Serializes a trace ID field.voidserializeTraceId(ProtoFieldInfo field, 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, 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 voidprotected abstract voidWrites end of repeated messages.protected abstract voidWrites end of a repeated message element.protected abstract voidprotected abstract voidprotected 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, 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, String spanId) protected voidwriteSpanId(ProtoFieldInfo field, 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, String string, int utf8Length, MarshalerContext context) protected abstract voidwriteTraceId(ProtoFieldInfo field, String traceId) protected voidwriteTraceId(ProtoFieldInfo field, 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 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 protobufboolfield.- Throws:
IOException
-
writeBool
Writes a protobufboolfield, even if it matches the default value.- Throws:
IOException
-
serializeEnum
Serializes a protobufenumfield.- Throws:
IOException
-
writeEnum
- Throws:
IOException
-
serializeUInt32
Serializes a protobufuint32field.- Throws:
IOException
-
writeUint32
- Throws:
IOException
-
serializeSInt32
Serializes a protobufsint32field.- Throws:
IOException
-
writeSInt32
- Throws:
IOException
-
serializeInt32
Serializes a protobufuint32field.- Throws:
IOException
-
writeint32
- Throws:
IOException
-
serializeInt64
Serializes a protobufint64field.- Throws:
IOException
-
serializeUInt64
Serializes a protobufuint64field.- Throws:
IOException
-
writeInt64
Writes a protobufint64field, even if it matches the default value.- Throws:
IOException
-
writeUInt64
Writes a protobufuint64field, even if it matches the default value.- Throws:
IOException
-
serializeFixed64
Serializes a protobuffixed64field.- Throws:
IOException
-
serializeFixed64Optional
Serializes a protobuffixed64field.- Throws:
IOException
-
writeFixed64
- Throws:
IOException
-
writeFixed64Value
- Throws:
IOException
-
writeUInt64Value
- Throws:
IOException
-
serializeByteAsFixed32
Serializes a byte as a protobuffixed32field. Ensures that there is no sign propagation if the high bit in the byte is set.- Throws:
IOException
-
serializeFixed32
Serializes a protobuffixed32field.- Throws:
IOException
-
writeFixed32
- Throws:
IOException
-
serializeDouble
Serializes a proto bufdoublefield.- Throws:
IOException
-
serializeDoubleOptional
Serializes a proto bufdoublefield.- Throws:
IOException
-
writeDouble
Writes a protobufdoublefield, even if it matches the default value.- Throws:
IOException
-
writeDoubleValue
- Throws:
IOException
-
serializeString
Serializes a protobufstringfield.utf8Bytesis 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 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:
IOException
-
writeString
Writes a protobufstringfield, 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 protobufbytesfield.- Throws:
IOException
-
serializeByteBuffer
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:
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 fixed64field.- Throws:
IOException
-
serializeRepeatedFixed64
Serializes arepeated fixed64field.- Throws:
IOException
-
serializeRepeatedUInt64
Serializes arepeated uint64field.- Throws:
IOException
-
serializeRepeatedUInt64
Serializes arepeated uint64field.- Throws:
IOException
-
serializeRepeatedUInt64
public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values) throws 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:
IOException
-
serializeRepeatedInt64
Serializes arepeated int64field.- Throws:
IOException
-
serializeRepeatedDouble
Serializes arepeated doublefield.- Throws:
IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, Marshaler[] repeatedMessage) throws IOException Serializesrepeated messagefield.- Throws:
IOException
-
serializeRepeatedMessage
public abstract void serializeRepeatedMessage(ProtoFieldInfo field, List<? extends Marshaler> repeatedMessage) throws IOException Serializesrepeated messagefield.- Throws:
IOException
-
serializeRepeatedMessageWithContext
public abstract <T> void serializeRepeatedMessageWithContext(ProtoFieldInfo field, List<? extends T> messages, StatelessMarshaler<T> marshaler, MarshalerContext context) throws IOException Serializesrepeated messagefield. 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 messagefield. 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 messagefield. 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 messagefield. 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:
closein interfaceAutoCloseable- Throws:
IOException
-