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..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); @@ -70,4 +75,14 @@ static AttributeKey> longArrayKey(String key) { static AttributeKey> doubleArrayKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_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); + } + + /** 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 1c51e36d644..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,8 +14,11 @@ public enum AttributeType { BOOLEAN, LONG, DOUBLE, + MAP, STRING_ARRAY, BOOLEAN_ARRAY, LONG_ARRAY, - DOUBLE_ARRAY + DOUBLE_ARRAY, + 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. *