Skip to content

Commit e2bf72d

Browse files
committed
Implementation
1 parent ab1d8c6 commit e2bf72d

20 files changed

Lines changed: 339 additions & 6 deletions

File tree

api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ default AttributesBuilder put(String key, boolean... value) {
164164
return put(booleanArrayKey(key), toList(value));
165165
}
166166

167+
/** Puts a byte array attribute into this. */
168+
default AttributesBuilder put(String key, byte[] value) {
169+
if (value == null) {
170+
return this;
171+
}
172+
return put(AttributeKey.byteArrayKey(key), value);
173+
}
174+
175+
/** Puts an {@link Attributes} attribute into this. */
176+
default AttributesBuilder put(String key, Attributes value) {
177+
if (value == null) {
178+
return this;
179+
}
180+
return put(AttributeKey.mapKey(key), value);
181+
}
182+
167183
/**
168184
* Puts all the provided attributes into this Builder.
169185
*

api/all/src/main/java/io/opentelemetry/api/common/Value.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ static Value<List<KeyValue>> of(Map<String, Value<?>> value) {
8484
return KeyValueList.createFromMap(value);
8585
}
8686

87-
static Value<List<KeyValue>> of(Attributes attributes) {
88-
// TODO
89-
}
90-
9187
/** Returns the type of this {@link Value}. Useful for building switch statements. */
9288
ValueType getType();
9389

api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public enum ExtendedAttributeType {
2121
BOOLEAN_ARRAY,
2222
LONG_ARRAY,
2323
DOUBLE_ARRAY,
24+
BYTE_ARRAY,
25+
VALUE_ARRAY,
26+
MAP,
2427
// Extended types unique to ExtendedAttributes
2528
EXTENDED_ATTRIBUTES;
2629
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ default <T> ExtendedAttributesBuilder put(String key, ExtendedAttributes value)
9797
return put(ExtendedAttributeKey.extendedAttributesKey(key), value);
9898
}
9999

100+
/** Puts a byte array attribute into this. */
101+
default ExtendedAttributesBuilder put(String key, byte[] value) {
102+
if (value == null) {
103+
return this;
104+
}
105+
return put(ExtendedAttributeKey.fromAttributeKey(AttributeKey.byteArrayKey(key)), value);
106+
}
107+
108+
/** Puts an {@link Attributes} attribute into this. */
109+
default ExtendedAttributesBuilder put(String key, Attributes value) {
110+
if (value == null) {
111+
return this;
112+
}
113+
return put(ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey(key)), value);
114+
}
115+
100116
/**
101117
* Puts a String array attribute into this.
102118
*

api/incubator/src/main/java/io/opentelemetry/api/incubator/internal/InternalExtendedAttributeKeyImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public static <T> AttributeKey<T> toAttributeKey(ExtendedAttributeKey<T> extende
138138
case DOUBLE_ARRAY:
139139
return InternalAttributeKeyImpl.create(
140140
extendedAttributeKey.getKey(), AttributeType.DOUBLE_ARRAY);
141+
case BYTE_ARRAY:
142+
return InternalAttributeKeyImpl.create(
143+
extendedAttributeKey.getKey(), AttributeType.BYTE_ARRAY);
144+
case VALUE_ARRAY:
145+
return InternalAttributeKeyImpl.create(
146+
extendedAttributeKey.getKey(), AttributeType.VALUE_ARRAY);
147+
case MAP:
148+
return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.MAP);
141149
case EXTENDED_ATTRIBUTES:
142150
return null;
143151
}
@@ -172,6 +180,15 @@ public static <T> ExtendedAttributeKey<T> toExtendedAttributeKey(AttributeKey<T>
172180
case DOUBLE_ARRAY:
173181
return InternalExtendedAttributeKeyImpl.create(
174182
attributeKey.getKey(), ExtendedAttributeType.DOUBLE_ARRAY);
183+
case BYTE_ARRAY:
184+
return InternalExtendedAttributeKeyImpl.create(
185+
attributeKey.getKey(), ExtendedAttributeType.BYTE_ARRAY);
186+
case VALUE_ARRAY:
187+
return InternalExtendedAttributeKeyImpl.create(
188+
attributeKey.getKey(), ExtendedAttributeType.VALUE_ARRAY);
189+
case MAP:
190+
return InternalExtendedAttributeKeyImpl.create(
191+
attributeKey.getKey(), ExtendedAttributeType.MAP);
175192
}
176193
throw new IllegalArgumentException("Unrecognized attributeKey type: " + attributeKey.getType());
177194
}

api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKeyTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ private static Stream<Arguments> attributeKeyArgs() {
7676
"key",
7777
ExtendedAttributeType.DOUBLE_ARRAY,
7878
AttributeKey.doubleArrayKey("key")),
79+
Arguments.of(
80+
ExtendedAttributeKey.fromAttributeKey(AttributeKey.byteArrayKey("key")),
81+
"key",
82+
ExtendedAttributeType.BYTE_ARRAY,
83+
AttributeKey.byteArrayKey("key")),
84+
Arguments.of(
85+
ExtendedAttributeKey.fromAttributeKey(AttributeKey.valueArrayKey("key")),
86+
"key",
87+
ExtendedAttributeType.VALUE_ARRAY,
88+
AttributeKey.valueArrayKey("key")),
89+
Arguments.of(
90+
ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey("key")),
91+
"key",
92+
ExtendedAttributeType.MAP,
93+
AttributeKey.mapKey("key")),
7994
Arguments.of(
8095
ExtendedAttributeKey.extendedAttributesKey("key"),
8196
"key",

api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.common.collect.ImmutableMap;
1111
import io.opentelemetry.api.common.AttributeKey;
1212
import io.opentelemetry.api.common.Attributes;
13+
import io.opentelemetry.api.common.Value;
1314
import java.util.Arrays;
1415
import java.util.Collections;
1516
import java.util.HashMap;
@@ -246,6 +247,27 @@ private static Stream<Arguments> attributesArgs() {
246247
.put(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.1, 2.2))
247248
.build(),
248249
ImmutableMap.builder().put("key", Arrays.asList(1.1, 2.2)).build()),
250+
Arguments.of(
251+
ExtendedAttributes.builder().put("key", new byte[] {1, 2}).build(),
252+
ImmutableMap.builder().put("key", new byte[] {1, 2}).build()),
253+
Arguments.of(
254+
ExtendedAttributes.builder()
255+
.put(
256+
ExtendedAttributeKey.fromAttributeKey(AttributeKey.valueArrayKey("key")),
257+
Arrays.asList(Value.of("one"), Value.of(2L)))
258+
.build(),
259+
ImmutableMap.builder()
260+
.put("key", Arrays.asList(Value.of("one"), Value.of(2L)))
261+
.build()),
262+
Arguments.of(
263+
ExtendedAttributes.builder()
264+
.put(
265+
ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey("key")),
266+
Attributes.builder().put("child", "value").build())
267+
.build(),
268+
ImmutableMap.builder()
269+
.put("key", Attributes.builder().put("child", "value").build())
270+
.build()),
249271
Arguments.of(
250272
ExtendedAttributes.builder()
251273
.put(
@@ -314,6 +336,12 @@ private static ExtendedAttributeKey<?> getKey(String key, Object value) {
314336
return ExtendedAttributeKey.longArrayKey(key);
315337
case DOUBLE_ARRAY:
316338
return ExtendedAttributeKey.doubleArrayKey(key);
339+
case BYTE_ARRAY:
340+
return ExtendedAttributeKey.fromAttributeKey(AttributeKey.byteArrayKey(key));
341+
case VALUE_ARRAY:
342+
return ExtendedAttributeKey.fromAttributeKey(AttributeKey.valueArrayKey(key));
343+
case MAP:
344+
return ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey(key));
317345
case EXTENDED_ATTRIBUTES:
318346
return ExtendedAttributeKey.extendedAttributesKey(key);
319347
}
@@ -334,11 +362,17 @@ private static ExtendedAttributeType getType(Object value) {
334362
if ((value instanceof Double) || (value instanceof Float)) {
335363
return ExtendedAttributeType.DOUBLE;
336364
}
365+
if (value instanceof byte[]) {
366+
return ExtendedAttributeType.BYTE_ARRAY;
367+
}
337368
if (value instanceof List) {
338369
List<Object> list = (List<Object>) value;
339370
if (list.isEmpty()) {
340371
throw new IllegalArgumentException("Empty list");
341372
}
373+
if (list.get(0) instanceof Value) {
374+
return ExtendedAttributeType.VALUE_ARRAY;
375+
}
342376
if (list.get(0) instanceof String) {
343377
return ExtendedAttributeType.STRING_ARRAY;
344378
}
@@ -352,6 +386,9 @@ private static ExtendedAttributeType getType(Object value) {
352386
return ExtendedAttributeType.DOUBLE_ARRAY;
353387
}
354388
}
389+
if (value instanceof Attributes) {
390+
return ExtendedAttributeType.MAP;
391+
}
355392
if ((value instanceof Map)) {
356393
return ExtendedAttributeType.EXTENDED_ATTRIBUTES;
357394
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
Comparing source compatibility of opentelemetry-api-1.56.0-SNAPSHOT.jar against opentelemetry-api-1.55.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.common.AttributeKey (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
GENERIC TEMPLATES: === T:java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.common.AttributeKey byteArrayKey(java.lang.String)
6+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.common.AttributeKey<io.opentelemetry.api.common.Attributes> mapKey(java.lang.String)
7+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.common.AttributeKey<java.util.List<io.opentelemetry.api.common.Value<?>>> valueArrayKey(java.lang.String)
8+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.common.AttributesBuilder (not serializable)
9+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
10+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.AttributesBuilder put(java.lang.String, byte[])
11+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.AttributesBuilder put(java.lang.String, io.opentelemetry.api.common.Attributes)
12+
*** MODIFIED ENUM: PUBLIC FINAL io.opentelemetry.api.common.AttributeType (compatible)
13+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
14+
+++ NEW FIELD: PUBLIC(+) STATIC(+) FINAL(+) io.opentelemetry.api.common.AttributeType BYTE_ARRAY
15+
+++ NEW FIELD: PUBLIC(+) STATIC(+) FINAL(+) io.opentelemetry.api.common.AttributeType VALUE_ARRAY
16+
+++ NEW FIELD: PUBLIC(+) STATIC(+) FINAL(+) io.opentelemetry.api.common.AttributeType MAP
17+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.common.Value (not serializable)
18+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
19+
GENERIC TEMPLATES: === T:java.lang.Object
20+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.common.Value<java.util.List<io.opentelemetry.api.common.KeyValue>> of(io.opentelemetry.api.common.Attributes)

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AttributeKeyValueStatelessMarshaler.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import io.opentelemetry.api.common.AttributeKey;
99
import io.opentelemetry.api.common.AttributeType;
10+
import io.opentelemetry.api.common.Attributes;
11+
import io.opentelemetry.api.common.Value;
1012
import io.opentelemetry.api.internal.InternalAttributeKeyImpl;
13+
import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
1114
import io.opentelemetry.exporter.internal.marshal.MarshalerContext;
1215
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
1316
import io.opentelemetry.exporter.internal.marshal.Serializer;
@@ -100,6 +103,21 @@ public int getBinarySerializedSize(
100103
(List<Object>) value,
101104
AttributeArrayAnyValueStatelessMarshaler.INSTANCE,
102105
context);
106+
case BYTE_ARRAY:
107+
return AnyValue.BYTES_VALUE.getTagSize()
108+
+ CodedOutputStream.computeByteArraySizeNoTag((byte[]) value);
109+
case VALUE_ARRAY:
110+
return StatelessMarshalerUtil.sizeMessageWithContext(
111+
AnyValue.ARRAY_VALUE,
112+
(List<Value<?>>) value,
113+
ArrayAnyValueStatelessMarshaler.INSTANCE,
114+
context);
115+
case MAP:
116+
return StatelessMarshalerUtil.sizeMessageWithContext(
117+
AnyValue.KVLIST_VALUE,
118+
(Attributes) value,
119+
MapAnyValueStatelessMarshaler.INSTANCE,
120+
context);
103121
}
104122
// Error prone ensures the switch statement is complete, otherwise only can happen with
105123
// unaligned versions which are not supported.
@@ -136,6 +154,23 @@ public void writeTo(
136154
AttributeArrayAnyValueStatelessMarshaler.INSTANCE,
137155
context);
138156
return;
157+
case BYTE_ARRAY:
158+
output.writeBytes(AnyValue.BYTES_VALUE, (byte[]) value);
159+
return;
160+
case VALUE_ARRAY:
161+
output.serializeMessageWithContext(
162+
AnyValue.ARRAY_VALUE,
163+
(List<Value<?>>) value,
164+
ArrayAnyValueStatelessMarshaler.INSTANCE,
165+
context);
166+
return;
167+
case MAP:
168+
output.serializeMessageWithContext(
169+
AnyValue.KVLIST_VALUE,
170+
(Attributes) value,
171+
MapAnyValueStatelessMarshaler.INSTANCE,
172+
context);
173+
return;
139174
}
140175
// Error prone ensures the switch statement is complete, otherwise only can happen with
141176
// unaligned versions which are not supported.

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/BytesAnyValueMarshaler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ static MarshalerWithSize create(ByteBuffer value) {
2727
return new BytesAnyValueMarshaler(bytes);
2828
}
2929

30+
static MarshalerWithSize create(byte[] value) {
31+
return new BytesAnyValueMarshaler(value);
32+
}
33+
3034
@Override
3135
public void writeTo(Serializer output) throws IOException {
3236
// Do not call serialize* method because we always have to write the message tag even if the

0 commit comments

Comments
 (0)