Skip to content

Commit

Permalink
Update the OTLP proto to the latest released version (0.4.0) (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwatson authored Jul 1, 2020
1 parent ee0d438 commit 74f52dd
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,92 @@
package io.opentelemetry.exporters.otlp;

import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.proto.common.v1.AttributeKeyValue;
import io.opentelemetry.proto.common.v1.AttributeKeyValue.ValueType;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.common.v1.ArrayValue;
import io.opentelemetry.proto.common.v1.InstrumentationLibrary;
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;

final class CommonAdapter {
static AttributeKeyValue toProtoAttribute(String key, AttributeValue attributeValue) {
AttributeKeyValue.Builder builder = AttributeKeyValue.newBuilder().setKey(key);
static KeyValue toProtoAttribute(String key, AttributeValue attributeValue) {
KeyValue.Builder builder = KeyValue.newBuilder().setKey(key);
switch (attributeValue.getType()) {
case STRING:
return builder
.setType(ValueType.STRING)
.setStringValue(attributeValue.getStringValue())
.setValue(AnyValue.newBuilder().setStringValue(attributeValue.getStringValue()).build())
.build();
case BOOLEAN:
return builder
.setType(ValueType.BOOL)
.setBoolValue(attributeValue.getBooleanValue())
.setValue(AnyValue.newBuilder().setBoolValue(attributeValue.getBooleanValue()).build())
.build();
case LONG:
return builder.setType(ValueType.INT).setIntValue(attributeValue.getLongValue()).build();
return builder
.setValue(AnyValue.newBuilder().setIntValue(attributeValue.getLongValue()).build())
.build();
case DOUBLE:
return builder
.setType(ValueType.DOUBLE)
.setDoubleValue(attributeValue.getDoubleValue())
.setValue(AnyValue.newBuilder().setDoubleValue(attributeValue.getDoubleValue()).build())
.build();
case BOOLEAN_ARRAY:
return builder
.setValue(
AnyValue.newBuilder()
.setArrayValue(makeBooleanArrayAnyValue(attributeValue))
.build())
.build();
case LONG_ARRAY:
return builder
.setValue(
AnyValue.newBuilder().setArrayValue(makeLongArrayAnyValue(attributeValue)).build())
.build();
case DOUBLE_ARRAY:
return builder
.setValue(
AnyValue.newBuilder()
.setArrayValue(makeDoubleArrayAnyValue(attributeValue))
.build())
.build();
case STRING_ARRAY:
return builder.setType(ValueType.UNRECOGNIZED).build();
return builder
.setValue(
AnyValue.newBuilder()
.setArrayValue(makeStringArrayAnyValue(attributeValue))
.build())
.build();
}
return builder.setValue(AnyValue.getDefaultInstance()).build();
}

private static ArrayValue makeDoubleArrayAnyValue(AttributeValue attributeValue) {
ArrayValue.Builder builder = ArrayValue.newBuilder();
for (Double doubleValue : attributeValue.getDoubleArrayValue()) {
builder.addValues(AnyValue.newBuilder().setDoubleValue(doubleValue).build());
}
return builder.build();
}

private static ArrayValue makeLongArrayAnyValue(AttributeValue attributeValue) {
ArrayValue.Builder builder = ArrayValue.newBuilder();
for (Long intValue : attributeValue.getLongArrayValue()) {
builder.addValues(AnyValue.newBuilder().setIntValue(intValue).build());
}
return builder.build();
}

private static ArrayValue makeStringArrayAnyValue(AttributeValue attributeValue) {
ArrayValue.Builder builder = ArrayValue.newBuilder();
for (String string : attributeValue.getStringArrayValue()) {
builder.addValues(AnyValue.newBuilder().setStringValue(string).build());
}
return builder.build();
}

private static ArrayValue makeBooleanArrayAnyValue(AttributeValue attributeValue) {
ArrayValue.Builder builder = ArrayValue.newBuilder();
for (Boolean bool : attributeValue.getBooleanArrayValue()) {
builder.addValues(AnyValue.newBuilder().setBoolValue(bool).build());
}
return builder.setType(ValueType.UNRECOGNIZED).build();
return builder.build();
}

static InstrumentationLibrary toProtoInstrumentationLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.opentelemetry.proto.metrics.v1.Int64DataPoint;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.metrics.v1.MetricDescriptor;
import io.opentelemetry.proto.metrics.v1.MetricDescriptor.Temporality;
import io.opentelemetry.proto.metrics.v1.MetricDescriptor.Type;
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
Expand Down Expand Up @@ -100,25 +101,26 @@ static Metric toProtoMetric(MetricData metricData) {
if (metricData.getPoints().isEmpty()) {
return builder.build();
}

switch (builder.getMetricDescriptor().getType()) {
case UNSPECIFIED:
case UNRECOGNIZED:
case INVALID_TYPE:
break;
case GAUGE_INT64:
case COUNTER_INT64:
builder.addAllInt64DataPoints(toInt64DataPoints(metricData.getPoints()));
case MONOTONIC_INT64:
case INT64:
builder.addAllInt64DataPoints(
toInt64DataPoints(metricData.getPoints(), metricData.getDescriptor()));
break;
case GAUGE_DOUBLE:
case COUNTER_DOUBLE:
builder.addAllDoubleDataPoints(toDoubleDataPoints(metricData.getPoints()));
case MONOTONIC_DOUBLE:
case DOUBLE:
builder.addAllDoubleDataPoints(
toDoubleDataPoints(metricData.getPoints(), metricData.getDescriptor()));
break;
case GAUGE_HISTOGRAM:
case CUMULATIVE_HISTOGRAM:
case HISTOGRAM:
// TODO: Add support for histogram.
break;
case SUMMARY:
builder.addAllSummaryDataPoints(toSummaryDataPoints(metricData.getPoints()));
builder.addAllSummaryDataPoints(
toSummaryDataPoints(metricData.getPoints(), metricData.getDescriptor()));
break;
}
return builder.build();
Expand All @@ -130,11 +132,25 @@ static MetricDescriptor toProtoMetricDescriptor(Descriptor descriptor) {
.setDescription(descriptor.getDescription())
.setUnit(descriptor.getUnit())
.setType(toProtoMetricDescriptorType(descriptor.getType()))
.addAllLabels(toProtoLabels(descriptor.getConstantLabels()))
.setTemporality(mapToTemporality(descriptor))
.build();
}

static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
private static Temporality mapToTemporality(Descriptor descriptor) {
switch (descriptor.getType()) {
case NON_MONOTONIC_LONG:
case NON_MONOTONIC_DOUBLE:
case MONOTONIC_LONG:
case MONOTONIC_DOUBLE:
return Temporality.CUMULATIVE;
case SUMMARY:
return Temporality.DELTA;
}
return Temporality.UNRECOGNIZED;
}

static Collection<Int64DataPoint> toInt64DataPoints(
Collection<Point> points, Descriptor descriptor) {
List<Int64DataPoint> result = new ArrayList<>(points.size());
for (Point point : points) {
LongPoint longPoint = (LongPoint) point;
Expand All @@ -144,6 +160,9 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
.setTimeUnixNano(longPoint.getEpochNanos())
.setValue(longPoint.getValue());
// Not calling directly addAllLabels because that generates couple of unnecessary allocations.
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
}
Collection<StringKeyValue> labels = toProtoLabels(longPoint.getLabels());
if (!labels.isEmpty()) {
builder.addAllLabels(labels);
Expand All @@ -153,7 +172,8 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
return result;
}

static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points) {
static Collection<DoubleDataPoint> toDoubleDataPoints(
Collection<Point> points, Descriptor descriptor) {
List<DoubleDataPoint> result = new ArrayList<>(points.size());
for (Point point : points) {
DoublePoint doublePoint = (DoublePoint) point;
Expand All @@ -163,6 +183,9 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
.setTimeUnixNano(doublePoint.getEpochNanos())
.setValue(doublePoint.getValue());
// Not calling directly addAllLabels because that generates couple of unnecessary allocations.
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
}
Collection<StringKeyValue> labels = toProtoLabels(doublePoint.getLabels());
if (!labels.isEmpty()) {
builder.addAllLabels(labels);
Expand All @@ -172,7 +195,8 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
return result;
}

static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points) {
static Collection<SummaryDataPoint> toSummaryDataPoints(
Collection<Point> points, Descriptor descriptor) {
List<SummaryDataPoint> result = new ArrayList<>(points.size());
for (Point point : points) {
SummaryPoint summaryPoint = (SummaryPoint) point;
Expand All @@ -184,6 +208,9 @@ static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points
.setSum(summaryPoint.getSum());
// Not calling directly addAllLabels because that generates couple of unnecessary allocations
// if empty list.
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
}
Collection<StringKeyValue> labels = toProtoLabels(summaryPoint.getLabels());
if (!labels.isEmpty()) {
builder.addAllLabels(labels);
Expand Down Expand Up @@ -221,17 +248,17 @@ static List<ValueAtPercentile> toProtoValueAtPercentiles(
static MetricDescriptor.Type toProtoMetricDescriptorType(Descriptor.Type descriptorType) {
switch (descriptorType) {
case NON_MONOTONIC_LONG:
return Type.GAUGE_INT64;
return Type.INT64;
case NON_MONOTONIC_DOUBLE:
return Type.GAUGE_DOUBLE;
return Type.DOUBLE;
case MONOTONIC_LONG:
return Type.COUNTER_INT64;
return Type.MONOTONIC_INT64;
case MONOTONIC_DOUBLE:
return Type.COUNTER_DOUBLE;
return Type.MONOTONIC_DOUBLE;
case SUMMARY:
return Type.SUMMARY;
}
return Type.UNSPECIFIED;
return Type.UNRECOGNIZED;
}

@SuppressWarnings("MixedMutabilityReturnType")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import static com.google.common.truth.Truth.assertThat;

import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.proto.common.v1.AttributeKeyValue;
import io.opentelemetry.proto.common.v1.AttributeKeyValue.ValueType;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.common.v1.ArrayValue;
import io.opentelemetry.proto.common.v1.InstrumentationLibrary;
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -34,43 +35,112 @@ public class CommonAdapterTest {
public void toProtoAttribute_Bool() {
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.booleanAttributeValue(true)))
.isEqualTo(
AttributeKeyValue.newBuilder()
KeyValue.newBuilder()
.setKey("key")
.setBoolValue(true)
.setType(ValueType.BOOL)
.setValue(AnyValue.newBuilder().setBoolValue(true).build())
.build());
}

@Test
public void toProtoAttribute_BoolArray() {
assertThat(
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(true, false)))
.isEqualTo(
KeyValue.newBuilder()
.setKey("key")
.setValue(
AnyValue.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(AnyValue.newBuilder().setBoolValue(true).build())
.addValues(AnyValue.newBuilder().setBoolValue(false).build())
.build())
.build())
.build());
}

@Test
public void toProtoAttribute_String() {
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.stringAttributeValue("string")))
.isEqualTo(
AttributeKeyValue.newBuilder()
KeyValue.newBuilder()
.setKey("key")
.setValue(AnyValue.newBuilder().setStringValue("string").build())
.build());
}

@Test
public void toProtoAttribute_StringArray() {
assertThat(
CommonAdapter.toProtoAttribute(
"key", AttributeValue.arrayAttributeValue("string1", "string2")))
.isEqualTo(
KeyValue.newBuilder()
.setKey("key")
.setStringValue("string")
.setType(ValueType.STRING)
.setValue(
AnyValue.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(AnyValue.newBuilder().setStringValue("string1").build())
.addValues(AnyValue.newBuilder().setStringValue("string2").build())
.build())
.build())
.build());
}

@Test
public void toProtoAttribute_Int() {
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.longAttributeValue(100)))
.isEqualTo(
AttributeKeyValue.newBuilder()
KeyValue.newBuilder()
.setKey("key")
.setIntValue(100)
.setType(ValueType.INT)
.setValue(AnyValue.newBuilder().setIntValue(100).build())
.build());
}

@Test
public void toProtoAttribute_IntArray() {
assertThat(
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100L, 200L)))
.isEqualTo(
KeyValue.newBuilder()
.setKey("key")
.setValue(
AnyValue.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(AnyValue.newBuilder().setIntValue(100).build())
.addValues(AnyValue.newBuilder().setIntValue(200).build())
.build())
.build())
.build());
}

@Test
public void toProtoAttribute_Double() {
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.doubleAttributeValue(100.3)))
.isEqualTo(
AttributeKeyValue.newBuilder()
KeyValue.newBuilder()
.setKey("key")
.setValue(AnyValue.newBuilder().setDoubleValue(100.3).build())
.build());
}

@Test
public void toProtoAttribute_DoubleArray() {
assertThat(
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100.3, 200.5)))
.isEqualTo(
KeyValue.newBuilder()
.setKey("key")
.setDoubleValue(100.3)
.setType(ValueType.DOUBLE)
.setValue(
AnyValue.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(AnyValue.newBuilder().setDoubleValue(100.3).build())
.addValues(AnyValue.newBuilder().setDoubleValue(200.5).build())
.build())
.build())
.build());
}

Expand Down
Loading

0 comments on commit 74f52dd

Please sign in to comment.