|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.api.internal; |
7 | 7 |
|
| 8 | +import io.opentelemetry.api.OpenTelemetry; |
| 9 | +import io.opentelemetry.api.incubator.ExtendedOpenTelemetry; |
| 10 | +import io.opentelemetry.api.incubator.config.ConfigProvider; |
| 11 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
8 | 12 | import java.util.Arrays; |
9 | 13 | import java.util.List; |
10 | 14 | import java.util.Locale; |
11 | 15 | import java.util.stream.Collectors; |
12 | 16 | import javax.annotation.Nullable; |
13 | 17 |
|
| 18 | +import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; |
| 19 | + |
14 | 20 | /** |
15 | 21 | * This class is internal and is hence not for public use. Its APIs are unstable and can change at |
16 | 22 | * any time. |
17 | 23 | */ |
18 | 24 | public final class ConfigPropertiesUtil { |
19 | 25 |
|
| 26 | + private static final boolean isIncubator = isIncubator(); |
| 27 | + |
| 28 | + private static boolean isIncubator() { |
| 29 | + try { |
| 30 | + Class.forName("io.opentelemetry.api.incubator.ExtendedOpenTelemetry"); |
| 31 | + return true; |
| 32 | + } catch (ClassNotFoundException e) { |
| 33 | + // The incubator module is not available. |
| 34 | + // This only happens in OpenTelemetry API instrumentation tests, where an older version of |
| 35 | + // OpenTelemetry API is used that does not have ExtendedOpenTelemetry. |
| 36 | + // Having the incubator module without ExtendedOpenTelemetry class should still return false |
| 37 | + // for those tests to avoid a ClassNotFoundException. |
| 38 | + return false; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @deprecated use {@link #getBoolean(OpenTelemetry, boolean, String...)} instead |
| 44 | + */ |
| 45 | + @Deprecated |
20 | 46 | public static boolean getBoolean(String propertyName, boolean defaultValue) { |
21 | 47 | String strValue = getString(propertyName); |
22 | 48 | return strValue == null ? defaultValue : Boolean.parseBoolean(strValue); |
23 | 49 | } |
24 | 50 |
|
| 51 | + /** |
| 52 | + * Returns the boolean value of the given property name from Declarative Config if available, |
| 53 | + * otherwise falls back to system properties and environment variables. |
| 54 | + */ |
| 55 | + public static boolean getBoolean( |
| 56 | + OpenTelemetry openTelemetry, boolean defaultValue, String... propertyName) { |
| 57 | + |
| 58 | + DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName); |
| 59 | + if (node != null) { |
| 60 | + return node.getBoolean(propertyName[propertyName.length - 1], defaultValue); |
| 61 | + } |
| 62 | + return getBoolean(toSystemProperty(propertyName), defaultValue); |
| 63 | + } |
| 64 | + |
25 | 65 | public static int getInt(String propertyName, int defaultValue) { |
26 | 66 | String strValue = getString(propertyName); |
27 | 67 | if (strValue == null) { |
@@ -67,5 +107,29 @@ private static String toEnvVarName(String propertyName) { |
67 | 107 | return propertyName.toUpperCase(Locale.ROOT).replace('-', '_').replace('.', '_'); |
68 | 108 | } |
69 | 109 |
|
| 110 | + @Nullable |
| 111 | + private static DeclarativeConfigProperties getDeclarativeConfigNode( |
| 112 | + OpenTelemetry openTelemetry, String... propertyName) { |
| 113 | + if (isIncubator && openTelemetry instanceof ExtendedOpenTelemetry) { |
| 114 | + ExtendedOpenTelemetry extendedOpenTelemetry = (ExtendedOpenTelemetry) openTelemetry; |
| 115 | + ConfigProvider configProvider = extendedOpenTelemetry.getConfigProvider(); |
| 116 | + DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig(); |
| 117 | + if (instrumentationConfig == null) { |
| 118 | + return empty(); |
| 119 | + } |
| 120 | + DeclarativeConfigProperties node = instrumentationConfig.getStructured("java", empty()); |
| 121 | + // last part is the leaf property |
| 122 | + for (int i = 0; i < propertyName.length - 1; i++) { |
| 123 | + node = node.getStructured(propertyName[i], empty()); |
| 124 | + } |
| 125 | + return node; |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + private static String toSystemProperty(String[] propertyName) { |
| 131 | + return String.join(".", propertyName).replace('_', '-'); |
| 132 | + } |
| 133 | + |
70 | 134 | private ConfigPropertiesUtil() {} |
71 | 135 | } |
0 commit comments