-
Notifications
You must be signed in to change notification settings - Fork 182
[dynamic control] wire up first policy #2833
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
...ontrol/src/test/java/io/opentelemetry/contrib/dynamic/policy/registry/PolicyInitTest.java
This file contains hidden or 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,137 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy.registry; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; | ||
| import io.opentelemetry.contrib.dynamic.policy.tracesampling.TraceSamplingRatePolicy; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| class PolicyInitTest { | ||
| @TempDir Path tempDir; | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws Exception { | ||
| PolicyInit.resetForTest(); | ||
| invokeStaticNoArg(TraceSamplingRatePolicy.class, "resetForTest"); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotInitializePolicyFromDeclarativeOnlyConfigInAutoConfigurationMode() { | ||
| AutoConfigurationCustomizer customizer = mock(AutoConfigurationCustomizer.class); | ||
| PolicyInit.init(customizer); | ||
| Function<ConfigProperties, Map<String, String>> propertiesCustomizer = | ||
| capturePropertiesCustomizer(customizer); | ||
|
|
||
| ConfigProperties config = mock(ConfigProperties.class); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(null); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)).thenReturn(null); | ||
| Map<String, String> ignored = propertiesCustomizer.apply(config); | ||
|
|
||
| assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNull(); | ||
| assertThat(ignored).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void initializesPolicyFromInitConfigInAutoConfigurationMode() throws Exception { | ||
| AutoConfigurationCustomizer customizer = mock(AutoConfigurationCustomizer.class); | ||
| PolicyInit.init(customizer); | ||
| Function<ConfigProperties, Map<String, String>> propertiesCustomizer = | ||
| capturePropertiesCustomizer(customizer); | ||
|
|
||
| Path configPath = tempDir.resolve("policy-init.json"); | ||
| Files.write(configPath, minimalJsonInitConfig().getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| ConfigProperties config = mock(ConfigProperties.class); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(null); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)) | ||
| .thenReturn(configPath.toString()); | ||
|
|
||
| Map<String, String> ignored = propertiesCustomizer.apply(config); | ||
|
|
||
| assertThat(ignored).isNotNull(); | ||
| assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void initializesRegisteredPolicyTypeFromDeclarativeConfig() { | ||
| ConfigProperties config = mock(ConfigProperties.class); | ||
|
|
||
| PolicyInit.initFromDeclarativeConfig( | ||
| telemetryPolicyNodeConfig(TraceSamplingRatePolicy.POLICY_TYPE), config); | ||
|
|
||
| assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void throwsWhenDeclarativeConfigUsesUnknownPolicyType() { | ||
| ConfigProperties config = mock(ConfigProperties.class); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> | ||
| PolicyInit.initFromDeclarativeConfig( | ||
| telemetryPolicyNodeConfig("trace_sampling_rate_policy"), config)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("Unknown policyType"); | ||
| } | ||
|
|
||
| private static Function<ConfigProperties, Map<String, String>> capturePropertiesCustomizer( | ||
| AutoConfigurationCustomizer customizer) { | ||
| @SuppressWarnings("unchecked") | ||
| ArgumentCaptor<Function<ConfigProperties, Map<String, String>>> captor = | ||
| ArgumentCaptor.forClass(Function.class); | ||
| verify(customizer).addPropertiesCustomizer(captor.capture()); | ||
| return captor.getValue(); | ||
| } | ||
|
|
||
| private static void invokeStaticNoArg(Class<?> targetClass, String methodName) throws Exception { | ||
| Method method = targetClass.getDeclaredMethod(methodName); | ||
| method.setAccessible(true); | ||
| method.invoke(null); | ||
| } | ||
|
|
||
| private static DeclarativeConfigProperties telemetryPolicyNodeConfig(String policyType) { | ||
| DeclarativeConfigProperties telemetryPolicy = mock(DeclarativeConfigProperties.class); | ||
| DeclarativeConfigProperties source = mock(DeclarativeConfigProperties.class); | ||
| DeclarativeConfigProperties mapping = mock(DeclarativeConfigProperties.class); | ||
|
|
||
| when(telemetryPolicy.getStructuredList(PolicyInitConfig.SOURCES_DECLARATIVE_KEY)) | ||
| .thenReturn(Collections.singletonList(source)); | ||
| when(source.getString(PolicyInitConfig.KIND_DECLARATIVE_KEY)).thenReturn("opamp"); | ||
| when(source.getString(PolicyInitConfig.FORMAT_DECLARATIVE_KEY)).thenReturn("jsonkeyvalue"); | ||
| when(source.getString(PolicyInitConfig.LOCATION_DECLARATIVE_KEY)).thenReturn("vendor"); | ||
| when(source.getStructuredList(PolicyInitConfig.MAPPINGS_DECLARATIVE_KEY)) | ||
| .thenReturn(Collections.singletonList(mapping)); | ||
| when(mapping.getString(PolicyInitConfig.SOURCE_KEY_DECLARATIVE_KEY)) | ||
| .thenReturn("sampling_rate"); | ||
| when(mapping.getString(PolicyInitConfig.POLICY_TYPE_DECLARATIVE_KEY)).thenReturn(policyType); | ||
| return telemetryPolicy; | ||
| } | ||
|
|
||
| private static String minimalJsonInitConfig() { | ||
| return "{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\",\"location\":\"vendor\"," | ||
| + "\"mappings\":[{\"sourceKey\":\"sampling_rate\",\"policyType\":\"" | ||
| + TraceSamplingRatePolicy.POLICY_TYPE | ||
| + "\"}]}]}"; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.