Skip to content

Commit 74f52dd

Browse files
authored
Update the OTLP proto to the latest released version (0.4.0) (#1390)
1 parent ee0d438 commit 74f52dd

File tree

7 files changed

+320
-133
lines changed

7 files changed

+320
-133
lines changed

exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,92 @@
1717
package io.opentelemetry.exporters.otlp;
1818

1919
import io.opentelemetry.common.AttributeValue;
20-
import io.opentelemetry.proto.common.v1.AttributeKeyValue;
21-
import io.opentelemetry.proto.common.v1.AttributeKeyValue.ValueType;
20+
import io.opentelemetry.proto.common.v1.AnyValue;
21+
import io.opentelemetry.proto.common.v1.ArrayValue;
2222
import io.opentelemetry.proto.common.v1.InstrumentationLibrary;
23+
import io.opentelemetry.proto.common.v1.KeyValue;
2324
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
2425

2526
final class CommonAdapter {
26-
static AttributeKeyValue toProtoAttribute(String key, AttributeValue attributeValue) {
27-
AttributeKeyValue.Builder builder = AttributeKeyValue.newBuilder().setKey(key);
27+
static KeyValue toProtoAttribute(String key, AttributeValue attributeValue) {
28+
KeyValue.Builder builder = KeyValue.newBuilder().setKey(key);
2829
switch (attributeValue.getType()) {
2930
case STRING:
3031
return builder
31-
.setType(ValueType.STRING)
32-
.setStringValue(attributeValue.getStringValue())
32+
.setValue(AnyValue.newBuilder().setStringValue(attributeValue.getStringValue()).build())
3333
.build();
3434
case BOOLEAN:
3535
return builder
36-
.setType(ValueType.BOOL)
37-
.setBoolValue(attributeValue.getBooleanValue())
36+
.setValue(AnyValue.newBuilder().setBoolValue(attributeValue.getBooleanValue()).build())
3837
.build();
3938
case LONG:
40-
return builder.setType(ValueType.INT).setIntValue(attributeValue.getLongValue()).build();
39+
return builder
40+
.setValue(AnyValue.newBuilder().setIntValue(attributeValue.getLongValue()).build())
41+
.build();
4142
case DOUBLE:
4243
return builder
43-
.setType(ValueType.DOUBLE)
44-
.setDoubleValue(attributeValue.getDoubleValue())
44+
.setValue(AnyValue.newBuilder().setDoubleValue(attributeValue.getDoubleValue()).build())
4545
.build();
4646
case BOOLEAN_ARRAY:
47+
return builder
48+
.setValue(
49+
AnyValue.newBuilder()
50+
.setArrayValue(makeBooleanArrayAnyValue(attributeValue))
51+
.build())
52+
.build();
4753
case LONG_ARRAY:
54+
return builder
55+
.setValue(
56+
AnyValue.newBuilder().setArrayValue(makeLongArrayAnyValue(attributeValue)).build())
57+
.build();
4858
case DOUBLE_ARRAY:
59+
return builder
60+
.setValue(
61+
AnyValue.newBuilder()
62+
.setArrayValue(makeDoubleArrayAnyValue(attributeValue))
63+
.build())
64+
.build();
4965
case STRING_ARRAY:
50-
return builder.setType(ValueType.UNRECOGNIZED).build();
66+
return builder
67+
.setValue(
68+
AnyValue.newBuilder()
69+
.setArrayValue(makeStringArrayAnyValue(attributeValue))
70+
.build())
71+
.build();
72+
}
73+
return builder.setValue(AnyValue.getDefaultInstance()).build();
74+
}
75+
76+
private static ArrayValue makeDoubleArrayAnyValue(AttributeValue attributeValue) {
77+
ArrayValue.Builder builder = ArrayValue.newBuilder();
78+
for (Double doubleValue : attributeValue.getDoubleArrayValue()) {
79+
builder.addValues(AnyValue.newBuilder().setDoubleValue(doubleValue).build());
80+
}
81+
return builder.build();
82+
}
83+
84+
private static ArrayValue makeLongArrayAnyValue(AttributeValue attributeValue) {
85+
ArrayValue.Builder builder = ArrayValue.newBuilder();
86+
for (Long intValue : attributeValue.getLongArrayValue()) {
87+
builder.addValues(AnyValue.newBuilder().setIntValue(intValue).build());
88+
}
89+
return builder.build();
90+
}
91+
92+
private static ArrayValue makeStringArrayAnyValue(AttributeValue attributeValue) {
93+
ArrayValue.Builder builder = ArrayValue.newBuilder();
94+
for (String string : attributeValue.getStringArrayValue()) {
95+
builder.addValues(AnyValue.newBuilder().setStringValue(string).build());
96+
}
97+
return builder.build();
98+
}
99+
100+
private static ArrayValue makeBooleanArrayAnyValue(AttributeValue attributeValue) {
101+
ArrayValue.Builder builder = ArrayValue.newBuilder();
102+
for (Boolean bool : attributeValue.getBooleanArrayValue()) {
103+
builder.addValues(AnyValue.newBuilder().setBoolValue(bool).build());
51104
}
52-
return builder.setType(ValueType.UNRECOGNIZED).build();
105+
return builder.build();
53106
}
54107

55108
static InstrumentationLibrary toProtoInstrumentationLibrary(

exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/MetricAdapter.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.opentelemetry.proto.metrics.v1.Int64DataPoint;
2525
import io.opentelemetry.proto.metrics.v1.Metric;
2626
import io.opentelemetry.proto.metrics.v1.MetricDescriptor;
27+
import io.opentelemetry.proto.metrics.v1.MetricDescriptor.Temporality;
2728
import io.opentelemetry.proto.metrics.v1.MetricDescriptor.Type;
2829
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
2930
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
@@ -100,25 +101,26 @@ static Metric toProtoMetric(MetricData metricData) {
100101
if (metricData.getPoints().isEmpty()) {
101102
return builder.build();
102103
}
103-
104104
switch (builder.getMetricDescriptor().getType()) {
105-
case UNSPECIFIED:
106105
case UNRECOGNIZED:
106+
case INVALID_TYPE:
107107
break;
108-
case GAUGE_INT64:
109-
case COUNTER_INT64:
110-
builder.addAllInt64DataPoints(toInt64DataPoints(metricData.getPoints()));
108+
case MONOTONIC_INT64:
109+
case INT64:
110+
builder.addAllInt64DataPoints(
111+
toInt64DataPoints(metricData.getPoints(), metricData.getDescriptor()));
111112
break;
112-
case GAUGE_DOUBLE:
113-
case COUNTER_DOUBLE:
114-
builder.addAllDoubleDataPoints(toDoubleDataPoints(metricData.getPoints()));
113+
case MONOTONIC_DOUBLE:
114+
case DOUBLE:
115+
builder.addAllDoubleDataPoints(
116+
toDoubleDataPoints(metricData.getPoints(), metricData.getDescriptor()));
115117
break;
116-
case GAUGE_HISTOGRAM:
117-
case CUMULATIVE_HISTOGRAM:
118+
case HISTOGRAM:
118119
// TODO: Add support for histogram.
119120
break;
120121
case SUMMARY:
121-
builder.addAllSummaryDataPoints(toSummaryDataPoints(metricData.getPoints()));
122+
builder.addAllSummaryDataPoints(
123+
toSummaryDataPoints(metricData.getPoints(), metricData.getDescriptor()));
122124
break;
123125
}
124126
return builder.build();
@@ -130,11 +132,25 @@ static MetricDescriptor toProtoMetricDescriptor(Descriptor descriptor) {
130132
.setDescription(descriptor.getDescription())
131133
.setUnit(descriptor.getUnit())
132134
.setType(toProtoMetricDescriptorType(descriptor.getType()))
133-
.addAllLabels(toProtoLabels(descriptor.getConstantLabels()))
135+
.setTemporality(mapToTemporality(descriptor))
134136
.build();
135137
}
136138

137-
static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
139+
private static Temporality mapToTemporality(Descriptor descriptor) {
140+
switch (descriptor.getType()) {
141+
case NON_MONOTONIC_LONG:
142+
case NON_MONOTONIC_DOUBLE:
143+
case MONOTONIC_LONG:
144+
case MONOTONIC_DOUBLE:
145+
return Temporality.CUMULATIVE;
146+
case SUMMARY:
147+
return Temporality.DELTA;
148+
}
149+
return Temporality.UNRECOGNIZED;
150+
}
151+
152+
static Collection<Int64DataPoint> toInt64DataPoints(
153+
Collection<Point> points, Descriptor descriptor) {
138154
List<Int64DataPoint> result = new ArrayList<>(points.size());
139155
for (Point point : points) {
140156
LongPoint longPoint = (LongPoint) point;
@@ -144,6 +160,9 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
144160
.setTimeUnixNano(longPoint.getEpochNanos())
145161
.setValue(longPoint.getValue());
146162
// Not calling directly addAllLabels because that generates couple of unnecessary allocations.
163+
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
164+
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
165+
}
147166
Collection<StringKeyValue> labels = toProtoLabels(longPoint.getLabels());
148167
if (!labels.isEmpty()) {
149168
builder.addAllLabels(labels);
@@ -153,7 +172,8 @@ static Collection<Int64DataPoint> toInt64DataPoints(Collection<Point> points) {
153172
return result;
154173
}
155174

156-
static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points) {
175+
static Collection<DoubleDataPoint> toDoubleDataPoints(
176+
Collection<Point> points, Descriptor descriptor) {
157177
List<DoubleDataPoint> result = new ArrayList<>(points.size());
158178
for (Point point : points) {
159179
DoublePoint doublePoint = (DoublePoint) point;
@@ -163,6 +183,9 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
163183
.setTimeUnixNano(doublePoint.getEpochNanos())
164184
.setValue(doublePoint.getValue());
165185
// Not calling directly addAllLabels because that generates couple of unnecessary allocations.
186+
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
187+
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
188+
}
166189
Collection<StringKeyValue> labels = toProtoLabels(doublePoint.getLabels());
167190
if (!labels.isEmpty()) {
168191
builder.addAllLabels(labels);
@@ -172,7 +195,8 @@ static Collection<DoubleDataPoint> toDoubleDataPoints(Collection<Point> points)
172195
return result;
173196
}
174197

175-
static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points) {
198+
static Collection<SummaryDataPoint> toSummaryDataPoints(
199+
Collection<Point> points, Descriptor descriptor) {
176200
List<SummaryDataPoint> result = new ArrayList<>(points.size());
177201
for (Point point : points) {
178202
SummaryPoint summaryPoint = (SummaryPoint) point;
@@ -184,6 +208,9 @@ static Collection<SummaryDataPoint> toSummaryDataPoints(Collection<Point> points
184208
.setSum(summaryPoint.getSum());
185209
// Not calling directly addAllLabels because that generates couple of unnecessary allocations
186210
// if empty list.
211+
if (descriptor.getConstantLabels() != null && !descriptor.getConstantLabels().isEmpty()) {
212+
builder.addAllLabels(toProtoLabels(descriptor.getConstantLabels()));
213+
}
187214
Collection<StringKeyValue> labels = toProtoLabels(summaryPoint.getLabels());
188215
if (!labels.isEmpty()) {
189216
builder.addAllLabels(labels);
@@ -221,17 +248,17 @@ static List<ValueAtPercentile> toProtoValueAtPercentiles(
221248
static MetricDescriptor.Type toProtoMetricDescriptorType(Descriptor.Type descriptorType) {
222249
switch (descriptorType) {
223250
case NON_MONOTONIC_LONG:
224-
return Type.GAUGE_INT64;
251+
return Type.INT64;
225252
case NON_MONOTONIC_DOUBLE:
226-
return Type.GAUGE_DOUBLE;
253+
return Type.DOUBLE;
227254
case MONOTONIC_LONG:
228-
return Type.COUNTER_INT64;
255+
return Type.MONOTONIC_INT64;
229256
case MONOTONIC_DOUBLE:
230-
return Type.COUNTER_DOUBLE;
257+
return Type.MONOTONIC_DOUBLE;
231258
case SUMMARY:
232259
return Type.SUMMARY;
233260
}
234-
return Type.UNSPECIFIED;
261+
return Type.UNRECOGNIZED;
235262
}
236263

237264
@SuppressWarnings("MixedMutabilityReturnType")

exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import io.opentelemetry.common.AttributeValue;
22-
import io.opentelemetry.proto.common.v1.AttributeKeyValue;
23-
import io.opentelemetry.proto.common.v1.AttributeKeyValue.ValueType;
22+
import io.opentelemetry.proto.common.v1.AnyValue;
23+
import io.opentelemetry.proto.common.v1.ArrayValue;
2424
import io.opentelemetry.proto.common.v1.InstrumentationLibrary;
25+
import io.opentelemetry.proto.common.v1.KeyValue;
2526
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
2627
import org.junit.Test;
2728
import org.junit.runner.RunWith;
@@ -34,43 +35,112 @@ public class CommonAdapterTest {
3435
public void toProtoAttribute_Bool() {
3536
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.booleanAttributeValue(true)))
3637
.isEqualTo(
37-
AttributeKeyValue.newBuilder()
38+
KeyValue.newBuilder()
3839
.setKey("key")
39-
.setBoolValue(true)
40-
.setType(ValueType.BOOL)
40+
.setValue(AnyValue.newBuilder().setBoolValue(true).build())
41+
.build());
42+
}
43+
44+
@Test
45+
public void toProtoAttribute_BoolArray() {
46+
assertThat(
47+
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(true, false)))
48+
.isEqualTo(
49+
KeyValue.newBuilder()
50+
.setKey("key")
51+
.setValue(
52+
AnyValue.newBuilder()
53+
.setArrayValue(
54+
ArrayValue.newBuilder()
55+
.addValues(AnyValue.newBuilder().setBoolValue(true).build())
56+
.addValues(AnyValue.newBuilder().setBoolValue(false).build())
57+
.build())
58+
.build())
4159
.build());
4260
}
4361

4462
@Test
4563
public void toProtoAttribute_String() {
4664
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.stringAttributeValue("string")))
4765
.isEqualTo(
48-
AttributeKeyValue.newBuilder()
66+
KeyValue.newBuilder()
67+
.setKey("key")
68+
.setValue(AnyValue.newBuilder().setStringValue("string").build())
69+
.build());
70+
}
71+
72+
@Test
73+
public void toProtoAttribute_StringArray() {
74+
assertThat(
75+
CommonAdapter.toProtoAttribute(
76+
"key", AttributeValue.arrayAttributeValue("string1", "string2")))
77+
.isEqualTo(
78+
KeyValue.newBuilder()
4979
.setKey("key")
50-
.setStringValue("string")
51-
.setType(ValueType.STRING)
80+
.setValue(
81+
AnyValue.newBuilder()
82+
.setArrayValue(
83+
ArrayValue.newBuilder()
84+
.addValues(AnyValue.newBuilder().setStringValue("string1").build())
85+
.addValues(AnyValue.newBuilder().setStringValue("string2").build())
86+
.build())
87+
.build())
5288
.build());
5389
}
5490

5591
@Test
5692
public void toProtoAttribute_Int() {
5793
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.longAttributeValue(100)))
5894
.isEqualTo(
59-
AttributeKeyValue.newBuilder()
95+
KeyValue.newBuilder()
6096
.setKey("key")
61-
.setIntValue(100)
62-
.setType(ValueType.INT)
97+
.setValue(AnyValue.newBuilder().setIntValue(100).build())
98+
.build());
99+
}
100+
101+
@Test
102+
public void toProtoAttribute_IntArray() {
103+
assertThat(
104+
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100L, 200L)))
105+
.isEqualTo(
106+
KeyValue.newBuilder()
107+
.setKey("key")
108+
.setValue(
109+
AnyValue.newBuilder()
110+
.setArrayValue(
111+
ArrayValue.newBuilder()
112+
.addValues(AnyValue.newBuilder().setIntValue(100).build())
113+
.addValues(AnyValue.newBuilder().setIntValue(200).build())
114+
.build())
115+
.build())
63116
.build());
64117
}
65118

66119
@Test
67120
public void toProtoAttribute_Double() {
68121
assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.doubleAttributeValue(100.3)))
69122
.isEqualTo(
70-
AttributeKeyValue.newBuilder()
123+
KeyValue.newBuilder()
124+
.setKey("key")
125+
.setValue(AnyValue.newBuilder().setDoubleValue(100.3).build())
126+
.build());
127+
}
128+
129+
@Test
130+
public void toProtoAttribute_DoubleArray() {
131+
assertThat(
132+
CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100.3, 200.5)))
133+
.isEqualTo(
134+
KeyValue.newBuilder()
71135
.setKey("key")
72-
.setDoubleValue(100.3)
73-
.setType(ValueType.DOUBLE)
136+
.setValue(
137+
AnyValue.newBuilder()
138+
.setArrayValue(
139+
ArrayValue.newBuilder()
140+
.addValues(AnyValue.newBuilder().setDoubleValue(100.3).build())
141+
.addValues(AnyValue.newBuilder().setDoubleValue(200.5).build())
142+
.build())
143+
.build())
74144
.build());
75145
}
76146

0 commit comments

Comments
 (0)