-
Notifications
You must be signed in to change notification settings - Fork 293
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
Add basic Scala Weaver sbt support #8189
Draft
daniel-mohedano
wants to merge
12
commits into
master
Choose a base branch
from
daniel.mohedano/weaver-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f10262f
weaver instrumentation skeleton
daniel-mohedano 9aebeb1
even simpler skeleton
daniel-mohedano 28bd1e7
changed instrumentation to type hierarchy
daniel-mohedano bc83850
basic suite and test reporting
daniel-mohedano 100aafe
weaver test reporting logic
daniel-mohedano 6fb4039
weaver integration tests
daniel-mohedano af77573
weaver integration test fixtures
daniel-mohedano 933ea53
update weaver integration test names
daniel-mohedano c28c082
Merge branch 'master' into daniel.mohedano/weaver-support
daniel-mohedano 9028e01
fix weaver integration test fixtures and other issues
daniel-mohedano 339d59d
refactors TestEventsHandler start and end times and pr fixes
daniel-mohedano af56922
add resource sharing integration tests for Weaver
daniel-mohedano 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
This file contains 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
This file contains 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
This file contains 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,33 @@ | ||
apply from: "$rootDir/gradle/java.gradle" | ||
apply plugin: 'scala' | ||
|
||
muzzle { | ||
pass { | ||
group = 'com.disneystreaming' | ||
module = 'weaver-cats_3' | ||
versions = '[0.8.4,)' | ||
} | ||
} | ||
|
||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
compileOnly group: 'com.disneystreaming', name: 'weaver-cats_3', version: '0.8.4' | ||
|
||
testImplementation testFixtures(project(':dd-java-agent:agent-ci-visibility')) | ||
|
||
testImplementation group: 'org.scala-lang', name: 'scala-library', version: '2.12.20' | ||
testImplementation group: 'com.disneystreaming', name: 'weaver-cats_3', version: '0.8.4' | ||
|
||
testImplementation group: 'com.disneystreaming', name: 'weaver-cats_3', version: '+' | ||
} | ||
|
||
compileTestGroovy { | ||
dependsOn compileTestScala | ||
classpath += files(sourceSets.test.scala.destinationDirectory) | ||
} | ||
|
||
compileLatestDepTestGroovy { | ||
dependsOn compileLatestDepTestScala | ||
classpath += files(sourceSets.latestDepTest.scala.destinationDirectory) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
...tion/weaver/src/main/java/datadog/trace/instrumentation/weaver/DatadogWeaverReporter.java
This file contains 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,136 @@ | ||
package datadog.trace.instrumentation.weaver; | ||
|
||
import datadog.trace.api.civisibility.InstrumentationBridge; | ||
import datadog.trace.api.civisibility.events.TestDescriptor; | ||
import datadog.trace.api.civisibility.events.TestEventsHandler; | ||
import datadog.trace.api.civisibility.events.TestSuiteDescriptor; | ||
import datadog.trace.api.civisibility.telemetry.tag.TestFrameworkInstrumentation; | ||
import datadog.trace.api.time.SystemTimeSource; | ||
import datadog.trace.util.AgentThreadFactory; | ||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import sbt.testing.TaskDef; | ||
import weaver.Result; | ||
import weaver.TestOutcome; | ||
import weaver.framework.SuiteFinished; | ||
import weaver.framework.SuiteStarted; | ||
import weaver.framework.TestFinished; | ||
|
||
public class DatadogWeaverReporter { | ||
|
||
private static final String TEST_FRAMEWORK = "weaver"; | ||
private static final String TEST_FRAMEWORK_VERSION = WeaverUtils.getWeaverVersion(); | ||
|
||
private static volatile TestEventsHandler<TestSuiteDescriptor, TestDescriptor> | ||
TEST_EVENTS_HANDLER; | ||
|
||
static { | ||
Runtime.getRuntime() | ||
.addShutdownHook( | ||
AgentThreadFactory.newAgentThread( | ||
AgentThreadFactory.AgentThread.CI_TEST_EVENTS_SHUTDOWN_HOOK, | ||
DatadogWeaverReporter::stop, | ||
false)); | ||
} | ||
|
||
public static synchronized void start() { | ||
if (TEST_EVENTS_HANDLER == null) { | ||
TEST_EVENTS_HANDLER = InstrumentationBridge.createTestEventsHandler("weaver", null, null); | ||
} | ||
} | ||
|
||
public static synchronized void stop() { | ||
if (TEST_EVENTS_HANDLER != null) { | ||
TEST_EVENTS_HANDLER.close(); | ||
TEST_EVENTS_HANDLER = null; | ||
} | ||
} | ||
|
||
public static void onSuiteStart(SuiteStarted event) { | ||
String testSuiteName = event.name(); | ||
Class<?> testClass = WeaverUtils.getClass(testSuiteName); | ||
Collection<String> categories = Collections.emptyList(); | ||
boolean parallelized = true; | ||
|
||
TEST_EVENTS_HANDLER.onTestSuiteStart( | ||
new TestSuiteDescriptor(testSuiteName, testClass), | ||
testSuiteName, | ||
TEST_FRAMEWORK, | ||
TEST_FRAMEWORK_VERSION, | ||
testClass, | ||
categories, | ||
parallelized, | ||
TestFrameworkInstrumentation.WEAVER); | ||
} | ||
|
||
public static void onSuiteFinish(SuiteFinished event) { | ||
String testSuiteName = event.name(); | ||
Class<?> testClass = WeaverUtils.getClass(testSuiteName); | ||
|
||
TEST_EVENTS_HANDLER.onTestSuiteFinish(new TestSuiteDescriptor(testSuiteName, testClass)); | ||
} | ||
|
||
public static void onTestFinished(TestFinished event, TaskDef taskDef) { | ||
if (!(event.outcome() instanceof TestOutcome.Default)) { | ||
// Cannot obtain desired information without the TestOutcome.Default fields | ||
return; | ||
} | ||
|
||
TestOutcome.Default testOutcome = (TestOutcome.Default) event.outcome(); | ||
String testSuiteName = taskDef.fullyQualifiedName(); | ||
Class<?> testClass = WeaverUtils.getClass(testSuiteName); | ||
TestSuiteDescriptor testSuiteDescriptor = new TestSuiteDescriptor(testSuiteName, testClass); | ||
String testName = event.outcome().name(); | ||
Object testQualifier = null; | ||
String testParameters = null; | ||
Collection<String> categories = Collections.emptyList(); | ||
TestDescriptor testDescriptor = | ||
new TestDescriptor(testSuiteName, testClass, testName, testParameters, testQualifier); | ||
String testMethodName = null; | ||
Method testMethod = null; | ||
boolean isRetry = false; | ||
|
||
// Only test finish is reported, so fake test start timestamp | ||
long endMicros = SystemTimeSource.INSTANCE.getCurrentTimeMicros(); | ||
long startMicros = endMicros - testOutcome.duration().toMicros(); | ||
TEST_EVENTS_HANDLER.onTestStart( | ||
testSuiteDescriptor, | ||
testDescriptor, | ||
testSuiteName, | ||
testName, | ||
TEST_FRAMEWORK, | ||
TEST_FRAMEWORK_VERSION, | ||
testParameters, | ||
categories, | ||
testClass, | ||
testMethodName, | ||
testMethod, | ||
isRetry, | ||
startMicros); | ||
|
||
if (testOutcome.result() instanceof Result.Ignored) { | ||
Result.Ignored result = (Result.Ignored) testOutcome.result(); | ||
String reason = result.reason().getOrElse(null); | ||
TEST_EVENTS_HANDLER.onTestSkip(testDescriptor, reason); | ||
} else if (testOutcome.result() instanceof Result.Cancelled) { | ||
Result.Cancelled result = (Result.Cancelled) testOutcome.result(); | ||
String reason = result.reason().getOrElse(null); | ||
TEST_EVENTS_HANDLER.onTestSkip(testDescriptor, reason); | ||
} else if (testOutcome.result() instanceof Result.Failure) { | ||
Result.Failure result = (Result.Failure) testOutcome.result(); | ||
Throwable throwable = result.source().getOrElse(null); | ||
TEST_EVENTS_HANDLER.onTestFailure(testDescriptor, throwable); | ||
} else if (testOutcome.result() instanceof Result.Failures) { | ||
Result.Failures result = (Result.Failures) testOutcome.result(); | ||
Throwable throwable = result.failures().head().source().getOrElse(null); | ||
TEST_EVENTS_HANDLER.onTestFailure(testDescriptor, throwable); | ||
} else if (testOutcome.result() instanceof Result.Exception) { | ||
Result.Exception result = (Result.Exception) testOutcome.result(); | ||
Throwable throwable = result.source(); | ||
TEST_EVENTS_HANDLER.onTestFailure(testDescriptor, throwable); | ||
} | ||
|
||
TEST_EVENTS_HANDLER.onTestFinish(testDescriptor, endMicros); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ion/weaver/src/main/java/datadog/trace/instrumentation/weaver/TaskDefAwareQueueProxy.java
This file contains 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,36 @@ | ||
package datadog.trace.instrumentation.weaver; | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import sbt.testing.TaskDef; | ||
import weaver.framework.RunEvent; | ||
import weaver.framework.SuiteFinished; | ||
import weaver.framework.SuiteStarted; | ||
import weaver.framework.TestFinished; | ||
|
||
public final class TaskDefAwareQueueProxy<T> extends ConcurrentLinkedQueue<T> { | ||
|
||
private final TaskDef taskDef; | ||
private final ConcurrentLinkedQueue<T> delegate; | ||
|
||
public TaskDefAwareQueueProxy(TaskDef taskDef, ConcurrentLinkedQueue<T> delegate) { | ||
this.taskDef = taskDef; | ||
this.delegate = delegate; | ||
DatadogWeaverReporter.start(); | ||
} | ||
|
||
@Override | ||
public T poll() { | ||
T event = delegate.poll(); | ||
if (event instanceof RunEvent) { | ||
// handle event here, using taskDef reference to get suite details | ||
if (event instanceof SuiteStarted) { | ||
DatadogWeaverReporter.onSuiteStart((SuiteStarted) event); | ||
} else if (event instanceof SuiteFinished) { | ||
DatadogWeaverReporter.onSuiteFinish((SuiteFinished) event); | ||
} else if (event instanceof TestFinished) { | ||
DatadogWeaverReporter.onTestFinished((TestFinished) event, taskDef); | ||
} | ||
} | ||
return event; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...tion/weaver/src/main/java/datadog/trace/instrumentation/weaver/WeaverInstrumentation.java
This file contains 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.weaver; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import net.bytebuddy.asm.Advice; | ||
import sbt.testing.TaskDef; | ||
import weaver.framework.SuiteEvent; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class WeaverInstrumentation extends InstrumenterModule.CiVisibility | ||
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
||
public WeaverInstrumentation() { | ||
super("ci-visibility", "weaver"); | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Set<TargetSystem> enabledSystems) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to override this method, since all it does is delegating to the parent class |
||
return super.isApplicable(enabledSystems); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "weaver.framework.SbtTask"; | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".DatadogWeaverReporter", | ||
packageName + ".TaskDefAwareQueueProxy", | ||
packageName + ".WeaverUtils", | ||
}; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isConstructor(), WeaverInstrumentation.class.getName() + "$SbtTaskCreationAdvice"); | ||
} | ||
|
||
public static class SbtTaskCreationAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onTaskCreation( | ||
@Advice.FieldValue(value = "queue", readOnly = false) | ||
ConcurrentLinkedQueue<SuiteEvent> queue, | ||
@Advice.FieldValue("taskDef") TaskDef taskDef) { | ||
queue = new TaskDefAwareQueueProxy<SuiteEvent>(taskDef, queue); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ Code Quality Violation
Consider adding super() or this() to your constructor (...read more)
In Java, it is suggested to call
super()
in an extended class. This rule will report a violation if both a call tosuper()
and an overloaded constructor are absent.