-
Couldn't load subscription status.
- Fork 1k
Support experimental declarative configuration #12265
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
trask
merged 8 commits into
open-telemetry:main
from
jack-berg:support-declarative-config
Oct 16, 2024
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
983515c
Support declarative configuration
jack-berg a1d69f4
Implement StructuredConfigPropertiesBridge
jack-berg ee9cf67
Fix class loading stuff
jack-berg 2fffe26
PR feedback
jack-berg a2534ba
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 3e2e6f1
Simplify tree walking
jack-berg 63256f7
remote extra blank lines
jack-berg e76bbac
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 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
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
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
215 changes: 215 additions & 0 deletions
215
.../java/io/opentelemetry/javaagent/bootstrap/internal/StructuredConfigPropertiesBridge.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,215 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.bootstrap.internal; | ||
|
|
||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.BiFunction; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A {@link ConfigProperties} which resolves properties based on {@link StructuredConfigProperties}. | ||
| * | ||
| * <p>Only properties starting with "otel.instrumentation." are resolved. Others return null (or | ||
| * default value if provided). | ||
| * | ||
| * <p>To resolve: | ||
| * | ||
| * <ul> | ||
| * <li>"otel.instrumentation" refers to the ".instrumentation.java" node | ||
| * <li>The portion of the property after "otel.instrumentation." is split into segments based on | ||
| * ".". | ||
| * <li>For each N-1 segment, we walk down the tree to find the relevant leaf {@link | ||
| * StructuredConfigProperties}. | ||
| * <li>We extract the property from the resolved {@link StructuredConfigProperties} using the last | ||
| * segment as the property key. | ||
| * </ul> | ||
| * | ||
| * <p>For example, given the following YAML, asking for {@code | ||
| * ConfigProperties#getString("otel.instrumentation.common.string_key")} yields "value": | ||
| * | ||
| * <pre> | ||
| * instrumentation: | ||
| * java: | ||
| * common: | ||
| * string_key: value | ||
| * </pre> | ||
| */ | ||
| public final class StructuredConfigPropertiesBridge implements ConfigProperties { | ||
|
|
||
| private static final StructuredConfigProperties EMPTY = new EmptyStructuredConfigProperties(); | ||
|
|
||
| private final StructuredConfigProperties javaInstrumentation; | ||
|
|
||
| public StructuredConfigPropertiesBridge( | ||
| StructuredConfigProperties rootStructuredConfigProperties) { | ||
| StructuredConfigProperties instrumentation = | ||
| rootStructuredConfigProperties.getStructured("instrumentation"); | ||
| if (instrumentation != null) { | ||
| javaInstrumentation = instrumentation.getStructured("java"); | ||
| } else { | ||
| javaInstrumentation = EMPTY; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getString(String propertyName) { | ||
| return getPropertyValue(propertyName, StructuredConfigProperties::getString); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Boolean getBoolean(String propertyName) { | ||
| return getPropertyValue(propertyName, StructuredConfigProperties::getBoolean); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Integer getInt(String propertyName) { | ||
| return getPropertyValue(propertyName, StructuredConfigProperties::getInt); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Long getLong(String propertyName) { | ||
| return getPropertyValue(propertyName, StructuredConfigProperties::getLong); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Double getDouble(String propertyName) { | ||
| return getPropertyValue(propertyName, StructuredConfigProperties::getDouble); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Duration getDuration(String propertyName) { | ||
| Long millis = getPropertyValue(propertyName, StructuredConfigProperties::getLong); | ||
| if (millis == null) { | ||
| return null; | ||
| } | ||
| return Duration.ofMillis(millis); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getList(String propertyName) { | ||
| List<String> propertyValue = | ||
| getPropertyValue( | ||
| propertyName, | ||
| (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); | ||
| return propertyValue == null ? Collections.emptyList() : propertyValue; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> getMap(String propertyName) { | ||
| StructuredConfigProperties propertyValue = | ||
| getPropertyValue(propertyName, StructuredConfigProperties::getStructured); | ||
| if (propertyValue == null) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| Map<String, String> result = new HashMap<>(); | ||
| propertyValue | ||
| .getPropertyKeys() | ||
| .forEach( | ||
| key -> { | ||
| String value = propertyValue.getString(key); | ||
| if (value == null) { | ||
| return; | ||
| } | ||
| result.put(key, value); | ||
| }); | ||
| return Collections.unmodifiableMap(result); | ||
| } | ||
|
|
||
| @Nullable | ||
| private <T> T getPropertyValue( | ||
| String property, BiFunction<StructuredConfigProperties, String, T> extractor) { | ||
| if (!property.startsWith("otel.instrumentation.")) { | ||
| return null; | ||
| } | ||
| String suffix = property.substring("otel.instrumentation.".length()); | ||
| // Split the remainder of the property on ".", and walk to the N-1 entry | ||
| String[] segments = suffix.split("\\."); | ||
| if (segments.length == 0) { | ||
| return null; | ||
| } | ||
| StructuredConfigProperties target = javaInstrumentation; | ||
| if (segments.length > 1) { | ||
| for (int i = 0; i < segments.length - 1; i++) { | ||
| StructuredConfigProperties newTarget = target.getStructured(segments[i]); | ||
| if (newTarget == null) { | ||
| target = EMPTY; | ||
| break; | ||
| } | ||
| target = newTarget; | ||
| } | ||
| } | ||
| String lastPart = segments[segments.length - 1]; | ||
| return extractor.apply(target, lastPart); | ||
| } | ||
|
|
||
| private static class EmptyStructuredConfigProperties implements StructuredConfigProperties { | ||
| @Nullable | ||
| @Override | ||
| public String getString(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Boolean getBoolean(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Integer getInt(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Long getLong(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public Double getDouble(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public <T> List<T> getScalarList(String s, Class<T> aClass) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public StructuredConfigProperties getStructured(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public List<StructuredConfigProperties> getStructuredList(String s) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getPropertyKeys() { | ||
| return Collections.emptySet(); | ||
| } | ||
| } | ||
| } |
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.
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.