Skip to content

Commit 0138d4f

Browse files
committed
use optional instead of providing default for string
1 parent d520185 commit 0138d4f

File tree

8 files changed

+39
-63
lines changed

8 files changed

+39
-63
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ SpanSuppressor buildSpanSuppressor() {
373373
String value =
374374
ConfigPropertiesUtil.isDeclarativeConfig(openTelemetry)
375375
? ConfigPropertiesUtil.getString(
376-
openTelemetry, "common", "experimental", "span_suppression_strategy")
376+
openTelemetry, "common", "experimental", "span_suppression_strategy")
377+
.orElse(null)
377378
: ConfigPropertiesUtil.getString(
378-
"otel.instrumentation.experimental.span-suppression-strategy");
379+
"otel.instrumentation.experimental.span-suppression-strategy")
380+
.orElse(null);
379381
return new SpanSuppressors.ByContextKey(
380382
SpanSuppressionStrategy.fromConfig(value).create(getSpanKeysFromAttributesExtractors()));
381383
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtil.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static boolean supportsDeclarativeConfig() {
4949
* Declarative Config.
5050
*/
5151
public static Optional<Boolean> getBoolean(String propertyName) {
52-
return Optional.ofNullable(getString(propertyName)).map(Boolean::parseBoolean);
52+
return getString(propertyName).map(Boolean::parseBoolean);
5353
}
5454

5555
/**
@@ -69,7 +69,7 @@ public static Optional<Boolean> getBoolean(OpenTelemetry openTelemetry, String..
6969
* variables.
7070
*/
7171
public static Optional<Integer> getInt(String propertyName) {
72-
return Optional.ofNullable(getString(propertyName))
72+
return getString(propertyName)
7373
.flatMap(
7474
s -> {
7575
try {
@@ -87,41 +87,26 @@ public static Optional<Integer> getInt(String propertyName) {
8787
* <p>It's recommended to use {@link #getString(OpenTelemetry, String...)} instead to support
8888
* Declarative Config.
8989
*/
90-
@Nullable
91-
public static String getString(String propertyName) {
90+
public static Optional<String> getString(String propertyName) {
9291
String value = System.getProperty(propertyName);
9392
if (value != null) {
94-
return value;
93+
return Optional.of(value);
9594
}
96-
return System.getenv(toEnvVarName(propertyName));
95+
return Optional.ofNullable(System.getenv(toEnvVarName(propertyName)));
9796
}
9897

9998
/**
10099
* Returns the string value of the given property name from Declarative Config if available,
101100
* otherwise falls back to system properties and environment variables.
102101
*/
103-
@Nullable
104-
public static String getString(OpenTelemetry openTelemetry, String... propertyName) {
102+
public static Optional<String> getString(OpenTelemetry openTelemetry, String... propertyName) {
105103
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
106104
if (node != null) {
107-
return node.getString(leaf(propertyName));
105+
return Optional.ofNullable(node.getString(leaf(propertyName)));
108106
}
109107
return getString(toSystemProperty(propertyName));
110108
}
111109

112-
/**
113-
* Returns the string value of the given property name from Declarative Config if available,
114-
* otherwise falls back to system properties and environment variables.
115-
*/
116-
public static String getStringOrFallback(
117-
OpenTelemetry openTelemetry, String defaultValue, String... propertyName) {
118-
String value = getString(openTelemetry, propertyName);
119-
if (value == null) {
120-
return defaultValue;
121-
}
122-
return value;
123-
}
124-
125110
/**
126111
* Returns the list of strings value of the given property name from Declarative Config if
127112
* available, otherwise falls back to system properties and environment variables.
@@ -131,11 +116,9 @@ public static List<String> getList(OpenTelemetry openTelemetry, String... proper
131116
if (node != null) {
132117
return node.getScalarList(leaf(propertyName), String.class, emptyList());
133118
}
134-
String value = getString(toSystemProperty(propertyName));
135-
if (value == null) {
136-
return emptyList();
137-
}
138-
return filterBlanksAndNulls(value.split(","));
119+
return getString(toSystemProperty(propertyName))
120+
.map(value -> filterBlanksAndNulls(value.split(",")))
121+
.orElse(emptyList());
139122
}
140123

141124
/** Returns true if the given OpenTelemetry instance supports Declarative Config. */

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/SemconvStability.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class SemconvStability {
3131
boolean oldCode = true;
3232
boolean stableCode = false;
3333

34-
String value = ConfigPropertiesUtil.getString("otel.semconv-stability.opt-in");
34+
String value = ConfigPropertiesUtil.getString("otel.semconv-stability.opt-in").orElse(null);
3535
if (value != null) {
3636
Set<String> values = new HashSet<>(asList(value.split(",")));
3737

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ class ConfigPropertiesUtilTest {
3232
@SetSystemProperty(key = "test.property.string", value = "sys")
3333
@Test
3434
void getString_systemProperty() {
35-
assertThat(ConfigPropertiesUtil.getString("test.property.string")).isEqualTo("sys");
35+
assertThat(ConfigPropertiesUtil.getString("test.property.string")).hasValue("sys");
3636
}
3737

3838
@SetEnvironmentVariable(key = "TEST_PROPERTY_STRING", value = "env")
3939
@Test
4040
void getString_environmentVariable() {
41-
assertThat(ConfigPropertiesUtil.getString("test.property.string")).isEqualTo("env");
41+
assertThat(ConfigPropertiesUtil.getString("test.property.string")).hasValue("env");
4242
}
4343

4444
@Test
4545
void getString_none() {
46-
assertThat(ConfigPropertiesUtil.getString("test.property.string")).isNull();
46+
assertThat(ConfigPropertiesUtil.getString("test.property.string")).isEmpty();
4747
}
4848

4949
@SetEnvironmentVariable(key = "OTEL_INSTRUMENTATION_TEST_PROPERTY_STRING", value = "env_value")
@@ -66,8 +66,9 @@ void getString_withOpenTelemetry_none() {
6666

6767
private static void assertString(String expected) {
6868
assertThat(
69-
ConfigPropertiesUtil.getStringOrFallback(
70-
OpenTelemetry.noop(), "default_value", "test", "property", "string"))
69+
ConfigPropertiesUtil.getString(
70+
OpenTelemetry.noop(), new String[] {"test", "property", "string"})
71+
.orElse("default_value"))
7172
.isEqualTo(expected);
7273
}
7374

@@ -83,9 +84,10 @@ public static Stream<Arguments> stringValuesProvider() {
8384
@ParameterizedTest
8485
@MethodSource("stringValuesProvider")
8586
void getString_declarativeConfig(Object property, String expected) {
87+
OpenTelemetry openTelemetry = DeclarativeConfiguration.create(model(property));
8688
assertThat(
87-
ConfigPropertiesUtil.getStringOrFallback(
88-
DeclarativeConfiguration.create(model(property)), "default_value", "foo", "bar"))
89+
ConfigPropertiesUtil.getString(openTelemetry, new String[] {"foo", "bar"})
90+
.orElse("default_value"))
8991
.isEqualTo(expected);
9092
}
9193

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/internal/ContextDataKeys.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,16 @@
1515
*/
1616
public final class ContextDataKeys {
1717
public static final String TRACE_ID_KEY =
18-
ConfigPropertiesUtil.getStringOrFallback(
19-
GlobalOpenTelemetry.get(),
20-
LoggingContextConstants.TRACE_ID,
21-
"common",
22-
"logging",
23-
"trace_id");
18+
ConfigPropertiesUtil.getString(GlobalOpenTelemetry.get(), "common", "logging", "trace_id")
19+
.orElse(LoggingContextConstants.TRACE_ID);
20+
2421
public static final String SPAN_ID_KEY =
25-
ConfigPropertiesUtil.getStringOrFallback(
26-
GlobalOpenTelemetry.get(),
27-
LoggingContextConstants.SPAN_ID,
28-
"common",
29-
"logging",
30-
"span_id");
22+
ConfigPropertiesUtil.getString(GlobalOpenTelemetry.get(), "common", "logging", "span_id")
23+
.orElse(LoggingContextConstants.SPAN_ID);
24+
3125
public static final String TRACE_FLAGS_KEY =
32-
ConfigPropertiesUtil.getStringOrFallback(
33-
GlobalOpenTelemetry.get(),
34-
LoggingContextConstants.TRACE_FLAGS,
35-
"common",
36-
"logging",
37-
"trace_flags");
26+
ConfigPropertiesUtil.getString(GlobalOpenTelemetry.get(), "common", "logging", "trace_flags")
27+
.orElse(LoggingContextConstants.TRACE_FLAGS);
3828

3929
private ContextDataKeys() {}
4030
}

instrumentation/servlet/servlet-common/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/servlet/ExperimentalSnippetHolder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88
import io.opentelemetry.api.GlobalOpenTelemetry;
99
import io.opentelemetry.api.OpenTelemetry;
1010
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
11+
import java.util.Optional;
1112

1213
public class ExperimentalSnippetHolder {
1314

1415
private static volatile String snippet = getSnippetSetting();
1516

1617
private static String getSnippetSetting() {
1718
OpenTelemetry openTelemetry = GlobalOpenTelemetry.get();
18-
String result =
19+
Optional<String> result =
1920
// otel.experimental.* does not fit the usual pattern of configuration properties for
2021
// instrumentations, so we need to handle both declarative and non-declarative configs here
2122
ConfigPropertiesUtil.isDeclarativeConfig(openTelemetry)
2223
? ConfigPropertiesUtil.getString(
2324
openTelemetry, "servlet", "experimental", "javascript-snippet")
2425
: ConfigPropertiesUtil.getString("otel.experimental.javascript-snippet");
25-
return result == null ? "" : result;
26+
return result.orElse("");
2627
}
2728

2829
public static void setSnippet(String newValue) {

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigurationFile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ static void resetForTest() {
4747
// visible for tests
4848
static Map<String, String> loadConfigFile() {
4949
// Reading from system property first and from env after
50-
String configurationFilePath = ConfigPropertiesUtil.getString(CONFIGURATION_FILE_PROPERTY);
50+
String configurationFilePath =
51+
ConfigPropertiesUtil.getString(CONFIGURATION_FILE_PROPERTY).orElse(null);
5152
if (configurationFilePath == null) {
5253
return emptyMap();
5354
}

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/EarlyInitAgentConfig.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ private EarlyInitAgentConfig(Map<String, String> configFileContents) {
2727

2828
@Nullable
2929
public String getString(String propertyName) {
30-
String value = ConfigPropertiesUtil.getString(propertyName);
31-
if (value != null) {
32-
return value;
33-
}
34-
return configFileContents.get(propertyName);
30+
return ConfigPropertiesUtil.getString(propertyName)
31+
.orElseGet(() -> configFileContents.get(propertyName));
3532
}
3633

3734
public boolean getBoolean(String propertyName, boolean defaultValue) {

0 commit comments

Comments
 (0)