(4172) Vararg versions of AttributeBuilder.put (#4188)

* Overload put with AttributeKey with String array

* Use generic instead of String

* Format and javadoc

* Cast to type

* New public method

* File based view configuration (#4163)

* Add experimental view config module

* Rename view-config to metric-incubator

* Switch naming from camelCase to snake_case

* Extend with attribute key filter

* Wire up to autoconfiguration

* Use snakeyaml instead of jackson

* PR feedback

* PR feedback

* Remove explicit okio dependency (#4187)

* Deprecate PrometheusCollector (#4185)

* Correct javadoc

Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>

* Cast to type

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>
Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
This commit is contained in:
Beppe Catanese 2022-02-24 23:04:57 +01:00 committed by GitHub
parent 389e64f2cf
commit 13fb460a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 9 deletions

View File

@ -16,6 +16,7 @@ import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
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. */
@ -100,6 +101,19 @@ public interface AttributesBuilder {
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.
*

View File

@ -289,6 +289,52 @@ class AttributesTest {
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 =

View File

@ -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[])

View File

@ -148,10 +148,10 @@ class ResourceTest {
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();

View File

@ -535,10 +535,10 @@ class SdkSpanTest {
.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}))