fix map converter (#13972)

This commit is contained in:
Gregor Zeitlinger 2025-06-11 03:09:53 +02:00 committed by GitHub
parent 9d4754d976
commit 71bce02440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 19 deletions

View File

@ -41,6 +41,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
/**
@ -62,18 +63,26 @@ public class OpenTelemetryAutoConfiguration {
public OpenTelemetryAutoConfiguration() {}
@Bean
@ConfigurationPropertiesBinding
OtelMapConverter otelMapConverter() {
// This is needed for otlp exporter headers and OtelResourceProperties.
// We need this converter, even if the SDK is disabled,
// because the properties are parsed before the SDK is disabled.
// We also need this converter if the OpenTelemetry bean is user supplied,
// because the environment variables may still contain a value that needs to be converted,
// even if the SDK is disabled (and the value thus ignored).
return new OtelMapConverter();
}
@Configuration
@Conditional(SdkEnabled.class)
@DependsOn("otelMapConverter")
@ConditionalOnMissingBean(OpenTelemetry.class)
static class OpenTelemetrySdkConfig {
@Bean
@ConfigurationPropertiesBinding
public OtelMapConverter otelMapConverter() {
// needed for otlp exporter headers and OtelResourceProperties
return new OtelMapConverter();
}
@Bean
public OpenTelemetrySdkComponentLoader openTelemetrySdkComponentLoader(
ApplicationContext applicationContext) {
@ -139,21 +148,10 @@ public class OpenTelemetryAutoConfiguration {
}
@Configuration
@DependsOn("otelMapConverter")
@ConditionalOnMissingBean(OpenTelemetry.class)
@ConditionalOnProperty(name = "otel.sdk.disabled", havingValue = "true")
static class DisabledOpenTelemetrySdkConfig {
@Bean
@ConfigurationPropertiesBinding
// Duplicated in OpenTelemetrySdkConfig and DisabledOpenTelemetrySdkConfig to not expose the
// converter in the public API
public OtelMapConverter otelMapConverter() {
// needed for otlp exporter headers and OtelResourceProperties
// we need this converter, even if the SDK is disabled,
// because the properties are parsed before the SDK is disabled
return new OtelMapConverter();
}
@Bean
public OpenTelemetry openTelemetry() {
logger.info("OpenTelemetry Spring Boot starter has been disabled");

View File

@ -8,6 +8,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.internal.propertie
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
@ -77,6 +78,19 @@ class SpringConfigPropertiesTest {
.containsExactly(entry("a", "1"), entry("b", "2")));
}
@ParameterizedTest
@MethodSource("headerKeys")
@DisplayName("should map headers from spring properties with user supplied OpenTelemetry bean")
void mapFlatHeadersWithUserSuppliedOtelBean(String key) {
this.contextRunner
.withSystemProperties(key + "=a=1,b=2")
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
.run(
context ->
assertThat(getConfig(context).getMap(key))
.containsExactly(entry("a", "1"), entry("b", "2")));
}
@ParameterizedTest
@MethodSource("headerKeys")
@DisplayName("should map headers from spring application.yaml")