-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breakout ResourceHolder from AwsXrayRemoteSamplerProvider
ResourceHolder provides generic utility for other components in the awsxray package, and could be more broadly used. In this commit, we are breaking out the ResourceHolder inner class to it's own class, and exposing the functionality of getResource, so that other classes can rely on it. Related issue: #789
- Loading branch information
Showing
4 changed files
with
91 additions
and
26 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/ResourceHolder.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,46 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.awsxray; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Currently the only way to read the Resource from autoconfiguration. Best would be if the SPI | ||
* could return a {@code Function<SamplerFactoryArgs, Sampler>} where SamplerFactoryArgs has | ||
* SDK-constructed components like Resource and Clock. | ||
*/ | ||
@AutoService(AutoConfigurationCustomizerProvider.class) | ||
public final class ResourceHolder implements AutoConfigurationCustomizerProvider { | ||
|
||
@Nullable static volatile Resource resource; | ||
|
||
@Override | ||
public void customize(AutoConfigurationCustomizer autoConfiguration) { | ||
autoConfiguration.addResourceCustomizer( | ||
(resource, config) -> { | ||
ResourceHolder.resource = resource; | ||
return resource; | ||
}); | ||
} | ||
|
||
/** | ||
* Returns held resource, unless resource is null, in which case {@link Resource#getDefault()} is | ||
* returned. This should not happen in practice, as {@link #customize} should be automatically | ||
* run, populating {@link #resource}. | ||
*/ | ||
public static Resource getResource() { | ||
Resource resourceReference = resource; | ||
if (resourceReference == null) { | ||
// Should never be the case in practice. | ||
resourceReference = Resource.getDefault(); | ||
} | ||
return resourceReference; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/ResourceHolderTest.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,43 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.awsxray; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.function.BiFunction; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link ResourceHolder}. Note that there isn't a great way to test the "default" | ||
* fallback logic, as when the test suite is run, the customize logic appears to be invoked. | ||
*/ | ||
public class ResourceHolderTest { | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void testCustomized() { | ||
Resource customizedResource = Resource.create(Attributes.empty()); | ||
AutoConfigurationCustomizer mockCustomizer = mock(AutoConfigurationCustomizer.class); | ||
ResourceHolder resourceHolder = new ResourceHolder(); | ||
when(mockCustomizer.addResourceCustomizer(any())) | ||
.thenAnswer( | ||
invocation -> { | ||
BiFunction<Resource, ConfigProperties, Resource> biFunction = | ||
(BiFunction<Resource, ConfigProperties, Resource>) invocation.getArguments()[0]; | ||
assertThat(biFunction.apply(customizedResource, null)).isEqualTo(customizedResource); | ||
return mockCustomizer; | ||
}); | ||
resourceHolder.customize(mockCustomizer); | ||
assertThat(ResourceHolder.getResource()).isEqualTo(customizedResource); | ||
} | ||
} |