Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix MapConverter bean condition #10346

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -32,15 +32,17 @@
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.expression.spel.standard.SpelExpressionParser;
Expand All @@ -65,17 +67,31 @@ public static class OpenTelemetrySdkConfig {

@Bean
@ConfigurationPropertiesBinding
@ConditionalOnBean({
OtelResourceAutoConfiguration.class,
OtlpLoggerExporterAutoConfiguration.class,
OtlpSpanExporterAutoConfiguration.class,
OtlpMetricExporterAutoConfiguration.class
})
@Conditional(MapConverterCondition.class)
public MapConverter mapConverter() {
// needed for otlp exporter headers and OtelResourceProperties
return new MapConverter();
}

static final class MapConverterCondition implements Condition {

private static final List<Class<?>> ANY_REQUIRED_BEANS =
Arrays.asList(
OtelResourceAutoConfiguration.class,
OtlpLoggerExporterAutoConfiguration.class,
OtlpSpanExporterAutoConfiguration.class,
OtlpMetricExporterAutoConfiguration.class);

@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {

return ANY_REQUIRED_BEANS.stream()
.anyMatch(s -> context.getBeanFactory().getBeanProvider(s).getIfAvailable() != null);
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Bean(destroyMethod = "") // SDK components are shutdown from the OpenTelemetry instance
@ConditionalOnMissingBean
public SdkTracerProvider sdkTracerProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@
OtelSpringStarterSmokeTest.TestConfiguration.class
},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"otel.exporter.otlp.enabled=false", "otel.metric.export.interval=100"
properties = {
"otel.exporter.otlp.enabled=false",
"otel.metric.export.interval=100",
"otel.exporter.otlp.headers=a=1,b=2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this test in the OpenTelemetryAutoConfigurationTest class? In this way, all the property configurations related to the OpenTelemetryAutoConfiguration will be tested in the same class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do that - then found out that context runner works differently - bean conditions are only evaluated with a @SpringBootTest

// We set the export interval of the metrics to 100 ms. The default value is 1 minute.
// the headers are simply set here to make sure that headers can be parsed, even with
// otel.exporter.otlp.enabled=false
})
class OtelSpringStarterSmokeTest {

Expand Down
Loading