All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.ScalaTestScopeRunnerAgentRule Maven / Gradle / Ivy

Go to download

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 ScalaTest runner.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.implementation.bytecode.assign.Assigner.Typing.DYNAMIC;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isAbstract;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

import com.undefinedlabs.scope.ScopeGlobalTracer;
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.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.scalatest.SucceededStatus$;
import org.scalatest.Suite;

public class ScalaTestScopeRunnerAgentRule extends AbstractScopeRunnerAgentRule {

  @Override
  protected ElementMatcher typeMatcher() {
    return hasSuperType(named("org.scalatest.Suite")).and(not(isAbstract()));
  }

  @Override
  protected ElementMatcher classLoaderMatcher() {
    return hasClassesNamed("org.scalatest.Suite");
  }

  @Override
  protected Map, String> transformers() {
    final Map, String> transformers = new HashMap<>();
    transformers.put(
        named("runTest"),
        ScalaTestScopeRunnerAgentRule.class.getName() + "$ScalaTestSuiteRunTestAdvice");
    return transformers;
  }

  public static class ScalaTestSuiteRunTestAdvice {

    @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
    public static Object enter(
        @Advice.This final Object thiz, @Advice.AllArguments final Object[] args) {
      final Suite suite = (Suite) thiz;
      final String testName = (String) args[0];
      final ScopeRunner runner = ScopeGlobalRunner.get();

      if (!runner.getTestStatuses(suite.suiteId(), testName).contains(TestStatus.CACHED)) {
        return null;
      } else {
        final Tracer tracer = ScopeGlobalTracer.get();
        final ScopeSettings settings = ScopeSettingsResolver.INSTANCE.get();
        if ((boolean) settings.getSetting(ScopeSettings.SCOPE_RUNNER_SEND_SPANS)) {
          final Span span =
              tracer
                  .buildSpan(testName)
                  .ignoreActiveSpan()
                  .withTag(TagKeys.COMPONENT, TagValues.Test.Framework.SCALATEST)
                  .withTag(TagKeys.SPAN_KIND, TagValues.SPAN_KIND_TEST)
                  .withTag(TagKeys.Test.TEST_NAME, testName)
                  .withTag(TagKeys.Test.TEST_SUITE, suite.suiteId())
                  .withTag(TagKeys.Test.TEST_FRAMEWORK, TagValues.Test.Framework.SCALATEST)
                  .withTag(TagKeys.Test.TEST_LANGUAGE, TagValues.Test.Language.SCALA)
                  .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.SCALATEST, suite.suiteId(), testName);
        return SucceededStatus$.MODULE$;
      }
    }

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Object thiz,
        @Advice.AllArguments final Object[] args,
        @Advice.Enter final Object fromEnter,
        @Advice.Return(readOnly = false, typing = DYNAMIC) Object returnObj) {
      if (fromEnter != null) {
        returnObj = SucceededStatus$.MODULE$;
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy