Skip to content

Commit 32715f0

Browse files
zeitlinger123liuziming
authored andcommitted
fix env var override for spring starter declarative config (open-telemetry#15424)
1 parent c2eae2b commit 32715f0

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ testing {
190190
dependencies {
191191
implementation(project())
192192
implementation("io.opentelemetry:opentelemetry-sdk")
193+
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
193194
implementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
194195
exclude("org.junit.vintage", "junit-vintage-engine")
195196
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/EmbeddedConfigFile.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
1212
import java.util.ArrayList;
1313
import java.util.HashMap;
14+
import java.util.Locale;
1415
import java.util.Map;
1516
import java.util.Objects;
1617
import java.util.regex.Matcher;
@@ -62,6 +63,24 @@ private static Map<String, String> extractSpringProperties(ConfigurableEnvironme
6263
if (Objects.equals(property, "")) {
6364
property = null; // spring returns empty string for yaml null
6465
}
66+
if (propertyName.contains("[")) {
67+
// fix override via env var or system property
68+
// see
69+
// https://docs.spring.io/spring-boot/reference/features/external-config.html#features.external-config.typesafe-configuration-properties.relaxed-binding
70+
// For example, the configuration property my.service[0].other would use an
71+
// environment variable named MY_SERVICE_0_OTHER.
72+
String envVarName =
73+
propertyName
74+
.replace("[", "_")
75+
.replace("]", "")
76+
.replace(".", "_")
77+
.toUpperCase(Locale.ROOT);
78+
String envVarValue = environment.getProperty(envVarName);
79+
if (envVarValue != null) {
80+
property = envVarValue;
81+
}
82+
}
83+
6584
props.put(propertyName.substring("otel.".length()), property);
6685
}
6786
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
4040
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizerProvider;
4141
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
42-
import java.io.IOException;
4342
import java.util.Collections;
4443
import java.util.List;
4544
import java.util.Optional;
@@ -157,7 +156,7 @@ static class EmbeddedConfigFileConfig {
157156

158157
@Bean
159158
public OpenTelemetryConfigurationModel openTelemetryConfigurationModel(
160-
ConfigurableEnvironment environment) throws IOException {
159+
ConfigurableEnvironment environment) {
161160
return EmbeddedConfigFile.extractModel(environment);
162161
}
163162

instrumentation/spring/spring-boot-autoconfigure/src/testDeclarativeConfig/java/io/opentelemetry/instrumentation/spring/autoconfigure/DeclarativeConfigTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,39 @@ void shouldNotLoadInstrumentationWhenExplicitlyDisabled() {
131131
"otel.instrumentation/development.java.spring_web.enabled=false")
132132
.run(context -> assertThat(context).doesNotHaveBean("otelRestTemplateBeanPostProcessor"));
133133
}
134+
135+
@Test
136+
void envVarOverrideSpringStyle() {
137+
this.contextRunner
138+
// this is typically set via env var
139+
.withSystemProperties(
140+
"OTEL_TRACER_PROVIDER_PROCESSORS_0_BATCH_EXPORTER_OTLP_HTTP_ENDPOINT=http://custom:4318/v1/traces")
141+
.run(
142+
context ->
143+
assertThat(context)
144+
.getBean(OpenTelemetry.class)
145+
.isNotNull()
146+
.satisfies(
147+
c ->
148+
assertThat(c.toString())
149+
.contains(
150+
"OtlpHttpSpanExporter{endpoint=http://custom:4318/v1/traces")));
151+
}
152+
153+
@Test
154+
void envVarOverrideOtelStyle() {
155+
this.contextRunner
156+
// this is typically set via env var
157+
.withSystemProperties("OTEL_EXPORTER_OTLP_ENDPOINT=http://custom:4318")
158+
.run(
159+
context ->
160+
assertThat(context)
161+
.getBean(OpenTelemetry.class)
162+
.isNotNull()
163+
.satisfies(
164+
c ->
165+
assertThat(c.toString())
166+
.contains(
167+
"OtlpHttpSpanExporter{endpoint=http://custom:4318/v1/traces")));
168+
}
134169
}

instrumentation/spring/spring-boot-autoconfigure/src/testDeclarativeConfig/resources/application.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ otel:
22
# "file_format" serves as opt-in to the new file format
33
file_format: "1.0-rc.1"
44

5+
tracer_provider:
6+
processors:
7+
- batch:
8+
exporter:
9+
otlp_http:
10+
endpoint: ${OTEL_EXPORTER_OTLP_ENDPOINT:http://localhost:4318}/v1/traces # to test that we can use env vars in declarative config
11+
512
# very lightweight test to make sure the declarative config is loaded
613
# the full config is tested in smoke-tests-otel-starter/spring-boot-2/src/testDeclarativeConfig
714
instrumentation/development:

0 commit comments

Comments
 (0)