|
| 1 | +package datadog.trace.instrumentation.karate2; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.isBridge; |
| 5 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 6 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; |
| 10 | + |
| 11 | +import com.google.auto.service.AutoService; |
| 12 | +import datadog.trace.agent.tooling.Instrumenter; |
| 13 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 14 | +import datadog.trace.api.Config; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * Drives test-retry execution policies (ATR / EFD / attempt-to-fix) and failure suppression for |
| 20 | + * Karate v2. |
| 21 | + * |
| 22 | + * <ul> |
| 23 | + * <li>{@code ScenarioRuntime#call()} is advised to re-run the scenario while the execution policy |
| 24 | + * is applicable, overriding the returned {@code ScenarioResult} with the final attempt. |
| 25 | + * <li>{@code ScenarioResult#addStepResult(StepResult)} is advised to replace a failing step with |
| 26 | + * a skipped one when the policy requests failure suppression. |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * Compiled for Java 8 (see {@link KarateInstrumentation}); the advice lives in the {@code java21} |
| 30 | + * source set. |
| 31 | + */ |
| 32 | +@AutoService(InstrumenterModule.class) |
| 33 | +public class KarateExecutionInstrumentation extends InstrumenterModule.CiVisibility |
| 34 | + implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { |
| 35 | + |
| 36 | + public KarateExecutionInstrumentation() { |
| 37 | + super("ci-visibility", "karate", "test-retry"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean isEnabled() { |
| 42 | + return super.isEnabled() && Config.get().isCiVisibilityExecutionPoliciesEnabled(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String[] knownMatchingTypes() { |
| 47 | + return new String[] {"io.karatelabs.core.ScenarioRuntime", "io.karatelabs.core.ScenarioResult"}; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String[] helperClassNames() { |
| 52 | + return new String[] { |
| 53 | + packageName + ".KarateUtils", |
| 54 | + packageName + ".TestEventsHandlerHolder", |
| 55 | + packageName + ".ExecutionContext", |
| 56 | + packageName + ".KarateTracingListener", |
| 57 | + packageName + ".KarateScenarioAdvice", |
| 58 | + packageName + ".KarateScenarioAdvice$RetryAdvice", |
| 59 | + packageName + ".KarateScenarioAdvice$SuppressErrorAdvice" |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Map<String, String> contextStore() { |
| 65 | + return Collections.singletonMap( |
| 66 | + "io.karatelabs.gherkin.Scenario", packageName + ".ExecutionContext"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void methodAdvice(MethodTransformer transformer) { |
| 71 | + // ScenarioRuntime#call() is the run()-equivalent; match the concrete ScenarioResult-returning |
| 72 | + // method, not the synthetic Callable#call() bridge. |
| 73 | + transformer.applyAdvice( |
| 74 | + named("call") |
| 75 | + .and(takesNoArguments()) |
| 76 | + .and(returns(named("io.karatelabs.core.ScenarioResult"))) |
| 77 | + .and(not(isBridge())), |
| 78 | + packageName + ".KarateScenarioAdvice$RetryAdvice"); |
| 79 | + |
| 80 | + // ScenarioResult#addStepResult(StepResult) |
| 81 | + transformer.applyAdvice( |
| 82 | + named("addStepResult") |
| 83 | + .and(takesArguments(1)) |
| 84 | + .and(takesArgument(0, named("io.karatelabs.core.StepResult"))), |
| 85 | + packageName + ".KarateScenarioAdvice$SuppressErrorAdvice"); |
| 86 | + } |
| 87 | +} |
0 commit comments