Add experimental ConditionalResourceProvider interface (#4731)
* Add experimental ConditionalResourceProvider interface * Add tests
This commit is contained in:
parent
1046253f3f
commit
6c11793bd2
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure.spi.internal;
|
||||
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
|
||||
/**
|
||||
* A resource provider that is only applied if the {@link #shouldApply(ConfigProperties, Resource)}
|
||||
* method returns {@code true}.
|
||||
*
|
||||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
|
||||
* at any time.
|
||||
*/
|
||||
public interface ConditionalResourceProvider extends ResourceProvider {
|
||||
|
||||
/**
|
||||
* If an implementation needs to apply only under certain conditions related to the config or the
|
||||
* existing state of the Resource being built, they can choose to override this default.
|
||||
*
|
||||
* @param config The auto configuration properties
|
||||
* @param existing The current state of the Resource being created
|
||||
* @return false to skip over this ResourceProvider, or true to use it
|
||||
*/
|
||||
boolean shouldApply(ConfigProperties config, Resource existing);
|
||||
}
|
||||
|
|
@ -47,6 +47,21 @@ testing {
|
|||
}
|
||||
}
|
||||
}
|
||||
val testConditionalResourceProvider by registering(JvmTestSuite::class) {
|
||||
dependencies {
|
||||
implementation(project(":semconv"))
|
||||
}
|
||||
|
||||
targets {
|
||||
all {
|
||||
testTask {
|
||||
environment("OTEL_TRACES_EXPORTER", "none")
|
||||
environment("OTEL_METRICS_EXPORTER", "none")
|
||||
environment("OTEL_LOGS_EXPORTER", "none")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val testConfigError by registering(JvmTestSuite::class) {
|
||||
dependencies {
|
||||
implementation(project(":extensions:trace-propagators"))
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import io.opentelemetry.api.common.Attributes;
|
|||
import io.opentelemetry.api.common.AttributesBuilder;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
import io.opentelemetry.sdk.resources.ResourceBuilder;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
|
|
@ -46,6 +47,10 @@ final class ResourceConfiguration {
|
|||
if (disabledProviders.contains(resourceProvider.getClass().getName())) {
|
||||
continue;
|
||||
}
|
||||
if (resourceProvider instanceof ConditionalResourceProvider
|
||||
&& !((ConditionalResourceProvider) resourceProvider).shouldApply(config, result)) {
|
||||
continue;
|
||||
}
|
||||
result = result.merge(resourceProvider.createResource(config));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConditionalResourceProviderTest {
|
||||
|
||||
@Test
|
||||
void shouldConditionallyProvideResourceAttributes_skipBasedOnPreviousResource() {
|
||||
AutoConfiguredOpenTelemetrySdk sdk =
|
||||
AutoConfiguredOpenTelemetrySdk.builder()
|
||||
.setResultAsGlobal(false)
|
||||
.registerShutdownHook(false)
|
||||
.build();
|
||||
|
||||
assertThat(sdk.getResource().getAttributes().asMap())
|
||||
.contains(entry(ResourceAttributes.SERVICE_NAME, "test-service"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldConditionallyProvideResourceAttributes_skipBasedOnConfig() {
|
||||
AutoConfiguredOpenTelemetrySdk sdk =
|
||||
AutoConfiguredOpenTelemetrySdk.builder()
|
||||
.setResultAsGlobal(false)
|
||||
.registerShutdownHook(false)
|
||||
.addPropertiesSupplier(() -> singletonMap("skip-first-resource-provider", "true"))
|
||||
.build();
|
||||
|
||||
assertThat(sdk.getResource().getAttributes().asMap())
|
||||
.contains(entry(ResourceAttributes.SERVICE_NAME, "test-service-2"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
|
||||
public class FirstResourceProvider implements ConditionalResourceProvider {
|
||||
|
||||
@Override
|
||||
public Resource createResource(ConfigProperties config) {
|
||||
return Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldApply(ConfigProperties config, Resource existing) {
|
||||
return !config.getBoolean("skip-first-resource-provider", false);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
||||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
|
||||
public class SecondResourceProvider implements ConditionalResourceProvider {
|
||||
|
||||
@Override
|
||||
public Resource createResource(ConfigProperties config) {
|
||||
return Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service-2"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldApply(ConfigProperties config, Resource existing) {
|
||||
String serviceName = existing.getAttribute(ResourceAttributes.SERVICE_NAME);
|
||||
return serviceName == null || "unknown_service:java".equals(serviceName);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
io.opentelemetry.sdk.autoconfigure.SecondResourceProvider
|
||||
io.opentelemetry.sdk.autoconfigure.FirstResourceProvider
|
||||
Loading…
Reference in New Issue