Skip to content

Commit 71bce02

Browse files
authored
fix map converter (#13972)
1 parent 9d4754d commit 71bce02

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.context.annotation.Bean;
4242
import org.springframework.context.annotation.Conditional;
4343
import org.springframework.context.annotation.Configuration;
44+
import org.springframework.context.annotation.DependsOn;
4445
import org.springframework.core.env.Environment;
4546

4647
/**
@@ -62,18 +63,26 @@ public class OpenTelemetryAutoConfiguration {
6263

6364
public OpenTelemetryAutoConfiguration() {}
6465

66+
@Bean
67+
@ConfigurationPropertiesBinding
68+
OtelMapConverter otelMapConverter() {
69+
// This is needed for otlp exporter headers and OtelResourceProperties.
70+
71+
// We need this converter, even if the SDK is disabled,
72+
// because the properties are parsed before the SDK is disabled.
73+
74+
// We also need this converter if the OpenTelemetry bean is user supplied,
75+
// because the environment variables may still contain a value that needs to be converted,
76+
// even if the SDK is disabled (and the value thus ignored).
77+
return new OtelMapConverter();
78+
}
79+
6580
@Configuration
6681
@Conditional(SdkEnabled.class)
82+
@DependsOn("otelMapConverter")
6783
@ConditionalOnMissingBean(OpenTelemetry.class)
6884
static class OpenTelemetrySdkConfig {
6985

70-
@Bean
71-
@ConfigurationPropertiesBinding
72-
public OtelMapConverter otelMapConverter() {
73-
// needed for otlp exporter headers and OtelResourceProperties
74-
return new OtelMapConverter();
75-
}
76-
7786
@Bean
7887
public OpenTelemetrySdkComponentLoader openTelemetrySdkComponentLoader(
7988
ApplicationContext applicationContext) {
@@ -139,21 +148,10 @@ public ConfigProperties otelProperties(
139148
}
140149

141150
@Configuration
151+
@DependsOn("otelMapConverter")
142152
@ConditionalOnMissingBean(OpenTelemetry.class)
143153
@ConditionalOnProperty(name = "otel.sdk.disabled", havingValue = "true")
144154
static class DisabledOpenTelemetrySdkConfig {
145-
146-
@Bean
147-
@ConfigurationPropertiesBinding
148-
// Duplicated in OpenTelemetrySdkConfig and DisabledOpenTelemetrySdkConfig to not expose the
149-
// converter in the public API
150-
public OtelMapConverter otelMapConverter() {
151-
// needed for otlp exporter headers and OtelResourceProperties
152-
// we need this converter, even if the SDK is disabled,
153-
// because the properties are parsed before the SDK is disabled
154-
return new OtelMapConverter();
155-
}
156-
157155
@Bean
158156
public OpenTelemetry openTelemetry() {
159157
logger.info("OpenTelemetry Spring Boot starter has been disabled");

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/SpringConfigPropertiesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99
import static org.assertj.core.api.Assertions.entry;
1010

11+
import io.opentelemetry.api.OpenTelemetry;
1112
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
1213
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1314
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
@@ -77,6 +78,19 @@ void mapFlatHeaders(String key) {
7778
.containsExactly(entry("a", "1"), entry("b", "2")));
7879
}
7980

81+
@ParameterizedTest
82+
@MethodSource("headerKeys")
83+
@DisplayName("should map headers from spring properties with user supplied OpenTelemetry bean")
84+
void mapFlatHeadersWithUserSuppliedOtelBean(String key) {
85+
this.contextRunner
86+
.withSystemProperties(key + "=a=1,b=2")
87+
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
88+
.run(
89+
context ->
90+
assertThat(getConfig(context).getMap(key))
91+
.containsExactly(entry("a", "1"), entry("b", "2")));
92+
}
93+
8094
@ParameterizedTest
8195
@MethodSource("headerKeys")
8296
@DisplayName("should map headers from spring application.yaml")

0 commit comments

Comments
 (0)