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

add SystemOutLogRecordExporter for spring starter #10420

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -13,8 +13,10 @@
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 LoggingMetricExporter} bean for tracing. */
@Configuration
Expand All @@ -31,9 +33,7 @@ public LoggingMetricExporter otelLoggingMetricExporter() {

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.logging.enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
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 LoggingSpanExporter} bean for tracing. */
@Configuration
Expand All @@ -31,9 +33,7 @@ public LoggingSpanExporter otelLoggingSpanExporter() {

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.logging.enabled",
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
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.core.type.AnnotatedTypeMetadata;

@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@Conditional(OtlpLogRecordExporterAutoConfiguration.CustomCondition.class)
Expand All @@ -32,9 +34,7 @@ public LogRecordExporter otelOtlpLogRecordExporter(ConfigProperties configProper

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
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;

@Configuration
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
Expand All @@ -34,9 +36,7 @@ public MetricExporter otelOtlpMetricExporter(ConfigProperties configProperties)

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
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 OtlpGrpcSpanExporter} for tracing.
Expand All @@ -39,9 +41,7 @@ public SpanExporter otelOtlpSpanExporter(ConfigProperties configProperties) {

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
"otel.exporter.otlp.enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 ZipkinSpanExporter} for tracing.
Expand All @@ -43,9 +45,7 @@ public ZipkinSpanExporter otelZipkinSpanExporter(ZipkinSpanExporterProperties pr

static final class CustomCondition implements Condition {
@Override
public boolean matches(
org.springframework.context.annotation.ConditionContext context,
org.springframework.core.type.AnnotatedTypeMetadata metadata) {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ExporterConfigEvaluator.isExporterEnabled(
context.getEnvironment(),
null,
Expand Down
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());
}
}
Loading