Clean the Logback instrumentation of the OTel starter (#12450)

This commit is contained in:
Jean Bisutti 2024-10-16 17:01:25 +02:00 committed by GitHub
parent 9e83898f0d
commit 802e8789bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 82 additions and 12 deletions

View File

@ -30,15 +30,14 @@ class LogbackAppenderInstaller {
private static boolean isLogbackAppenderAddable( private static boolean isLogbackAppenderAddable(
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) { ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
Boolean otelSdkDisableProperty = boolean otelSdkDisabled =
evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled"); evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled", false);
Boolean logbackInstrumentationEnabledProperty = boolean logbackInstrumentationEnabled =
evaluateBooleanProperty( evaluateBooleanProperty(
applicationEnvironmentPreparedEvent, "otel.instrumentation.logback-appender.enabled"); applicationEnvironmentPreparedEvent,
return otelSdkDisableProperty == null "otel.instrumentation.logback-appender.enabled",
|| !otelSdkDisableProperty.booleanValue() true);
|| logbackInstrumentationEnabledProperty == null return !otelSdkDisabled && logbackInstrumentationEnabled;
|| logbackInstrumentationEnabledProperty.booleanValue();
} }
private static void reInitializeOpenTelemetryAppender( private static void reInitializeOpenTelemetryAppender(
@ -141,6 +140,15 @@ class LogbackAppenderInstaller {
.getProperty(property, Boolean.class); .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<OpenTelemetryAppender> findOpenTelemetryAppender() { private static Optional<OpenTelemetryAppender> findOpenTelemetryAppender() {
ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory(); ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory();
if (!(loggerFactorySpi instanceof LoggerContext)) { if (!(loggerFactorySpi instanceof LoggerContext)) {

View File

@ -5,22 +5,46 @@
package io.opentelemetry.spring.smoketest; 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.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage; 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.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
@SpringBootTest( @SpringBootTest(
classes = { classes = {
OtelSpringStarterSmokeTestApplication.class, OtelSpringStarterSmokeTestApplication.class,
AbstractOtelSpringStarterSmokeTest.TestConfiguration.class AbstractOtelSpringStarterSmokeTest.TestConfiguration.class,
SpringSmokeOtelConfiguration.class
}, },
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {"otel.sdk.disabled=true"}) properties = {"otel.sdk.disabled=true"})
@DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will @DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will
// fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists // fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists
class OtelSpringStarterDisabledSmokeTest { class OtelSpringStarterDisabledSmokeTest extends AbstractSpringStarterSmokeTest {
@Autowired private TestRestTemplate testRestTemplate;
@Test @Test
void shouldStartApplication() { void shouldNotSendTelemetry() throws InterruptedException {
// make sure we can still start the application with the disabled property testRestTemplate.getForObject(OtelSpringStarterSmokeTestController.PING, String.class);
// See SpringSmokeOtelConfiguration
Thread.sleep(200);
List<SpanData> exportedSpans = testing.getExportedSpans();
assertThat(exportedSpans).isEmpty();
List<MetricData> exportedMetrics = testing.getExportedMetrics();
assertThat(exportedMetrics).isEmpty();
List<LogRecordData> exportedLogRecords = testing.getExportedLogRecords();
assertThat(exportedLogRecords).isEmpty();
} }
} }

View File

@ -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<LogRecordData> exportedLogRecords = testing.getExportedLogRecords();
assertThat(exportedLogRecords).isEmpty();
}
}