mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Move ConditionalOnEnabledLoggingExport to opentelemetry module
This commit moves the `ConditionalOnEnabledLoggingExport` condition from the "spring-boot-actuator-autoconfigure" to the "spring-boot-opentelemetry" one, because without that the logging export feature requires the actuator module to be on the classpath. Fixes gh-48488
This commit is contained in:
-51
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-present 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;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* {@link Conditional @Conditional} that checks whether logging export is enabled. It
|
||||
* matches if the value of the {@code management.logging.export.enabled} property is
|
||||
* {@code true} or if it is not configured. If the {@link #value() logging exporter name}
|
||||
* is set, the {@code management.logging.export.<name>.enabled} property can be used to
|
||||
* control the behavior for the specific logging exporter. In that case, the
|
||||
* exporter-specific property takes precedence over the global property.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Dmytro Nosan
|
||||
* @since 3.4.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnEnabledLoggingExportCondition.class)
|
||||
public @interface ConditionalOnEnabledLoggingExport {
|
||||
|
||||
/**
|
||||
* Name of the logging exporter.
|
||||
* @return the name of the logging exporter
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-present 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link SpringBootCondition} to check whether logging exporter is enabled.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Dmytro Nosan
|
||||
* @see ConditionalOnEnabledLoggingExport
|
||||
*/
|
||||
class OnEnabledLoggingExportCondition extends SpringBootCondition {
|
||||
|
||||
private static final String GLOBAL_PROPERTY = "management.logging.export.enabled";
|
||||
|
||||
private static final String EXPORTER_PROPERTY = "management.logging.export.%s.enabled";
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
String loggingExporter = getExporterName(metadata);
|
||||
if (StringUtils.hasLength(loggingExporter)) {
|
||||
String formattedExporterProperty = EXPORTER_PROPERTY.formatted(loggingExporter);
|
||||
Boolean exporterLoggingEnabled = context.getEnvironment()
|
||||
.getProperty(formattedExporterProperty, Boolean.class);
|
||||
if (exporterLoggingEnabled != null) {
|
||||
return new ConditionOutcome(exporterLoggingEnabled,
|
||||
ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
|
||||
.because(formattedExporterProperty + " is " + exporterLoggingEnabled));
|
||||
}
|
||||
}
|
||||
Boolean globalLoggingEnabled = context.getEnvironment().getProperty(GLOBAL_PROPERTY, Boolean.class);
|
||||
if (globalLoggingEnabled != null) {
|
||||
return new ConditionOutcome(globalLoggingEnabled,
|
||||
ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
|
||||
.because(GLOBAL_PROPERTY + " is " + globalLoggingEnabled));
|
||||
}
|
||||
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
|
||||
.because("is enabled by default"));
|
||||
}
|
||||
|
||||
private static @Nullable String getExporterName(AnnotatedTypeMetadata metadata) {
|
||||
Map<String, @Nullable Object> attributes = metadata
|
||||
.getAnnotationAttributes(ConditionalOnEnabledLoggingExport.class.getName());
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return (String) attributes.get("value");
|
||||
}
|
||||
|
||||
}
|
||||
-134
@@ -1,134 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-present 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;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link OnEnabledLoggingExportCondition}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
class OnEnabledLoggingExportConditionTests {
|
||||
|
||||
private static final String GLOBAL_PROPERTY_NAME = "management.logging.export.enabled";
|
||||
|
||||
private static final String OTLP_PROPERTY_NAME = "management.logging.export.otlp.enabled";
|
||||
|
||||
@Test
|
||||
void shouldMatchIfNoPropertyIsSet() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(), mockMetadata(""));
|
||||
assertThat(outcome.isMatch()).isTrue();
|
||||
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledLoggingExport is enabled by default");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotMatchIfGlobalPropertyIsFalse() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition
|
||||
.getMatchOutcome(mockConditionContext(Map.of(GLOBAL_PROPERTY_NAME, "false")), mockMetadata(""));
|
||||
assertThat(outcome.isMatch()).isFalse();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.enabled is false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMatchIfGlobalPropertyIsTrue() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(Map.of(GLOBAL_PROPERTY_NAME, "true")),
|
||||
mockMetadata(""));
|
||||
assertThat(outcome.isMatch()).isTrue();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.enabled is true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotMatchIfExporterPropertyIsFalse() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(Map.of(OTLP_PROPERTY_NAME, "false")),
|
||||
mockMetadata("otlp"));
|
||||
assertThat(outcome.isMatch()).isFalse();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.otlp.enabled is false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMatchIfExporterPropertyIsTrue() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(Map.of(OTLP_PROPERTY_NAME, "true")),
|
||||
mockMetadata("otlp"));
|
||||
assertThat(outcome.isMatch()).isTrue();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.otlp.enabled is true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void exporterPropertyShouldOverrideGlobalPropertyIfTrue() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(
|
||||
mockConditionContext(Map.of(GLOBAL_PROPERTY_NAME, "false", OTLP_PROPERTY_NAME, "true")),
|
||||
mockMetadata("otlp"));
|
||||
assertThat(outcome.isMatch()).isTrue();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.otlp.enabled is true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void exporterPropertyShouldOverrideGlobalPropertyIfFalse() {
|
||||
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
|
||||
ConditionOutcome outcome = condition.getMatchOutcome(
|
||||
mockConditionContext(Map.of(GLOBAL_PROPERTY_NAME, "true", OTLP_PROPERTY_NAME, "false")),
|
||||
mockMetadata("otlp"));
|
||||
assertThat(outcome.isMatch()).isFalse();
|
||||
assertThat(outcome.getMessage())
|
||||
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.otlp.enabled is false");
|
||||
}
|
||||
|
||||
private ConditionContext mockConditionContext() {
|
||||
return mockConditionContext(Collections.emptyMap());
|
||||
}
|
||||
|
||||
private ConditionContext mockConditionContext(Map<String, String> properties) {
|
||||
ConditionContext context = mock(ConditionContext.class);
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
properties.forEach(environment::setProperty);
|
||||
given(context.getEnvironment()).willReturn(environment);
|
||||
return context;
|
||||
}
|
||||
|
||||
private AnnotatedTypeMetadata mockMetadata(String exporter) {
|
||||
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
|
||||
given(metadata.getAnnotationAttributes(ConditionalOnEnabledLoggingExport.class.getName()))
|
||||
.willReturn(Map.of("value", exporter));
|
||||
return metadata;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user