-
Notifications
You must be signed in to change notification settings - Fork 178
[dynamic control] Add pipeline config initialization from file #2753
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
Open
jackshirazi
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
jackshirazi:policy23
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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.
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
156 changes: 156 additions & 0 deletions
156
.../src/test/java/io/opentelemetry/contrib/dynamic/policy/registry/PolicyInitConfigTest.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,156 @@ | ||
| /* | ||
| * 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.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.opentelemetry.contrib.dynamic.policy.source.SourceFormat; | ||
| import io.opentelemetry.contrib.dynamic.policy.source.SourceKind; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class PolicyInitConfigTest { | ||
|
|
||
| @TempDir Path tempDir; | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesReturnsNullWhenBothPathsMissing() { | ||
| 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); | ||
|
|
||
| assertThat(PolicyInitConfig.readFromConfigProperties(config)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesPrefersYamlWhenBothPathsPresent() throws Exception { | ||
| Path jsonPath = tempDir.resolve("policy-init.json"); | ||
| Files.write(jsonPath, jsonWithLocation("from-json").getBytes(StandardCharsets.UTF_8)); | ||
| Path yamlPath = tempDir.resolve("policy-init.yaml"); | ||
| Files.write(yamlPath, yamlWithLocation("from-yaml").getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| ConfigProperties config = mock(ConfigProperties.class); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)) | ||
| .thenReturn(yamlPath.toString()); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)) | ||
| .thenReturn(jsonPath.toString()); | ||
|
|
||
| PolicyInitConfig initConfig = PolicyInitConfig.readFromConfigProperties(config); | ||
|
|
||
| assertThat(initConfig).isNotNull(); | ||
| assertThat(initConfig.getSources()).hasSize(1); | ||
| assertThat(initConfig.getSources().get(0).getLocation()).isEqualTo("from-yaml"); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesFallsBackToJsonWhenYamlBlank() throws Exception { | ||
| Path jsonPath = tempDir.resolve("policy-init.json"); | ||
| Files.write(jsonPath, jsonWithLocation("from-json").getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| ConfigProperties config = mock(ConfigProperties.class); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(" "); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)) | ||
| .thenReturn(jsonPath.toString()); | ||
|
|
||
| PolicyInitConfig initConfig = PolicyInitConfig.readFromConfigProperties(config); | ||
|
|
||
| assertThat(initConfig).isNotNull(); | ||
| assertThat(initConfig.getSources()).hasSize(1); | ||
| assertThat(initConfig.getSources().get(0).getLocation()).isEqualTo("from-json"); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesReadsJsonWhenYamlPathMissing() throws Exception { | ||
| Path configPath = tempDir.resolve("policy-init.json"); | ||
| Files.write(configPath, minimalJsonConfig().getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| PolicyInitConfig config = configFromPaths(null, configPath.toString()); | ||
|
|
||
| assertThat(config.getSources()).hasSize(1); | ||
| PolicySourceConfig source = config.getSources().get(0); | ||
| assertThat(source.getKind()).isEqualTo(SourceKind.OPAMP); | ||
| assertThat(source.getFormat()).isEqualTo(SourceFormat.JSONKEYVALUE); | ||
| assertThat(source.getLocation()).isEqualTo("vendor"); | ||
| assertThat(source.getMappings()).hasSize(1); | ||
| assertThat(source.getMappings().get(0).getSourceKey()).isEqualTo("sampling_rate"); | ||
| assertThat(source.getMappings().get(0).getPolicyType()).isEqualTo("trace_sampling_rate_policy"); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesReadsYamlWhenYamlPathProvided() throws Exception { | ||
| Path configPath = tempDir.resolve("policy-init.yaml"); | ||
| Files.write(configPath, minimalYamlConfig().getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| PolicyInitConfig config = configFromPaths(configPath.toString(), null); | ||
|
|
||
| assertThat(config.getSources()).hasSize(1); | ||
| PolicySourceConfig source = config.getSources().get(0); | ||
| assertThat(source.getKind()).isEqualTo(SourceKind.OPAMP); | ||
| assertThat(source.getFormat()).isEqualTo(SourceFormat.JSONKEYVALUE); | ||
| assertThat(source.getMappings()).hasSize(1); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesReturnsNullOnFileReadFailure() { | ||
| Path missing = tempDir.resolve("missing-policy-init.json"); | ||
|
|
||
| assertThat(configFromPaths(null, missing.toString())).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void readFromConfigPropertiesReturnsNullOnParseFailure() throws Exception { | ||
| Path badJson = tempDir.resolve("bad-policy-init.json"); | ||
| Files.write(badJson, "{}".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| assertThat(configFromPaths(null, badJson.toString())).isNull(); | ||
| } | ||
|
|
||
| private static PolicyInitConfig configFromPaths(String yamlPath, String jsonPath) { | ||
| ConfigProperties config = mock(ConfigProperties.class); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(yamlPath); | ||
| when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)).thenReturn(jsonPath); | ||
| return PolicyInitConfig.readFromConfigProperties(config); | ||
| } | ||
|
|
||
| private static String minimalJsonConfig() { | ||
| return "{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\",\"location\":\"vendor\"," | ||
| + "\"mappings\":[{\"sourceKey\":\"sampling_rate\",\"policyType\":\"trace_sampling_rate_policy\"}]}]}"; | ||
| } | ||
|
|
||
| private static String jsonWithLocation(String location) { | ||
| return "{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\",\"location\":\"" | ||
| + location | ||
| + "\",\"mappings\":[{\"sourceKey\":\"sampling_rate\",\"policyType\":\"trace_sampling_rate_policy\"}]}]}"; | ||
| } | ||
|
|
||
| private static String minimalYamlConfig() { | ||
| return "sources:\n" | ||
| + " - kind: opamp\n" | ||
| + " format: jsonkeyvalue\n" | ||
| + " location: vendor\n" | ||
| + " mappings:\n" | ||
| + " - sourceKey: sampling_rate\n" | ||
| + " policyType: trace_sampling_rate_policy\n"; | ||
| } | ||
|
|
||
| private static String yamlWithLocation(String location) { | ||
| return "sources:\n" | ||
| + " - kind: opamp\n" | ||
| + " format: jsonkeyvalue\n" | ||
| + " location: " | ||
| + location | ||
| + "\n" | ||
| + " mappings:\n" | ||
| + " - sourceKey: sampling_rate\n" | ||
| + " policyType: trace_sampling_rate_policy\n"; | ||
| } | ||
| } |
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.