Skip to content

Commit

Permalink
[INLONG-11455][Sort] Only one OpenTelemetryAppender should be registe…
Browse files Browse the repository at this point in the history
…red (#11480)
  • Loading branch information
qy-liuhuo authored Nov 14, 2024
1 parent 4a705be commit ed165b7
Showing 1 changed file with 72 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.message.SimpleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -172,64 +175,82 @@ private void createOpenTelemetrySdk() {
SDK = sdkBuilder.build();
}

/**
* Add OpenTelemetryAppender to Log4j
*/
private void addOpenTelemetryAppender() {
org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(false);
LoggerContext loggerContext = (LoggerContext) context;
Configuration config = loggerContext.getConfiguration();
// Create OpenTelemetryAppender
OpenTelemetryAppender otelAppender = OpenTelemetryAppender.builder()
.setName("OpenTelemetryAppender")
.setLayout(this.layout)
.build();
otelAppender.start();
// add OpenTelemetryAppender to configuration
config.addAppender(otelAppender);
// Get Root Logger Configuration
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
// Add OpenTelemetryAppender to Root Logger
loggerConfig.addAppender(otelAppender, this.logLevel, null);
// refresh logger context
loggerContext.updateLoggers();
}

/**
* Remove OpenTelemetryAppender from Log4j
*/
private void removeOpenTelemetryAppender() {
org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(false);
LoggerContext loggerContext = (LoggerContext) context;
Configuration config = loggerContext.getConfiguration();
config.getAppenders().values().forEach(appender -> {
// Remove OpenTelemetryAppender
if (appender instanceof OpenTelemetryAppender) {
config.getRootLogger().removeAppender(appender.getName());
appender.stop();
}
});
// refresh logger context
loggerContext.updateLoggers();
}

/**
* Install OpenTelemetryLogger for the application
*/
public void install() {
addOpenTelemetryAppender();
createOpenTelemetrySdk();
OpenTelemetryAppender.install(SDK);
LOG.info("OpenTelemetryLogger installed");
public boolean install() {
synchronized (OpenTelemetryLogger.class) {
org.apache.logging.log4j.spi.LoggerContext loggerContextSpi = LogManager.getContext(false);
if (!(loggerContextSpi instanceof LoggerContext)) {
LOG.warn("LoggerContext is not instance of org.apache.logging.log4j.core.LoggerContext");
return false;
}
LoggerContext loggerContext = (LoggerContext) loggerContextSpi;
Configuration config = loggerContext.getConfiguration();
for (Appender appender : config.getAppenders().values()) {
if (appender instanceof OpenTelemetryAppender) {
LOG.info("OpenTelemetryLogger already installed");
return false;
}
}
// Create OpenTelemetryAppender
OpenTelemetryAppender otelAppender = OpenTelemetryAppender.builder()
.setName("OpenTelemetryAppender")
.setLayout(this.layout)
.build();
otelAppender.start();
// add OpenTelemetryAppender to configuration
config.addAppender(otelAppender);
// Get Root Logger Configuration
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
// Add OpenTelemetryAppender to Root Logger
loggerConfig.addAppender(otelAppender, this.logLevel, null);
// refresh logger context
loggerContext.updateLoggers();
// create OpenTelemetry SDK
createOpenTelemetrySdk();
// install OpenTelemetryAppender
OpenTelemetryAppender.install(SDK);
LOG.info("OpenTelemetryLogger installed");
otelAppender.append(
new Log4jLogEvent.Builder()
.setLevel(Level.INFO)
.setMessage(new SimpleMessage("OpenTelemetryLogger installed"))
.build());
return true;
}
}

/**
* Uninstall OpenTelemetryLogger
*/
public void uninstall() {
LOG.info("OpenTelemetryLogger uninstalled");
SDK.close();
removeOpenTelemetryAppender();

public boolean uninstall() {
synchronized (OpenTelemetryLogger.class) {
if (SDK == null) {
LOG.warn("OpenTelemetryLogger is not installed");
return false;
}
org.apache.logging.log4j.spi.LoggerContext loggerContextSpi = LogManager.getContext(false);
if (!(loggerContextSpi instanceof LoggerContext)) {
LOG.warn("LoggerContext is not instance of org.apache.logging.log4j.core.LoggerContext");
return false;
}
LoggerContext loggerContext = (LoggerContext) loggerContextSpi;
Configuration config = loggerContext.getConfiguration();
config.getAppenders().values().forEach(appender -> {
if (appender instanceof OpenTelemetryAppender) {
LOG.info("OpenTelemetryLogger uninstalled");
appender.append(
new Log4jLogEvent.Builder()
.setLevel(Level.INFO)
.setMessage(new SimpleMessage("OpenTelemetryLogger uninstalled"))
.build());
config.getRootLogger().removeAppender(appender.getName());
appender.stop();
}
});
SDK.close();
return true;
}
}
}

0 comments on commit ed165b7

Please sign in to comment.