Skip to content

Commit d1ba858

Browse files
authored
Align SpringConfigProperties with DefaultConfigProperties (#12398)
1 parent d646ad6 commit d1ba858

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigProperties.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties;
77

8+
import io.opentelemetry.api.internal.ConfigUtil;
89
import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
910
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1011
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
@@ -65,8 +66,9 @@ public static ConfigProperties create(
6566
@Nullable
6667
@Override
6768
public String getString(String name) {
68-
String value = environment.getProperty(name, String.class);
69-
if (value == null && name.equals("otel.exporter.otlp.protocol")) {
69+
String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name);
70+
String value = environment.getProperty(normalizedName, String.class);
71+
if (value == null && normalizedName.equals("otel.exporter.otlp.protocol")) {
7072
// SDK autoconfigure module defaults to `grpc`, but this module aligns with recommendation
7173
// in specification to default to `http/protobuf`
7274
return OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF;
@@ -77,35 +79,46 @@ public String getString(String name) {
7779
@Nullable
7880
@Override
7981
public Boolean getBoolean(String name) {
80-
return or(environment.getProperty(name, Boolean.class), otelSdkProperties.getBoolean(name));
82+
return or(
83+
environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Boolean.class),
84+
otelSdkProperties.getBoolean(name));
8185
}
8286

8387
@Nullable
8488
@Override
8589
public Integer getInt(String name) {
86-
return or(environment.getProperty(name, Integer.class), otelSdkProperties.getInt(name));
90+
return or(
91+
environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Integer.class),
92+
otelSdkProperties.getInt(name));
8793
}
8894

8995
@Nullable
9096
@Override
9197
public Long getLong(String name) {
92-
return or(environment.getProperty(name, Long.class), otelSdkProperties.getLong(name));
98+
return or(
99+
environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Long.class),
100+
otelSdkProperties.getLong(name));
93101
}
94102

95103
@Nullable
96104
@Override
97105
public Double getDouble(String name) {
98-
return or(environment.getProperty(name, Double.class), otelSdkProperties.getDouble(name));
106+
return or(
107+
environment.getProperty(ConfigUtil.normalizeEnvironmentVariableKey(name), Double.class),
108+
otelSdkProperties.getDouble(name));
99109
}
100110

101111
@SuppressWarnings("unchecked")
102112
@Override
103113
public List<String> getList(String name) {
104-
if (name.equals("otel.propagators")) {
114+
115+
String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name);
116+
117+
if (normalizedName.equals("otel.propagators")) {
105118
return propagationProperties.getPropagators();
106119
}
107120

108-
return or(environment.getProperty(name, List.class), otelSdkProperties.getList(name));
121+
return or(environment.getProperty(normalizedName, List.class), otelSdkProperties.getList(name));
109122
}
110123

111124
@Nullable
@@ -123,8 +136,10 @@ public Duration getDuration(String name) {
123136
@Override
124137
public Map<String, String> getMap(String name) {
125138
Map<String, String> otelSdkMap = otelSdkProperties.getMap(name);
139+
140+
String normalizedName = ConfigUtil.normalizeEnvironmentVariableKey(name);
126141
// maps from config properties are not supported by Environment, so we have to fake it
127-
switch (name) {
142+
switch (normalizedName) {
128143
case "otel.resource.attributes":
129144
return mergeWithOtel(resourceProperties.getAttributes(), otelSdkMap);
130145
case "otel.exporter.otlp.headers":
@@ -139,7 +154,7 @@ public Map<String, String> getMap(String name) {
139154
break;
140155
}
141156

142-
String value = environment.getProperty(name);
157+
String value = environment.getProperty(normalizedName);
143158
if (value == null) {
144159
return otelSdkMap;
145160
}

smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractOtelSpringStarterSmokeTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ AutoConfigurationCustomizerProvider propagatorCustomizer() {
104104
Attributes.of(
105105
AttributeKey.booleanKey("keyFromResourceCustomizer"), true))));
106106
}
107+
108+
@Bean
109+
AutoConfigurationCustomizerProvider customizerUsingPropertyDefinedInaSpringFile() {
110+
return customizer ->
111+
customizer.addResourceCustomizer(
112+
(resource, config) -> {
113+
String valueForKeyDeclaredZsEnvVariable = config.getString("APPLICATION_PROP");
114+
assertThat(valueForKeyDeclaredZsEnvVariable).isNotEmpty();
115+
116+
String valueForKeyWithDash = config.getString("application.prop-with-dash");
117+
assertThat(valueForKeyWithDash).isNotEmpty();
118+
119+
return resource;
120+
});
121+
}
107122
}
108123

109124
@Test

smoke-tests-otel-starter/spring-boot-common/src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ otel:
1919
attributes:
2020
attributeFromYaml: true # boolean will be automatically converted to string by spring
2121

22+
application:
23+
prop: propValue
24+
prop-with-dash: provWithDashValue
25+
2226
spring:
2327
kafka:
2428
bootstrap-servers: localhost:9094

0 commit comments

Comments
 (0)