Skip to content

Commit

Permalink
fix disable logback instraumentation
Browse files Browse the repository at this point in the history
simply implementation

add test
  • Loading branch information
jfbreault committed Jun 18, 2024
1 parent 8e10717 commit b49d2bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
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();
}
}

0 comments on commit b49d2bb

Please sign in to comment.