Clean up spring-boot-autoconfigure exporter logic (#6374)

This commit is contained in:
Mateusz Rzeszutek 2022-08-02 09:35:19 +02:00 committed by GitHub
parent 06dfd59fcd
commit 61aef2e413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 153 additions and 81 deletions

View File

@ -14,7 +14,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "otel.exporter.logging") @ConfigurationProperties(prefix = "otel.exporter.logging")
public final class LoggingExporterProperties { public final class LoggingExporterProperties {
private boolean enabled = true; private boolean enabled = false;
private final SignalProperties traces = new SignalProperties(); private final SignalProperties traces = new SignalProperties();
private final SignalProperties metrics = new SignalProperties(); private final SignalProperties metrics = new SignalProperties();
@ -36,7 +36,7 @@ public final class LoggingExporterProperties {
public static class SignalProperties { public static class SignalProperties {
private boolean enabled = true; private boolean enabled = false;
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;

View File

@ -9,27 +9,37 @@ import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.logging.LoggingSpanExporter; import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** Configures {@link LoggingSpanExporter} bean for tracing. */ /** Configures {@link LoggingSpanExporter} bean for tracing. */
@Configuration @Configuration
@EnableConfigurationProperties(LoggingExporterProperties.class) @EnableConfigurationProperties(LoggingExporterProperties.class)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class) @AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnProperty( @Conditional(LoggingMetricExporterAutoConfiguration.AnyPropertyEnabled.class)
prefix = "otel.exporter.logging",
name = {"enabled", "metrics.enabled"},
matchIfMissing = true)
@ConditionalOnClass(LoggingMetricExporter.class) @ConditionalOnClass(LoggingMetricExporter.class)
public class LoggingMetricExporterAutoConfiguration { public class LoggingMetricExporterAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean
public LoggingMetricExporter otelLoggingMetricExporter() { public LoggingMetricExporter otelLoggingMetricExporter() {
return LoggingMetricExporter.create(); return LoggingMetricExporter.create();
} }
static final class AnyPropertyEnabled extends AnyNestedCondition {
AnyPropertyEnabled() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty("otel.exporter.logging.enabled")
static class LoggingEnabled {}
@ConditionalOnProperty("otel.exporter.logging.metrics.enabled")
static class LoggingMetricsEnabled {}
}
} }

View File

@ -8,21 +8,20 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging;
import io.opentelemetry.exporter.logging.LoggingSpanExporter; import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** Configures {@link LoggingSpanExporter} bean for tracing. */ /** Configures {@link LoggingSpanExporter} bean for tracing. */
@Configuration @Configuration
@EnableConfigurationProperties(LoggingExporterProperties.class) @EnableConfigurationProperties(LoggingExporterProperties.class)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class) @AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnProperty( @Conditional(LoggingSpanExporterAutoConfiguration.AnyPropertyEnabled.class)
prefix = "otel.exporter.logging",
name = {"enabled", "traces.enabled"},
matchIfMissing = true)
@ConditionalOnClass(LoggingSpanExporter.class) @ConditionalOnClass(LoggingSpanExporter.class)
public class LoggingSpanExporterAutoConfiguration { public class LoggingSpanExporterAutoConfiguration {
@ -31,4 +30,17 @@ public class LoggingSpanExporterAutoConfiguration {
public LoggingSpanExporter otelLoggingSpanExporter() { public LoggingSpanExporter otelLoggingSpanExporter() {
return LoggingSpanExporter.create(); return LoggingSpanExporter.create();
} }
static final class AnyPropertyEnabled extends AnyNestedCondition {
AnyPropertyEnabled() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty("otel.exporter.logging.enabled")
static class LoggingEnabled {}
@ConditionalOnProperty("otel.exporter.logging.traces.enabled")
static class LoggingTracesEnabled {}
}
} }

View File

@ -0,0 +1,55 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging.LoggingMetricExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpMetricExporterAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
class MetricExporterAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(
OtlpMetricExporterAutoConfiguration.class,
LoggingMetricExporterAutoConfiguration.class));
@Test
void defaultConfiguration() {
contextRunner.run(
context -> {
assertThat(context.getBean("otelOtlpGrpcMetricExporter", OtlpGrpcMetricExporter.class))
.as("OTLP exporter is enabled by default")
.isNotNull();
assertThat(context.containsBean("otelLoggingMetricExporter"))
.as("Logging exporter is not created unless explicitly configured")
.isFalse();
});
}
@Test
void loggingEnabledByConfiguration() {
contextRunner
.withPropertyValues("otel.exporter.logging.enabled=true")
.run(
context -> {
assertThat(
context.getBean("otelOtlpGrpcMetricExporter", OtlpGrpcMetricExporter.class))
.as("OTLP exporter is present even with logging enabled")
.isNotNull();
assertThat(context.getBean("otelLoggingMetricExporter", LoggingMetricExporter.class))
.as("Logging exporter is explicitly enabled")
.isNotNull();
});
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging.LoggingSpanExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpSpanExporterAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
class SpanExporterAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(
OtlpSpanExporterAutoConfiguration.class,
LoggingSpanExporterAutoConfiguration.class));
@Test
void defaultConfiguration() {
contextRunner.run(
context -> {
assertThat(context.getBean("otelOtlpGrpcSpanExporter", OtlpGrpcSpanExporter.class))
.as("OTLP exporter is enabled by default")
.isNotNull();
assertThat(context.containsBean("otelLoggingSpanExporter"))
.as("Logging exporter is not created unless explicitly configured")
.isFalse();
});
}
@Test
void loggingEnabledByConfiguration() {
contextRunner
.withPropertyValues("otel.exporter.logging.enabled=true")
.run(
context -> {
assertThat(context.getBean("otelOtlpGrpcSpanExporter", OtlpGrpcSpanExporter.class))
.as("OTLP exporter is present even with logging enabled")
.isNotNull();
assertThat(context.getBean("otelLoggingSpanExporter", LoggingSpanExporter.class))
.as("Logging exporter is explicitly enabled")
.isNotNull();
});
}
}

View File

@ -3,14 +3,12 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters; package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.jaeger;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter; import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.jaeger.JaegerSpanExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.jaeger.JaegerSpanExporterProperties;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;

View File

@ -60,9 +60,6 @@ class LoggingMetricExporterAutoConfigurationTest {
@Test @Test
void noProperties() { void noProperties() {
runner.run( runner.run(context -> assertThat(context.containsBean("otelLoggingMetricExporter")).isFalse());
context ->
assertThat(context.getBean("otelLoggingMetricExporter", LoggingMetricExporter.class))
.isNotNull());
} }
} }

View File

@ -27,7 +27,7 @@ class LoggingSpanExporterAutoConfigurationTest {
@Test @Test
@DisplayName("when exporters are ENABLED should initialize LoggingSpanExporter bean") @DisplayName("when exporters are ENABLED should initialize LoggingSpanExporter bean")
void loggingEnabled() { void loggingEnabled() {
this.contextRunner contextRunner
.withPropertyValues("otel.exporter.logging.enabled=true") .withPropertyValues("otel.exporter.logging.enabled=true")
.run( .run(
context -> context ->
@ -37,7 +37,7 @@ class LoggingSpanExporterAutoConfigurationTest {
@Test @Test
void loggingTracesEnabled() { void loggingTracesEnabled() {
this.contextRunner contextRunner
.withPropertyValues("otel.exporter.logging.traces.enabled=true") .withPropertyValues("otel.exporter.logging.traces.enabled=true")
.run( .run(
context -> context ->
@ -48,7 +48,7 @@ class LoggingSpanExporterAutoConfigurationTest {
@Test @Test
@DisplayName("when exporters are DISABLED should NOT initialize LoggingSpanExporter bean") @DisplayName("when exporters are DISABLED should NOT initialize LoggingSpanExporter bean")
void loggingDisabled() { void loggingDisabled() {
this.contextRunner contextRunner
.withPropertyValues("otel.exporter.logging.enabled=false") .withPropertyValues("otel.exporter.logging.enabled=false")
.run(context -> assertThat(context.containsBean("otelLoggingSpanExporter")).isFalse()); .run(context -> assertThat(context.containsBean("otelLoggingSpanExporter")).isFalse());
} }
@ -56,7 +56,7 @@ class LoggingSpanExporterAutoConfigurationTest {
@Test @Test
@DisplayName("when exporters are DISABLED should NOT initialize LoggingSpanExporter bean") @DisplayName("when exporters are DISABLED should NOT initialize LoggingSpanExporter bean")
void loggingTracesDisabled() { void loggingTracesDisabled() {
this.contextRunner contextRunner
.withPropertyValues("otel.exporter.logging.traces.enabled=false") .withPropertyValues("otel.exporter.logging.traces.enabled=false")
.run(context -> assertThat(context.containsBean("otelLoggingSpanExporter")).isFalse()); .run(context -> assertThat(context.containsBean("otelLoggingSpanExporter")).isFalse());
} }
@ -65,9 +65,7 @@ class LoggingSpanExporterAutoConfigurationTest {
@DisplayName( @DisplayName(
"when exporter enabled property is MISSING should initialize LoggingSpanExporter bean") "when exporter enabled property is MISSING should initialize LoggingSpanExporter bean")
void exporterPresentByDefault() { void exporterPresentByDefault() {
this.contextRunner.run( contextRunner.run(
context -> context -> assertThat(context.containsBean("otelLoggingSpanExporter")).isFalse());
assertThat(context.getBean("otelLoggingSpanExporter", LoggingSpanExporter.class))
.isNotNull());
} }
} }

View File

@ -3,14 +3,12 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters; package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter; import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpanExporterAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpanExporterProperties;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;

View File

@ -1,36 +0,0 @@
# OpenTelemetry OTLP Exporter Starter
OpenTelemetry OTLP Exporter Starter is a starter package that includes the opentelemetry-api, opentelemetry-sdk, opentelemetry-extension-annotations, opentelmetry-logging-exporter, opentelemetry-spring-boot-autoconfigurations and spring framework starters required to setup distributed tracing. It also provides the [opentelemetry-exporters-otlp](https://github.com/open-telemetry/opentelemetry-java/tree/main/exporters/otlp) artifact and corresponding exporter auto-configuration. Check out [opentelemetry-spring-boot-autoconfigure](../../spring-boot-autoconfigure/README.md#features) for the list of supported libraries and features.
## Quickstart
### Add these dependencies to your project.
Replace `OPENTELEMETRY_VERSION` with the latest stable [release](https://search.maven.org/search?q=g:io.opentelemetry).
- Minimum version: `1.1.0`
#### Maven
```xml
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-otlp-exporter-starter</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
</dependencies>
```
#### Gradle
```groovy
implementation("io.opentelemetry.instrumentation:opentelemetry-otlp-exporter-starter:OPENTELEMETRY_VERSION")
```
### Starter Guide
Check out [OpenTelemetry Manual Instrumentation](https://opentelemetry.io/docs/instrumentation/java/manual/) to learn more about
using the OpenTelemetry API to instrument your code.

View File

@ -1,14 +0,0 @@
plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
}
group = "io.opentelemetry.instrumentation"
val versions: Map<String, String> by project
dependencies {
api("org.springframework.boot:spring-boot-starter:${versions["org.springframework.boot"]}")
api(project(":instrumentation:spring:starters:spring-starter"))
api("io.opentelemetry:opentelemetry-exporter-otlp")
}

View File

@ -14,6 +14,7 @@ dependencies {
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
api("io.opentelemetry:opentelemetry-api") api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-exporter-logging") api("io.opentelemetry:opentelemetry-exporter-logging")
api("io.opentelemetry:opentelemetry-exporter-otlp")
api("io.opentelemetry:opentelemetry-sdk") api("io.opentelemetry:opentelemetry-sdk")
api(project(":instrumentation-annotations")) api(project(":instrumentation-annotations"))
} }

View File

@ -447,7 +447,6 @@ include(":instrumentation:spring:spring-ws-2.0:javaagent")
include(":instrumentation:spring:spring-boot-autoconfigure") include(":instrumentation:spring:spring-boot-autoconfigure")
include(":instrumentation:spring:starters:spring-starter") include(":instrumentation:spring:starters:spring-starter")
include(":instrumentation:spring:starters:jaeger-exporter-starter") include(":instrumentation:spring:starters:jaeger-exporter-starter")
include(":instrumentation:spring:starters:otlp-exporter-starter")
include(":instrumentation:spring:starters:zipkin-exporter-starter") include(":instrumentation:spring:starters:zipkin-exporter-starter")
include(":instrumentation:spymemcached-2.12:javaagent") include(":instrumentation:spymemcached-2.12:javaagent")
include(":instrumentation:struts-2.3:javaagent") include(":instrumentation:struts-2.3:javaagent")