Skip to content

Commit 188acb4

Browse files
committed
fix env var override for spring starter declarative config
1 parent 1951761 commit 188acb4

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ testing {
180180
dependencies {
181181
implementation(project())
182182
implementation("io.opentelemetry:opentelemetry-sdk")
183+
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
183184
implementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
184185
exclude("org.junit.vintage", "junit-vintage-engine")
185186
}

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/testDeclarativeConfig/java/io/opentelemetry/instrumentation/spring/autoconfigure/DeclarativeConfigTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,23 @@ void shouldNotLoadInstrumentationWhenExplicitlyDisabled() {
131131
"otel.instrumentation/development.java.spring_web.enabled=false")
132132
.run(context -> assertThat(context).doesNotHaveBean("otelRestTemplateBeanPostProcessor"));
133133
}
134+
135+
@Test
136+
void envVarOverride() {
137+
this.contextRunner
138+
// this is typically set via env var
139+
// OTEL_TRACER_PROVIDER_PROCESSORS_0_BATCH_EXPORTER_OTLP_HTTP_ENDPOINT
140+
.withSystemProperties(
141+
"OTEL_TRACER_PROVIDER_PROCESSORS_0_BATCH_EXPORTER_OTLP_HTTP_ENDPOINT=http://custom:4318/v1/traces")
142+
.run(
143+
context ->
144+
assertThat(context)
145+
.getBean(OpenTelemetry.class)
146+
.isNotNull()
147+
.satisfies(
148+
c ->
149+
assertThat(c.toString())
150+
.contains(
151+
"OtlpHttpSpanExporter{endpoint=http://custom:4318/v1/traces")));
152+
}
134153
}

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: 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)