Skip to content

Commit 0eac215

Browse files
committed
updates
1 parent 5374ac2 commit 0eac215

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

api/all/src/main/java/io/opentelemetry/api/common/ArrayBackedAttributes.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import io.opentelemetry.api.internal.ImmutableKeyValuePairs;
99
import java.util.ArrayList;
10+
import java.util.Collections;
1011
import java.util.Comparator;
1112
import java.util.List;
1213
import javax.annotation.Nullable;
@@ -46,12 +47,55 @@ public AttributesBuilder toBuilder() {
4647
@Override
4748
@Nullable
4849
public <T> T get(AttributeKey<T> key) {
49-
if (key != null && key.getType() == AttributeType.VALUE) {
50+
if (key == null) {
51+
return null;
52+
}
53+
if (key.getType() == AttributeType.VALUE) {
5054
return (T) getAsValue(key.getKey());
5155
}
56+
// Check if we're looking for an array type but have a VALUE with empty array
57+
if (isArrayType(key.getType())) {
58+
T value = (T) super.get(key);
59+
if (value == null) {
60+
// Check if there's a VALUE with the same key that contains an empty array
61+
Value<?> valueAttr = getValueAttribute(key.getKey());
62+
if (valueAttr != null && isEmptyArray(valueAttr)) {
63+
return (T) Collections.emptyList();
64+
}
65+
}
66+
return value;
67+
}
5268
return (T) super.get(key);
5369
}
5470

71+
private static boolean isArrayType(AttributeType type) {
72+
return type == AttributeType.STRING_ARRAY
73+
|| type == AttributeType.LONG_ARRAY
74+
|| type == AttributeType.DOUBLE_ARRAY
75+
|| type == AttributeType.BOOLEAN_ARRAY;
76+
}
77+
78+
@Nullable
79+
private Value<?> getValueAttribute(String keyName) {
80+
List<Object> data = data();
81+
for (int i = 0; i < data.size(); i += 2) {
82+
AttributeKey<?> currentKey = (AttributeKey<?>) data.get(i);
83+
if (currentKey.getKey().equals(keyName) && currentKey.getType() == AttributeType.VALUE) {
84+
return (Value<?>) data.get(i + 1);
85+
}
86+
}
87+
return null;
88+
}
89+
90+
private static boolean isEmptyArray(Value<?> value) {
91+
if (value.getType() != ValueType.ARRAY) {
92+
return false;
93+
}
94+
@SuppressWarnings("unchecked")
95+
List<Value<?>> arrayValues = (List<Value<?>>) value.getValue();
96+
return arrayValues.isEmpty();
97+
}
98+
5599
@Nullable
56100
private Value<?> getAsValue(String keyName) {
57101
// Find any attribute with the same key name and convert it to Value

api/all/src/test/java/io/opentelemetry/api/common/AttributesTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,29 @@ void simpleAttributeRetrievedAsComplexValue() {
808808
assertThat(attributes.get(doubleKey("double"))).isEqualTo(3.14);
809809
assertThat(attributes.get(booleanKey("boolean"))).isEqualTo(true);
810810
}
811+
812+
@Test
813+
void emptyValueArrayRetrievedAsAnyArrayType() {
814+
// When putting an empty VALUE array, it should be retrievable as any array type
815+
Attributes attributes =
816+
Attributes.builder().put(valueKey("key"), Value.of(Collections.emptyList())).build();
817+
818+
// Should be able to retrieve as any array type and get an empty list
819+
assertThat(attributes.get(stringArrayKey("key"))).isEqualTo(Collections.emptyList());
820+
assertThat(attributes.get(longArrayKey("key"))).isEqualTo(Collections.emptyList());
821+
assertThat(attributes.get(doubleArrayKey("key"))).isEqualTo(Collections.emptyList());
822+
assertThat(attributes.get(booleanArrayKey("key"))).isEqualTo(Collections.emptyList());
823+
824+
// Should also be retrievable as VALUE
825+
assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(Collections.emptyList()));
826+
827+
// forEach should show VALUE type (since empty arrays are stored as VALUE)
828+
Map<AttributeKey<?>, Object> entriesSeen = new LinkedHashMap<>();
829+
attributes.forEach(entriesSeen::put);
830+
assertThat(entriesSeen).containsExactly(entry(valueKey("key"), Value.of(Collections.emptyList())));
831+
832+
// asMap should show VALUE type
833+
assertThat(attributes.asMap())
834+
.containsExactly(entry(valueKey("key"), Value.of(Collections.emptyList())));
835+
}
811836
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.longKey;
1515
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringArrayKey;
1616
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringKey;
17-
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.valueKey;
1817

1918
import io.opentelemetry.api.common.AttributeKey;
2019
import io.opentelemetry.api.common.Attributes;
@@ -93,7 +92,7 @@ default ExtendedAttributesBuilder put(String key, boolean value) {
9392
* <p>Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and
9493
* pre-allocate your keys, if possible.
9594
*
96-
* @deprecated Use {@link #put(AttributeKey, Value)} with {@link Value#of(java.util.Map)} instead.
95+
* @deprecated Use {@link #put(ExtendedAttributeKey, Object)} with {@link Value#of(java.util.Map)} instead.
9796
* @return this Builder
9897
*/
9998
@Deprecated

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributeAssertion.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static AttributeAssertion create(
5858
case LONG_ARRAY:
5959
case DOUBLE_ARRAY:
6060
return assertThat((List<?>) value);
61+
case VALUE:
62+
return assertThat(value);
6163
}
6264
throw new IllegalArgumentException("Unknown type for key " + key);
6365
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LogRecordDataAssert.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ public <T> LogRecordDataAssert hasBodyField(AttributeKey<T> key, T value) {
344344
return hasBodyField(
345345
key.getKey(),
346346
Value.of(((List<Double>) value).stream().map(Value::of).collect(toList())));
347+
case VALUE:
348+
return hasBodyField(key.getKey(), (Value<?>) value);
347349
}
348-
return this;
350+
throw new IllegalArgumentException("Unknown type for key " + key);
349351
}
350352

351353
/** Asserts the log has the given attributes. */

0 commit comments

Comments
 (0)