Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental ConditionalResourceProvider interface (#4731)
* Add experimental ConditionalResourceProvider interface * Add tests
- Loading branch information
Mateusz Rzeszutek
committed
Sep 8, 2022
1 parent
1046253
commit 6c11793
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...ain/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ConditionalResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...urceProvider/java/io/opentelemetry/sdk/autoconfigure/ConditionalResourceProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...tionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/FirstResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/SecondResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...vider/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
io.opentelemetry.sdk.autoconfigure.SecondResourceProvider | ||
io.opentelemetry.sdk.autoconfigure.FirstResourceProvider |