Skip to content

Commit

Permalink
How to support complex attributes in logs/events?
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 14, 2024
1 parent 09b8db6 commit ffedc12
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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

0 comments on commit ffedc12

Please sign in to comment.