-
Notifications
You must be signed in to change notification settings - Fork 344
Add execution instrumentation for Karate v2 #11928
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
Merged
gh-worker-dd-mergequeue-cf854d
merged 2 commits into
master
from
daniel.mohedano/karate-v2-execution
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...0/src/main/java/datadog/trace/instrumentation/karate2/KarateExecutionInstrumentation.java
This file contains hidden or 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,87 @@ | ||
| package datadog.trace.instrumentation.karate2; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isBridge; | ||
| import static net.bytebuddy.matcher.ElementMatchers.not; | ||
| import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.Config; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Drives test-retry execution policies (ATR / EFD / attempt-to-fix) and failure suppression for | ||
| * Karate v2. | ||
| * | ||
| * <ul> | ||
| * <li>{@code ScenarioRuntime#call()} is advised to re-run the scenario while the execution policy | ||
| * is applicable, overriding the returned {@code ScenarioResult} with the final attempt. | ||
| * <li>{@code ScenarioResult#addStepResult(StepResult)} is advised to replace a failing step with | ||
| * a skipped one when the policy requests failure suppression. | ||
| * </ul> | ||
| * | ||
| * Compiled for Java 8 (see {@link KarateInstrumentation}); the advice lives in the {@code java21} | ||
| * source set. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public class KarateExecutionInstrumentation extends InstrumenterModule.CiVisibility | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public KarateExecutionInstrumentation() { | ||
| super("ci-visibility", "karate", "test-retry"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled() { | ||
| return super.isEnabled() && Config.get().isCiVisibilityExecutionPoliciesEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] {"io.karatelabs.core.ScenarioRuntime", "io.karatelabs.core.ScenarioResult"}; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| packageName + ".KarateUtils", | ||
| packageName + ".TestEventsHandlerHolder", | ||
| packageName + ".ExecutionContext", | ||
| packageName + ".KarateTracingListener", | ||
| packageName + ".KarateScenarioAdvice", | ||
| packageName + ".KarateScenarioAdvice$RetryAdvice", | ||
| packageName + ".KarateScenarioAdvice$SuppressErrorAdvice" | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| return Collections.singletonMap( | ||
| "io.karatelabs.gherkin.Scenario", packageName + ".ExecutionContext"); | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| // ScenarioRuntime#call() is the run()-equivalent; match the concrete ScenarioResult-returning | ||
| // method, not the synthetic Callable#call() bridge. | ||
| transformer.applyAdvice( | ||
| named("call") | ||
| .and(takesNoArguments()) | ||
| .and(returns(named("io.karatelabs.core.ScenarioResult"))) | ||
| .and(not(isBridge())), | ||
| packageName + ".KarateScenarioAdvice$RetryAdvice"); | ||
|
|
||
| // ScenarioResult#addStepResult(StepResult) | ||
| transformer.applyAdvice( | ||
| named("addStepResult") | ||
| .and(takesArguments(1)) | ||
| .and(takesArgument(0, named("io.karatelabs.core.StepResult"))), | ||
| packageName + ".KarateScenarioAdvice$SuppressErrorAdvice"); | ||
| } | ||
| } | ||
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
...te/karate-2.0/src/main/java21/datadog/trace/instrumentation/karate2/ExecutionContext.java
This file contains hidden or 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,56 @@ | ||
| package datadog.trace.instrumentation.karate2; | ||
|
|
||
| import datadog.trace.api.civisibility.config.TestIdentifier; | ||
| import datadog.trace.api.civisibility.config.TestSourceData; | ||
| import datadog.trace.api.civisibility.execution.TestExecutionPolicy; | ||
| import io.karatelabs.gherkin.Scenario; | ||
| import java.util.Collection; | ||
|
|
||
| public class ExecutionContext { | ||
|
|
||
| private final TestExecutionPolicy executionPolicy; | ||
| private boolean suppressFailures; | ||
| private Throwable suppressedError; | ||
|
|
||
| public ExecutionContext(TestExecutionPolicy executionPolicy) { | ||
| this.executionPolicy = executionPolicy; | ||
| } | ||
|
|
||
| public void setSuppressFailures(boolean suppressFailures) { | ||
| this.suppressFailures = suppressFailures; | ||
| } | ||
|
|
||
| public boolean shouldSuppressFailures() { | ||
| return suppressFailures; | ||
| } | ||
|
|
||
| public TestExecutionPolicy getExecutionPolicy() { | ||
| return executionPolicy; | ||
| } | ||
|
|
||
| /** | ||
| * Karate v2 {@code StepResult} is immutable (no {@code setFailedReason}/{@code setErrorIgnored}), | ||
| * so a failure that was suppressed for retry purposes cannot be carried on the replacement step. | ||
| * We stash it here instead, so the tracing listener can still report the failure to CI | ||
| * Visibility. | ||
| */ | ||
| public void setSuppressedError(Throwable suppressedError) { | ||
| if (this.suppressedError == null) { | ||
| this.suppressedError = suppressedError; | ||
| } | ||
| } | ||
|
|
||
| public Throwable getAndClearSuppressedError() { | ||
| Throwable suppressedError = this.suppressedError; | ||
| this.suppressedError = null; | ||
| return suppressedError; | ||
| } | ||
|
|
||
| public static ExecutionContext create(Scenario scenario) { | ||
| TestIdentifier testIdentifier = KarateUtils.toTestIdentifier(scenario); | ||
| Collection<String> testTags = KarateUtils.getCategories(scenario.getTagsEffective()); | ||
| return new ExecutionContext( | ||
| TestEventsHandlerHolder.TEST_EVENTS_HANDLER.executionPolicy( | ||
| testIdentifier, TestSourceData.UNKNOWN, testTags)); | ||
| } | ||
| } |
7 changes: 6 additions & 1 deletion
7
...karate-2.0/src/main/java21/datadog/trace/instrumentation/karate2/KarateBuilderAdvice.java
This file contains hidden or 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
100 changes: 100 additions & 0 deletions
100
...arate-2.0/src/main/java21/datadog/trace/instrumentation/karate2/KarateScenarioAdvice.java
This file contains hidden or 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,100 @@ | ||
| package datadog.trace.instrumentation.karate2; | ||
|
|
||
| import datadog.trace.api.civisibility.execution.TestExecutionPolicy; | ||
| import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import io.karatelabs.core.RunEvent; | ||
| import io.karatelabs.core.RunListener; | ||
| import io.karatelabs.core.ScenarioResult; | ||
| import io.karatelabs.core.ScenarioRuntime; | ||
| import io.karatelabs.core.StepResult; | ||
| import io.karatelabs.gherkin.Scenario; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** Advice classes for {@code io.karatelabs.core.ScenarioRuntime}/{@code ScenarioResult}. */ | ||
| public class KarateScenarioAdvice { | ||
|
|
||
| public static class RetryAdvice { | ||
| @Advice.OnMethodEnter | ||
| public static void beforeExecute(@Advice.This ScenarioRuntime scenarioRuntime) { | ||
| ExecutionContext executionContext = | ||
| InstrumentationContext.get(Scenario.class, ExecutionContext.class) | ||
| .computeIfAbsent(scenarioRuntime.getScenario(), ExecutionContext::create); | ||
|
|
||
| // Indicate beforehand whether failures should be suppressed. This aligns the ordering with | ||
| // the rest of the frameworks. | ||
| TestExecutionPolicy executionPolicy = executionContext.getExecutionPolicy(); | ||
| executionContext.setSuppressFailures(executionPolicy.suppressFailures()); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit | ||
| public static void afterExecute( | ||
| @Advice.This ScenarioRuntime scenarioRuntime, | ||
| @Advice.Return(readOnly = false) ScenarioResult result) { | ||
| if (CallDepthThreadLocalMap.incrementCallDepth(ScenarioRuntime.class) > 0) { | ||
| // nested call (a retry invoked below, or a called scenario) | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| Scenario scenario = scenarioRuntime.getScenario(); | ||
| ExecutionContext context = | ||
| InstrumentationContext.get(Scenario.class, ExecutionContext.class).get(scenario); | ||
| if (context == null) { | ||
| return; | ||
| } | ||
|
|
||
| ScenarioResult finalResult = result; | ||
| TestExecutionPolicy executionPolicy = context.getExecutionPolicy(); | ||
| while (executionPolicy.applicable()) { | ||
| ScenarioRuntime retry = | ||
| new ScenarioRuntime(scenarioRuntime.getFeatureRuntime(), scenario); | ||
| finalResult = retry.call(); | ||
|
daniel-mohedano marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // override the return value so the final attempt is the one recorded. | ||
| result = finalResult; | ||
|
daniel-mohedano marked this conversation as resolved.
daniel-mohedano marked this conversation as resolved.
|
||
| } finally { | ||
| CallDepthThreadLocalMap.reset(ScenarioRuntime.class); | ||
| } | ||
| } | ||
|
|
||
| // Karate 2.0.0 and above | ||
| public static void muzzleCheck(RunListener runListener) { | ||
| runListener.onEvent((RunEvent) null); | ||
| } | ||
| } | ||
|
|
||
| public static class SuppressErrorAdvice { | ||
| @Advice.OnMethodEnter | ||
| public static void onAddingStepResult( | ||
| @Advice.Argument(value = 0, readOnly = false) StepResult stepResult, | ||
| @Advice.FieldValue("scenario") Scenario scenario) { | ||
|
|
||
| if (stepResult.isFailed()) { | ||
| ExecutionContext executionContext = | ||
| InstrumentationContext.get(Scenario.class, ExecutionContext.class).get(scenario); | ||
| if (executionContext == null) { | ||
| return; | ||
| } | ||
|
|
||
| // Suppress every failing step of a to-be-retried attempt (not just the first): with | ||
| // continueOnStepFailure a single attempt can add multiple failing steps, and any leak | ||
| // would mark the retry attempt's result failed. | ||
| if (executionContext.shouldSuppressFailures()) { | ||
| // v2 StepResult is immutable: preserve the error out-of-band, then replace the failing | ||
| // step with a skipped one so the scenario no longer counts as failed. | ||
| executionContext.setSuppressedError(stepResult.getError()); | ||
| stepResult = StepResult.skipped(stepResult.getStep(), stepResult.getStartTime()); | ||
|
daniel-mohedano marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| // Karate 2.0.0 and above | ||
| public static void muzzleCheck(RunListener runListener) { | ||
| runListener.onEvent((RunEvent) null); | ||
| } | ||
| } | ||
|
|
||
| private KarateScenarioAdvice() {} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.