From 284de9315ddbf1cf852b696536ccca9751ea9c1d Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Thu, 6 Jul 2023 17:25:11 +0200 Subject: [PATCH 1/3] Initialize appenders in the spring boot starter --- .../build.gradle.kts | 4 ++ .../OpenTelemetryAutoConfiguration.java | 4 -- ...penTelemetryAppenderAutoConfiguration.java | 53 +++++++++++++++++++ .../OpenTelemetryAutoConfigurationTest.java | 7 --- .../TraceAspectAutoConfigurationTest.java | 7 --- ...egerSpanExporterAutoConfigurationTest.java | 7 --- ...ngMetricExporterAutoConfigurationTest.java | 7 --- ...gingSpanExporterAutoConfigurationTest.java | 7 --- .../OtlpLogExporterAutoConfigurationTest.java | 7 --- ...lpMetricExporterAutoConfigurationTest.java | 7 --- ...OtlpSpanExporterAutoConfigurationTest.java | 7 --- ...pkinSpanExporterAutoConfigurationTest.java | 7 --- .../RestTemplateAutoConfigurationTest.java | 7 --- .../WebClientAutoConfigurationTest.java | 7 --- .../MicrometerShimAutoConfigurationTest.java | 7 --- .../PropagationAutoConfigurationTest.java | 7 --- .../PropagationPropertiesTest.java | 7 --- .../OtelResourceAutoConfigurationTest.java | 7 --- .../resources/OtelResourcePropertiesTest.java | 7 --- .../SpringResourceConfigPropertiesTest.java | 7 --- ...MvcFilterAutoConfigurationSpring6Test.java | 2 - .../WebMvcFilterAutoConfigurationTest.java | 2 - 22 files changed, 57 insertions(+), 127 deletions(-) create mode 100644 instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java diff --git a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts index 8cd1255fd92c..dd6cfee87acd 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts +++ b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts @@ -41,6 +41,10 @@ dependencies { compileOnly("io.opentelemetry:opentelemetry-exporter-otlp") compileOnly("io.opentelemetry:opentelemetry-exporter-zipkin") compileOnly(project(":instrumentation-annotations")) + compileOnly(project(":instrumentation:log4j:log4j-appender-2.17:library")) + compileOnly("org.apache.logging.log4j:log4j-core:2.17.0") + compileOnly(project(":instrumentation:logback:logback-appender-1.0:library")) + compileOnly("ch.qos.logback:logback-classic:1.0.0") compileOnly(project(":instrumentation:resources:library")) annotationProcessor("com.google.auto.service:auto-service") diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java index 68e1e5be6121..369a9fd22607 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java @@ -5,7 +5,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.context.propagation.ContextPropagators; @@ -141,9 +140,6 @@ public OpenTelemetry openTelemetry( ContextPropagators propagators = propagatorsProvider.getIfAvailable(ContextPropagators::noop); - // global is needed for logging appenders - GlobalOpenTelemetry.set(OpenTelemetrySdk.builder().setLoggerProvider(loggerProvider).build()); - return OpenTelemetrySdk.builder() .setTracerProvider(tracerProvider) .setMeterProvider(meterProvider) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java new file mode 100644 index 000000000000..060b0bc4cb48 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.logging; + +import io.opentelemetry.api.OpenTelemetry; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@SuppressWarnings("OtelPrivateConstructorForUtilityClass") +@ConditionalOnBean(OpenTelemetry.class) +public class OpenTelemetryAppenderAutoConfiguration { + + @Bean + @ConditionalOnProperty( + prefix = "otel.springboot.log4j-appender", + name = "enabled", + matchIfMissing = true) + @ConditionalOnClass({ + io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.class + }) + ApplicationListener log4jOtelAppenderInitializer( + OpenTelemetry openTelemetry) { + return event -> { + io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.install( + openTelemetry); + }; + } + + @Bean + @ConditionalOnProperty( + prefix = "otel.springboot.logback-appender", + name = "enabled", + matchIfMissing = true) + @ConditionalOnClass({ + io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.class + }) + ApplicationListener logbackOtelAppenderInitializer( + OpenTelemetry openTelemetry) { + return event -> { + io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install( + openTelemetry); + }; + } +} diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java index 35809251e5c8..67312508ced8 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java @@ -8,7 +8,6 @@ import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration; @@ -17,7 +16,6 @@ import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.SdkTracerProvider; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -37,11 +35,6 @@ public OpenTelemetry customOpenTelemetry() { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName( "when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry") diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/aspects/TraceAspectAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/aspects/TraceAspectAutoConfigurationTest.java index 0f61763ac496..3dc05203bf0a 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/aspects/TraceAspectAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/aspects/TraceAspectAutoConfigurationTest.java @@ -7,9 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,11 +22,6 @@ public class TraceAspectAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, TraceAspectAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when aspects are ENABLED should initialize WithSpanAspect bean") void aspectsEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/jaeger/JaegerSpanExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/jaeger/JaegerSpanExporterAutoConfigurationTest.java index b46645121c17..b1d0bf4492d5 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/jaeger/JaegerSpanExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/jaeger/JaegerSpanExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -25,11 +23,6 @@ class JaegerSpanExporterAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, JaegerSpanExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when exporters are ENABLED should initialize JaegerGrpcSpanExporter bean") void exportersEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingMetricExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingMetricExporterAutoConfigurationTest.java index 62e30cdd899c..5b1e8355e1ec 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingMetricExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingMetricExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.logging.LoggingMetricExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -24,11 +22,6 @@ class LoggingMetricExporterAutoConfigurationTest { OpenTelemetryAutoConfiguration.class, LoggingMetricExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test void loggingEnabled() { runner diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingSpanExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingSpanExporterAutoConfigurationTest.java index 3f0f9c2a20fa..34abb3200ec2 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingSpanExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/logging/LoggingSpanExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.logging.LoggingSpanExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -26,11 +24,6 @@ class LoggingSpanExporterAutoConfigurationTest { OpenTelemetryAutoConfiguration.class, LoggingSpanExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when exporters are ENABLED should initialize LoggingSpanExporter bean") void loggingEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpLogExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpLogExporterAutoConfigurationTest.java index 8fde124356ab..540eac6ee217 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpLogExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpLogExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -23,11 +21,6 @@ class OtlpLogExporterAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, OtlpLoggerExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test void otlpEnabled() { runner diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpMetricExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpMetricExporterAutoConfigurationTest.java index 9b8ac94017c4..67b2365579f0 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpMetricExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpMetricExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -23,11 +21,6 @@ class OtlpMetricExporterAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, OtlpMetricExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test void otlpEnabled() { runner diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpSpanExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpSpanExporterAutoConfigurationTest.java index 108afea8856d..d606aae3adaa 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpSpanExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpSpanExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -25,11 +23,6 @@ class OtlpSpanExporterAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, OtlpSpanExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when exporters are ENABLED should initialize OtlpGrpcSpanExporter bean") void otlpEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/zipkin/ZipkinSpanExporterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/zipkin/ZipkinSpanExporterAutoConfigurationTest.java index f5120be939b1..7e6aa89a4515 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/zipkin/ZipkinSpanExporterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/zipkin/ZipkinSpanExporterAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -25,11 +23,6 @@ class ZipkinSpanExporterAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, ZipkinSpanExporterAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when exporters are ENABLED should initialize ZipkinSpanExporter bean") void exportersEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java index 3a98e6a3f607..84224f625238 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java @@ -7,9 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,11 +22,6 @@ class RestTemplateAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, RestTemplateAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when httpclients are ENABLED should initialize RestTemplateInterceptor bean") void httpClientsEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java index aaa7fc2468a5..ea90e219014c 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java @@ -7,9 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,11 +22,6 @@ class WebClientAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, WebClientAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when httpclients are ENABLED should initialize WebClientBeanPostProcessor bean") void httpClientsEnabled() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/metrics/MicrometerShimAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/metrics/MicrometerShimAutoConfigurationTest.java index 4e62b622ee6b..7e66044cd0db 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/metrics/MicrometerShimAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/metrics/MicrometerShimAutoConfigurationTest.java @@ -8,10 +8,8 @@ import static org.assertj.core.api.Assertions.assertThat; import io.micrometer.core.instrument.MeterRegistry; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -25,11 +23,6 @@ class MicrometerShimAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, MicrometerShimAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test void metricsEnabled() { runner diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationAutoConfigurationTest.java index b5bc165f0ced..a20162cec993 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationAutoConfigurationTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,11 +22,6 @@ class PropagationAutoConfigurationTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, PropagationAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when propagation is ENABLED should initialize PropagationAutoConfiguration bean") void shouldBeConfigured() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationPropertiesTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationPropertiesTest.java index 7d0dbbe2306c..6c746b0c92be 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationPropertiesTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/propagators/PropagationPropertiesTest.java @@ -7,10 +7,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import java.util.Arrays; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -24,11 +22,6 @@ public class PropagationPropertiesTest { AutoConfigurations.of( OpenTelemetryAutoConfiguration.class, PropagationAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when propagation is SET should set PropagationProperties with given propagators") void hasType() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfigurationTest.java index e0ac7e58320f..17506fffe3ed 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfigurationTest.java @@ -7,9 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -22,11 +20,6 @@ public class OtelResourceAutoConfigurationTest { AutoConfigurations.of( OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName( "when otel.springboot.resource.enabled is set to true configuration should be initialized") diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourcePropertiesTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourcePropertiesTest.java index 89881bafa64c..2b882b5cf7ee 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourcePropertiesTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourcePropertiesTest.java @@ -8,8 +8,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.GlobalOpenTelemetry; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -21,11 +19,6 @@ public class OtelResourcePropertiesTest { .withPropertyValues("otel.springboot.resource.enabled=true") .withConfiguration(AutoConfigurations.of(OtelResourceAutoConfiguration.class)); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when attributes are SET should set OtelResourceProperties with given attributes") void hasAttributes() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceConfigPropertiesTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceConfigPropertiesTest.java index 969a81d5a369..93756ae1c4ed 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceConfigPropertiesTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceConfigPropertiesTest.java @@ -8,8 +8,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import io.opentelemetry.api.GlobalOpenTelemetry; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -19,11 +17,6 @@ class SpringResourceConfigPropertiesTest { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); - @BeforeEach - void resetGlobalLoggerProvider() { - GlobalOpenTelemetry.resetForTest(); - } - @Test @DisplayName("when map is set in properties in a row it should be available in config") void shouldInitializeAttributesByMapInArow() { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6Test.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6Test.java index 22ab4809381d..5383154c80c1 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6Test.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationSpring6Test.java @@ -8,7 +8,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assumptions.assumeTrue; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import jakarta.servlet.Filter; import org.junit.jupiter.api.BeforeEach; @@ -29,7 +28,6 @@ class WebMvcFilterAutoConfigurationSpring6Test { @BeforeEach void setUp() { assumeTrue(Boolean.getBoolean("testLatestDeps")); - GlobalOpenTelemetry.resetForTest(); } @Test diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationTest.java index 4e44a5cbcf54..6c82ae182315 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMvcFilterAutoConfigurationTest.java @@ -8,7 +8,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assumptions.assumeFalse; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; import javax.servlet.Filter; import org.junit.jupiter.api.BeforeEach; @@ -28,7 +27,6 @@ class WebMvcFilterAutoConfigurationTest { @BeforeEach void setUp() { assumeFalse(Boolean.getBoolean("testLatestDeps")); - GlobalOpenTelemetry.resetForTest(); } @Test From 0b3233ddf667a6476b2bf4c3f9b4c8835063e3ba Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Fri, 7 Jul 2023 09:49:16 +0200 Subject: [PATCH 2/3] code review comments, add tests --- .../build.gradle.kts | 63 +++++++++++--- ...penTelemetryAppenderAutoConfiguration.java | 40 +++++---- .../main/resources/META-INF/spring.factories | 1 + ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../logging/LogbackAppenderTest.java | 87 +++++++++++++++++++ .../resources/logback-test.xml | 19 ++++ .../testing/LibraryTestRunner.java | 14 ++- 7 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/LogbackAppenderTest.java create mode 100644 instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/resources/logback-test.xml diff --git a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts index dd6cfee87acd..ce279dbc5159 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts +++ b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts @@ -81,24 +81,59 @@ if (latestDepTest) { } } -tasks.compileTestJava { - options.compilerArgs.add("-parameters") +testing { + suites { + val testLogbackAppender by registering(JvmTestSuite::class) { + dependencies { + implementation(project()) + implementation(project(":testing-common")) + implementation("io.opentelemetry:opentelemetry-sdk") + implementation("io.opentelemetry:opentelemetry-sdk-testing") + implementation("org.springframework.boot:spring-boot-autoconfigure:$springBootVersion") + + implementation(project(":instrumentation:logback:logback-appender-1.0:library")) + // using the same versions as in the spring-boot-autoconfigure + implementation("ch.qos.logback:logback-classic") { + version { + strictly("1.2.11") + } + } + implementation("org.slf4j:slf4j-api") { + version { + strictly("1.7.32") + } + } + } + } + } } -tasks.withType().configureEach { - usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) +tasks { + check { + dependsOn(testing.suites) + } + + compileTestJava { + options.compilerArgs.add("-parameters") + } + + test { + usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) + } - systemProperty("testLatestDeps", latestDepTest) + withType().configureEach { + systemProperty("testLatestDeps", latestDepTest) - // required on jdk17 - jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") - jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") + // required on jdk17 + jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") + jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") - // disable tests on openj9 18 because they often crash JIT compiler - val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion) - val testOnOpenJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" } - ?: false - if (testOnOpenJ9 && testJavaVersion?.majorVersion == "18") { - enabled = false + // disable tests on openj9 18 because they often crash JIT compiler + val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion) + val testOnOpenJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" } + ?: false + if (testOnOpenJ9 && testJavaVersion?.majorVersion == "18") { + enabled = false + } } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java index 060b0bc4cb48..eb066d15f8e1 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java @@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -19,7 +19,7 @@ @ConditionalOnBean(OpenTelemetry.class) public class OpenTelemetryAppenderAutoConfiguration { - @Bean + @Configuration @ConditionalOnProperty( prefix = "otel.springboot.log4j-appender", name = "enabled", @@ -27,15 +27,19 @@ public class OpenTelemetryAppenderAutoConfiguration { @ConditionalOnClass({ io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.class }) - ApplicationListener log4jOtelAppenderInitializer( - OpenTelemetry openTelemetry) { - return event -> { - io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.install( - openTelemetry); - }; + static class Log4jAppenderConfig { + + @Bean + ApplicationListener log4jOtelAppenderInitializer( + OpenTelemetry openTelemetry) { + return event -> { + io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.install( + openTelemetry); + }; + } } - @Bean + @Configuration @ConditionalOnProperty( prefix = "otel.springboot.logback-appender", name = "enabled", @@ -43,11 +47,17 @@ ApplicationListener log4jOtelAppenderInitializer( @ConditionalOnClass({ io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.class }) - ApplicationListener logbackOtelAppenderInitializer( - OpenTelemetry openTelemetry) { - return event -> { - io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install( - openTelemetry); - }; + static class LogbackAppenderConfig { + + @Bean + ApplicationListener logbackOtelAppenderInitializer( + OpenTelemetry openTelemetry) { + System.out.println("listener bean"); + return event -> { + System.out.println("running the listener"); + io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install( + openTelemetry); + }; + } } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 2d5f5144909f..1f42548a23f6 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -10,6 +10,7 @@ io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpa io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.kafka.KafkaInstrumentationAutoConfiguration,\ +io.opentelemetry.instrumentation.spring.autoconfigure.logging.OpenTelemetryAppenderAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration,\ io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration,\ diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 0e9748f20b63..86874a6f705b 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -9,6 +9,7 @@ io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpa io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.kafka.KafkaInstrumentationAutoConfiguration +io.opentelemetry.instrumentation.spring.autoconfigure.logging.OpenTelemetryAppenderAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/LogbackAppenderTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/LogbackAppenderTest.java new file mode 100644 index 000000000000..b27eef5142a2 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/LogbackAppenderTest.java @@ -0,0 +1,87 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.logging; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender; +import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +class LogbackAppenderTest { + + @RegisterExtension + static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); + + @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); + + @BeforeEach + void setUp() { + // reset the appender + OpenTelemetryAppender.install(null); + } + + @Configuration + static class TestingOpenTelemetryConfiguration { + + @Bean + public OpenTelemetry openTelemetry() { + return testing.getOpenTelemetry(); + } + } + + @Test + void shouldInitializeAppender() { + Map properties = new HashMap<>(); + properties.put("logging.config", "classpath:logback-test.xml"); + + SpringApplication app = + new SpringApplication( + TestingOpenTelemetryConfiguration.class, OpenTelemetryAppenderAutoConfiguration.class); + app.setDefaultProperties(properties); + ConfigurableApplicationContext context = app.run(); + cleanup.deferCleanup(context); + + LoggerFactory.getLogger("test").info("test log message"); + + assertThat(testing.logRecords()) + .anySatisfy( + logRecord -> { + assertThat(logRecord.getInstrumentationScopeInfo().getName()).isEqualTo("test"); + assertThat(logRecord.getBody().asString()).contains("test log message"); + }); + } + + @Test + void shouldNotInitializeAppenderWhenDisabled() { + Map properties = new HashMap<>(); + properties.put("logging.config", "classpath:logback-test.xml"); + properties.put("otel.springboot.logback-appender.enabled", "false"); + + SpringApplication app = + new SpringApplication( + TestingOpenTelemetryConfiguration.class, OpenTelemetryAppenderAutoConfiguration.class); + app.setDefaultProperties(properties); + ConfigurableApplicationContext context = app.run(); + cleanup.deferCleanup(context); + + LoggerFactory.getLogger("test").info("test log message"); + + assertThat(testing.logRecords()).isEmpty(); + } +} diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/resources/logback-test.xml b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/resources/logback-test.xml new file mode 100644 index 000000000000..6b49823644ff --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/resources/logback-test.xml @@ -0,0 +1,19 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java index e22444cb99b0..c70a9d73b4ab 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java @@ -13,12 +13,15 @@ import io.opentelemetry.exporter.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.common.CompletableResultCode; +import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.data.LogRecordData; +import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.data.AggregationTemporality; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.export.MetricReader; import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; +import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter; import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; import io.opentelemetry.sdk.trace.ReadWriteSpan; @@ -28,7 +31,6 @@ import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; import java.time.Duration; -import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -41,6 +43,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner { private static final OpenTelemetrySdk openTelemetry; private static final InMemorySpanExporter testSpanExporter; private static final InMemoryMetricExporter testMetricExporter; + private static final InMemoryLogRecordExporter testLogRecordExporter; private static final MetricReader metricReader; private static boolean forceFlushCalled; @@ -49,6 +52,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner { testSpanExporter = InMemorySpanExporter.create(); testMetricExporter = InMemoryMetricExporter.create(AggregationTemporality.DELTA); + testLogRecordExporter = InMemoryLogRecordExporter.create(); metricReader = PeriodicMetricReader.builder(testMetricExporter) @@ -66,6 +70,10 @@ public final class LibraryTestRunner extends InstrumentationTestRunner { .addSpanProcessor(SimpleSpanProcessor.create(testSpanExporter)) .build()) .setMeterProvider(SdkMeterProvider.builder().registerMetricReader(metricReader).build()) + .setLoggerProvider( + SdkLoggerProvider.builder() + .addLogRecordProcessor(SimpleLogRecordProcessor.create(testLogRecordExporter)) + .build()) .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) .buildAndRegisterGlobal(); } @@ -98,6 +106,7 @@ public void clearAllExportedData() { openTelemetry.getSdkMeterProvider().forceFlush().join(10, TimeUnit.SECONDS); testSpanExporter.reset(); testMetricExporter.reset(); + testLogRecordExporter.reset(); forceFlushCalled = false; } @@ -123,8 +132,7 @@ public List getExportedMetrics() { @Override public List getExportedLogRecords() { - // no logs support yet - return Collections.emptyList(); + return testLogRecordExporter.getFinishedLogRecordItems(); } @Override From 33abb1063e2daf51abed6fc71012f87f98c52a90 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Fri, 7 Jul 2023 11:31:31 +0200 Subject: [PATCH 3/3] remove printlns --- .../logging/OpenTelemetryAppenderAutoConfiguration.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java index eb066d15f8e1..25763fbabf16 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/logging/OpenTelemetryAppenderAutoConfiguration.java @@ -52,9 +52,7 @@ static class LogbackAppenderConfig { @Bean ApplicationListener logbackOtelAppenderInitializer( OpenTelemetry openTelemetry) { - System.out.println("listener bean"); return event -> { - System.out.println("running the listener"); io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install( openTelemetry); };