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 com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.deps.org.apache.commons.lang3.StringUtils;
import com.undefinedlabs.scope.rules.frameworks.model.junit5.*;
import com.undefinedlabs.scope.rules.transformer.ScopeAgentAdvicedTransformer;
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 net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import org.junit.platform.engine.TestDescriptor;
import java.util.Collections;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class JUnit5ScopeRunnerAgentRule extends AbstractScopeRunnerAgentRule {
@Override
protected Iterable extends AgentBuilder> transformers() {
final Class instrumentedClass = lookUp("org.junit.platform.engine.TestDescriptor");
return (instrumentedClass != null) ? Collections.singleton(newAgentBuilder()
.type(isSubTypeOf(instrumentedClass), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(JUnit5RunnerAdvice.class, named("addChild"))))
: emptyCollection();
}
public static class JUnit5RunnerAdvice {
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
public static Object enter(@Advice.AllArguments 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