-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 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
43 changes: 43 additions & 0 deletions
43
...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,43 @@ | ||
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.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 void methodAdvice(MethodTransformer transformer) { | ||
// it does not work with typeInitializer() | ||
transformer.applyAdvice(isConstructor(), packageName + ".JpmsClearanceAdvice"); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...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,13 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
import java.util.WeakHashMap; | ||
|
||
public class JpmsAdvisingHelper { | ||
private static final WeakHashMap<Module, Boolean> ALREADY_PROCESSED_CACHE = new WeakHashMap<>(); | ||
|
||
public static boolean isModuleAlreadyProcessed(final Module module) { | ||
return Boolean.TRUE.equals(ALREADY_PROCESSED_CACHE.putIfAbsent(module, Boolean.TRUE)); | ||
} | ||
|
||
private JpmsAdvisingHelper() {} | ||
} |
20 changes: 20 additions & 0 deletions
20
...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,20 @@ | ||
package datadog.trace.instrumentation.mule4; | ||
|
||
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 Module module = self.getClass().getModule(); | ||
if (module == null || JpmsAdvisingHelper.isModuleAlreadyProcessed(module)) { | ||
return; | ||
} | ||
for (String pn : module.getPackages()) { | ||
try { | ||
module.addExports(pn, module.getClassLoader().getUnnamedModule()); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
} | ||
} |