Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Mode: Adding 2nd part support for synchronous instruments - exponential histogram #6058

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -128,6 +129,31 @@ 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.
*
* <p>NOTE: This method has the same logic as {@link #sizeRepeatedUInt64(ProtoFieldInfo, long[])}
* )} but instead of using a primitive array it uses {@link DynamicPrimitiveLongList} to avoid
* boxing/unboxing
*/
public static int sizeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values) {
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
if (values.isEmpty()) {
return 0;
}

int payloadSize = 0;
for (int i = 0; i < values.size(); i++) {
long v = values.getLong(i);
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 double field. */
public static int sizeRepeatedDouble(ProtoFieldInfo field, List<Double> values) {
// Same as fixed64.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.exporter.internal.marshal;

import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -191,6 +192,7 @@ public void serializeMessage(ProtoFieldInfo field, Marshaler message) throws IOE
writeEndMessage();
}

@SuppressWarnings("SameParameterValue")
protected abstract void writeStartRepeatedPrimitive(
ProtoFieldInfo field, int protoSizePerElement, int numElements) throws IOException;

Expand Down Expand Up @@ -243,6 +245,32 @@ public void serializeRepeatedUInt64(ProtoFieldInfo field, long[] values) throws
writeEndRepeatedVarint();
}

/**
* Serializes a {@code repeated uint64} field.
*
* <p>NOTE: This is the same as {@link #serializeRepeatedUInt64(ProtoFieldInfo, long[])} but
* instead of taking a primitive array it takes a {@link DynamicPrimitiveLongList} as input.
*/
public void serializeRepeatedUInt64(ProtoFieldInfo field, DynamicPrimitiveLongList values)
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
if (values.isEmpty()) {
return;
}

int payloadSize = 0;
for (int i = 0; i < values.size(); i++) {
long v = values.getLong(i);
payloadSize += CodedOutputStream.computeUInt64SizeNoTag(v);
}

writeStartRepeatedVarint(field, payloadSize);
for (int i = 0; i < values.size(); i++) {
long value = values.getLong(i);
writeUInt64Value(value);
}
writeEndRepeatedVarint();
}

/** Serializes a {@code repeated double} field. */
public void serializeRepeatedDouble(ProtoFieldInfo field, List<Double> values)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.proto.metrics.v1.internal.ExponentialHistogramDataPoint;
import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import io.opentelemetry.sdk.internal.PrimitiveLongList;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramBuckets;
import java.io.IOException;
Expand All @@ -35,16 +36,29 @@ private ExponentialHistogramBucketsMarshaler(int offset, List<Long> counts) {
@Override
protected void writeTo(Serializer output) throws IOException {
output.serializeSInt32(ExponentialHistogramDataPoint.Buckets.OFFSET, offset);
output.serializeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS, PrimitiveLongList.toArray(counts));
if (counts instanceof DynamicPrimitiveLongList) {
output.serializeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS, (DynamicPrimitiveLongList) counts);
} else {
output.serializeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS, PrimitiveLongList.toArray(counts));
}
}

private static int calculateSize(int offset, List<Long> counts) {
int size = 0;
size += MarshalerUtil.sizeSInt32(ExponentialHistogramDataPoint.Buckets.OFFSET, offset);
size +=
MarshalerUtil.sizeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS, PrimitiveLongList.toArray(counts));
if (counts instanceof DynamicPrimitiveLongList) {
size +=
MarshalerUtil.sizeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS,
(DynamicPrimitiveLongList) counts);
} else {
size +=
MarshalerUtil.sizeRepeatedUInt64(
ExponentialHistogramDataPoint.Buckets.BUCKET_COUNTS,
PrimitiveLongList.toArray(counts));
}
return size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.opentelemetry.proto.metrics.v1.Summary;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramPointData;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
Expand All @@ -61,6 +62,8 @@
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryPointData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableValueAtQuantile;
import io.opentelemetry.sdk.metrics.internal.data.MutableExponentialHistogramBuckets;
import io.opentelemetry.sdk.metrics.internal.data.MutableExponentialHistogramPointData;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -508,6 +511,105 @@ void exponentialHistogramDataPoints() {
.build());
}

@SuppressWarnings("PointlessArithmeticExpression")
@Test
void exponentialHistogramReusableDataPoints() {
assertThat(
toExponentialHistogramDataPoints(
ImmutableList.of(
new MutableExponentialHistogramPointData()
.set(
0,
123.4,
1,
/* hasMin= */ false,
0,
/* hasMax= */ false,
0,
new MutableExponentialHistogramBuckets()
.set(0, 0, 0, DynamicPrimitiveLongList.empty()),
new MutableExponentialHistogramBuckets()
.set(0, 0, 0, DynamicPrimitiveLongList.empty()),
123,
456,
Attributes.empty(),
Collections.emptyList()),
new MutableExponentialHistogramPointData()
.set(
0,
123.4,
1,
/* hasMin= */ true,
3.3,
/* hasMax= */ true,
80.1,
new MutableExponentialHistogramBuckets()
.set(0, 1, 1 + 0 + 2, DynamicPrimitiveLongList.of(1L, 0L, 2L)),
new MutableExponentialHistogramBuckets()
.set(0, 0, 0, DynamicPrimitiveLongList.empty()),
123,
456,
Attributes.of(stringKey("key"), "value"),
ImmutableList.of(
ImmutableDoubleExemplarData.create(
Attributes.of(stringKey("test"), "value"),
2,
SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
TraceFlags.getDefault(),
TraceState.getDefault()),
1.5))))))
.containsExactly(
ExponentialHistogramDataPoint.newBuilder()
.setStartTimeUnixNano(123)
.setTimeUnixNano(456)
.setCount(1)
.setScale(0)
.setSum(123.4)
.setZeroCount(1)
.setPositive(
ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0)) // no buckets
.setNegative(
ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0)) // no buckets
.build(),
ExponentialHistogramDataPoint.newBuilder()
.setStartTimeUnixNano(123)
.setTimeUnixNano(456)
.setCount(4) // Counts in positive, negative, and zero count.
.addAllAttributes(
singletonList(
KeyValue.newBuilder().setKey("key").setValue(stringValue("value")).build()))
.setScale(0)
.setSum(123.4)
.setMin(3.3)
.setMax(80.1)
.setZeroCount(1)
.setPositive(
ExponentialHistogramDataPoint.Buckets.newBuilder()
.setOffset(1)
.addBucketCounts(1)
.addBucketCounts(0)
.addBucketCounts(2))
.setNegative(
ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0)) // no buckets
.addExemplars(
Exemplar.newBuilder()
.setTimeUnixNano(2)
.addFilteredAttributes(
KeyValue.newBuilder()
.setKey("test")
.setValue(stringValue("value"))
.build())
.setSpanId(ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 2}))
.setTraceId(
ByteString.copyFrom(
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}))
.setAsDouble(1.5)
.build())
.build());
}

@Test
void toProtoMetric_monotonic() {
assertThat(
Expand Down
Loading
Loading