Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;

/**
Expand All @@ -57,18 +58,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) {
Expand Down Expand Up @@ -129,21 +138,10 @@ public ConfigProperties otelProperties(
}

@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() {
return OpenTelemetry.noop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -77,6 +78,19 @@ void mapFlatHeaders(String key) {
.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")
Expand Down
Loading