-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[🍒 8187] Fix crash using Mule with JPMS (#8195)
* Mule: do not crash with JPMS (cherry picked from commit 0a7bf86) * muzzle (cherry picked from commit 4af59aa) * Applying suggestions and use ClassValue (cherry picked from commit 817c5ed)
- Loading branch information
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ion/mule-4/src/main/java/datadog/trace/instrumentation/mule4/JpmsMuleInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.agent.tooling.muzzle.Reference; | ||
import datadog.trace.api.Platform; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class JpmsMuleInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.HasMethodAdvice, Instrumenter.ForKnownTypes { | ||
public JpmsMuleInstrumentation() { | ||
super("mule", "mule-jpms"); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return super.isEnabled() && Platform.isJavaVersionAtLeast(9); | ||
} | ||
|
||
@Override | ||
public String[] knownMatchingTypes() { | ||
return new String[] { | ||
// same module but they can be initialized in any order | ||
"org.mule.runtime.tracer.customization.impl.info.ExecutionInitialSpanInfo", | ||
"org.mule.runtime.tracer.customization.impl.provider.LazyInitialSpanInfo", | ||
}; | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".JpmsAdvisingHelper", | ||
}; | ||
} | ||
|
||
@Override | ||
public Reference[] additionalMuzzleReferences() { | ||
return new Reference[] { | ||
// added in 4.5.0 | ||
new Reference.Builder("org.mule.runtime.tracer.api.EventTracer") | ||
.withMethod( | ||
new String[0], | ||
Reference.EXPECTS_NON_STATIC | Reference.EXPECTS_PUBLIC, | ||
"endCurrentSpan", | ||
"V", | ||
"Lorg/mule/runtime/api/event/Event;") | ||
.build(), | ||
}; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
// it does not work with typeInitializer() | ||
transformer.applyAdvice(isConstructor(), packageName + ".JpmsClearanceAdvice"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...tation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import datadog.trace.api.GenericClassValue; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class JpmsAdvisingHelper { | ||
public static final ClassValue<AtomicBoolean> ALREADY_PROCESSED_CACHE = | ||
GenericClassValue.constructing(AtomicBoolean.class); | ||
|
||
private JpmsAdvisingHelper() {} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import static datadog.trace.instrumentation.mule4.JpmsAdvisingHelper.ALREADY_PROCESSED_CACHE; | ||
|
||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
|
||
public class JpmsClearanceAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void openOnReturn(@Advice.This(typing = Assigner.Typing.DYNAMIC) Object self) { | ||
final Class<?> cls = self.getClass(); | ||
if (ALREADY_PROCESSED_CACHE.get(cls).compareAndSet(false, true)) { | ||
final Module module = cls.getModule(); | ||
if (module != null) { | ||
try { | ||
// This call needs imperatively to be done from the same module we're adding exports | ||
// because the jdk is checking that the caller belongs to the same module. | ||
// The code of this advice is getting inlined into the constructor of the class belonging | ||
// to that package so it will work. Moving the same to a helper won't. | ||
module.addExports(cls.getPackageName(), module.getClassLoader().getUnnamedModule()); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
} | ||
} | ||
} |