New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add experimental ConditionalResourceProvider interface #4731
Merged
jack-berg
merged 2 commits into
open-telemetry:main
from
mateuszrzeszutek:conditional-resource-provider
Sep 8, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice I support 👍