feat:add the shortcut of removeif in ResourceBuilder (#4286)

* fix:add an ending period to form notes

* feat:add the shortcut of removeif in ResourceBuilder

* fix:fix the errors of check

* feat:run two gradlew task to achieve check

* fix:fix format violations

* feat:remove extra function and modify the unit test

* fix: fix format violations

* feat:format code and modify unit test

* feat:make unit test specification

* feat:cleanup for normalize
This commit is contained in:
Brian Yan 2022-03-22 12:03:55 +08:00 committed by GitHub
parent 22d385c763
commit e54e7ac4dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 6 deletions

View File

@ -76,12 +76,8 @@ final class ResourceConfiguration {
static Resource filterAttributes(Resource resource, ConfigProperties configProperties) {
Set<String> disabledKeys = new HashSet<>(configProperties.getList(DISABLED_ATTRIBUTE_KEYS));
Attributes filteredAttributes =
resource.getAttributes().toBuilder()
.removeIf(attributeKey -> disabledKeys.contains(attributeKey.getKey()))
.build();
ResourceBuilder builder = Resource.builder().putAll(filteredAttributes);
ResourceBuilder builder =
resource.toBuilder().removeIf(attributeKey -> disabledKeys.contains(attributeKey.getKey()));
if (resource.getSchemaUrl() != null) {
builder.setSchemaUrl(resource.getSchemaUrl());

View File

@ -8,6 +8,7 @@ package io.opentelemetry.sdk.resources;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.util.function.Predicate;
import javax.annotation.Nullable;
/**
@ -173,6 +174,12 @@ public class ResourceBuilder {
return this;
}
/** Remove all attributes that satisfy the given predicate from {@link Resource}. */
public ResourceBuilder removeIf(Predicate<AttributeKey<?>> filter) {
attributesBuilder.removeIf(filter);
return this;
}
/**
* Assign an OpenTelemetry schema URL to the resulting Resource.
*

View File

@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.testing.EqualsTester;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
@ -330,4 +331,44 @@ class ResourceTest {
assertThat(newResource.getAttribute(stringKey("foo"))).isEqualTo("val");
assertThat(newResource.getSchemaUrl()).isEqualTo("http://example.com");
}
@Test
public void removeIf() {
assertThat(Resource.builder().removeIf(unused -> true).build()).isEqualTo(Resource.empty());
assertThat(Resource.builder().removeIf(key -> key.getKey().equals("key1")).build())
.isEqualTo(Resource.empty());
assertThat(
Resource.builder()
.put("key1", "value1")
.removeIf(key -> key.getKey().equals("key1"))
.removeIf(key -> key.getKey().equals("key1"))
.build())
.isEqualTo(Resource.empty());
assertThat(
Resource.builder()
.put("key1", "value1")
.put("key1", "value2")
.put("key2", "value2")
.put("key3", "value3")
.removeIf(key -> key.getKey().equals("key1"))
.build())
.isEqualTo(Resource.builder().put("key2", "value2").put("key3", "value3").build());
assertThat(
Resource.builder()
.put("key1", "value1A")
.put("key1", true)
.removeIf(
key ->
key.getKey().equals("key1") && key.getType().equals(AttributeType.STRING))
.build())
.isEqualTo(Resource.builder().put("key1", true).build());
assertThat(
Resource.builder()
.put("key1", "value1")
.put("key2", "value2")
.put("foo", "bar")
.removeIf(key -> key.getKey().matches("key.*"))
.build())
.isEqualTo(Resource.builder().put("foo", "bar").build());
}
}