Skip to content

Commit 802e878

Browse files
authored
Clean the Logback instrumentation of the OTel starter (#12450)
1 parent 9e83898 commit 802e878

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ static void install(ApplicationEnvironmentPreparedEvent applicationEnvironmentPr
3030

3131
private static boolean isLogbackAppenderAddable(
3232
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
33-
Boolean otelSdkDisableProperty =
34-
evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled");
35-
Boolean logbackInstrumentationEnabledProperty =
33+
boolean otelSdkDisabled =
34+
evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled", false);
35+
boolean logbackInstrumentationEnabled =
3636
evaluateBooleanProperty(
37-
applicationEnvironmentPreparedEvent, "otel.instrumentation.logback-appender.enabled");
38-
return otelSdkDisableProperty == null
39-
|| !otelSdkDisableProperty.booleanValue()
40-
|| logbackInstrumentationEnabledProperty == null
41-
|| logbackInstrumentationEnabledProperty.booleanValue();
37+
applicationEnvironmentPreparedEvent,
38+
"otel.instrumentation.logback-appender.enabled",
39+
true);
40+
return !otelSdkDisabled && logbackInstrumentationEnabled;
4241
}
4342

4443
private static void reInitializeOpenTelemetryAppender(
@@ -141,6 +140,15 @@ private static Boolean evaluateBooleanProperty(
141140
.getProperty(property, Boolean.class);
142141
}
143142

143+
private static boolean evaluateBooleanProperty(
144+
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent,
145+
String property,
146+
boolean defaultValue) {
147+
return applicationEnvironmentPreparedEvent
148+
.getEnvironment()
149+
.getProperty(property, Boolean.class, defaultValue);
150+
}
151+
144152
private static Optional<OpenTelemetryAppender> findOpenTelemetryAppender() {
145153
ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory();
146154
if (!(loggerFactorySpi instanceof LoggerContext)) {

smoke-tests-otel-starter/spring-boot-3/src/test/java/io/opentelemetry/spring/smoketest/OtelSpringStarterDisabledSmokeTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,46 @@
55

66
package io.opentelemetry.spring.smoketest;
77

8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.sdk.logs.data.LogRecordData;
11+
import io.opentelemetry.sdk.metrics.data.MetricData;
12+
import io.opentelemetry.sdk.trace.data.SpanData;
13+
import java.util.List;
814
import org.junit.jupiter.api.Test;
915
import org.junit.jupiter.api.condition.DisabledInNativeImage;
16+
import org.springframework.beans.factory.annotation.Autowired;
1017
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.boot.test.web.client.TestRestTemplate;
1119

1220
@SpringBootTest(
1321
classes = {
1422
OtelSpringStarterSmokeTestApplication.class,
15-
AbstractOtelSpringStarterSmokeTest.TestConfiguration.class
23+
AbstractOtelSpringStarterSmokeTest.TestConfiguration.class,
24+
SpringSmokeOtelConfiguration.class
1625
},
26+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
1727
properties = {"otel.sdk.disabled=true"})
1828
@DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will
1929
// fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists
20-
class OtelSpringStarterDisabledSmokeTest {
30+
class OtelSpringStarterDisabledSmokeTest extends AbstractSpringStarterSmokeTest {
31+
32+
@Autowired private TestRestTemplate testRestTemplate;
2133

2234
@Test
23-
void shouldStartApplication() {
24-
// make sure we can still start the application with the disabled property
35+
void shouldNotSendTelemetry() throws InterruptedException {
36+
testRestTemplate.getForObject(OtelSpringStarterSmokeTestController.PING, String.class);
37+
38+
// See SpringSmokeOtelConfiguration
39+
Thread.sleep(200);
40+
41+
List<SpanData> exportedSpans = testing.getExportedSpans();
42+
assertThat(exportedSpans).isEmpty();
43+
44+
List<MetricData> exportedMetrics = testing.getExportedMetrics();
45+
assertThat(exportedMetrics).isEmpty();
46+
47+
List<LogRecordData> exportedLogRecords = testing.getExportedLogRecords();
48+
assertThat(exportedLogRecords).isEmpty();
2549
}
2650
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.spring.smoketest;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.sdk.logs.data.LogRecordData;
11+
import java.util.List;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.condition.DisabledInNativeImage;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
16+
@SpringBootTest(
17+
classes = {
18+
OtelSpringStarterSmokeTestApplication.class,
19+
AbstractOtelSpringStarterSmokeTest.TestConfiguration.class,
20+
SpringSmokeOtelConfiguration.class
21+
},
22+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
23+
properties = {"otel.instrumentation.logback-appender.enabled=false"})
24+
@DisabledInNativeImage // Without this the native tests in the OtelSpringStarterSmokeTest class will
25+
// fail with org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" already exists
26+
class OtelSpringStarterWithLogbackInstrumentationDisabledSmokeTest
27+
extends AbstractSpringStarterSmokeTest {
28+
29+
@Test
30+
void shouldNotSendLogRecordTelemetry() throws InterruptedException {
31+
32+
// See SpringSmokeOtelConfiguration
33+
Thread.sleep(200);
34+
35+
List<LogRecordData> exportedLogRecords = testing.getExportedLogRecords();
36+
assertThat(exportedLogRecords).isEmpty();
37+
}
38+
}

0 commit comments

Comments
 (0)