|
| 1 | +package datadog.trace.instrumentation.junit4; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 5 | + |
| 6 | +import com.google.auto.service.AutoService; |
| 7 | +import datadog.trace.agent.tooling.Instrumenter; |
| 8 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 10 | +import net.bytebuddy.asm.Advice; |
| 11 | +import org.junit.runner.manipulation.InvalidOrderingException; |
| 12 | +import org.junit.runner.manipulation.Ordering; |
| 13 | +import org.junit.runners.model.FrameworkMethod; |
| 14 | + |
| 15 | +@AutoService(InstrumenterModule.class) |
| 16 | +public class JUnit4BeforeAfterInstrumentation extends InstrumenterModule.CiVisibility |
| 17 | + implements Instrumenter.ForKnownTypes { |
| 18 | + |
| 19 | + public JUnit4BeforeAfterInstrumentation() { |
| 20 | + super("ci-visibility", "junit-4", "setup-teardown"); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public String[] knownMatchingTypes() { |
| 25 | + return new String[] { |
| 26 | + "org.junit.internal.runners.statements.RunBefores", |
| 27 | + "org.junit.internal.runners.statements.RunAfters", |
| 28 | + "org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters$RunBeforeParams", |
| 29 | + "org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters$RunAfterParams", |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String[] helperClassNames() { |
| 35 | + return new String[] { |
| 36 | + packageName + ".JUnit4BeforeAfterOperationsTracer", |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void methodAdvice(MethodTransformer transformer) { |
| 42 | + transformer.applyAdvice( |
| 43 | + named("invokeMethod") |
| 44 | + .and(takesArgument(0, named("org.junit.runners.model.FrameworkMethod"))), |
| 45 | + JUnit4BeforeAfterInstrumentation.class.getName() + "$RunBeforesAftersAdvice"); |
| 46 | + } |
| 47 | + |
| 48 | + public static class RunBeforesAftersAdvice { |
| 49 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 50 | + public static AgentScope startCallSpan(@Advice.Argument(0) final FrameworkMethod method) { |
| 51 | + return JUnit4BeforeAfterOperationsTracer.startTrace(method.getMethod()); |
| 52 | + } |
| 53 | + |
| 54 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 55 | + public static void finishCallSpan( |
| 56 | + @Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) { |
| 57 | + JUnit4BeforeAfterOperationsTracer.endTrace(scope, throwable); |
| 58 | + } |
| 59 | + |
| 60 | + // JUnit 4.13 and above |
| 61 | + public static void muzzleCheck(final Ordering ord) { |
| 62 | + try { |
| 63 | + ord.apply(null); |
| 64 | + } catch (InvalidOrderingException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments