Skip to content

Commit b6016be

Browse files
committed
update profiles exporters for changes in proto v1.8.0-alpha
1 parent 78e8056 commit b6016be

File tree

7 files changed

+169
-17
lines changed

7 files changed

+169
-17
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.exporter.otlp.internal.data;
7+
8+
import com.google.auto.value.AutoValue;
9+
import io.opentelemetry.api.common.Value;
10+
import io.opentelemetry.exporter.otlp.profiles.KeyValueAndUnitData;
11+
import javax.annotation.concurrent.Immutable;
12+
13+
/**
14+
* Auto value implementation of {@link KeyValueAndUnitData}, which describes a Key Value pair with
15+
* optional unit for the value.
16+
*
17+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
18+
* at any time.
19+
*/
20+
@Immutable
21+
@AutoValue
22+
public abstract class ImmutableKeyValueAndUnitData implements KeyValueAndUnitData {
23+
24+
/** Returns a {@link KeyValueAndUnitData} for the given parameters. */
25+
public static ImmutableKeyValueAndUnitData create(
26+
int keyStringIndex, Value<?> value, int unitStringIndex) {
27+
return new AutoValue_ImmutableKeyValueAndUnitData(keyStringIndex, value, unitStringIndex);
28+
}
29+
30+
ImmutableKeyValueAndUnitData() {}
31+
}

exporters/otlp/profiles/src/main/java/io/opentelemetry/exporter/otlp/internal/data/ImmutableProfileDictionaryData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static ProfileDictionaryData create(
3939
List<FunctionData> functionTable,
4040
List<LinkData> linkTable,
4141
List<String> stringTable,
42-
List<KeyValueAndUnitData<?>> attributeTable,
42+
List<KeyValueAndUnitData> attributeTable,
4343
List<StackData> stackTable) {
4444
return new AutoValue_ImmutableProfileDictionaryData(
4545
mappingTable,

exporters/otlp/profiles/src/main/java/io/opentelemetry/exporter/otlp/profiles/KeyValueAndUnitData.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55

66
package io.opentelemetry.exporter.otlp.profiles;
77

8-
import io.opentelemetry.exporter.internal.otlp.AttributeKeyValue;
8+
import io.opentelemetry.api.common.Value;
99

1010
/**
11-
* Describes a KeyValue pair with optional unit description for the value.
11+
* Describes a KeyValue pair with optional unit description for the value. Similar to
12+
* io.opentelemetry.api.common.KeyValue, but dictionary-centric.
1213
*
1314
* @see "common.proto::KeyValue"
1415
* @see "profiles.proto::KeyValueAndUnit"
1516
*/
16-
public interface KeyValueAndUnitData<T> extends AttributeKeyValue<T> {
17+
public interface KeyValueAndUnitData {
1718

1819
/** Index into string table. */
19-
String getKeyStringIndex();
20+
int getKeyStringIndex();
2021

21-
// T getValue();
22+
Value<?> getValue();
2223

2324
/** Index into string table. 0 indicates implicit (via semconv) or undefined. */
24-
String getUnitStringIndex();
25+
int getUnitStringIndex();
2526
}
26-
// TODO JH finish me, then add Marshaler, tests
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.Marshaler;
9+
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
10+
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
11+
import io.opentelemetry.exporter.internal.marshal.Serializer;
12+
import io.opentelemetry.exporter.internal.otlp.AnyValueMarshaler;
13+
import io.opentelemetry.proto.profiles.v1development.internal.KeyValueAndUnit;
14+
import java.io.IOException;
15+
import java.util.List;
16+
import java.util.function.Consumer;
17+
18+
final class KeyValueAndUnitMarshaler extends MarshalerWithSize {
19+
20+
private static final KeyValueAndUnitMarshaler[] EMPTY_REPEATED = new KeyValueAndUnitMarshaler[0];
21+
22+
private final int keyStringIndex;
23+
private final Marshaler valueMarshaler;
24+
private final int unitStringIndex;
25+
26+
static KeyValueAndUnitMarshaler create(KeyValueAndUnitData keyValueAndUnitData) {
27+
Marshaler valueMarshaler = AnyValueMarshaler.create(keyValueAndUnitData.getValue());
28+
return new KeyValueAndUnitMarshaler(
29+
keyValueAndUnitData.getKeyStringIndex(),
30+
valueMarshaler,
31+
keyValueAndUnitData.getUnitStringIndex());
32+
}
33+
34+
static KeyValueAndUnitMarshaler[] createRepeated(List<KeyValueAndUnitData> items) {
35+
if (items.isEmpty()) {
36+
return EMPTY_REPEATED;
37+
}
38+
39+
KeyValueAndUnitMarshaler[] keyValueAndUnitMarshalers =
40+
new KeyValueAndUnitMarshaler[items.size()];
41+
items.forEach(
42+
new Consumer<KeyValueAndUnitData>() {
43+
int index = 0;
44+
45+
@Override
46+
public void accept(KeyValueAndUnitData keyValueAndUnitData) {
47+
keyValueAndUnitMarshalers[index++] =
48+
KeyValueAndUnitMarshaler.create(keyValueAndUnitData);
49+
}
50+
});
51+
return keyValueAndUnitMarshalers;
52+
}
53+
54+
private KeyValueAndUnitMarshaler(
55+
int keyStringIndex, Marshaler valueMarshaler, int unitStringIndex) {
56+
super(calculateSize(keyStringIndex, valueMarshaler, unitStringIndex));
57+
this.keyStringIndex = keyStringIndex;
58+
this.valueMarshaler = valueMarshaler;
59+
this.unitStringIndex = unitStringIndex;
60+
}
61+
62+
@Override
63+
protected void writeTo(Serializer output) throws IOException {
64+
output.serializeInt32(KeyValueAndUnit.KEY_STRINDEX, keyStringIndex);
65+
output.serializeMessage(KeyValueAndUnit.VALUE, valueMarshaler);
66+
output.serializeInt64(KeyValueAndUnit.UNIT_STRINDEX, unitStringIndex);
67+
}
68+
69+
private static int calculateSize(
70+
int keyStringIndex, Marshaler valueMarshaler, int unitStringIndex) {
71+
int size = 0;
72+
size += MarshalerUtil.sizeInt32(KeyValueAndUnit.KEY_STRINDEX, keyStringIndex);
73+
size += MarshalerUtil.sizeMessage(KeyValueAndUnit.VALUE, valueMarshaler);
74+
size += MarshalerUtil.sizeInt32(KeyValueAndUnit.UNIT_STRINDEX, unitStringIndex);
75+
return size;
76+
}
77+
}

exporters/otlp/profiles/src/main/java/io/opentelemetry/exporter/otlp/profiles/ProfileDictionaryData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface ProfileDictionaryData {
3737
List<String> getStringTable();
3838

3939
/** Lookup table for attributes. */
40-
List<KeyValueAndUnitData<?>> getAttributeTable();
40+
List<KeyValueAndUnitData> getAttributeTable();
4141

4242
/** Lookup table for stacks. */
4343
List<StackData> getStackTable();

exporters/otlp/profiles/src/main/java/io/opentelemetry/exporter/otlp/profiles/ProfileDictionaryMarshaler.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
99
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
1010
import io.opentelemetry.exporter.internal.marshal.Serializer;
11-
import io.opentelemetry.exporter.internal.otlp.KeyValueMarshaler;
1211
import io.opentelemetry.proto.profiles.v1development.internal.ProfilesDictionary;
1312
import java.io.IOException;
1413
import java.nio.charset.StandardCharsets;
15-
import java.util.Collections;
1614

1715
final class ProfileDictionaryMarshaler extends MarshalerWithSize {
1816

@@ -21,7 +19,7 @@ final class ProfileDictionaryMarshaler extends MarshalerWithSize {
2119
private final FunctionMarshaler[] functionTableMarshalers;
2220
private final LinkMarshaler[] linkTableMarshalers;
2321
private final byte[][] stringTable;
24-
private final KeyValueMarshaler[] attributeTableMarshalers; // TODO
22+
private final KeyValueAndUnitMarshaler[] attributeTableMarshalers;
2523
private final StackMarshaler[] stackTableMarshalers;
2624

2725
static ProfileDictionaryMarshaler create(ProfileDictionaryData profileDictionaryData) {
@@ -41,9 +39,8 @@ static ProfileDictionaryMarshaler create(ProfileDictionaryData profileDictionary
4139
profileDictionaryData.getStringTable().get(i).getBytes(StandardCharsets.UTF_8);
4240
}
4341

44-
KeyValueMarshaler[] attributeTableMarshalers =
45-
KeyValueMarshaler.createRepeated(Collections.emptyList());
46-
// TODO JH profileDictionaryData.getAttributeTable()
42+
KeyValueAndUnitMarshaler[] attributeTableMarshalers =
43+
KeyValueAndUnitMarshaler.createRepeated(profileDictionaryData.getAttributeTable());
4744
StackMarshaler[] stackTableMarshalers =
4845
StackMarshaler.createRepeated(profileDictionaryData.getStackTable());
4946

@@ -63,7 +60,7 @@ private ProfileDictionaryMarshaler(
6360
FunctionMarshaler[] functionTableMarshalers,
6461
LinkMarshaler[] linkTableMarshalers,
6562
byte[][] stringTableUtf8,
66-
KeyValueMarshaler[] attributeTableMarshalers,
63+
KeyValueAndUnitMarshaler[] attributeTableMarshalers,
6764
StackMarshaler[] stackTableMarshalers) {
6865
super(
6966
calculateSize(
@@ -100,7 +97,7 @@ private static int calculateSize(
10097
FunctionMarshaler[] functionMarshalers,
10198
LinkMarshaler[] linkMarshalers,
10299
byte[][] stringTable,
103-
KeyValueMarshaler[] attributeTableMarshalers,
100+
KeyValueAndUnitMarshaler[] attributeTableMarshalers,
104101
StackMarshaler[] stackTableMarshalers) {
105102
int size;
106103
size = 0;

exporters/otlp/profiles/src/test/java/io/opentelemetry/exporter/otlp/profiles/ProfilesRequestMarshalerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import com.google.protobuf.Message;
1313
import com.google.protobuf.util.JsonFormat;
1414
import io.opentelemetry.api.common.Attributes;
15+
import io.opentelemetry.api.common.Value;
1516
import io.opentelemetry.exporter.internal.marshal.Marshaler;
1617
import io.opentelemetry.exporter.otlp.internal.data.ImmutableFunctionData;
18+
import io.opentelemetry.exporter.otlp.internal.data.ImmutableKeyValueAndUnitData;
1719
import io.opentelemetry.exporter.otlp.internal.data.ImmutableLineData;
1820
import io.opentelemetry.exporter.otlp.internal.data.ImmutableLinkData;
1921
import io.opentelemetry.exporter.otlp.internal.data.ImmutableLocationData;
@@ -23,8 +25,10 @@
2325
import io.opentelemetry.exporter.otlp.internal.data.ImmutableSampleData;
2426
import io.opentelemetry.exporter.otlp.internal.data.ImmutableStackData;
2527
import io.opentelemetry.exporter.otlp.internal.data.ImmutableValueTypeData;
28+
import io.opentelemetry.proto.common.v1.AnyValue;
2629
import io.opentelemetry.proto.common.v1.InstrumentationScope;
2730
import io.opentelemetry.proto.profiles.v1development.Function;
31+
import io.opentelemetry.proto.profiles.v1development.KeyValueAndUnit;
2832
import io.opentelemetry.proto.profiles.v1development.Line;
2933
import io.opentelemetry.proto.profiles.v1development.Link;
3034
import io.opentelemetry.proto.profiles.v1development.Location;
@@ -123,6 +127,49 @@ void compareRepeatedLineMarshaling() {
123127
}
124128
}
125129

130+
@Test
131+
void compareKeyValueAndUnitMarshaling() {
132+
KeyValueAndUnitData input = ImmutableKeyValueAndUnitData.create(1, Value.of("foo"), 3);
133+
KeyValueAndUnit builderResult =
134+
KeyValueAndUnit.newBuilder()
135+
.setKeyStrindex(1)
136+
.setValue(AnyValue.newBuilder().setStringValue("foo").build())
137+
.setUnitStrindex(3)
138+
.build();
139+
140+
KeyValueAndUnit roundTripResult =
141+
parse(KeyValueAndUnit.getDefaultInstance(), KeyValueAndUnitMarshaler.create(input));
142+
assertThat(roundTripResult).isEqualTo(builderResult);
143+
}
144+
145+
@Test
146+
void compareRepeatedKeyValueAndUnitMarshaling() {
147+
List<KeyValueAndUnitData> inputs = new ArrayList<>();
148+
inputs.add(ImmutableKeyValueAndUnitData.create(1, Value.of("foo"), 3));
149+
inputs.add(ImmutableKeyValueAndUnitData.create(4, Value.of("bar"), 6));
150+
151+
List<KeyValueAndUnit> builderResults = new ArrayList<>();
152+
builderResults.add(
153+
KeyValueAndUnit.newBuilder()
154+
.setKeyStrindex(1)
155+
.setValue(AnyValue.newBuilder().setStringValue("foo").build())
156+
.setUnitStrindex(3)
157+
.build());
158+
builderResults.add(
159+
KeyValueAndUnit.newBuilder()
160+
.setKeyStrindex(4)
161+
.setValue(AnyValue.newBuilder().setStringValue("bar").build())
162+
.setUnitStrindex(6)
163+
.build());
164+
165+
KeyValueAndUnitMarshaler[] marshalers = KeyValueAndUnitMarshaler.createRepeated(inputs);
166+
167+
for (int i = 0; i < marshalers.length; i++) {
168+
KeyValueAndUnit roundTripResult = parse(KeyValueAndUnit.getDefaultInstance(), marshalers[i]);
169+
assertThat(roundTripResult).isEqualTo(builderResults.get(i));
170+
}
171+
}
172+
126173
@Test
127174
void compareLinkMarshaling() {
128175
String traceId = "0123456789abcdef0123456789abcdef";

0 commit comments

Comments
 (0)