-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SystemOutLogRecordExporter for spring starter (#10420)
- Loading branch information
1 parent
acb078b
commit 29354a7
Showing
8 changed files
with
155 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...n/spring/autoconfigure/exporters/logging/SystemOutLogRecordExporterAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging; | ||
|
||
import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.internal.ExporterConfigEvaluator; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
/** Configures {@link SystemOutLogRecordExporter} bean for tracing. */ | ||
@Configuration | ||
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class) | ||
@Conditional(SystemOutLogRecordExporterAutoConfiguration.CustomCondition.class) | ||
@ConditionalOnClass(SystemOutLogRecordExporter.class) | ||
public class SystemOutLogRecordExporterAutoConfiguration { | ||
|
||
@Bean(destroyMethod = "") // SDK components are shutdown from the OpenTelemetry instance | ||
@ConditionalOnMissingBean | ||
public SystemOutLogRecordExporter otelSystemOutLogRecordExporter() { | ||
return SystemOutLogRecordExporter.create(); | ||
} | ||
|
||
static final class CustomCondition implements Condition { | ||
@Override | ||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
return ExporterConfigEvaluator.isExporterEnabled( | ||
context.getEnvironment(), | ||
"otel.exporter.logging.enabled", | ||
"otel.exporter.logging.logs.enabled", | ||
"otel.logs.exporter", | ||
"logging", | ||
false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ring/autoconfigure/exporters/logging/SystemOutLogRecordExporterAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
/** Spring Boot auto configuration test for {@link SystemOutLogRecordExporter}. */ | ||
class SystemOutLogRecordExporterAutoConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of( | ||
OpenTelemetryAutoConfiguration.class, | ||
SystemOutLogRecordExporterAutoConfiguration.class)); | ||
|
||
@Test | ||
void loggingEnabledNew() { | ||
contextRunner | ||
.withPropertyValues("otel.logs.exporter=logging") | ||
.run( | ||
context -> | ||
assertThat( | ||
context.getBean( | ||
"otelSystemOutLogRecordExporter", SystemOutLogRecordExporter.class)) | ||
.isNotNull()); | ||
} | ||
|
||
@Test | ||
@DisplayName("when exporters are ENABLED should initialize SystemOutLogRecordExporter bean") | ||
void loggingEnabled() { | ||
contextRunner | ||
.withPropertyValues("otel.exporter.logging.enabled=true") | ||
.run( | ||
context -> | ||
assertThat( | ||
context.getBean( | ||
"otelSystemOutLogRecordExporter", SystemOutLogRecordExporter.class)) | ||
.isNotNull()); | ||
} | ||
|
||
@Test | ||
void loggingLogsEnabled() { | ||
contextRunner | ||
.withPropertyValues("otel.exporter.logging.logs.enabled=true") | ||
.run( | ||
context -> | ||
assertThat( | ||
context.getBean( | ||
"otelSystemOutLogRecordExporter", SystemOutLogRecordExporter.class)) | ||
.isNotNull()); | ||
} | ||
|
||
@Test | ||
@DisplayName("when exporters are DISABLED should NOT initialize SystemOutLogRecordExporter bean") | ||
void loggingDisabled() { | ||
contextRunner | ||
.withPropertyValues("otel.exporter.logging.enabled=false") | ||
.run( | ||
context -> | ||
assertThat(context.containsBean("otelSystemOutLogRecordExporter")).isFalse()); | ||
} | ||
|
||
@Test | ||
@DisplayName("when exporters are DISABLED should NOT initialize SystemOutLogRecordExporter bean") | ||
void loggingLogsDisabled() { | ||
contextRunner | ||
.withPropertyValues("otel.exporter.logging.logs.enabled=false") | ||
.run( | ||
context -> | ||
assertThat(context.containsBean("otelSystemOutLogRecordExporter")).isFalse()); | ||
} | ||
|
||
@Test | ||
@DisplayName( | ||
"when exporter enabled property is MISSING should initialize SystemOutLogRecordExporter bean") | ||
void exporterPresentByDefault() { | ||
contextRunner.run( | ||
context -> assertThat(context.containsBean("otelSystemOutLogRecordExporter")).isFalse()); | ||
} | ||
} |