Skip to content
Closed
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 @@ -6,6 +6,7 @@
package io.opentelemetry.api.incubator.common;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -93,8 +94,13 @@ static ExtendedAttributeKey<List<Double>> doubleArrayKey(String key) {
return fromAttributeKey(AttributeKey.doubleArrayKey(key));
}

/** Returns a new ExtendedAttributeKey for Map valued attributes. */
static ExtendedAttributeKey<ExtendedAttributes> extendedAttributesKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.EXTENDED_ATTRIBUTES);
/** Returns a new ExtendedAttributeKey for map valued attributes. */
static ExtendedAttributeKey<ExtendedAttributes> mapKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.MAP);
}

/** Returns a new ExtendedAttributeKey for {@link Value} valued attributes. */
static ExtendedAttributeKey<Value<?>> valueKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public enum ExtendedAttributeType {
LONG_ARRAY,
DOUBLE_ARRAY,
// Extended types unique to ExtendedAttributes
EXTENDED_ATTRIBUTES;
MAP,
VALUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*
* <p>"extended" refers an extended set of allowed value types compared to standard {@link
* Attributes}. Notably, {@link ExtendedAttributes} values can be of type {@link
* ExtendedAttributeType#EXTENDED_ATTRIBUTES}, allowing nested {@link ExtendedAttributes} of
* arbitrary depth.
* ExtendedAttributeType#MAP}, allowing nested {@link ExtendedAttributes} of arbitrary depth, or
* {@link ExtendedAttributeType#VALUE}, enabling usage of {@link io.opentelemetry.api.common.Value}
* instances.
*
* <p>Where standard {@link Attributes} are accepted everyone that OpenTelemetry represents key /
* value pairs, {@link ExtendedAttributes} are only accepted in select places, such as log records
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -94,7 +95,22 @@ default ExtendedAttributesBuilder put(String key, boolean value) {
* @return this Builder
*/
default <T> ExtendedAttributesBuilder put(String key, ExtendedAttributes value) {
return put(ExtendedAttributeKey.extendedAttributesKey(key), value);
return put(ExtendedAttributeKey.mapKey(key), value);
}

/**
* Puts a {@link Value} attribute into this.
*
* <p>Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @return this Builder
*/
default ExtendedAttributesBuilder put(String key, Value<?> value) {
if (value == null) {
return this;
}
return put(ExtendedAttributeKey.valueKey(key), value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public static <T> AttributeKey<T> toAttributeKey(ExtendedAttributeKey<T> extende
case DOUBLE_ARRAY:
return InternalAttributeKeyImpl.create(
extendedAttributeKey.getKey(), AttributeType.DOUBLE_ARRAY);
case EXTENDED_ATTRIBUTES:
case MAP:
case VALUE:
return null;
}
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ private static Stream<Arguments> attributeKeyArgs() {
"key",
ExtendedAttributeType.DOUBLE_ARRAY,
AttributeKey.doubleArrayKey("key")),
Arguments.of(ExtendedAttributeKey.mapKey("key"), "key", ExtendedAttributeType.MAP, null),
Arguments.of(
ExtendedAttributeKey.extendedAttributesKey("key"),
"key",
ExtendedAttributeType.EXTENDED_ATTRIBUTES,
null));
ExtendedAttributeKey.valueKey("key"), "key", ExtendedAttributeType.VALUE, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -95,7 +96,7 @@ private static void assertEquals(
assertThat(actual.size()).isEqualTo(expected.size());
actual.forEach(
(key, value) -> {
if (key.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) {
if (key.getType() == ExtendedAttributeType.MAP) {
assertEquals(
((ExtendedAttributes) value).asMap(),
(Map<String, Object>) expected.get(key.getKey()));
Expand All @@ -116,7 +117,9 @@ void asAttributes(ExtendedAttributes extendedAttributes, Map<String, Object> exp
});

long expectedSize =
expectedMap.values().stream().filter(value -> !(value instanceof Map)).count();
expectedMap.values().stream()
.filter(value -> !(value instanceof Map) && !(value instanceof Value))
.count();
assertThat(attributes.size()).isEqualTo(expectedSize);
}

Expand Down Expand Up @@ -161,7 +164,7 @@ private static ExtendedAttributes fromMap(Map<String, Object> map) {
map.forEach(
(key, value) -> {
ExtendedAttributeKey<?> extendedAttributeKey = getKey(key, value);
if (extendedAttributeKey.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) {
if (extendedAttributeKey.getType() == ExtendedAttributeType.MAP) {
builder.put(
(ExtendedAttributeKey<ExtendedAttributes>) extendedAttributeKey,
fromMap((Map<String, Object>) value));
Expand Down Expand Up @@ -212,6 +215,9 @@ private static Stream<Arguments> attributesArgs() {
ImmutableMap.builder()
.put("key", ImmutableMap.builder().put("child", "value").build())
.build()),
Arguments.of(
ExtendedAttributes.builder().put("key", Value.of("value")).build(),
ImmutableMap.builder().put("key", Value.of("value")).build()),
Arguments.of(
ExtendedAttributes.builder()
.put(ExtendedAttributeKey.stringKey("key"), "value")
Expand Down Expand Up @@ -249,12 +255,17 @@ private static Stream<Arguments> attributesArgs() {
Arguments.of(
ExtendedAttributes.builder()
.put(
ExtendedAttributeKey.extendedAttributesKey("key"),
ExtendedAttributeKey.mapKey("key"),
ExtendedAttributes.builder().put("child", "value").build())
.build(),
ImmutableMap.builder()
.put("key", ImmutableMap.builder().put("child", "value").build())
.build()),
Arguments.of(
ExtendedAttributes.builder()
.put(ExtendedAttributeKey.valueKey("key"), Value.of("value"))
.build(),
ImmutableMap.builder().put("key", Value.of("value")).build()),
// Multiple entries
Arguments.of(
ExtendedAttributes.builder()
Expand All @@ -268,6 +279,7 @@ private static Stream<Arguments> attributesArgs() {
.put("key8", 1L, 2L)
.put("key9", 1.1, 2.2)
.put("key10", ExtendedAttributes.builder().put("child", "value").build())
.put("key11", Value.of("value"))
.build(),
ImmutableMap.builder()
.put("key1", "value1")
Expand All @@ -280,14 +292,15 @@ private static Stream<Arguments> attributesArgs() {
.put("key8", Arrays.asList(1L, 2L))
.put("key9", Arrays.asList(1.1, 2.2))
.put("key10", ImmutableMap.builder().put("child", "value").build())
.put("key11", Value.of("value"))
.build()));
}

private static Map<String, Object> toMap(ExtendedAttributes extendedAttributes) {
Map<String, Object> map = new HashMap<>();
extendedAttributes.forEach(
(key, value) -> {
if (key.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) {
if (key.getType() == ExtendedAttributeType.MAP) {
map.put(key.getKey(), toMap((ExtendedAttributes) value));
return;
}
Expand All @@ -314,8 +327,10 @@ private static ExtendedAttributeKey<?> getKey(String key, Object value) {
return ExtendedAttributeKey.longArrayKey(key);
case DOUBLE_ARRAY:
return ExtendedAttributeKey.doubleArrayKey(key);
case EXTENDED_ATTRIBUTES:
return ExtendedAttributeKey.extendedAttributesKey(key);
case MAP:
return ExtendedAttributeKey.mapKey(key);
case VALUE:
return ExtendedAttributeKey.valueKey(key);
}
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -353,7 +368,10 @@ private static ExtendedAttributeType getType(Object value) {
}
}
if ((value instanceof Map)) {
return ExtendedAttributeType.EXTENDED_ATTRIBUTES;
return ExtendedAttributeType.MAP;
}
if (value instanceof Value) {
return ExtendedAttributeType.VALUE;
}
throw new IllegalArgumentException("Unrecognized value type: " + value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
import io.opentelemetry.api.logs.Logger;
Expand Down Expand Up @@ -104,8 +105,8 @@ private static String flipCoin() {
AttributeKey<List<Double>> doubleArrKey = AttributeKey.doubleArrayKey("acme.double_array");

// Extended keys
ExtendedAttributeKey<ExtendedAttributes> mapKey =
ExtendedAttributeKey.extendedAttributesKey("acme.map");
ExtendedAttributeKey<ExtendedAttributes> mapKey = ExtendedAttributeKey.mapKey("acme.map");
ExtendedAttributeKey<Value<?>> valueKey = ExtendedAttributeKey.valueKey("acme.value");

@Test
@SuppressLogger(ExtendedLogsBridgeApiUsageTest.class)
Expand All @@ -125,6 +126,7 @@ void extendedAttributesUsage() {
.put(
mapKey,
ExtendedAttributes.builder().put("childStr", "value").put("childLong", 1L).build())
.put(valueKey, Value.of("value"))
.build();

// Retrieval
Expand All @@ -139,6 +141,7 @@ void extendedAttributesUsage() {
assertThat(extendedAttributes.get(mapKey))
.isEqualTo(
ExtendedAttributes.builder().put("childStr", "value").put("childLong", 1L).build());
assertThat(extendedAttributes.get(valueKey)).isEqualTo(Value.of("value"));

// Iteration
// Output:
Expand All @@ -148,7 +151,8 @@ void extendedAttributesUsage() {
// acme.double_array(DOUBLE_ARRAY): [1.1, 2.2]
// acme.long(LONG): 1
// acme.long_array(LONG_ARRAY): [1, 2]
// acme.map(EXTENDED_ATTRIBUTES): {childLong=1, childStr="value"}
// acme.map(MAP): {childLong=1, childStr="value"}
// acme.value(VALUE): ValueString{value}
// acme.string(STRING): value
// acme.string_array(STRING_ARRAY): [value1, value2]
extendedAttributes.forEach(
Expand Down Expand Up @@ -184,6 +188,7 @@ void logRecordBuilder_ExtendedAttributes() {
.setAttribute(
mapKey,
ExtendedAttributes.builder().put("childStr", "value").put("childLong", 1L).build())
.setAttribute(valueKey, Value.of("value"))
.setAllAttributes(Attributes.builder().put("key1", "value").build())
.setAllAttributes(ExtendedAttributes.builder().put("key2", "value").build())
.emit();
Expand Down Expand Up @@ -229,6 +234,7 @@ void logRecordBuilder_ExtendedAttributes() {
.put("childStr", "value")
.put("childLong", 1L)
.build())
.put(valueKey, Value.of("value"))
.put("key1", "value")
.put("key2", "value")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
import io.opentelemetry.api.incubator.common.ExtendedAttributeType;
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
Expand Down Expand Up @@ -168,12 +169,15 @@ public int getBinarySerializedSize(
(List<Object>) value,
AttributeArrayAnyValueStatelessMarshaler.INSTANCE,
context);
case EXTENDED_ATTRIBUTES:
case MAP:
return StatelessMarshalerUtil.sizeMessageWithContext(
AnyValue.KVLIST_VALUE,
(ExtendedAttributes) value,
ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE,
context);
case VALUE:
return AnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize(
(Value<?>) value, context);
}
// Error prone ensures the switch statement is complete, otherwise only can happen with
// unaligned versions which are not supported.
Expand Down Expand Up @@ -213,13 +217,16 @@ public void writeTo(
AttributeArrayAnyValueStatelessMarshaler.INSTANCE,
context);
return;
case EXTENDED_ATTRIBUTES:
case MAP:
output.serializeMessageWithContext(
AnyValue.KVLIST_VALUE,
(ExtendedAttributes) value,
ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE,
context);
return;
case VALUE:
AnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Value<?>) value, context);
return;
}
// Error prone ensures the switch statement is complete, otherwise only can happen with
// unaligned versions which are not supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.exporter.internal.otlp;

import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
import io.opentelemetry.api.incubator.common.ExtendedAttributes;
import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl;
Expand Down Expand Up @@ -110,12 +111,14 @@ private static KeyValueMarshaler create(ExtendedAttributeKey<?> attributeKey, Ob
case DOUBLE_ARRAY:
return new KeyValueMarshaler(
keyUtf8, ArrayAnyValueMarshaler.createDouble((List<Double>) value));
case EXTENDED_ATTRIBUTES:
case MAP:
return new KeyValueMarshaler(
keyUtf8,
new KeyValueListAnyValueMarshaler(
new KeyValueListAnyValueMarshaler.KeyValueListMarshaler(
createForExtendedAttributes((ExtendedAttributes) value))));
case VALUE:
return new KeyValueMarshaler(keyUtf8, AnyValueMarshaler.create((Value<?>) value));
}
// Error prone ensures the switch statement is complete, otherwise only can happen with
// unaligned versions which are not supported.
Expand Down
Loading