From 23ad3331d7789676c9baffc249e66edd71c4b80b Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 16 Sep 2025 10:20:11 -0700 Subject: [PATCH 1/2] POC: Complex attributes (Option A) --- .../io/opentelemetry/api/common/AttributeKey.java | 12 ++++++++++++ .../io/opentelemetry/api/common/AttributeType.java | 5 ++++- .../main/java/io/opentelemetry/api/common/Value.java | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java index 7d012aa14ca..ca47e8c3270 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java @@ -70,4 +70,16 @@ static AttributeKey> longArrayKey(String key) { static AttributeKey> doubleArrayKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY); } + + static AttributeKey byteArrayKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.BYTE_ARRAY); + } + + static AttributeKey>> valueArrayKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.VALUE_ARRAY); + } + + static AttributeKey mapKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.MAP); + } } diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java index 1c51e36d644..eb4d2739764 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java @@ -17,5 +17,8 @@ public enum AttributeType { STRING_ARRAY, BOOLEAN_ARRAY, LONG_ARRAY, - DOUBLE_ARRAY + DOUBLE_ARRAY, + BYTE_ARRAY, + VALUE_ARRAY, + MAP } diff --git a/api/all/src/main/java/io/opentelemetry/api/common/Value.java b/api/all/src/main/java/io/opentelemetry/api/common/Value.java index a29be801e27..084fcb19479 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/Value.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/Value.java @@ -84,6 +84,10 @@ static Value> of(Map> value) { return KeyValueList.createFromMap(value); } + static Value> of(Attributes attributes) { + // TODO + } + /** Returns the type of this {@link Value}. Useful for building switch statements. */ ValueType getType(); From a5e23ab82f3bcad9a132667b091a16ca48272caf Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 4 Nov 2025 15:41:49 -0800 Subject: [PATCH 2/2] simplify --- .../api/common/AttributeKey.java | 19 ++++---- .../api/common/AttributeType.java | 6 +-- .../api/common/AttributesBuilder.java | 48 +++++++++++++++++++ .../io/opentelemetry/api/common/Value.java | 4 -- 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java index ca47e8c3270..1f9022928fb 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java @@ -51,6 +51,11 @@ static AttributeKey doubleKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE); } + /** Returns a new AttributeKey for {@link Attributes} (Map) valued attributes. */ + static AttributeKey mapKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.MAP); + } + /** Returns a new AttributeKey for List<String> valued attributes. */ static AttributeKey> stringArrayKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.STRING_ARRAY); @@ -71,15 +76,13 @@ static AttributeKey> doubleArrayKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY); } - static AttributeKey byteArrayKey(String key) { - return InternalAttributeKeyImpl.create(key, AttributeType.BYTE_ARRAY); + /** Returns a new AttributeKey for List<Attributes> (List of Maps) valued attributes. */ + static AttributeKey> mapArrayKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.MAP_ARRAY); } - static AttributeKey>> valueArrayKey(String key) { - return InternalAttributeKeyImpl.create(key, AttributeType.VALUE_ARRAY); - } - - static AttributeKey mapKey(String key) { - return InternalAttributeKeyImpl.create(key, AttributeType.MAP); + /** Returns a new AttributeKey for generic {@link Value} valued attributes. */ + static AttributeKey> valueKey(String key) { + return InternalAttributeKeyImpl.create(key, AttributeType.VALUE); } } diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java index eb4d2739764..679153b3ca9 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributeType.java @@ -14,11 +14,11 @@ public enum AttributeType { BOOLEAN, LONG, DOUBLE, + MAP, STRING_ARRAY, BOOLEAN_ARRAY, LONG_ARRAY, DOUBLE_ARRAY, - BYTE_ARRAY, - VALUE_ARRAY, - MAP + MAP_ARRAY, + VALUE } diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java index 6623d470137..906f7e584d6 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributesBuilder.java @@ -12,8 +12,11 @@ import static io.opentelemetry.api.common.AttributeKey.doubleKey; import static io.opentelemetry.api.common.AttributeKey.longArrayKey; import static io.opentelemetry.api.common.AttributeKey.longKey; +import static io.opentelemetry.api.common.AttributeKey.mapArrayKey; +import static io.opentelemetry.api.common.AttributeKey.mapKey; import static io.opentelemetry.api.common.AttributeKey.stringArrayKey; import static io.opentelemetry.api.common.AttributeKey.stringKey; +import static io.opentelemetry.api.common.AttributeKey.valueKey; import java.util.Arrays; import java.util.List; @@ -91,6 +94,21 @@ default AttributesBuilder put(String key, boolean value) { return put(booleanKey(key), value); } + /** + * Puts an {@link Attributes} (Map) attribute into this. + * + *

Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate + * your keys, if possible. + * + * @return this Builder + */ + default AttributesBuilder put(String key, Attributes value) { + if (value == null) { + return this; + } + return put(mapKey(key), value); + } + /** * Puts a String array attribute into this. * @@ -164,6 +182,36 @@ default AttributesBuilder put(String key, boolean... value) { return put(booleanArrayKey(key), toList(value)); } + /** + * Puts an {@link Attributes} array (List of Maps) attribute into this. + * + *

Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate + * your keys, if possible. + * + * @return this Builder + */ + default AttributesBuilder put(String key, Attributes... value) { + if (value == null) { + return this; + } + return put(mapArrayKey(key), toList(value)); + } + + /** + * Puts a generic ({@link Value}) attribute into this. + * + *

Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate + * your keys, if possible. + * + * @return this Builder + */ + default AttributesBuilder put(String key, Value value) { + if (value == null) { + return this; + } + return put(valueKey(key), value); + } + /** * Puts all the provided attributes into this Builder. * diff --git a/api/all/src/main/java/io/opentelemetry/api/common/Value.java b/api/all/src/main/java/io/opentelemetry/api/common/Value.java index 084fcb19479..a29be801e27 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/Value.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/Value.java @@ -84,10 +84,6 @@ static Value> of(Map> value) { return KeyValueList.createFromMap(value); } - static Value> of(Attributes attributes) { - // TODO - } - /** Returns the type of this {@link Value}. Useful for building switch statements. */ ValueType getType();