Skip to content

Commit 67ea9b9

Browse files
committed
Complex Attributes - incubating implementation
1 parent 578f82b commit 67ea9b9

File tree

9 files changed

+58
-4
lines changed

9 files changed

+58
-4
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.api.incubator.common;
77

88
import io.opentelemetry.api.common.AttributeKey;
9+
import io.opentelemetry.api.common.Value;
910
import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl;
1011
import java.util.List;
1112
import javax.annotation.Nullable;
@@ -97,4 +98,9 @@ static ExtendedAttributeKey<List<Double>> doubleArrayKey(String key) {
9798
static ExtendedAttributeKey<ExtendedAttributes> extendedAttributesKey(String key) {
9899
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.EXTENDED_ATTRIBUTES);
99100
}
101+
102+
/** Returns a new ExtendedAttributeKey for {@link Value} valued attributes. */
103+
static ExtendedAttributeKey<Value<?>> valueKey(String key) {
104+
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.VALUE);
105+
}
100106
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ public enum ExtendedAttributeType {
2222
LONG_ARRAY,
2323
DOUBLE_ARRAY,
2424
// Extended types unique to ExtendedAttributes
25-
EXTENDED_ATTRIBUTES;
25+
EXTENDED_ATTRIBUTES,
26+
VALUE;
2627
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
* <p>"extended" refers an extended set of allowed value types compared to standard {@link
2020
* Attributes}. Notably, {@link ExtendedAttributes} values can be of type {@link
2121
* ExtendedAttributeType#EXTENDED_ATTRIBUTES}, allowing nested {@link ExtendedAttributes} of
22-
* arbitrary depth.
22+
* arbitrary depth, and {@link ExtendedAttributeType#VALUE}, allowing attributes backed by {@link
23+
* io.opentelemetry.api.common.Value}.
2324
*
2425
* <p>Where standard {@link Attributes} are accepted everyone that OpenTelemetry represents key /
2526
* value pairs, {@link ExtendedAttributes} are only accepted in select places, such as log records

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.longKey;
1515
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringArrayKey;
1616
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringKey;
17+
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.valueKey;
1718

1819
import io.opentelemetry.api.common.AttributeKey;
1920
import io.opentelemetry.api.common.Attributes;
21+
import io.opentelemetry.api.common.Value;
2022
import java.util.Arrays;
2123
import java.util.List;
2224
import java.util.function.Predicate;
@@ -97,6 +99,18 @@ default <T> ExtendedAttributesBuilder put(String key, ExtendedAttributes value)
9799
return put(ExtendedAttributeKey.extendedAttributesKey(key), value);
98100
}
99101

102+
/**
103+
* Puts a {@link Value} attribute into this.
104+
*
105+
* <p>Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and
106+
* pre-allocate your keys, if possible.
107+
*
108+
* @return this Builder
109+
*/
110+
default ExtendedAttributesBuilder put(String key, Value<?> value) {
111+
return put(valueKey(key), value);
112+
}
113+
100114
/**
101115
* Puts a String array attribute into this.
102116
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public static <T> AttributeKey<T> toAttributeKey(ExtendedAttributeKey<T> extende
139139
return InternalAttributeKeyImpl.create(
140140
extendedAttributeKey.getKey(), AttributeType.DOUBLE_ARRAY);
141141
case EXTENDED_ATTRIBUTES:
142+
case VALUE:
142143
return null;
143144
}
144145
throw new IllegalArgumentException(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ private static Stream<Arguments> attributeKeyArgs() {
8080
ExtendedAttributeKey.extendedAttributesKey("key"),
8181
"key",
8282
ExtendedAttributeType.EXTENDED_ATTRIBUTES,
83-
null));
83+
null),
84+
Arguments.of(
85+
ExtendedAttributeKey.valueKey("key"), "key", ExtendedAttributeType.VALUE, null));
8486
}
8587
}

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

Lines changed: 20 additions & 1 deletion
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;
@@ -116,7 +117,10 @@ void asAttributes(ExtendedAttributes extendedAttributes, Map<String, Object> exp
116117
});
117118

118119
long expectedSize =
119-
expectedMap.values().stream().filter(value -> !(value instanceof Map)).count();
120+
expectedMap.values().stream()
121+
.filter(value -> !(value instanceof Map))
122+
.filter(value -> !(value instanceof Value))
123+
.count();
120124
assertThat(attributes.size()).isEqualTo(expectedSize);
121125
}
122126

@@ -212,6 +216,9 @@ private static Stream<Arguments> attributesArgs() {
212216
ImmutableMap.builder()
213217
.put("key", ImmutableMap.builder().put("child", "value").build())
214218
.build()),
219+
Arguments.of(
220+
ExtendedAttributes.builder().put("key", Value.of("value")).build(),
221+
ImmutableMap.builder().put("key", Value.of("value")).build()),
215222
Arguments.of(
216223
ExtendedAttributes.builder()
217224
.put(ExtendedAttributeKey.stringKey("key"), "value")
@@ -255,6 +262,11 @@ private static Stream<Arguments> attributesArgs() {
255262
ImmutableMap.builder()
256263
.put("key", ImmutableMap.builder().put("child", "value").build())
257264
.build()),
265+
Arguments.of(
266+
ExtendedAttributes.builder()
267+
.put(ExtendedAttributeKey.valueKey("key"), Value.of("value"))
268+
.build(),
269+
ImmutableMap.builder().put("key", Value.of("value")).build()),
258270
// Multiple entries
259271
Arguments.of(
260272
ExtendedAttributes.builder()
@@ -268,6 +280,7 @@ private static Stream<Arguments> attributesArgs() {
268280
.put("key8", 1L, 2L)
269281
.put("key9", 1.1, 2.2)
270282
.put("key10", ExtendedAttributes.builder().put("child", "value").build())
283+
.put("key11", Value.of("value"))
271284
.build(),
272285
ImmutableMap.builder()
273286
.put("key1", "value1")
@@ -280,6 +293,7 @@ private static Stream<Arguments> attributesArgs() {
280293
.put("key8", Arrays.asList(1L, 2L))
281294
.put("key9", Arrays.asList(1.1, 2.2))
282295
.put("key10", ImmutableMap.builder().put("child", "value").build())
296+
.put("key11", Value.of("value"))
283297
.build()));
284298
}
285299

@@ -316,6 +330,8 @@ private static ExtendedAttributeKey<?> getKey(String key, Object value) {
316330
return ExtendedAttributeKey.doubleArrayKey(key);
317331
case EXTENDED_ATTRIBUTES:
318332
return ExtendedAttributeKey.extendedAttributesKey(key);
333+
case VALUE:
334+
return ExtendedAttributeKey.valueKey(key);
319335
}
320336
throw new IllegalArgumentException();
321337
}
@@ -355,6 +371,9 @@ private static ExtendedAttributeType getType(Object value) {
355371
if ((value instanceof Map)) {
356372
return ExtendedAttributeType.EXTENDED_ATTRIBUTES;
357373
}
374+
if (value instanceof Value<?>) {
375+
return ExtendedAttributeType.VALUE;
376+
}
358377
throw new IllegalArgumentException("Unrecognized value type: " + value);
359378
}
360379
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.otlp;
77

8+
import io.opentelemetry.api.common.Value;
89
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
910
import io.opentelemetry.api.incubator.common.ExtendedAttributeType;
1011
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
@@ -174,6 +175,9 @@ public int getBinarySerializedSize(
174175
(ExtendedAttributes) value,
175176
ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE,
176177
context);
178+
case VALUE:
179+
return AnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize(
180+
(Value<?>) value, context);
177181
}
178182
// Error prone ensures the switch statement is complete, otherwise only can happen with
179183
// unaligned versions which are not supported.
@@ -220,6 +224,9 @@ public void writeTo(
220224
ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE,
221225
context);
222226
return;
227+
case VALUE:
228+
AnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Value<?>) value, context);
229+
return;
223230
}
224231
// Error prone ensures the switch statement is complete, otherwise only can happen with
225232
// unaligned versions which are not supported.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.otlp;
77

8+
import io.opentelemetry.api.common.Value;
89
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
910
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
1011
import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl;
@@ -116,6 +117,8 @@ private static KeyValueMarshaler create(ExtendedAttributeKey<?> attributeKey, Ob
116117
new KeyValueListAnyValueMarshaler(
117118
new KeyValueListAnyValueMarshaler.KeyValueListMarshaler(
118119
createForExtendedAttributes((ExtendedAttributes) value))));
120+
case VALUE:
121+
return new KeyValueMarshaler(keyUtf8, AnyValueMarshaler.create((Value<?>) value));
119122
}
120123
// Error prone ensures the switch statement is complete, otherwise only can happen with
121124
// unaligned versions which are not supported.

0 commit comments

Comments
 (0)