Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prototype] How to support complex attributes in logs/events? (Option A) #6958

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,24 @@ static AttributeKey<List<Long>> longArrayKey(String key) {
static AttributeKey<List<Double>> doubleArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY);
}

/**
* Returns a new AttributeKey for map valued attributes.
*
* <p>IMPORTANT: map valued attributes are only supported by Logs. Spans and Metrics do not
* support map valued attributes.
*/
static AttributeKey<Attributes> mapKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.MAP);
}

/**
* Returns a new AttributeKey for List&lt;Attributes&gt; valued attributes.
*
* <p>IMPORTANT: {@code List&lt;Attributes&gt;} valued attributes are only supported by Logs.
* Spans and Metrics do not support {@code List&lt;Attributes&gt;} valued attributes.
*/
static AttributeKey<List<Attributes>> mapArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.MAP_ARRAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public enum AttributeType {
STRING_ARRAY,
BOOLEAN_ARRAY,
LONG_ARRAY,
DOUBLE_ARRAY
DOUBLE_ARRAY,
MAP,
MAP_ARRAY
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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;

Expand Down Expand Up @@ -86,6 +88,24 @@ default AttributesBuilder put(String key, boolean value) {
return put(booleanKey(key), value);
}

/**
* Puts a map into this. If {@code AttributesBuilder} previously contained a value for the key,
* the old value is replaced by the specified map.
*
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* <p>IMPORTANT: maps are only supported by Logs. Spans and Metrics do not support maps.
*
* @return this Builder
*/
default AttributesBuilder put(String key, Attributes attributes) {
if (attributes == null) {
return this;
}
return put(mapKey(key), attributes);
}

/**
* Puts a String array attribute into this.
*
Expand Down Expand Up @@ -159,6 +179,25 @@ default AttributesBuilder put(String key, boolean... value) {
return put(booleanArrayKey(key), toList(value));
}

/**
* Puts an array of maps into this. If {@code AttributesBuilder} previously contained a value for
* the key, the old value is replaced by the specified array of maps.
*
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* <p>IMPORTANT: arrays of maps are only supported by Logs. Spans and Metrics do not support
* arrays of maps.
*
* @return this Builder
*/
default AttributesBuilder put(String key, Attributes... value) {
if (value == null) {
return this;
}
return put(mapArrayKey(key), Arrays.asList(value));
}

/**
* Puts all the provided attributes into this Builder.
*
Expand Down
Loading