Skip to content
Merged
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 @@ -16,6 +16,7 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/** A builder of {@link Attributes} supporting an arbitrary number of key-value pairs. */
Expand Down Expand Up @@ -100,6 +101,19 @@ default AttributesBuilder put(String key, String... value) {
return put(stringArrayKey(key), Arrays.asList(value));
}

/**
* Puts a List attribute into this.
*
* @return this Builder
*/
@SuppressWarnings("unchecked")
default <T> AttributesBuilder put(AttributeKey<List<T>> key, T... value) {
if (value == null) {
return this;
}
return put(key, Arrays.asList(value));
}

/**
* Puts a Long array attribute into this.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,52 @@ void builder() {
assertThat(attributes).isEqualTo(wantAttributes);
}

@Test
void builderWithAttributeKeyList() {
Attributes attributes =
Attributes.builder()
.put("string", "value1")
.put(longKey("long"), 10)
.put(stringArrayKey("anotherString"), "value1", "value2", "value3")
.put(longArrayKey("anotherLong"), 10L, 20L, 30L)
.put(booleanArrayKey("anotherBoolean"), true, false, true)
.build();

Attributes wantAttributes =
Attributes.of(
stringKey("string"),
"value1",
longKey("long"),
10L,
stringArrayKey("anotherString"),
Arrays.asList("value1", "value2", "value3"),
longArrayKey("anotherLong"),
Arrays.asList(10L, 20L, 30L),
booleanArrayKey("anotherBoolean"),
Arrays.asList(true, false, true));
assertThat(attributes).isEqualTo(wantAttributes);

AttributesBuilder newAttributes = attributes.toBuilder();
newAttributes.put("newKey", "newValue");
assertThat(newAttributes.build())
.isEqualTo(
Attributes.of(
stringKey("string"),
"value1",
longKey("long"),
10L,
stringArrayKey("anotherString"),
Arrays.asList("value1", "value2", "value3"),
longArrayKey("anotherLong"),
Arrays.asList(10L, 20L, 30L),
booleanArrayKey("anotherBoolean"),
Arrays.asList(true, false, true),
stringKey("newKey"),
"newValue"));
// Original not mutated.
assertThat(attributes).isEqualTo(wantAttributes);
}

@Test
void builder_arrayTypes() {
Attributes attributes =
Expand Down
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.common.AttributesBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++! NEW METHOD: PUBLIC(+) io.opentelemetry.api.common.AttributesBuilder put(io.opentelemetry.api.common.AttributeKey, java.lang.Object[])
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ void create_NullEmptyArray() {
assertThat(resource.getAttributes().size()).isEqualTo(8);

// Null arrays should be dropped
attributes.put(stringArrayKey("NullArrayStringKey"), null);
attributes.put(longArrayKey("NullArrayLongKey"), null);
attributes.put(doubleArrayKey("NullArrayDoubleKey"), null);
attributes.put(booleanArrayKey("NullArrayBooleanKey"), null);
attributes.put(stringArrayKey("NullArrayStringKey"), (String[]) null);
attributes.put(longArrayKey("NullArrayLongKey"), (Long[]) null);
attributes.put(doubleArrayKey("NullArrayDoubleKey"), (Double[]) null);
attributes.put(booleanArrayKey("NullArrayBooleanKey"), (Boolean[]) null);

resource = Resource.create(attributes.build());
assertThat(resource.getAttributes()).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ void setAllAttributes() {
.put(doubleArrayKey("ArrayDoubleKey"), Arrays.asList(0.1, 2.3, 4.5, 6.7, 8.9))
.put(booleanArrayKey("ArrayBooleanKey"), Arrays.asList(true, false, false, true))
// These should be dropped
.put(stringArrayKey("NullArrayStringKey"), null)
.put(longArrayKey("NullArrayLongKey"), null)
.put(doubleArrayKey("NullArrayDoubleKey"), null)
.put(booleanArrayKey("NullArrayBooleanKey"), null)
.put(stringArrayKey("NullArrayStringKey"), (String[]) null)
.put(longArrayKey("NullArrayLongKey"), (Long[]) null)
.put(doubleArrayKey("NullArrayDoubleKey"), (Double[]) null)
.put(booleanArrayKey("NullArrayBooleanKey"), (Boolean[]) null)
// These should be maintained
.put(longArrayKey("ArrayWithNullLongKey"), Arrays.asList(new Long[] {null}))
.put(stringArrayKey("ArrayWithNullStringKey"), Arrays.asList(new String[] {null}))
Expand Down