-
Notifications
You must be signed in to change notification settings - Fork 178
[dynamic control] Add SourceKind as sources are a known list #2722
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
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
63 changes: 63 additions & 0 deletions
63
dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/source/SourceKind.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,63 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy.source; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Identifies where policy configuration is loaded from for registry initialization (e.g. local | ||
| * file, OpAMP, HTTP). Distinct from {@link SourceFormat}, which describes how individual policy | ||
| * lines or payloads are encoded (key-value vs JSON). | ||
| */ | ||
| public enum SourceKind { | ||
| /** Policies loaded from a local file (e.g. line-per-policy file). */ | ||
| FILE("file"), | ||
|
|
||
| /** Policies delivered via OpAMP (remote management). */ | ||
| OPAMP("opamp"), | ||
|
|
||
| /** Policies fetched from an HTTP/HTTPS endpoint. */ | ||
| HTTP("http"), | ||
|
|
||
| /** User-defined or extension provider. */ | ||
| CUSTOM("custom"); | ||
|
|
||
| private final String configValue; | ||
|
|
||
| SourceKind(String configValue) { | ||
| this.configValue = configValue; | ||
| } | ||
|
|
||
| /** | ||
| * Stable string used in registry JSON configuration (lowercase). | ||
| * | ||
| * @return the config value for this kind | ||
| */ | ||
| public String configValue() { | ||
| return configValue; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the value used in JSON configuration. Leading and trailing whitespace is removed, then | ||
| * the remainder is matched case-insensitively against {@link #configValue()} for each kind. | ||
| * | ||
| * @param value the string from config (e.g. {@code "file"}, {@code "OPAMP"}) | ||
| * @return the matching kind | ||
| * @throws NullPointerException if value is null | ||
| * @throws IllegalArgumentException if no kind matches the trimmed value | ||
| */ | ||
| public static SourceKind fromConfigValue(String value) { | ||
| Objects.requireNonNull(value, "value cannot be null"); | ||
| String normalized = value.trim().toLowerCase(Locale.ROOT); | ||
| for (SourceKind kind : values()) { | ||
| if (kind.configValue.equals(normalized)) { | ||
| return kind; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unknown source kind: " + value); | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
...-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/source/SourceKindTest.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,47 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy.source; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SourceKindTest { | ||
|
|
||
| @Test | ||
| void configValuesAreStableLowercase() { | ||
| assertThat(SourceKind.FILE.configValue()).isEqualTo("file"); | ||
| assertThat(SourceKind.OPAMP.configValue()).isEqualTo("opamp"); | ||
| assertThat(SourceKind.HTTP.configValue()).isEqualTo("http"); | ||
| assertThat(SourceKind.CUSTOM.configValue()).isEqualTo("custom"); | ||
| } | ||
|
|
||
| @Test | ||
| void fromConfigValueParsesCaseInsensitive() { | ||
| assertThat(SourceKind.fromConfigValue("FILE")).isEqualTo(SourceKind.FILE); | ||
| assertThat(SourceKind.fromConfigValue("Opamp")).isEqualTo(SourceKind.OPAMP); | ||
| } | ||
|
|
||
| @Test | ||
| void fromConfigValueTrimsWhitespace() { | ||
| assertThat(SourceKind.fromConfigValue(" http ")).isEqualTo(SourceKind.HTTP); | ||
| } | ||
|
|
||
| @Test | ||
| void fromConfigValueRejectsNullInput() { | ||
| assertThatThrownBy(() -> SourceKind.fromConfigValue(null)) | ||
| .isInstanceOf(NullPointerException.class) | ||
| .hasMessage("value cannot be null"); | ||
| } | ||
|
|
||
| @Test | ||
| void fromConfigValueRejectsUnknownValue() { | ||
| assertThatThrownBy(() -> SourceKind.fromConfigValue("unknown")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Unknown source kind: unknown"); | ||
| } | ||
| } |
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.