Skip to content

Commit 0ce4dbd

Browse files
committed
Polish "Add property to control log exporting"
See gh-42813
1 parent e9b3b97 commit 0ce4dbd

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.context.annotation.Conditional;
2626

2727
/**
28-
* {@link Conditional @Conditional} that checks whether logging exporter is enabled. It
28+
* {@link Conditional @Conditional} that checks whether logging export is enabled. It
2929
* matches if the value of the {@code management.logging.export.enabled} property is
3030
* {@code true} or if it is not configured. If the {@link #value() logging exporter name}
3131
* is set, the {@code management.<name>.logging.export.enabled} property can be used to
@@ -39,8 +39,8 @@
3939
@Retention(RetentionPolicy.RUNTIME)
4040
@Target({ ElementType.TYPE, ElementType.METHOD })
4141
@Documented
42-
@Conditional(OnEnabledLoggingCondition.class)
43-
public @interface ConditionalOnEnabledLogging {
42+
@Conditional(OnEnabledLoggingExportCondition.class)
43+
public @interface ConditionalOnEnabledLoggingExport {
4444

4545
/**
4646
* Name of the logging exporter.
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
*
3131
* @author Moritz Halbritter
3232
* @author Dmytro Nosan
33-
* @see ConditionalOnEnabledLogging
33+
* @see ConditionalOnEnabledLoggingExport
3434
*/
35-
class OnEnabledLoggingCondition extends SpringBootCondition {
35+
class OnEnabledLoggingExportCondition extends SpringBootCondition {
3636

3737
private static final String GLOBAL_PROPERTY = "management.logging.export.enabled";
3838

@@ -46,22 +46,23 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
4646
.getProperty(EXPORTER_PROPERTY.formatted(loggingExporter), Boolean.class);
4747
if (exporterLoggingEnabled != null) {
4848
return new ConditionOutcome(exporterLoggingEnabled,
49-
ConditionMessage.forCondition(ConditionalOnEnabledLogging.class)
49+
ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
5050
.because(EXPORTER_PROPERTY.formatted(loggingExporter) + " is " + exporterLoggingEnabled));
5151
}
5252
}
5353
Boolean globalLoggingEnabled = context.getEnvironment().getProperty(GLOBAL_PROPERTY, Boolean.class);
5454
if (globalLoggingEnabled != null) {
5555
return new ConditionOutcome(globalLoggingEnabled,
56-
ConditionMessage.forCondition(ConditionalOnEnabledLogging.class)
56+
ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
5757
.because(GLOBAL_PROPERTY + " is " + globalLoggingEnabled));
5858
}
59-
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledLogging.class)
60-
.because("logging is enabled by default"));
59+
return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnEnabledLoggingExport.class)
60+
.because("is enabled by default"));
6161
}
6262

6363
private static String getExporterName(AnnotatedTypeMetadata metadata) {
64-
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnabledLogging.class.getName());
64+
Map<String, Object> attributes = metadata
65+
.getAnnotationAttributes(ConditionalOnEnabledLoggingExport.class.getName());
6566
if (attributes == null) {
6667
return null;
6768
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/otlp/OtlpLoggingAutoConfiguration.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
2121
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
2222

23-
import org.springframework.boot.actuate.autoconfigure.logging.ConditionalOnEnabledLogging;
2423
import org.springframework.boot.autoconfigure.AutoConfiguration;
2524
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2625
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -37,7 +36,6 @@
3736
@ConditionalOnClass({ SdkLoggerProvider.class, OpenTelemetry.class, OtlpHttpLogRecordExporter.class })
3837
@EnableConfigurationProperties(OtlpLoggingProperties.class)
3938
@Import({ OtlpLoggingConfigurations.ConnectionDetails.class, OtlpLoggingConfigurations.Exporters.class })
40-
@ConditionalOnEnabledLogging("otlp")
4139
public class OtlpLoggingAutoConfiguration {
4240

4341
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/otlp/OtlpLoggingConfigurations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
2424
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder;
2525

26+
import org.springframework.boot.actuate.autoconfigure.logging.ConditionalOnEnabledLoggingExport;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -76,6 +77,7 @@ public String getUrl(Transport transport) {
7677
@Configuration(proxyBeanMethods = false)
7778
@ConditionalOnMissingBean({ OtlpGrpcLogRecordExporter.class, OtlpHttpLogRecordExporter.class })
7879
@ConditionalOnBean(OtlpLoggingConnectionDetails.class)
80+
@ConditionalOnEnabledLoggingExport("otlp")
7981
static class Exporters {
8082

8183
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
{
313313
"name": "management.logging.export.enabled",
314314
"type": "java.lang.Boolean",
315-
"description": "Whether the auto-configuration for exporting log record data is enabled",
315+
"description": "Whether auto-configuration of logging is enabled to export logs.",
316316
"defaultValue": true
317317
},
318318
{
@@ -2076,7 +2076,7 @@
20762076
{
20772077
"name": "management.otlp.logging.export.enabled",
20782078
"type": "java.lang.Boolean",
2079-
"description": "Whether auto-configuration for exporting OTLP log records is enabled."
2079+
"description": "Whether auto-configuration of logging is enabled to export OTLP logs."
20802080
},
20812081
{
20822082
"name": "management.otlp.tracing.export.enabled",
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,81 +31,81 @@
3131
import static org.mockito.Mockito.mock;
3232

3333
/**
34-
* Tests for {@link OnEnabledLoggingCondition}.
34+
* Tests for {@link OnEnabledLoggingExportCondition}.
3535
*
3636
* @author Moritz Halbritter
3737
* @author Dmytro Nosan
3838
*/
39-
class OnEnabledLoggingConditionTests {
39+
class OnEnabledLoggingExportConditionTests {
4040

4141
@Test
4242
void shouldMatchIfNoPropertyIsSet() {
43-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
43+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
4444
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(), mockMetadata(""));
4545
assertThat(outcome.isMatch()).isTrue();
46-
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledLogging logging is enabled by default");
46+
assertThat(outcome.getMessage()).isEqualTo("@ConditionalOnEnabledLoggingExport is enabled by default");
4747
}
4848

4949
@Test
5050
void shouldNotMatchIfGlobalPropertyIsFalse() {
51-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
51+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
5252
ConditionOutcome outcome = condition.getMatchOutcome(
5353
mockConditionContext(Map.of("management.logging.export.enabled", "false")), mockMetadata(""));
5454
assertThat(outcome.isMatch()).isFalse();
5555
assertThat(outcome.getMessage())
56-
.isEqualTo("@ConditionalOnEnabledLogging management.logging.export.enabled is false");
56+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.enabled is false");
5757
}
5858

5959
@Test
6060
void shouldMatchIfGlobalPropertyIsTrue() {
61-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
61+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
6262
ConditionOutcome outcome = condition.getMatchOutcome(
6363
mockConditionContext(Map.of("management.logging.export.enabled", "true")), mockMetadata(""));
6464
assertThat(outcome.isMatch()).isTrue();
6565
assertThat(outcome.getMessage())
66-
.isEqualTo("@ConditionalOnEnabledLogging management.logging.export.enabled is true");
66+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.logging.export.enabled is true");
6767
}
6868

6969
@Test
7070
void shouldNotMatchIfExporterPropertyIsFalse() {
71-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
71+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
7272
ConditionOutcome outcome = condition.getMatchOutcome(
7373
mockConditionContext(Map.of("management.otlp.logging.export.enabled", "false")), mockMetadata("otlp"));
7474
assertThat(outcome.isMatch()).isFalse();
7575
assertThat(outcome.getMessage())
76-
.isEqualTo("@ConditionalOnEnabledLogging management.otlp.logging.export.enabled is false");
76+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.otlp.logging.export.enabled is false");
7777
}
7878

7979
@Test
8080
void shouldMatchIfExporterPropertyIsTrue() {
81-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
81+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
8282
ConditionOutcome outcome = condition.getMatchOutcome(
8383
mockConditionContext(Map.of("management.otlp.logging.export.enabled", "true")), mockMetadata("otlp"));
8484
assertThat(outcome.isMatch()).isTrue();
8585
assertThat(outcome.getMessage())
86-
.isEqualTo("@ConditionalOnEnabledLogging management.otlp.logging.export.enabled is true");
86+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.otlp.logging.export.enabled is true");
8787
}
8888

8989
@Test
9090
void exporterPropertyShouldOverrideGlobalPropertyIfTrue() {
91-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
91+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
9292
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
9393
Map.of("management.logging.enabled", "false", "management.otlp.logging.export.enabled", "true")),
9494
mockMetadata("otlp"));
9595
assertThat(outcome.isMatch()).isTrue();
9696
assertThat(outcome.getMessage())
97-
.isEqualTo("@ConditionalOnEnabledLogging management.otlp.logging.export.enabled is true");
97+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.otlp.logging.export.enabled is true");
9898
}
9999

100100
@Test
101101
void exporterPropertyShouldOverrideGlobalPropertyIfFalse() {
102-
OnEnabledLoggingCondition condition = new OnEnabledLoggingCondition();
102+
OnEnabledLoggingExportCondition condition = new OnEnabledLoggingExportCondition();
103103
ConditionOutcome outcome = condition.getMatchOutcome(mockConditionContext(
104104
Map.of("management.logging.enabled", "true", "management.otlp.logging.export.enabled", "false")),
105105
mockMetadata("otlp"));
106106
assertThat(outcome.isMatch()).isFalse();
107107
assertThat(outcome.getMessage())
108-
.isEqualTo("@ConditionalOnEnabledLogging management.otlp.logging.export.enabled is false");
108+
.isEqualTo("@ConditionalOnEnabledLoggingExport management.otlp.logging.export.enabled is false");
109109
}
110110

111111
private ConditionContext mockConditionContext() {
@@ -122,7 +122,7 @@ private ConditionContext mockConditionContext(Map<String, String> properties) {
122122

123123
private AnnotatedTypeMetadata mockMetadata(String exporter) {
124124
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
125-
given(metadata.getAnnotationAttributes(ConditionalOnEnabledLogging.class.getName()))
125+
given(metadata.getAnnotationAttributes(ConditionalOnEnabledLoggingExport.class.getName()))
126126
.willReturn(Map.of("value", exporter));
127127
return metadata;
128128
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/otlp/OtlpLoggingAutoConfigurationTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,26 @@ void shouldNotSupplyBeansIfDependencyIsMissing(String packageName) {
7373
});
7474
}
7575

76+
@Test
77+
void shouldBackOffWhenLoggingExportPropertyIsNotEnabled() {
78+
this.contextRunner
79+
.withPropertyValues("management.logging.export.enabled=false",
80+
"management.otlp.logging.endpoint=http://localhost:4318/v1/logs")
81+
.run((context) -> {
82+
assertThat(context).hasSingleBean(OtlpLoggingConnectionDetails.class);
83+
assertThat(context).doesNotHaveBean(LogRecordExporter.class);
84+
});
85+
}
86+
7687
@Test
7788
void shouldBackOffWhenOtlpLoggingExportPropertyIsNotEnabled() {
78-
this.contextRunner.withPropertyValues("management.otlp.logging.export.enabled=false").run((context) -> {
79-
assertThat(context).doesNotHaveBean(OtlpLoggingConnectionDetails.class);
80-
assertThat(context).doesNotHaveBean(LogRecordExporter.class);
81-
});
89+
this.contextRunner
90+
.withPropertyValues("management.otlp.logging.export.enabled=false",
91+
"management.otlp.logging.endpoint=http://localhost:4318/v1/logs")
92+
.run((context) -> {
93+
assertThat(context).hasSingleBean(OtlpLoggingConnectionDetails.class);
94+
assertThat(context).doesNotHaveBean(LogRecordExporter.class);
95+
});
8296
}
8397

8498
@Test

0 commit comments

Comments
 (0)