Skip to content

Commit 26b3d41

Browse files
authored
Add Marshalers for profiling signal type (#6565)
1 parent 468b528 commit 26b3d41

File tree

16 files changed

+1352
-0
lines changed

16 files changed

+1352
-0
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/JsonSerializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ protected void writeUInt64Value(long value) throws IOException {
8181
generator.writeString(Long.toString(value));
8282
}
8383

84+
@Override
85+
public void writeUInt64(ProtoFieldInfo field, long value) throws IOException {
86+
generator.writeStringField(field.getJsonName(), Long.toString(value));
87+
}
88+
8489
@Override
8590
protected void writeFixed32(ProtoFieldInfo field, int value) throws IOException {
8691
generator.writeNumberField(field.getJsonName(), value);

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/MarshalerUtil.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ public static int sizeRepeatedUInt64(ProtoFieldInfo field, long[] values) {
129129
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
130130
}
131131

132+
/**
133+
* Returns the size of a repeated uint64 field.
134+
*
135+
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
136+
* and an actual payload of repeated varints.
137+
*/
138+
public static int sizeRepeatedUInt64(ProtoFieldInfo field, List<Long> values) {
139+
if (values.isEmpty()) {
140+
return 0;
141+
}
142+
143+
int payloadSize = 0;
144+
for (long v : values) {
145+
payloadSize += CodedOutputStream.computeUInt64SizeNoTag(v);
146+
}
147+
148+
// tag size + payload indicator size + actual payload size
149+
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
150+
}
151+
132152
/**
133153
* Returns the size of a repeated uint64 field.
134154
*
@@ -154,6 +174,46 @@ public static int sizeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongL
154174
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
155175
}
156176

177+
/**
178+
* Returns the size of a repeated int64 field.
179+
*
180+
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
181+
* and an actual payload of repeated varints.
182+
*/
183+
public static int sizeRepeatedInt64(ProtoFieldInfo field, long[] values) {
184+
if (values.length == 0) {
185+
return 0;
186+
}
187+
188+
int payloadSize = 0;
189+
for (long v : values) {
190+
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
191+
}
192+
193+
// tag size + payload indicator size + actual payload size
194+
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
195+
}
196+
197+
/**
198+
* Returns the size of a repeated int64 field.
199+
*
200+
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
201+
* and an actual payload of repeated varints.
202+
*/
203+
public static int sizeRepeatedInt64(ProtoFieldInfo field, List<Long> values) {
204+
if (values.isEmpty()) {
205+
return 0;
206+
}
207+
208+
int payloadSize = 0;
209+
for (long v : values) {
210+
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
211+
}
212+
213+
// tag size + payload indicator size + actual payload size
214+
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
215+
}
216+
157217
/** Returns the size of a repeated double field. */
158218
public static int sizeRepeatedDouble(ProtoFieldInfo field, List<Double> values) {
159219
// Same as fixed64.
@@ -207,6 +267,14 @@ public static int sizeInt64(ProtoFieldInfo field, long message) {
207267
return field.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(message);
208268
}
209269

270+
/** Returns the size of a uint64 field. */
271+
public static int sizeUInt64(ProtoFieldInfo field, long message) {
272+
if (message == 0) {
273+
return 0;
274+
}
275+
return field.getTagSize() + CodedOutputStream.computeUInt64SizeNoTag(message);
276+
}
277+
210278
/** Returns the size of a uint32 field. */
211279
public static int sizeUInt32(ProtoFieldInfo field, int message) {
212280
if (message == 0) {

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/ProtoSerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public void writeInt64(ProtoFieldInfo field, long value) throws IOException {
108108
output.writeInt64NoTag(value);
109109
}
110110

111+
@Override
112+
public void writeUInt64(ProtoFieldInfo field, long value) throws IOException {
113+
output.writeUInt32NoTag(field.getTag());
114+
output.writeUInt64NoTag(value);
115+
}
116+
111117
@Override
112118
protected void writeFixed64(ProtoFieldInfo field, long value) throws IOException {
113119
output.writeUInt32NoTag(field.getTag());

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Serializer.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,20 @@ public void serializeInt64(ProtoFieldInfo field, long value) throws IOException
139139
writeInt64(field, value);
140140
}
141141

142+
/** Serializes a protobuf {@code uint64} field. */
143+
public void serializeUInt64(ProtoFieldInfo field, long value) throws IOException {
144+
if (value == 0) {
145+
return;
146+
}
147+
writeUInt64(field, value);
148+
}
149+
142150
/** Writes a protobuf {@code int64} field, even if it matches the default value. */
143151
public abstract void writeInt64(ProtoFieldInfo field, long value) throws IOException;
144152

153+
/** Writes a protobuf {@code uint64} field, even if it matches the default value. */
154+
public abstract void writeUInt64(ProtoFieldInfo field, long value) throws IOException;
155+
145156
/** Serializes a protobuf {@code fixed64} field. */
146157
public void serializeFixed64(ProtoFieldInfo field, long value) throws IOException {
147158
if (value == 0) {
@@ -340,6 +351,24 @@ public void serializeRepeatedUInt64(ProtoFieldInfo field, long[] values) throws
340351
writeEndRepeatedVarint();
341352
}
342353

354+
/** Serializes a {@code repeated uint64} field. */
355+
public void serializeRepeatedUInt64(ProtoFieldInfo field, List<Long> values) throws IOException {
356+
if (values.isEmpty()) {
357+
return;
358+
}
359+
360+
int payloadSize = 0;
361+
for (long v : values) {
362+
payloadSize += CodedOutputStream.computeUInt64SizeNoTag(v);
363+
}
364+
365+
writeStartRepeatedVarint(field, payloadSize);
366+
for (long value : values) {
367+
writeUInt64Value(value);
368+
}
369+
writeEndRepeatedVarint();
370+
}
371+
343372
/**
344373
* Serializes a {@code repeated uint64} field.
345374
*
@@ -366,6 +395,24 @@ public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongLi
366395
writeEndRepeatedVarint();
367396
}
368397

398+
/** Serializes a {@code repeated int64} field. */
399+
public void serializeRepeatedInt64(ProtoFieldInfo field, List<Long> values) throws IOException {
400+
if (values.isEmpty()) {
401+
return;
402+
}
403+
404+
int payloadSize = 0;
405+
for (long v : values) {
406+
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
407+
}
408+
409+
writeStartRepeatedVarint(field, payloadSize);
410+
for (long value : values) {
411+
writeUInt64Value(value);
412+
}
413+
writeEndRepeatedVarint();
414+
}
415+
369416
/** Serializes a {@code repeated double} field. */
370417
public void serializeRepeatedDouble(ProtoFieldInfo field, List<Double> values)
371418
throws IOException {

exporters/otlp/common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ wire {
4242
"opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest",
4343
"opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest",
4444
"opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest",
45+
"opentelemetry.proto.collector.profiles.v1experimental.ExportProfilesServiceRequest"
4546
)
4647

4748
custom {

exporters/otlp/profiles/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ plugins {
99
description = "OpenTelemetry - Profiles Exporter"
1010
otelJava.moduleName.set("io.opentelemetry.exporter.otlp.profiles")
1111

12+
val versions: Map<String, String> by project
1213
dependencies {
1314
api(project(":sdk:common"))
15+
api(project(":exporters:common"))
16+
implementation(project(":exporters:otlp:common"))
1417

1518
annotationProcessor("com.google.auto.value:auto-value")
19+
20+
testImplementation("com.fasterxml.jackson.core:jackson-databind")
21+
testImplementation("com.google.protobuf:protobuf-java-util")
22+
testImplementation("io.opentelemetry.proto:opentelemetry-proto")
1623
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.proto.profiles.v1experimental.internal.AttributeUnit;
12+
import java.io.IOException;
13+
import java.util.List;
14+
import java.util.function.Consumer;
15+
16+
final class AttributeUnitMarshaler extends MarshalerWithSize {
17+
18+
private static final AttributeUnitMarshaler[] EMPTY_REPEATED = new AttributeUnitMarshaler[0];
19+
20+
private final long attributeKey;
21+
private final long unitIndex;
22+
23+
static AttributeUnitMarshaler create(AttributeUnitData attributeUnitData) {
24+
return new AttributeUnitMarshaler(
25+
attributeUnitData.getAttributeKey(), attributeUnitData.getUnitIndex());
26+
}
27+
28+
static AttributeUnitMarshaler[] createRepeated(List<AttributeUnitData> items) {
29+
if (items.isEmpty()) {
30+
return EMPTY_REPEATED;
31+
}
32+
33+
AttributeUnitMarshaler[] attributeUnitMarshalers = new AttributeUnitMarshaler[items.size()];
34+
items.forEach(
35+
item ->
36+
new Consumer<AttributeUnitData>() {
37+
int index = 0;
38+
39+
@Override
40+
public void accept(AttributeUnitData attributeUnitData) {
41+
attributeUnitMarshalers[index++] = AttributeUnitMarshaler.create(attributeUnitData);
42+
}
43+
});
44+
return attributeUnitMarshalers;
45+
}
46+
47+
private AttributeUnitMarshaler(long attributeKey, long unitIndex) {
48+
super(calculateSize(attributeKey, unitIndex));
49+
this.attributeKey = attributeKey;
50+
this.unitIndex = unitIndex;
51+
}
52+
53+
@Override
54+
protected void writeTo(Serializer output) throws IOException {
55+
output.serializeInt64(AttributeUnit.ATTRIBUTE_KEY, attributeKey);
56+
output.serializeInt64(AttributeUnit.UNIT, unitIndex);
57+
}
58+
59+
private static int calculateSize(long attributeKey, long unitIndex) {
60+
int size;
61+
size = 0;
62+
size += MarshalerUtil.sizeInt64(AttributeUnit.ATTRIBUTE_KEY, attributeKey);
63+
size += MarshalerUtil.sizeInt64(AttributeUnit.UNIT, unitIndex);
64+
return size;
65+
}
66+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.proto.profiles.v1experimental.internal.Function;
12+
import java.io.IOException;
13+
import java.util.List;
14+
import java.util.function.Consumer;
15+
16+
final class FunctionMarshaler extends MarshalerWithSize {
17+
18+
private static final FunctionMarshaler[] EMPTY_REPEATED = new FunctionMarshaler[0];
19+
20+
private final long nameIndex;
21+
private final long systemNameIndex;
22+
private final long filenameIndex;
23+
private final long startLine;
24+
25+
static FunctionMarshaler create(FunctionData functionData) {
26+
return new FunctionMarshaler(
27+
functionData.getNameIndex(),
28+
functionData.getSystemNameIndex(),
29+
functionData.getFilenameIndex(),
30+
functionData.getStartLine());
31+
}
32+
33+
static FunctionMarshaler[] createRepeated(List<FunctionData> items) {
34+
if (items.isEmpty()) {
35+
return EMPTY_REPEATED;
36+
}
37+
38+
FunctionMarshaler[] functionMarshalers = new FunctionMarshaler[items.size()];
39+
items.forEach(
40+
item ->
41+
new Consumer<FunctionData>() {
42+
int index = 0;
43+
44+
@Override
45+
public void accept(FunctionData functionData) {
46+
functionMarshalers[index++] = FunctionMarshaler.create(functionData);
47+
}
48+
});
49+
return functionMarshalers;
50+
}
51+
52+
private FunctionMarshaler(
53+
long nameIndex, long systemNameIndex, long filenameIndex, long startLine) {
54+
super(calculateSize(nameIndex, systemNameIndex, filenameIndex, startLine));
55+
this.nameIndex = nameIndex;
56+
this.systemNameIndex = systemNameIndex;
57+
this.filenameIndex = filenameIndex;
58+
this.startLine = startLine;
59+
}
60+
61+
@Override
62+
protected void writeTo(Serializer output) throws IOException {
63+
output.serializeInt64(Function.NAME, nameIndex);
64+
output.serializeInt64(Function.SYSTEM_NAME, systemNameIndex);
65+
output.serializeInt64(Function.FILENAME, filenameIndex);
66+
output.serializeInt64(Function.START_LINE, startLine);
67+
}
68+
69+
private static int calculateSize(
70+
long nameIndex, long systemNameIndex, long filenameIndex, long startLine) {
71+
int size = 0;
72+
size += MarshalerUtil.sizeInt64(Function.NAME, nameIndex);
73+
size += MarshalerUtil.sizeInt64(Function.SYSTEM_NAME, systemNameIndex);
74+
size += MarshalerUtil.sizeInt64(Function.FILENAME, filenameIndex);
75+
size += MarshalerUtil.sizeInt64(Function.START_LINE, startLine);
76+
return size;
77+
}
78+
}

0 commit comments

Comments
 (0)