Skip to content

Commit cb4dd0b

Browse files
committed
fix map converter
1 parent 9113598 commit cb4dd0b

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,25 @@ public class OpenTelemetryAutoConfiguration {
5757

5858
public OpenTelemetryAutoConfiguration() {}
5959

60+
@Bean
61+
@ConfigurationPropertiesBinding
62+
public OtelMapConverter otelMapConverter() {
63+
// This is needed for otlp exporter headers and OtelResourceProperties.
64+
65+
// We need this converter, even if the SDK is disabled,
66+
// because the properties are parsed before the SDK is disabled.
67+
68+
// We also need this converter if the OpenTelemetry bean is user supplied,
69+
// because the environment variables may still contain a value that needs to be converted,
70+
// even if the SDK is disabled (and the value thus ignored).
71+
return new OtelMapConverter();
72+
}
73+
6074
@Configuration
6175
@Conditional(SdkEnabled.class)
6276
@ConditionalOnMissingBean(OpenTelemetry.class)
6377
static class OpenTelemetrySdkConfig {
6478

65-
@Bean
66-
@ConfigurationPropertiesBinding
67-
public OtelMapConverter otelMapConverter() {
68-
// needed for otlp exporter headers and OtelResourceProperties
69-
return new OtelMapConverter();
70-
}
71-
7279
@Bean
7380
public OpenTelemetrySdkComponentLoader openTelemetrySdkComponentLoader(
7481
ApplicationContext applicationContext) {
@@ -132,18 +139,6 @@ public ConfigProperties otelProperties(
132139
@ConditionalOnMissingBean(OpenTelemetry.class)
133140
@ConditionalOnProperty(name = "otel.sdk.disabled", havingValue = "true")
134141
static class DisabledOpenTelemetrySdkConfig {
135-
136-
@Bean
137-
@ConfigurationPropertiesBinding
138-
// Duplicated in OpenTelemetrySdkConfig and DisabledOpenTelemetrySdkConfig to not expose the
139-
// converter in the public API
140-
public OtelMapConverter otelMapConverter() {
141-
// needed for otlp exporter headers and OtelResourceProperties
142-
// we need this converter, even if the SDK is disabled,
143-
// because the properties are parsed before the SDK is disabled
144-
return new OtelMapConverter();
145-
}
146-
147142
@Bean
148143
public OpenTelemetry openTelemetry() {
149144
return OpenTelemetry.noop();

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)