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

[INLONG-11455][Sort] Only one OpenTelemetryAppender should be registered #11480

Merged
merged 6 commits into from
Nov 14, 2024
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;
}
}
}
Loading