-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add system properties bridge #15339
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
Closed
zeitlinger
wants to merge
54
commits into
open-telemetry:main
from
zeitlinger:system-properties-bridge
Closed
Add system properties bridge #15339
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
d572c69
add system properties bridge
zeitlinger 2770d86
add system properties bridge
zeitlinger 5a3c8dd
add tests
zeitlinger e29c7bb
add list
zeitlinger 0626c95
refactor
zeitlinger efaef9f
refactor
zeitlinger bd9a69f
string
zeitlinger 07cc6fa
string
zeitlinger 7a64c91
string
zeitlinger cf01131
string
zeitlinger e3a1e60
string
zeitlinger 8b22e17
string
zeitlinger 73ec70c
string
zeitlinger f9f3df8
string
zeitlinger 93f173f
javadoc
zeitlinger 76b4683
add contract
zeitlinger cb3931b
add contract
zeitlinger f2fd6c4
remove contract
zeitlinger e7558dc
remove contract
zeitlinger 1ee4e6d
inline method to avoid class not found exception for DeclarativeConfi…
zeitlinger 8329a1e
pr review
zeitlinger aebd419
use optional instead of providing default for boolean
zeitlinger 0bd5b32
use optional instead of providing default for int
zeitlinger 65c7f44
use optional instead of providing default for list
zeitlinger eae795e
use optional instead of providing default for string
zeitlinger 80e8567
fix
zeitlinger 7e1f313
fix
zeitlinger 7acb8d2
fix
zeitlinger 77741fa
split pr
zeitlinger f3a6303
split pr
zeitlinger 6b9fe65
fix
zeitlinger d4e6219
fix
zeitlinger 165a99b
pr review
zeitlinger 28c9472
revert aws test
zeitlinger 5d845ac
use "/development" instead of "experimental" in declarative config
zeitlinger 75a4c5f
use "/development" instead of "experimental" in declarative config
zeitlinger c6e8b7c
fix
zeitlinger 7d33506
docs
zeitlinger b9695fc
avoid double bridge
zeitlinger dd9657d
normalize system props before lookup to avoid ambiguity of "experimen…
zeitlinger a390973
normalize system props before lookup to avoid ambiguity of "experimen…
zeitlinger a399bb8
normalize system props before lookup to avoid ambiguity of "experimen…
zeitlinger 9c42e5a
normalize system props before lookup to avoid ambiguity of "experimen…
zeitlinger ee4d8ac
rename js snippet property
zeitlinger dbda350
normalize system props before lookup to avoid ambiguity of "experimen…
zeitlinger 7bbf141
add changelog
zeitlinger e3fa175
format
zeitlinger ff31147
remove param that is always false
zeitlinger 5dff2c2
remove performance optimization
zeitlinger 753419e
remove performance optimization
zeitlinger 708ff2a
avoid lambda in early phase
zeitlinger 533555d
deprecated fallback
trask a6abcb6
cleanup
zeitlinger c5bfe9c
fix experimental in the middle
zeitlinger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,16 @@ | |
|
|
||
| package io.opentelemetry.instrumentation.api.internal; | ||
|
|
||
| import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; | ||
| import static java.util.Collections.emptyList; | ||
|
|
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.incubator.ExtendedOpenTelemetry; | ||
| import io.opentelemetry.api.incubator.config.ConfigProvider; | ||
| import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import javax.annotation.Nullable; | ||
|
|
||
|
|
@@ -17,11 +24,51 @@ | |
| */ | ||
| public final class ConfigPropertiesUtil { | ||
|
|
||
| private static final boolean supportsDeclarativeConfig = supportsDeclarativeConfig(); | ||
|
|
||
| private static boolean supportsDeclarativeConfig() { | ||
| try { | ||
| Class.forName("io.opentelemetry.api.incubator.ExtendedOpenTelemetry"); | ||
| return true; | ||
| } catch (ClassNotFoundException e) { | ||
| // The incubator module is not available. | ||
| // This only happens in OpenTelemetry API instrumentation tests, where an older version of | ||
| // OpenTelemetry API is used that does not have ExtendedOpenTelemetry. | ||
| // Having the incubator module without ExtendedOpenTelemetry class should still return false | ||
| // for those tests to avoid a ClassNotFoundException. | ||
zeitlinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the boolean value of the given property name from system properties and environment | ||
| * variables. | ||
| * | ||
| * <p>It's recommended to use {@link #getBoolean(OpenTelemetry, String...)} instead to support | ||
| * Declarative Config. | ||
| */ | ||
| public static boolean getBoolean(String propertyName, boolean defaultValue) { | ||
| String strValue = getString(propertyName); | ||
| return strValue == null ? defaultValue : Boolean.parseBoolean(strValue); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the boolean value of the given property name from Declarative Config if available, | ||
| * otherwise falls back to system properties and environment variables. | ||
| */ | ||
| public static Optional<Boolean> getBoolean(OpenTelemetry openTelemetry, String... propertyName) { | ||
| DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName); | ||
| if (node != null) { | ||
| return Optional.ofNullable(node.getBoolean(leaf(propertyName))); | ||
| } | ||
| String strValue = getString(toSystemProperty(propertyName)); | ||
| return strValue == null ? Optional.empty() : Optional.of(Boolean.parseBoolean(strValue)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the int value of the given property name from system properties and environment | ||
| * variables. | ||
| */ | ||
| public static int getInt(String propertyName, int defaultValue) { | ||
| String strValue = getString(propertyName); | ||
| if (strValue == null) { | ||
|
|
@@ -34,26 +81,47 @@ public static int getInt(String propertyName, int defaultValue) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the string value of the given property name from system properties and environment | ||
| * variables. | ||
| * | ||
| * <p>It's recommended to use {@link #getString(OpenTelemetry, String...)} instead to support | ||
| * Declarative Config. | ||
| */ | ||
| @Nullable | ||
| public static String getString(String propertyName) { | ||
| String value = System.getProperty(propertyName); | ||
| if (value != null) { | ||
| return value; | ||
| } | ||
| return System.getenv(toEnvVarName(propertyName)); | ||
| return ConfigUtil.getString(ConfigUtil.normalizePropertyKey(propertyName)); | ||
| } | ||
|
|
||
| public static String getString(String propertyName, String defaultValue) { | ||
| String strValue = getString(propertyName); | ||
| return strValue == null ? defaultValue : strValue; | ||
| /** | ||
| * Returns the string value of the given property name from Declarative Config if available, | ||
| * otherwise falls back to system properties and environment variables. | ||
| */ | ||
| public static Optional<String> getString(OpenTelemetry openTelemetry, String... propertyName) { | ||
| DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName); | ||
| if (node != null) { | ||
| return Optional.ofNullable(node.getString(leaf(propertyName))); | ||
| } | ||
| return Optional.ofNullable(getString(toSystemProperty(propertyName))); | ||
| } | ||
|
|
||
| public static List<String> getList(String propertyName, List<String> defaultValue) { | ||
| String value = getString(propertyName); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| /** | ||
| * Returns the list of strings value of the given property name from Declarative Config if | ||
| * available, otherwise falls back to system properties and environment variables. | ||
| */ | ||
| public static List<String> getList(OpenTelemetry openTelemetry, String... propertyName) { | ||
| DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName); | ||
| if (node != null) { | ||
| return node.getScalarList(leaf(propertyName), String.class, emptyList()); | ||
| } | ||
| return filterBlanksAndNulls(value.split(",")); | ||
| return Optional.ofNullable(getString(toSystemProperty(propertyName))) | ||
| .map(value -> filterBlanksAndNulls(value.split(","))) | ||
| .orElse(emptyList()); | ||
| } | ||
|
|
||
| /** Returns true if the given OpenTelemetry instance supports Declarative Config. */ | ||
| public static boolean isDeclarativeConfig(OpenTelemetry openTelemetry) { | ||
| return supportsDeclarativeConfig && openTelemetry instanceof ExtendedOpenTelemetry; | ||
| } | ||
|
|
||
|
Comment on lines
+122
to
+126
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only for the test (see last comment) |
||
| private static List<String> filterBlanksAndNulls(String[] values) { | ||
|
|
@@ -63,8 +131,45 @@ private static List<String> filterBlanksAndNulls(String[] values) { | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static String toEnvVarName(String propertyName) { | ||
| return propertyName.toUpperCase(Locale.ROOT).replace('-', '_').replace('.', '_'); | ||
| private static String leaf(String[] propertyName) { | ||
| return propertyName[propertyName.length - 1]; | ||
| } | ||
|
|
||
| @Nullable | ||
| private static DeclarativeConfigProperties getDeclarativeConfigNode( | ||
| OpenTelemetry openTelemetry, String... propertyName) { | ||
| if (isDeclarativeConfig(openTelemetry)) { | ||
| ExtendedOpenTelemetry extendedOpenTelemetry = (ExtendedOpenTelemetry) openTelemetry; | ||
| ConfigProvider configProvider = extendedOpenTelemetry.getConfigProvider(); | ||
| return getConfigProperties(configProvider, propertyName); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Returns the DeclarativeConfigProperties node for the given property name parts. */ | ||
| public static DeclarativeConfigProperties getConfigProperties( | ||
| ConfigProvider configProvider, String[] propertyName) { | ||
| DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig(); | ||
| if (instrumentationConfig == null) { | ||
| return empty(); | ||
| } | ||
| DeclarativeConfigProperties node = instrumentationConfig.getStructured("java", empty()); | ||
| // last part is the leaf property | ||
| for (int i = 0; i < propertyName.length - 1; i++) { | ||
| node = node.getStructured(propertyName[i], empty()); | ||
| } | ||
| return node; | ||
| } | ||
|
|
||
| public static String toSystemProperty(String[] propertyName) { | ||
| for (int i = 0; i < propertyName.length; i++) { | ||
| String node = propertyName[i]; | ||
| if (node.endsWith("/development")) { | ||
| String prefix = node.contains("experimental") ? "" : "experimental."; | ||
| propertyName[i] = prefix + node.substring(0, node.length() - 12); | ||
| } | ||
| } | ||
| return "otel.instrumentation." + String.join(".", propertyName).replace('_', '-'); | ||
| } | ||
|
|
||
| private ConfigPropertiesUtil() {} | ||
|
|
||
81 changes: 81 additions & 0 deletions
81
...mentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ConfigUtil.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,81 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.internal; | ||
|
|
||
| import java.util.ConcurrentModificationException; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Configuration utilities. | ||
| * | ||
| * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
| * at any time. | ||
| * | ||
| * <p>Copied from <a | ||
| * href="https://github.com/open-telemetry/opentelemetry-java/blob/main/api/all/src/main/java/io/opentelemetry/api/internal/ConfigUtil.java">SDK</a> | ||
| * because some tests target an SDK version where this class does not exist. | ||
| */ | ||
| final class ConfigUtil { | ||
|
|
||
| private ConfigUtil() {} | ||
|
|
||
| /** | ||
| * Returns a copy of system properties which is safe to iterate over. | ||
| * | ||
| * <p>In java 8 and android environments, iterating through system properties may trigger {@link | ||
| * ConcurrentModificationException}. This method ensures callers can iterate safely without risk | ||
| * of exception. See https://github.com/open-telemetry/opentelemetry-java/issues/6732 for details. | ||
| */ | ||
| public static Properties safeSystemProperties() { | ||
| return (Properties) System.getProperties().clone(); | ||
| } | ||
|
|
||
| /** | ||
| * Return the system property or environment variable for the {@code key}. | ||
| * | ||
| * <p>Normalize the {@code key} using {@link #normalizePropertyKey(String)}. Match to system | ||
| * property keys also normalized with {@link #normalizePropertyKey(String)}. Match to environment | ||
| * variable keys normalized with {@link #normalizeEnvironmentVariableKey(String)}. System | ||
| * properties take priority over environment variables. | ||
| * | ||
| * @param key the property key | ||
| * @return the system property if not null, or the environment variable if not null, or {@code | ||
| * null} | ||
| */ | ||
| @Nullable | ||
| public static String getString(String key) { | ||
| String normalizedKey = normalizePropertyKey(key); | ||
|
|
||
| for (Map.Entry<Object, Object> entry : safeSystemProperties().entrySet()) { | ||
| if (normalizedKey.equals(normalizePropertyKey(entry.getKey().toString()))) { | ||
| return entry.getValue().toString(); | ||
| } | ||
| } | ||
|
|
||
| for (Map.Entry<String, String> entry : System.getenv().entrySet()) { | ||
| if (normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) { | ||
| return entry.getValue(); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize an environment variable key by converting to lower case and replacing "_" with ".". | ||
| */ | ||
| public static String normalizeEnvironmentVariableKey(String key) { | ||
| return key.toLowerCase(Locale.ROOT).replace("_", "."); | ||
| } | ||
|
|
||
| /** Normalize a property key by converting to lower case and replacing "-" with ".". */ | ||
| public static String normalizePropertyKey(String key) { | ||
| return key.toLowerCase(Locale.ROOT).replace("-", "."); | ||
| } | ||
| } |
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.