Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unecessary addition of the Logback appender #11610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 otelSdkDisableProperty =
evaluateBooleanProperty(applicationEnvironmentPreparedEvent, "otel.sdk.disabled", false);
boolean logbackInstrumentationEnabledProperty =
evaluateBooleanProperty(
applicationEnvironmentPreparedEvent, "otel.instrumentation.logback-appender.enabled");
return otelSdkDisableProperty == null
|| !otelSdkDisableProperty.booleanValue()
|| logbackInstrumentationEnabledProperty == null
|| logbackInstrumentationEnabledProperty.booleanValue();
applicationEnvironmentPreparedEvent,
"otel.instrumentation.logback-appender.enabled",
true);
return !otelSdkDisableProperty && logbackInstrumentationEnabledProperty;
}

private static void reInitializeOpenTelemetryAppender(
Expand Down Expand Up @@ -133,6 +132,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<OpenTelemetryAppender> findOpenTelemetryAppender() {
ILoggerFactory loggerFactorySpi = LoggerFactory.getILoggerFactory();
if (!(loggerFactorySpi instanceof LoggerContext)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,40 @@ void shouldNotInitializeAppenderWhenDisabled() {

assertThat(testing.logRecords()).isEmpty();
}

@Test
void shouldNotInitializeAppenderWhenSdkDisabled() {
Map<String, Object> properties = new HashMap<>();
properties.put("logging.config", "classpath:logback-test.xml");
properties.put("otel.sdk.disabled", "true");

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();
}

@Test
void shouldNotInitializeAppenderWhenInstrumentationDisabled() {
Map<String, Object> properties = new HashMap<>();
properties.put("logging.config", "classpath:logback-test.xml");
properties.put("otel.instrumentation.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();
}
}
Loading