|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.exporter.otlp.profiles; |
| 7 | + |
| 8 | +import io.opentelemetry.exporter.internal.marshal.MarshalerUtil; |
| 9 | +import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize; |
| 10 | +import io.opentelemetry.exporter.internal.marshal.Serializer; |
| 11 | +import io.opentelemetry.exporter.internal.otlp.KeyValueMarshaler; |
| 12 | +import io.opentelemetry.proto.profiles.v1experimental.internal.ProfileContainer; |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +final class ProfileContainerMarshaler extends MarshalerWithSize { |
| 16 | + |
| 17 | + private final byte[] profileId; |
| 18 | + private final long startEpochNanos; |
| 19 | + private final long endEpochNanos; |
| 20 | + private final KeyValueMarshaler[] attributeMarshalers; |
| 21 | + private final int droppedAttributesCount; |
| 22 | + private final byte[] originalPayloadFormatUtf8; |
| 23 | + private final byte[] originalPayload; |
| 24 | + private final ProfileMarshaler profileMarshaler; |
| 25 | + |
| 26 | + static ProfileContainerMarshaler create(ProfileContainerData profileContainerData) { |
| 27 | + int droppedAttributesCount = |
| 28 | + profileContainerData.getTotalAttributeCount() - profileContainerData.getAttributes().size(); |
| 29 | + |
| 30 | + // Not ideal, but this will do for now. ByteBuffer support in |
| 31 | + // Serialzer/CodedOutputStream/MarshalerUtilwill follow in a separate step. |
| 32 | + byte[] originalPayload = new byte[profileContainerData.getOriginalPayload().remaining()]; |
| 33 | + profileContainerData.getOriginalPayload().get(originalPayload); |
| 34 | + |
| 35 | + return new ProfileContainerMarshaler( |
| 36 | + profileContainerData.getProfileIdBytes(), |
| 37 | + profileContainerData.getStartEpochNanos(), |
| 38 | + profileContainerData.getEndEpochNanos(), |
| 39 | + KeyValueMarshaler.createForAttributes(profileContainerData.getAttributes()), |
| 40 | + droppedAttributesCount, |
| 41 | + MarshalerUtil.toBytes(profileContainerData.getOriginalPayloadFormat()), |
| 42 | + originalPayload, |
| 43 | + ProfileMarshaler.create(profileContainerData.getProfile())); |
| 44 | + } |
| 45 | + |
| 46 | + private ProfileContainerMarshaler( |
| 47 | + byte[] profileId, |
| 48 | + long startEpochNanos, |
| 49 | + long endEpochNanos, |
| 50 | + KeyValueMarshaler[] attributeMarshalers, |
| 51 | + int droppedAttributesCount, |
| 52 | + byte[] originalPayloadFormat, |
| 53 | + byte[] originalPayload, |
| 54 | + ProfileMarshaler profileMarshaler) { |
| 55 | + super( |
| 56 | + calculateSize( |
| 57 | + profileId, |
| 58 | + startEpochNanos, |
| 59 | + endEpochNanos, |
| 60 | + attributeMarshalers, |
| 61 | + droppedAttributesCount, |
| 62 | + originalPayloadFormat, |
| 63 | + originalPayload, |
| 64 | + profileMarshaler)); |
| 65 | + this.profileId = profileId; |
| 66 | + this.startEpochNanos = startEpochNanos; |
| 67 | + this.endEpochNanos = endEpochNanos; |
| 68 | + this.attributeMarshalers = attributeMarshalers; |
| 69 | + this.droppedAttributesCount = droppedAttributesCount; |
| 70 | + this.originalPayloadFormatUtf8 = originalPayloadFormat; |
| 71 | + this.originalPayload = originalPayload; |
| 72 | + this.profileMarshaler = profileMarshaler; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void writeTo(Serializer output) throws IOException { |
| 77 | + output.serializeBytes(ProfileContainer.PROFILE_ID, profileId); |
| 78 | + output.serializeFixed64(ProfileContainer.START_TIME_UNIX_NANO, startEpochNanos); |
| 79 | + output.serializeFixed64(ProfileContainer.END_TIME_UNIX_NANO, endEpochNanos); |
| 80 | + output.serializeRepeatedMessage(ProfileContainer.ATTRIBUTES, attributeMarshalers); |
| 81 | + output.serializeUInt32(ProfileContainer.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); |
| 82 | + output.serializeString(ProfileContainer.ORIGINAL_PAYLOAD_FORMAT, originalPayloadFormatUtf8); |
| 83 | + output.serializeBytes(ProfileContainer.ORIGINAL_PAYLOAD, originalPayload); |
| 84 | + output.serializeMessage(ProfileContainer.PROFILE, profileMarshaler); |
| 85 | + } |
| 86 | + |
| 87 | + private static int calculateSize( |
| 88 | + byte[] profileId, |
| 89 | + long startEpochNanos, |
| 90 | + long endEpochNanos, |
| 91 | + KeyValueMarshaler[] attributeMarshalers, |
| 92 | + int droppedAttributesCount, |
| 93 | + byte[] originalPayloadFormat, |
| 94 | + byte[] originalPayload, |
| 95 | + ProfileMarshaler profileMarshaler) { |
| 96 | + int size; |
| 97 | + size = 0; |
| 98 | + size += MarshalerUtil.sizeBytes(ProfileContainer.PROFILE_ID, profileId); |
| 99 | + size += MarshalerUtil.sizeFixed64(ProfileContainer.START_TIME_UNIX_NANO, startEpochNanos); |
| 100 | + size += MarshalerUtil.sizeFixed64(ProfileContainer.END_TIME_UNIX_NANO, endEpochNanos); |
| 101 | + size += MarshalerUtil.sizeRepeatedMessage(ProfileContainer.ATTRIBUTES, attributeMarshalers); |
| 102 | + size += |
| 103 | + MarshalerUtil.sizeUInt32(ProfileContainer.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); |
| 104 | + size += |
| 105 | + MarshalerUtil.sizeBytes(ProfileContainer.ORIGINAL_PAYLOAD_FORMAT, originalPayloadFormat); |
| 106 | + size += MarshalerUtil.sizeBytes(ProfileContainer.ORIGINAL_PAYLOAD, originalPayload); |
| 107 | + size += MarshalerUtil.sizeMessage(ProfileContainer.PROFILE, profileMarshaler); |
| 108 | + return size; |
| 109 | + } |
| 110 | +} |
0 commit comments