Skip to content

Commit

Permalink
Add Marshalers for profiling signal type (#6565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalliday authored Jul 23, 2024
1 parent 468b528 commit 26b3d41
Show file tree
Hide file tree
Showing 16 changed files with 1,352 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ protected void writeUInt64Value(long value) throws IOException {
generator.writeString(Long.toString(value));
}

@Override
public void writeUInt64(ProtoFieldInfo field, long value) throws IOException {
generator.writeStringField(field.getJsonName(), Long.toString(value));
}

@Override
protected void writeFixed32(ProtoFieldInfo field, int value) throws IOException {
generator.writeNumberField(field.getJsonName(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ public static int sizeRepeatedUInt64(ProtoFieldInfo field, long[] values) {
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
}

/**
* Returns the size of a repeated uint64 field.
*
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
* and an actual payload of repeated varints.
*/
public static int sizeRepeatedUInt64(ProtoFieldInfo field, List<Long> values) {
if (values.isEmpty()) {
return 0;
}

int payloadSize = 0;
for (long v : values) {
payloadSize += CodedOutputStream.computeUInt64SizeNoTag(v);
}

// tag size + payload indicator size + actual payload size
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
}

/**
* Returns the size of a repeated uint64 field.
*
Expand All @@ -154,6 +174,46 @@ public static int sizeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongL
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
}

/**
* Returns the size of a repeated int64 field.
*
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
* and an actual payload of repeated varints.
*/
public static int sizeRepeatedInt64(ProtoFieldInfo field, long[] values) {
if (values.length == 0) {
return 0;
}

int payloadSize = 0;
for (long v : values) {
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
}

// tag size + payload indicator size + actual payload size
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
}

/**
* Returns the size of a repeated int64 field.
*
* <p>Packed repeated fields contain the tag, an integer representing the incoming payload size,
* and an actual payload of repeated varints.
*/
public static int sizeRepeatedInt64(ProtoFieldInfo field, List<Long> values) {
if (values.isEmpty()) {
return 0;
}

int payloadSize = 0;
for (long v : values) {
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
}

// tag size + payload indicator size + actual payload size
return field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(payloadSize) + payloadSize;
}

/** Returns the size of a repeated double field. */
public static int sizeRepeatedDouble(ProtoFieldInfo field, List<Double> values) {
// Same as fixed64.
Expand Down Expand Up @@ -207,6 +267,14 @@ public static int sizeInt64(ProtoFieldInfo field, long message) {
return field.getTagSize() + CodedOutputStream.computeInt64SizeNoTag(message);
}

/** Returns the size of a uint64 field. */
public static int sizeUInt64(ProtoFieldInfo field, long message) {
if (message == 0) {
return 0;
}
return field.getTagSize() + CodedOutputStream.computeUInt64SizeNoTag(message);
}

/** Returns the size of a uint32 field. */
public static int sizeUInt32(ProtoFieldInfo field, int message) {
if (message == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public void writeInt64(ProtoFieldInfo field, long value) throws IOException {
output.writeInt64NoTag(value);
}

@Override
public void writeUInt64(ProtoFieldInfo field, long value) throws IOException {
output.writeUInt32NoTag(field.getTag());
output.writeUInt64NoTag(value);
}

@Override
protected void writeFixed64(ProtoFieldInfo field, long value) throws IOException {
output.writeUInt32NoTag(field.getTag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,20 @@ public void serializeInt64(ProtoFieldInfo field, long value) throws IOException
writeInt64(field, value);
}

/** Serializes a protobuf {@code uint64} field. */
public void serializeUInt64(ProtoFieldInfo field, long value) throws IOException {
if (value == 0) {
return;
}
writeUInt64(field, value);
}

/** Writes a protobuf {@code int64} field, even if it matches the default value. */
public abstract void writeInt64(ProtoFieldInfo field, long value) throws IOException;

/** Writes a protobuf {@code uint64} field, even if it matches the default value. */
public abstract void writeUInt64(ProtoFieldInfo field, long value) throws IOException;

/** Serializes a protobuf {@code fixed64} field. */
public void serializeFixed64(ProtoFieldInfo field, long value) throws IOException {
if (value == 0) {
Expand Down Expand Up @@ -340,6 +351,24 @@ public void serializeRepeatedUInt64(ProtoFieldInfo field, long[] values) throws
writeEndRepeatedVarint();
}

/** Serializes a {@code repeated uint64} field. */
public void serializeRepeatedUInt64(ProtoFieldInfo field, List<Long> values) throws IOException {
if (values.isEmpty()) {
return;
}

int payloadSize = 0;
for (long v : values) {
payloadSize += CodedOutputStream.computeUInt64SizeNoTag(v);
}

writeStartRepeatedVarint(field, payloadSize);
for (long value : values) {
writeUInt64Value(value);
}
writeEndRepeatedVarint();
}

/**
* Serializes a {@code repeated uint64} field.
*
Expand All @@ -366,6 +395,24 @@ public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongLi
writeEndRepeatedVarint();
}

/** Serializes a {@code repeated int64} field. */
public void serializeRepeatedInt64(ProtoFieldInfo field, List<Long> values) throws IOException {
if (values.isEmpty()) {
return;
}

int payloadSize = 0;
for (long v : values) {
payloadSize += CodedOutputStream.computeInt64SizeNoTag(v);
}

writeStartRepeatedVarint(field, payloadSize);
for (long value : values) {
writeUInt64Value(value);
}
writeEndRepeatedVarint();
}

/** Serializes a {@code repeated double} field. */
public void serializeRepeatedDouble(ProtoFieldInfo field, List<Double> values)
throws IOException {
Expand Down
1 change: 1 addition & 0 deletions exporters/otlp/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ wire {
"opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest",
"opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest",
"opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest",
"opentelemetry.proto.collector.profiles.v1experimental.ExportProfilesServiceRequest"
)

custom {
Expand Down
7 changes: 7 additions & 0 deletions exporters/otlp/profiles/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ plugins {
description = "OpenTelemetry - Profiles Exporter"
otelJava.moduleName.set("io.opentelemetry.exporter.otlp.profiles")

val versions: Map<String, String> by project
dependencies {
api(project(":sdk:common"))
api(project(":exporters:common"))
implementation(project(":exporters:otlp:common"))

annotationProcessor("com.google.auto.value:auto-value")

testImplementation("com.fasterxml.jackson.core:jackson-databind")
testImplementation("com.google.protobuf:protobuf-java-util")
testImplementation("io.opentelemetry.proto:opentelemetry-proto")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.otlp.profiles;

import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.profiles.v1experimental.internal.AttributeUnit;
import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

final class AttributeUnitMarshaler extends MarshalerWithSize {

private static final AttributeUnitMarshaler[] EMPTY_REPEATED = new AttributeUnitMarshaler[0];

private final long attributeKey;
private final long unitIndex;

static AttributeUnitMarshaler create(AttributeUnitData attributeUnitData) {
return new AttributeUnitMarshaler(
attributeUnitData.getAttributeKey(), attributeUnitData.getUnitIndex());
}

static AttributeUnitMarshaler[] createRepeated(List<AttributeUnitData> items) {
if (items.isEmpty()) {
return EMPTY_REPEATED;
}

AttributeUnitMarshaler[] attributeUnitMarshalers = new AttributeUnitMarshaler[items.size()];
items.forEach(
item ->
new Consumer<AttributeUnitData>() {
int index = 0;

@Override
public void accept(AttributeUnitData attributeUnitData) {
attributeUnitMarshalers[index++] = AttributeUnitMarshaler.create(attributeUnitData);
}
});
return attributeUnitMarshalers;
}

private AttributeUnitMarshaler(long attributeKey, long unitIndex) {
super(calculateSize(attributeKey, unitIndex));
this.attributeKey = attributeKey;
this.unitIndex = unitIndex;
}

@Override
protected void writeTo(Serializer output) throws IOException {
output.serializeInt64(AttributeUnit.ATTRIBUTE_KEY, attributeKey);
output.serializeInt64(AttributeUnit.UNIT, unitIndex);
}

private static int calculateSize(long attributeKey, long unitIndex) {
int size;
size = 0;
size += MarshalerUtil.sizeInt64(AttributeUnit.ATTRIBUTE_KEY, attributeKey);
size += MarshalerUtil.sizeInt64(AttributeUnit.UNIT, unitIndex);
return size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.otlp.profiles;

import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.profiles.v1experimental.internal.Function;
import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

final class FunctionMarshaler extends MarshalerWithSize {

private static final FunctionMarshaler[] EMPTY_REPEATED = new FunctionMarshaler[0];

private final long nameIndex;
private final long systemNameIndex;
private final long filenameIndex;
private final long startLine;

static FunctionMarshaler create(FunctionData functionData) {
return new FunctionMarshaler(
functionData.getNameIndex(),
functionData.getSystemNameIndex(),
functionData.getFilenameIndex(),
functionData.getStartLine());
}

static FunctionMarshaler[] createRepeated(List<FunctionData> items) {
if (items.isEmpty()) {
return EMPTY_REPEATED;
}

FunctionMarshaler[] functionMarshalers = new FunctionMarshaler[items.size()];
items.forEach(
item ->
new Consumer<FunctionData>() {
int index = 0;

@Override
public void accept(FunctionData functionData) {
functionMarshalers[index++] = FunctionMarshaler.create(functionData);
}
});
return functionMarshalers;
}

private FunctionMarshaler(
long nameIndex, long systemNameIndex, long filenameIndex, long startLine) {
super(calculateSize(nameIndex, systemNameIndex, filenameIndex, startLine));
this.nameIndex = nameIndex;
this.systemNameIndex = systemNameIndex;
this.filenameIndex = filenameIndex;
this.startLine = startLine;
}

@Override
protected void writeTo(Serializer output) throws IOException {
output.serializeInt64(Function.NAME, nameIndex);
output.serializeInt64(Function.SYSTEM_NAME, systemNameIndex);
output.serializeInt64(Function.FILENAME, filenameIndex);
output.serializeInt64(Function.START_LINE, startLine);
}

private static int calculateSize(
long nameIndex, long systemNameIndex, long filenameIndex, long startLine) {
int size = 0;
size += MarshalerUtil.sizeInt64(Function.NAME, nameIndex);
size += MarshalerUtil.sizeInt64(Function.SYSTEM_NAME, systemNameIndex);
size += MarshalerUtil.sizeInt64(Function.FILENAME, filenameIndex);
size += MarshalerUtil.sizeInt64(Function.START_LINE, startLine);
return size;
}
}
Loading

0 comments on commit 26b3d41

Please sign in to comment.