com.undefinedlabs.scope.rules.JUnit5ScopeRunnerAgentRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scope-runner-junit5 Show documentation
Show all versions of scope-runner-junit5 Show documentation
Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly
identify, troubleshoot and fix failed builds.
This artifact contains the classes to instrument the JUnit5 runner.
package com.undefinedlabs.scope.rules;
import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.named;
import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.rules.frameworks.model.junit5.JUnit5ScopeUniqueId;
import com.undefinedlabs.scope.runner.ScopeGlobalRunner;
import com.undefinedlabs.scope.runner.ScopeRunner;
import com.undefinedlabs.scope.runner.TestStatus;
import com.undefinedlabs.scope.runner.rules.AbstractScopeRunnerAgentRule;
import com.undefinedlabs.scope.settings.ScopeSettings;
import com.undefinedlabs.scope.settings.ScopeSettingsResolver;
import com.undefinedlabs.scope.statistics.Statistics;
import com.undefinedlabs.scope.utils.SpanUtils;
import com.undefinedlabs.scope.utils.baggage.BaggageKeys;
import com.undefinedlabs.scope.utils.baggage.BaggageValues;
import com.undefinedlabs.scope.utils.tag.TagKeys;
import com.undefinedlabs.scope.utils.tag.TagValues;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.commons.lang3.StringUtils;
import org.junit.platform.engine.TestDescriptor;
public class JUnit5ScopeRunnerAgentRule extends AbstractScopeRunnerAgentRule {
@Override
protected ElementMatcher super TypeDescription> typeMatcher() {
return hasSuperType(named("org.junit.platform.engine.TestDescriptor"));
}
@Override
protected ElementMatcher classLoaderMatcher() {
return hasClassesNamed("org.junit.platform.engine.TestDescriptor");
}
@Override
protected Map extends ElementMatcher super MethodDescription>, String> transformers() {
final Map, String> transformers = new HashMap<>();
transformers.put(
named("addChild"), JUnit5ScopeRunnerAgentRule.class.getName() + "$JUnit5RunnerAdvice");
return transformers;
}
public static class JUnit5RunnerAdvice {
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
public static Object enter(@Advice.AllArguments final Object[] args) {
if (args.length == 0) {
return null;
}
final ScopeSettings settings = ScopeSettingsResolver.INSTANCE.get();
final JUnit5ScopeUniqueId jUnit5ScopeUniqueId =
new JUnit5ScopeUniqueId(((TestDescriptor) args[0]).getUniqueId().toString());
final ScopeRunner runner = ScopeGlobalRunner.get();
final String testName =
StringUtils.isNotEmpty(jUnit5ScopeUniqueId.getMethodName())
? (jUnit5ScopeUniqueId.getMethodName()
+ (StringUtils.isNotEmpty(jUnit5ScopeUniqueId.getInvocation())
? "[" + jUnit5ScopeUniqueId.getInvocation() + "]"
: ""))
: null;
if (StringUtils.isNotEmpty(jUnit5ScopeUniqueId.getClassName())
&& StringUtils.isNotEmpty(testName)) {
final List testStatuses =
runner.getTestStatuses(jUnit5ScopeUniqueId.getClassName(), testName);
if (testStatuses.contains(TestStatus.CACHED)) {
if ((boolean) settings.getSetting(ScopeSettings.SCOPE_RUNNER_SEND_SPANS)) {
final Tracer tracer = ScopeGlobalTracer.get();
final Span span =
tracer
.buildSpan(testName)
.ignoreActiveSpan()
.withTag(TagKeys.COMPONENT, TagValues.Test.Framework.JUNIT_5)
.withTag(TagKeys.SPAN_KIND, TagValues.SPAN_KIND_TEST)
.withTag(TagKeys.Test.TEST_NAME, testName)
.withTag(TagKeys.Test.TEST_SUITE, jUnit5ScopeUniqueId.getClassName())
.withTag(TagKeys.Test.TEST_FRAMEWORK, TagValues.Test.Framework.JUNIT_5)
.withTag(TagKeys.Test.TEST_LANGUAGE, TagValues.Test.Language.JAVA)
.withTag(TagKeys.Test.TEST_STATUS, TagValues.Test.TEST_CACHE)
.withTag(TagKeys.ERROR, false)
.start();
span.setBaggageItem(BaggageKeys.TRACE_KIND, BaggageValues.TRACE_KIND_TEST);
span.finish(SpanUtils.INSTANCE.getStartTimestampMicros(span));
}
Statistics.INSTANCE.registerRunnerCachedTest(
TagValues.Test.Framework.JUNIT_5, jUnit5ScopeUniqueId.getClassName(), testName);
}
return testStatuses.contains(TestStatus.CACHED) ? new Object() : null;
}
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy