diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java index de9ad52f4b7b..c2ef98d65f1d 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java @@ -30,15 +30,14 @@ static void install(ApplicationEnvironmentPreparedEvent applicationEnvironmentPr private static boolean isLogbackAppenderAddable( ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) { - Boolean otelSdkDisableProperty = - evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled"); - Boolean logbackInstrumentationEnabledProperty = + boolean otelSdkDisabled = + evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled", false); + boolean logbackInstrumentationEnabled = evaluateBooleanProperty( - applicationEnvironmentPreparedEvent, "otel.instrumentation.logback-appender.enabled"); - return otelSdkDisableProperty == null - || !otelSdkDisableProperty.booleanValue() - || logbackInstrumentationEnabledProperty == null - || logbackInstrumentationEnabledProperty.booleanValue(); + applicationEnvironmentPreparedEvent, + "otel.instrumentation.logback-appender.enabled", + true); + return !otelSdkDisabled && logbackInstrumentationEnabled; } private static void reInitializeOpenTelemetryAppender( @@ -141,6 +140,15 @@ private static Boolean evaluateBooleanProperty( .getProperty(property, Boolean.class); } + private static boolean evaluateBooleanProperty( + ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent, + String property, + boolean defaultValue) { + return applicationEnvironmentPreparedEvent + .getEnvironment() + .getProperty(property, Boolean.class, defaultValue); + } + private static Optional findOpenTelemetryAppender() { ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory(); if (!(loggerFactorySpi instanceof LoggerContext)) { diff --git a/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterDisabledSmokeTest.java b/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterDisabledSmokeTest.java index 29bae87fa73c..21a264375d0e 100644 --- a/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterDisabledSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterDisabledSmokeTest.java @@ -5,22 +5,46 @@ package io.opentelemetry.spring.smoketest; +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.sdk.logs.data.LogRecordData; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.trace.data.SpanData; +import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledInNativeImage; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; @SpringBootTest( classes = { OtelSpringStarterSmokeTestApplication.class, - AbstractOtelSpringStarterSmokeTest.TestConfiguration.class + AbstractOtelSpringStarterSmokeTest.TestConfiguration.class, + SpringSmokeOtelConfiguration.class }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"otel.sdk.disabled=true"}) @DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will // fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists -class OtelSpringStarterDisabledSmokeTest { +class OtelSpringStarterDisabledSmokeTest extends AbstractSpringStarterSmokeTest { + + @Autowired private TestRestTemplate testRestTemplate; @Test - void shouldStartApplication() { - // make sure we can still start the application with the disabled property + void shouldNotSendTelemetry() throws InterruptedException { + testRestTemplate.getForObject(OtelSpringStarterSmokeTestController.PING, String.class); + + // See SpringSmokeOtelConfiguration + Thread.sleep(200); + + List exportedSpans = testing.getExportedSpans(); + assertThat(exportedSpans).isEmpty(); + + List exportedMetrics = testing.getExportedMetrics(); + assertThat(exportedMetrics).isEmpty(); + + List exportedLogRecords = testing.getExportedLogRecords(); + assertThat(exportedLogRecords).isEmpty(); } } diff --git a/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterWithLogbackInstrumentationDisabledSmokeTest.java b/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterWithLogbackInstrumentationDisabledSmokeTest.java new file mode 100644 index 000000000000..62a7625a4b46 --- /dev/null +++ b/smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterWithLogbackInstrumentationDisabledSmokeTest.java @@ -0,0 +1,38 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.spring.smoketest; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.sdk.logs.data.LogRecordData; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledInNativeImage; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest( + classes = { + OtelSpringStarterSmokeTestApplication.class, + AbstractOtelSpringStarterSmokeTest.TestConfiguration.class, + SpringSmokeOtelConfiguration.class + }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = {"otel.instrumentation.logback-appender.enabled=false"}) +@DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will +// fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists +class OtelSpringStarterWithLogbackInstrumentationDisabledSmokeTest + extends AbstractSpringStarterSmokeTest { + + @Test + void shouldNotSendLogRecordTelemetry() throws InterruptedException { + + // See SpringSmokeOtelConfiguration + Thread.sleep(200); + + List exportedLogRecords = testing.getExportedLogRecords(); + assertThat(exportedLogRecords).isEmpty(); + } +}