Skip to content

Commit

Permalink
Closes #794 - Added support for JBoss LogManager MDC (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Kunz authored and mariusoe committed Jul 3, 2020
1 parent 648de5f commit 02dae0f
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public class TraceIdMDCInjectionSettings {
* Slf4j injection will only take place, if this field and {@link #enabled} are true.
*/
private boolean slf4jEnabled;

/**
* JBoss LogManager injection will only take place, if this field and {@link #enabled} are true.
*/
private boolean jbossLogmanagerEnabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ inspectit:
log4j1-enabled: true
# Opt-out option for Log4J2
log4j2-enabled: true
# Opt-out option for JBoss Logmanager
jboss-logmanager-enabled: true
trace-id-auto-injection:
# whether the trace id should automatically injected into log statements
enabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import rocks.inspectit.ocelot.core.AgentImpl;
import rocks.inspectit.ocelot.core.config.InspectitConfigChangedEvent;
import rocks.inspectit.ocelot.core.config.InspectitEnvironment;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters.Log4J1MDCAdapter;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters.Log4J2MDCAdapter;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters.MDCAdapter;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters.Slf4jMDCAdapter;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters.*;
import rocks.inspectit.ocelot.core.instrumentation.event.IClassDiscoveryListener;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -73,6 +70,7 @@ void registerAdapters() {
mdcAdapterBuilders.put(Slf4jMDCAdapter.MDC_CLASS, Slf4jMDCAdapter::get);
mdcAdapterBuilders.put(Log4J2MDCAdapter.THREAD_CONTEXT_CLASS, Log4J2MDCAdapter::get);
mdcAdapterBuilders.put(Log4J1MDCAdapter.MDC_CLASS, Log4J1MDCAdapter::get);
mdcAdapterBuilders.put(JBossLogmanagerMDCAdapter.MDC_CLASS, JBossLogmanagerMDCAdapter::get);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters;

import rocks.inspectit.ocelot.config.model.tracing.TraceIdMDCInjectionSettings;
import rocks.inspectit.ocelot.core.utils.WeakMethodReference;

/**
* Provides access to JBoss Logmanagers ThreadContext.
*
* @author boris_unckel
*/
public class JBossLogmanagerMDCAdapter extends AbstractStaticMapMDCAdapter {

/**
* The name of the MDC class of JBoss Logmanager.
*/
public static final String MDC_CLASS = "org.jboss.logmanager.MDC";

private JBossLogmanagerMDCAdapter(WeakMethodReference put, WeakMethodReference get, WeakMethodReference remove) {
super(put, get, remove);
}

/**
* Creates an Adapater given a JBoss Logmanager MDC class.
*
* @param mdcClazz the org.jboss.logmanager.MDC class
*
* @return an adapter for setting values on the given MDC.
*/
public static JBossLogmanagerMDCAdapter get(Class<?> mdcClazz) {
try {
WeakMethodReference put = WeakMethodReference.create(mdcClazz, "put", String.class, String.class);
WeakMethodReference get = WeakMethodReference.create(mdcClazz, "get", String.class);
WeakMethodReference remove = WeakMethodReference.create(mdcClazz, "remove", String.class);
return new JBossLogmanagerMDCAdapter(put, get, remove);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("JBoss MDC class did not contain expected methods", e);
}
}

@Override
public boolean isEnabledForConfig(TraceIdMDCInjectionSettings settings) {
return settings.isJbossLogmanagerEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public static Log4J1MDCAdapter get(Class<?> mdcClazz) {

@Override
public boolean isEnabledForConfig(TraceIdMDCInjectionSettings settings) {
return settings.isLog4j1Enabled() && settings.isEnabled();
return settings.isLog4j1Enabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public static Log4J2MDCAdapter get(Class<?> mdcClazz) {

@Override
public boolean isEnabledForConfig(TraceIdMDCInjectionSettings settings) {
return settings.isLog4j2Enabled() && settings.isEnabled();
return settings.isLog4j2Enabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public static Slf4jMDCAdapter get(Class<?> mdcClazz) {

@Override
public boolean isEnabledForConfig(TraceIdMDCInjectionSettings settings) {
return settings.isSlf4jEnabled() && settings.isEnabled();
return settings.isSlf4jEnabled();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package rocks.inspectit.ocelot.core.instrumentation.correlation.log.adapters;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import rocks.inspectit.ocelot.core.instrumentation.correlation.log.MDCAccess;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class JBossLogmanagerMDCAdapterTest {

private static class DummyMDC {

private static Map<String, String> backingMap = new HashMap<>();

public static String get(String key) {
return backingMap.get(key);
}

public static String put(String key, String value) {
return backingMap.put(key, value);
}

public static String remove(String key) {
return backingMap.remove(key);
}
}

@Nested
public static class Set {

@Test
void verifyMDCAccess() {
DummyMDC.put("my_key", "default");
JBossLogmanagerMDCAdapter adapter = JBossLogmanagerMDCAdapter.get(DummyMDC.class);

MDCAccess.Undo undo = adapter.set("my_key", "overridden");
assertThat(DummyMDC.get("my_key")).isEqualTo("overridden");

undo.close();
assertThat(DummyMDC.get("my_key")).isEqualTo("default");

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ inspectit:
slf4j-enabled: true # Set to "false" to disable slf4J-Support
log4j1-enabled: true # Set to "false" to disable Log4J Version 1 Support
log4j2-enabled: true # Set to "false" to disable Log4J Version 2 Support
jboss-logmanager-enabled: true # Set to "false" to disable JBoss Logmanager support
```


Expand Down

0 comments on commit 02dae0f

Please sign in to comment.