Relocate OpenTelemetryLoggingAutoConfiguration and SdkLoggerProviderBuilderCustomizer to an OpenTelemetry-specific package

Closes gh-44647
This commit is contained in:
Moritz Halbritter
2025-04-08 11:01:48 +02:00
parent 62e145e7fd
commit 2ca0897b88
9 changed files with 190 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,46 +16,17 @@
package org.springframework.boot.actuate.autoconfigure.logging;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.resources.Resource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry logging.
*
* @author Toshiaki Maki
* @since 3.4.0
* @deprecated since 3.5.0 for removal in 4.0.0 in favor of
* {@link org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.OpenTelemetryLoggingAutoConfiguration}
*/
@AutoConfiguration
@ConditionalOnClass({ SdkLoggerProvider.class, OpenTelemetry.class })
@Deprecated(since = "3.4.0", forRemoval = true)
public class OpenTelemetryLoggingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
BatchLogRecordProcessor batchLogRecordProcessor(ObjectProvider<LogRecordExporter> logRecordExporters) {
return BatchLogRecordProcessor.builder(LogRecordExporter.composite(logRecordExporters.orderedStream().toList()))
.build();
}
@Bean
@ConditionalOnMissingBean
SdkLoggerProvider otelSdkLoggerProvider(Resource resource, ObjectProvider<LogRecordProcessor> logRecordProcessors,
ObjectProvider<SdkLoggerProviderBuilderCustomizer> customizers) {
SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder().setResource(resource);
logRecordProcessors.orderedStream().forEach(builder::addLogRecordProcessor);
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,8 +25,11 @@ import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
*
* @author Toshiaki Maki
* @since 3.4.0
* @deprecated since 3.5.0 for removal in 4.0.0 in favor of
* {@link org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.SdkLoggerProviderBuilderCustomizer}
*/
@FunctionalInterface
@Deprecated(since = "3.5.0", forRemoval = true)
public interface SdkLoggerProviderBuilderCustomizer {
/**

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.resources.Resource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry logging.
*
* @author Toshiaki Maki
* @since 3.5.0
*/
@AutoConfiguration
@ConditionalOnClass({ SdkLoggerProvider.class, OpenTelemetry.class })
public class OpenTelemetryLoggingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
BatchLogRecordProcessor batchLogRecordProcessor(ObjectProvider<LogRecordExporter> logRecordExporters) {
return BatchLogRecordProcessor.builder(LogRecordExporter.composite(logRecordExporters.orderedStream().toList()))
.build();
}
@Bean
@ConditionalOnMissingBean
@SuppressWarnings("removal")
SdkLoggerProvider otelSdkLoggerProvider(Resource resource, ObjectProvider<LogRecordProcessor> logRecordProcessors,
ObjectProvider<SdkLoggerProviderBuilderCustomizer> customizers,
ObjectProvider<org.springframework.boot.actuate.autoconfigure.logging.SdkLoggerProviderBuilderCustomizer> deprecatedCustomizers) {
SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder().setResource(resource);
logRecordProcessors.orderedStream().forEach(builder::addLogRecordProcessor);
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
deprecatedCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
/**
* Callback interface that can be used to customize the {@link SdkLoggerProviderBuilder}
* that is used to create the auto-configured {@link SdkLoggerProvider}.
*
* @author Toshiaki Maki
* @since 3.5.0
*/
@FunctionalInterface
public interface SdkLoggerProviderBuilderCustomizer {
/**
* Customize the given {@code builder}.
* @param builder the builder to customize
*/
void customize(SdkLoggerProviderBuilder builder);
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for logging over OpenTelemetry.
*/
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;

View File

@@ -34,7 +34,7 @@ org.springframework.boot.actuate.autoconfigure.ldap.LdapHealthContributorAutoCon
org.springframework.boot.actuate.autoconfigure.liquibase.LiquibaseEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.LoggersEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.OpenTelemetryLoggingAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.OpenTelemetryLoggingAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingAutoConfiguration
org.springframework.boot.actuate.autoconfigure.mail.MailHealthContributorAutoConfiguration
org.springframework.boot.actuate.autoconfigure.management.HeapDumpWebEndpointAutoConfiguration

View File

@@ -1,2 +1,3 @@
org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryAutoConfiguration=org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryTracingAutoConfiguration
org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration=org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingAutoConfiguration
org.springframework.boot.actuate.autoconfigure.logging.OpenTelemetryLoggingAutoConfiguration=org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.OpenTelemetryLoggingAutoConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.logging;
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
@@ -45,6 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link OpenTelemetryLoggingAutoConfiguration}.
*
* @author Toshiaki Maki
* @author Moritz Halbritter
*/
class OpenTelemetryLoggingAutoConfigurationTests {
@@ -120,16 +121,33 @@ class OpenTelemetryLoggingAutoConfigurationTests {
});
}
@Test
@SuppressWarnings("removal")
void shouldApplyDeprecatedCustomizers() {
this.contextRunner.withUserConfiguration(DeprecatedSdkLoggerProviderBuilderCustomizerConfig.class)
.run((context) -> {
assertThat(context).hasSingleBean(SdkLoggerProvider.class);
assertThat(context.getBeansOfType(
org.springframework.boot.actuate.autoconfigure.logging.SdkLoggerProviderBuilderCustomizer.class))
.hasSize(1);
assertThat(context).hasBean("deprecatedSdkLoggerProviderBuilderCustomizer");
assertThat(context
.getBean("deprecatedSdkLoggerProviderBuilderCustomizer",
DeprecatedNoopSdkLoggerProviderBuilderCustomizer.class)
.called()).isEqualTo(1);
});
}
@Configuration(proxyBeanMethods = false)
public static class CustomConfig {
@Bean
public BatchLogRecordProcessor customBatchLogRecordProcessor() {
BatchLogRecordProcessor customBatchLogRecordProcessor() {
return BatchLogRecordProcessor.builder(new NoopLogRecordExporter()).build();
}
@Bean
public SdkLoggerProvider customSdkLoggerProvider() {
SdkLoggerProvider customSdkLoggerProvider() {
return SdkLoggerProvider.builder().build();
}
@@ -139,12 +157,12 @@ class OpenTelemetryLoggingAutoConfigurationTests {
public static class MultipleLogRecordExportersConfig {
@Bean
public LogRecordExporter customLogRecordExporter1() {
LogRecordExporter customLogRecordExporter1() {
return new NoopLogRecordExporter();
}
@Bean
public LogRecordExporter customLogRecordExporter2() {
LogRecordExporter customLogRecordExporter2() {
return new NoopLogRecordExporter();
}
@@ -154,12 +172,12 @@ class OpenTelemetryLoggingAutoConfigurationTests {
public static class MultipleLogRecordProcessorsConfig {
@Bean
public LogRecordProcessor customLogRecordProcessor1() {
LogRecordProcessor customLogRecordProcessor1() {
return new NoopLogRecordProcessor();
}
@Bean
public LogRecordProcessor customLogRecordProcessor2() {
LogRecordProcessor customLogRecordProcessor2() {
return new NoopLogRecordProcessor();
}
@@ -169,17 +187,27 @@ class OpenTelemetryLoggingAutoConfigurationTests {
public static class MultipleSdkLoggerProviderBuilderCustomizersConfig {
@Bean
public SdkLoggerProviderBuilderCustomizer customSdkLoggerProviderBuilderCustomizer1() {
SdkLoggerProviderBuilderCustomizer customSdkLoggerProviderBuilderCustomizer1() {
return new NoopSdkLoggerProviderBuilderCustomizer();
}
@Bean
public SdkLoggerProviderBuilderCustomizer customSdkLoggerProviderBuilderCustomizer2() {
SdkLoggerProviderBuilderCustomizer customSdkLoggerProviderBuilderCustomizer2() {
return new NoopSdkLoggerProviderBuilderCustomizer();
}
}
@Configuration(proxyBeanMethods = false)
public static class DeprecatedSdkLoggerProviderBuilderCustomizerConfig {
@Bean
DeprecatedNoopSdkLoggerProviderBuilderCustomizer deprecatedSdkLoggerProviderBuilderCustomizer() {
return new DeprecatedNoopSdkLoggerProviderBuilderCustomizer();
}
}
static class NoopLogRecordExporter implements LogRecordExporter {
@Override
@@ -210,7 +238,24 @@ class OpenTelemetryLoggingAutoConfigurationTests {
static class NoopSdkLoggerProviderBuilderCustomizer implements SdkLoggerProviderBuilderCustomizer {
final AtomicInteger called = new AtomicInteger(0);
private final AtomicInteger called = new AtomicInteger(0);
@Override
public void customize(SdkLoggerProviderBuilder builder) {
this.called.incrementAndGet();
}
int called() {
return this.called.get();
}
}
@SuppressWarnings("removal")
static class DeprecatedNoopSdkLoggerProviderBuilderCustomizer
implements org.springframework.boot.actuate.autoconfigure.logging.SdkLoggerProviderBuilderCustomizer {
private final AtomicInteger called = new AtomicInteger(0);
@Override
public void customize(SdkLoggerProviderBuilder builder) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.logging.OpenTelemetryLoggingAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.OpenTelemetryLoggingAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;