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:
parent
22d385c763
commit
e54e7ac4dc
|
@ -76,12 +76,8 @@ final class ResourceConfiguration {
|
||||||
static Resource filterAttributes(Resource resource, ConfigProperties configProperties) {
|
static Resource filterAttributes(Resource resource, ConfigProperties configProperties) {
|
||||||
Set<String> disabledKeys = new HashSet<>(configProperties.getList(DISABLED_ATTRIBUTE_KEYS));
|
Set<String> disabledKeys = new HashSet<>(configProperties.getList(DISABLED_ATTRIBUTE_KEYS));
|
||||||
|
|
||||||
Attributes filteredAttributes =
|
ResourceBuilder builder =
|
||||||
resource.getAttributes().toBuilder()
|
resource.toBuilder().removeIf(attributeKey -> disabledKeys.contains(attributeKey.getKey()));
|
||||||
.removeIf(attributeKey -> disabledKeys.contains(attributeKey.getKey()))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
ResourceBuilder builder = Resource.builder().putAll(filteredAttributes);
|
|
||||||
|
|
||||||
if (resource.getSchemaUrl() != null) {
|
if (resource.getSchemaUrl() != null) {
|
||||||
builder.setSchemaUrl(resource.getSchemaUrl());
|
builder.setSchemaUrl(resource.getSchemaUrl());
|
||||||
|
|
|
@ -8,6 +8,7 @@ package io.opentelemetry.sdk.resources;
|
||||||
import io.opentelemetry.api.common.AttributeKey;
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
import io.opentelemetry.api.common.Attributes;
|
import io.opentelemetry.api.common.Attributes;
|
||||||
import io.opentelemetry.api.common.AttributesBuilder;
|
import io.opentelemetry.api.common.AttributesBuilder;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -173,6 +174,12 @@ public class ResourceBuilder {
|
||||||
return this;
|
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.
|
* Assign an OpenTelemetry schema URL to the resulting Resource.
|
||||||
*
|
*
|
||||||
|
|
|
@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import com.google.common.testing.EqualsTester;
|
import com.google.common.testing.EqualsTester;
|
||||||
import io.opentelemetry.api.common.AttributeKey;
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
|
import io.opentelemetry.api.common.AttributeType;
|
||||||
import io.opentelemetry.api.common.Attributes;
|
import io.opentelemetry.api.common.Attributes;
|
||||||
import io.opentelemetry.api.common.AttributesBuilder;
|
import io.opentelemetry.api.common.AttributesBuilder;
|
||||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||||
|
@ -330,4 +331,44 @@ class ResourceTest {
|
||||||
assertThat(newResource.getAttribute(stringKey("foo"))).isEqualTo("val");
|
assertThat(newResource.getAttribute(stringKey("foo"))).isEqualTo("val");
|
||||||
assertThat(newResource.getSchemaUrl()).isEqualTo("http://example.com");
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue