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

JUL log correlation #2724

Merged
merged 32 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e5f78b0
first working draft
SylvainJuge Jul 25, 2022
0721aaa
use static methods
SylvainJuge Jul 25, 2022
2f53689
use single ecs logging version
SylvainJuge Jul 26, 2022
761fc4d
enhance javadoc
SylvainJuge Aug 2, 2022
95ff860
small refator + only capture SEVERE with JUL
SylvainJuge Aug 2, 2022
893a254
Update all the JulMdc from the agent
SylvainJuge Aug 3, 2022
6d8105f
minor cleanup
SylvainJuge Aug 3, 2022
b49edc0
improved and simpler instrumentation
SylvainJuge Aug 3, 2022
1905234
maybe the final simplification
SylvainJuge Aug 8, 2022
338aa2d
Update pom.xml
eyalkoren Aug 21, 2022
5ee3c1c
ignore plugin classes for instrumentation
SylvainJuge Aug 22, 2022
180e790
override Jul formatter for MDC reading
SylvainJuge Aug 22, 2022
230a900
remove unused code
SylvainJuge Aug 22, 2022
705335f
simplify JUL Mdc instrumentation
SylvainJuge Aug 22, 2022
c6c5a6b
remove unnecesary deps + pkg access
SylvainJuge Aug 22, 2022
272a067
slightly better wording
SylvainJuge Aug 22, 2022
09602a0
reformat
SylvainJuge Aug 22, 2022
208904a
post review changes
SylvainJuge Aug 23, 2022
89d8a7b
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Aug 23, 2022
b89c337
do not ignore plugin CL for now
SylvainJuge Aug 23, 2022
2486d0e
update doc
SylvainJuge Aug 23, 2022
f05eb48
ignore only in ECS instrumentation for now
SylvainJuge Aug 25, 2022
d4cf2f3
update documentation
SylvainJuge Aug 25, 2022
72f997b
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Aug 25, 2022
ddd9f4c
clarify JUL support doc
SylvainJuge Aug 25, 2022
de590a4
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Sep 9, 2022
348504f
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Sep 19, 2022
8f0d7bf
cleanup
SylvainJuge Sep 19, 2022
3d29bd7
minor format fixes
SylvainJuge Sep 19, 2022
76af93f
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Sep 28, 2022
f316120
Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge Oct 3, 2022
b6ddf62
fix changelog
SylvainJuge Oct 3, 2022
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 @@ -19,6 +19,7 @@
package co.elastic.apm.agent.bci;

import co.elastic.apm.agent.bci.bytebuddy.AnnotationValueOffsetMappingFactory;
import co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers;
import co.elastic.apm.agent.bci.bytebuddy.ErrorLoggingListener;
import co.elastic.apm.agent.bci.bytebuddy.FailSafeDeclaredMethodsCompiler;
import co.elastic.apm.agent.bci.bytebuddy.InstallationListenerImpl;
Expand Down Expand Up @@ -684,6 +685,7 @@ public Iterable<? extends List<Class<?>>> onError(int index, List<Class<?>> batc
? new LruTypePoolCache(TypePool.Default.ReaderMode.FAST).scheduleEntryEviction()
: AgentBuilder.PoolStrategy.Default.FAST)
.ignore(any(), isReflectionClassLoader())
.ignore(any(), CustomElementMatchers.isPluginClassLoader()) // ignore classes loaded by plugin for instrumentation
.or(any(), classLoaderWithName("org.codehaus.groovy.runtime.callsite.CallSiteClassLoader"))
.or(nameStartsWith("org.aspectj."))
.or(nameStartsWith("org.groovy."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@
public class CustomElementMatchers {

private static final Logger logger = LoggerFactory.getLogger(CustomElementMatchers.class);

private static final ElementMatcher.Junction.AbstractBase<ClassLoader> AGENT_CLASS_LOADER_MATCHER = new ElementMatcher.Junction.AbstractBase<ClassLoader>() {
@Override
public boolean matches(@Nullable ClassLoader classLoader) {
return ClassLoaderUtils.isAgentClassLoader(classLoader);
}
};

private static final ElementMatcher.Junction.AbstractBase<ClassLoader> PLUGIN_CLASS_LOADER_MATCHER = new ElementMatcher.Junction.AbstractBase<ClassLoader>() {
@Override
public boolean matches(@Nullable ClassLoader classLoader) {
return ClassLoaderUtils.isPluginClassLoader(classLoader);
}
};

public static ElementMatcher.Junction<NamedElement> isInAnyPackage(Collection<String> includedPackages,
ElementMatcher.Junction<NamedElement> defaultIfEmpty) {
if (includedPackages.isEmpty()) {
Expand Down Expand Up @@ -182,10 +190,15 @@ public boolean matches(@Nullable ProtectionDomain protectionDomain) {
};
}


public static ElementMatcher.Junction<ClassLoader> isAgentClassLoader() {
return AGENT_CLASS_LOADER_MATCHER;
}

public static ElementMatcher.Junction<ClassLoader> isPluginClassLoader() {
return PLUGIN_CLASS_LOADER_MATCHER;
}

private enum Matcher {
LTE {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,13 @@
*/
package co.elastic.apm.agent.util;

import co.elastic.apm.agent.bci.classloading.ExternalPluginClassLoader;
import co.elastic.apm.agent.bci.classloading.IndyPluginClassLoader;

import javax.annotation.Nullable;

public class ClassLoaderUtils {

/**
* Checks whether the provided {@link ClassLoader} may be unloaded like a web application class loader, for example.
* <p>
* If the class loader can't be unloaded, it is safe to use {@link ThreadLocal}s and to reuse the {@code WeakConcurrentMap.LookupKey}.
* Otherwise, the use of {@link ThreadLocal}s may lead to class loader leaks as it prevents the class loader this class
* is loaded by to unload.
* </p>
*
* @param classLoader The class loader to check.
* @return {@code true} if the provided class loader can be unloaded.
*/
public static boolean isPersistentClassLoader(@Nullable ClassLoader classLoader) {
try {
return classLoader == null // bootstrap class loader
|| classLoader == ClassLoader.getSystemClassLoader()
|| classLoader == ClassLoader.getSystemClassLoader().getParent(); // ext/platfrom class loader;
} catch (Throwable ignored) {
return false;
}
}

public static boolean isAgentClassLoader(@Nullable ClassLoader classLoader) {
return (classLoader != null && classLoader.getClass().getName().startsWith("co.elastic.apm")) ||
// This one also covers unit tests, where the app class loader loads the agent
Expand All @@ -52,4 +34,14 @@ public static boolean isAgentClassLoader(@Nullable ClassLoader classLoader) {
public static boolean isBootstrapClassLoader(@Nullable ClassLoader classLoader) {
return classLoader == null;
}

public static boolean isPluginClassLoader(@Nullable ClassLoader classLoader) {
if(classLoader == null){
SylvainJuge marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
String clName = classLoader.getClass().getName();
return (IndyPluginClassLoader.class.getName().equals(clName)
|| ExternalPluginClassLoader.class.getName().equals(clName));
felixbarny marked this conversation as resolved.
Show resolved Hide resolved

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {

public static class AdviceClass {

@Advice.OnMethodEnter(suppress = Throwable.class, inline = false, skipOn = Advice.OnNonDefaultValue.class)
public static boolean onEnter() {
return true;
}

@Advice.AssignReturned.ToReturned
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
public static Map<String, String> onExit() {
Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
<artifactId>jul-ecs-formatter</artifactId>
<version>${version.ecs.logging}</version>
</dependency>
<dependency>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-ecs-logging-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-logging-plugin-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String getAdviceClassName() {

public static class LoggingAdvice {

private static final LoggerErrorHelper helper =new LoggerErrorHelper(LoggingAdvice.class, tracer);
private static final LoggerErrorHelper helper = new LoggerErrorHelper(LoggingAdvice.class, tracer);

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ protected String getAppenderName(StreamHandler handler) {
protected Formatter createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion,
@Nullable String serviceNodeName, @Nullable Map<String, String> additionalFields,
Formatter originalFormatter) {
EcsFormatter ecsFormatter = new EcsFormatter();
EcsFormatter ecsFormatter = new EcsFormatter() {
@Override
protected Map<String, String> getMdcEntries() {
// using internal tracer state as ECS formatter is not instrumented within the agent plugin
return CorrelationIdMapAdapter.get();
}
};
ecsFormatter.setServiceName(serviceName);
ecsFormatter.setServiceVersion(serviceVersion);
ecsFormatter.setServiceNodeName(serviceNodeName);
Expand Down