|
| 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.v1development.internal.ProfilesDictionary; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +final class ProfileDictionaryMarshaler extends MarshalerWithSize { |
| 17 | + |
| 18 | + private final MappingMarshaler[] mappingTableMarshalers; |
| 19 | + private final LocationMarshaler[] locationTableMarshalers; |
| 20 | + private final FunctionMarshaler[] functionTableMarshalers; |
| 21 | + private final KeyValueMarshaler[] attributeTableMarshalers; |
| 22 | + private final AttributeUnitMarshaler[] attributeUnitMarshalers; |
| 23 | + private final LinkMarshaler[] linkTableMarshalers; |
| 24 | + private final byte[][] stringTable; |
| 25 | + |
| 26 | + static ProfileDictionaryMarshaler create(ProfileDictionaryData profileDictionaryData) { |
| 27 | + |
| 28 | + MappingMarshaler[] mappingMarshalers = |
| 29 | + MappingMarshaler.createRepeated(profileDictionaryData.getMappingTable()); |
| 30 | + LocationMarshaler[] locationMarshalers = |
| 31 | + LocationMarshaler.createRepeated(profileDictionaryData.getLocationTable()); |
| 32 | + FunctionMarshaler[] functionMarshalers = |
| 33 | + FunctionMarshaler.createRepeated(profileDictionaryData.getFunctionTable()); |
| 34 | + KeyValueMarshaler[] attributeTableMarshalers = |
| 35 | + KeyValueMarshaler.createRepeated(profileDictionaryData.getAttributeTable()); |
| 36 | + AttributeUnitMarshaler[] attributeUnitsMarshalers = |
| 37 | + AttributeUnitMarshaler.createRepeated(profileDictionaryData.getAttributeUnits()); |
| 38 | + LinkMarshaler[] linkMarshalers = |
| 39 | + LinkMarshaler.createRepeated(profileDictionaryData.getLinkTable()); |
| 40 | + |
| 41 | + byte[][] convertedStrings = new byte[profileDictionaryData.getStringTable().size()][]; |
| 42 | + for (int i = 0; i < profileDictionaryData.getStringTable().size(); i++) { |
| 43 | + convertedStrings[i] = |
| 44 | + profileDictionaryData.getStringTable().get(i).getBytes(StandardCharsets.UTF_8); |
| 45 | + } |
| 46 | + |
| 47 | + return new ProfileDictionaryMarshaler( |
| 48 | + mappingMarshalers, |
| 49 | + locationMarshalers, |
| 50 | + functionMarshalers, |
| 51 | + attributeTableMarshalers, |
| 52 | + attributeUnitsMarshalers, |
| 53 | + linkMarshalers, |
| 54 | + convertedStrings); |
| 55 | + } |
| 56 | + |
| 57 | + private ProfileDictionaryMarshaler( |
| 58 | + MappingMarshaler[] mappingTableMarshalers, |
| 59 | + LocationMarshaler[] locationTableMarshalers, |
| 60 | + FunctionMarshaler[] functionTableMarshalers, |
| 61 | + KeyValueMarshaler[] attributeTableMarshalers, |
| 62 | + AttributeUnitMarshaler[] attributeUnitMarshalers, |
| 63 | + LinkMarshaler[] linkTableMarshalers, |
| 64 | + byte[][] stringTableUtf8) { |
| 65 | + super( |
| 66 | + calculateSize( |
| 67 | + mappingTableMarshalers, |
| 68 | + locationTableMarshalers, |
| 69 | + functionTableMarshalers, |
| 70 | + attributeTableMarshalers, |
| 71 | + attributeUnitMarshalers, |
| 72 | + linkTableMarshalers, |
| 73 | + stringTableUtf8)); |
| 74 | + this.mappingTableMarshalers = mappingTableMarshalers; |
| 75 | + this.locationTableMarshalers = locationTableMarshalers; |
| 76 | + this.functionTableMarshalers = functionTableMarshalers; |
| 77 | + this.attributeTableMarshalers = attributeTableMarshalers; |
| 78 | + this.attributeUnitMarshalers = attributeUnitMarshalers; |
| 79 | + this.linkTableMarshalers = linkTableMarshalers; |
| 80 | + this.stringTable = stringTableUtf8; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void writeTo(Serializer output) throws IOException { |
| 85 | + output.serializeRepeatedMessage(ProfilesDictionary.MAPPING_TABLE, mappingTableMarshalers); |
| 86 | + output.serializeRepeatedMessage(ProfilesDictionary.LOCATION_TABLE, locationTableMarshalers); |
| 87 | + output.serializeRepeatedMessage(ProfilesDictionary.FUNCTION_TABLE, functionTableMarshalers); |
| 88 | + output.serializeRepeatedMessage(ProfilesDictionary.ATTRIBUTE_TABLE, attributeTableMarshalers); |
| 89 | + output.serializeRepeatedMessage(ProfilesDictionary.ATTRIBUTE_UNITS, attributeUnitMarshalers); |
| 90 | + output.serializeRepeatedMessage(ProfilesDictionary.LINK_TABLE, linkTableMarshalers); |
| 91 | + output.serializeRepeatedString(ProfilesDictionary.STRING_TABLE, stringTable); |
| 92 | + } |
| 93 | + |
| 94 | + private static int calculateSize( |
| 95 | + MappingMarshaler[] mappingMarshalers, |
| 96 | + LocationMarshaler[] locationMarshalers, |
| 97 | + FunctionMarshaler[] functionMarshalers, |
| 98 | + KeyValueMarshaler[] attributeTableMarshalers, |
| 99 | + AttributeUnitMarshaler[] attributeUnitMarshalers, |
| 100 | + LinkMarshaler[] linkMarshalers, |
| 101 | + byte[][] stringTable) { |
| 102 | + int size; |
| 103 | + size = 0; |
| 104 | + size += MarshalerUtil.sizeRepeatedMessage(ProfilesDictionary.MAPPING_TABLE, mappingMarshalers); |
| 105 | + size += |
| 106 | + MarshalerUtil.sizeRepeatedMessage(ProfilesDictionary.LOCATION_TABLE, locationMarshalers); |
| 107 | + size += |
| 108 | + MarshalerUtil.sizeRepeatedMessage(ProfilesDictionary.FUNCTION_TABLE, functionMarshalers); |
| 109 | + size += |
| 110 | + MarshalerUtil.sizeRepeatedMessage( |
| 111 | + ProfilesDictionary.ATTRIBUTE_TABLE, attributeTableMarshalers); |
| 112 | + size += |
| 113 | + MarshalerUtil.sizeRepeatedMessage( |
| 114 | + ProfilesDictionary.ATTRIBUTE_UNITS, attributeUnitMarshalers); |
| 115 | + size += MarshalerUtil.sizeRepeatedMessage(ProfilesDictionary.LINK_TABLE, linkMarshalers); |
| 116 | + size += MarshalerUtil.sizeRepeatedString(ProfilesDictionary.STRING_TABLE, stringTable); |
| 117 | + |
| 118 | + return size; |
| 119 | + } |
| 120 | +} |
0 commit comments