Skip to content

Add new org to Weaver instrumentation #9195

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions dd-java-agent/instrumentation/weaver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ muzzle {
module = 'weaver-cats_3'
versions = '[0.8.4,)'
}

pass {
group = 'org.typelevel'
module = 'weaver-cats_3'
versions = '[0.9.0,)'
}
}

addTestSuiteForDir('latestDepTest', 'test')
// weaver 0.8.4 is the earliest supported version before the org migration
addTestSuiteForDir('weaver084Test', 'test')

dependencies {
compileOnly group: 'com.disneystreaming', name: 'weaver-cats_3', version: '0.8.4'
compileOnly group: 'org.typelevel', name: 'weaver-cats_3', version: '0.9.0'

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: '+'
testImplementation group: 'org.typelevel', name: 'weaver-cats_3', version: '0.9.0'
latestDepTestImplementation group: 'org.typelevel', name: 'weaver-cats_3', version: '+'
weaver084TestImplementation group: 'com.disneystreaming', name: 'weaver-cats_3', version: '0.8.4'
}

compileTestGroovy {
Expand All @@ -31,3 +39,8 @@ compileLatestDepTestGroovy {
dependsOn compileLatestDepTestScala
classpath += files(sourceSets.latestDepTest.scala.destinationDirectory)
}

compileWeaver084TestGroovy {
dependsOn compileWeaver084TestScala
classpath += files(sourceSets.weaver084Test.scala.destinationDirectory)
}
8 changes: 4 additions & 4 deletions dd-java-agent/instrumentation/weaver/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ com.datadoghq.okio:okio:1.17.6=compileClasspath,instrumentPluginClasspath,latest
com.datadoghq:dd-javac-plugin-client:0.2.2=compileClasspath,instrumentPluginClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,muzzleBootstrap,muzzleTooling,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.datadoghq:java-dogstatsd-client:4.4.3=instrumentPluginClasspath,latestDepTestRuntimeClasspath,muzzleTooling,runtimeClasspath,testRuntimeClasspath
com.datadoghq:sketches-java:0.8.3=instrumentPluginClasspath,latestDepTestRuntimeClasspath,muzzleTooling,runtimeClasspath,testRuntimeClasspath
com.disneystreaming:weaver-cats-core_3:0.8.4=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.disneystreaming:weaver-cats_3:0.8.4=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.disneystreaming:weaver-core_3:0.8.4=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.disneystreaming:weaver-framework_3:0.8.4=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.typelevel:weaver-cats-core_3:0.9.2=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.typelevel:weaver-cats_3:0.9.2=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.typelevel:weaver-core_3:0.9.2=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.typelevel:weaver-framework_3:0.9.2=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.eed3si9n.expecty:expecty_3:0.16.0=compileClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.eed3si9n:shaded-jawn-parser_2.13:1.3.2=zinc
com.eed3si9n:shaded-scalajson_2.13:1.0.0-M4=zinc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sbt.testing.TaskDef;
import weaver.Result;
import weaver.TestOutcome;
import weaver.framework.RunEvent;
import weaver.framework.SuiteFinished;
import weaver.framework.SuiteStarted;
import weaver.framework.TestFinished;
Expand Down Expand Up @@ -42,6 +43,19 @@ public static synchronized void stop() {
}
}

public static void processEvent(Object event, TaskDef taskDef) {
if (event instanceof RunEvent) {
// handle event here, using taskDef reference to get suite details
if (event instanceof SuiteStarted) {
onSuiteStart((SuiteStarted) event);
} else if (event instanceof SuiteFinished) {
onSuiteFinish((SuiteFinished) event);
} else if (event instanceof TestFinished) {
onTestFinished((TestFinished) event, taskDef);
}
}
}

public static void onSuiteStart(SuiteStarted event) {
String testSuiteName = event.name();
Class<?> testClass = WeaverUtils.getClass(testSuiteName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package datadog.trace.instrumentation.weaver;

import java.util.concurrent.ConcurrentLinkedQueue;
import sbt.testing.TaskDef;

public final class TaskDefAwareConcurrentLinkedQueueProxy<T> extends ConcurrentLinkedQueue<T> {

private final TaskDef taskDef;
private final ConcurrentLinkedQueue<T> delegate;

public TaskDefAwareConcurrentLinkedQueueProxy(
TaskDef taskDef, ConcurrentLinkedQueue<T> delegate) {
super();
this.taskDef = taskDef;
this.delegate = delegate;
DatadogWeaverReporter.start();
}

@Override
public T poll() {
T event = delegate.poll();
DatadogWeaverReporter.processEvent(event, taskDef);
return event;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package datadog.trace.instrumentation.weaver;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import sbt.testing.TaskDef;

public final class TaskDefAwareLinkedBlockingQueueProxy<T> extends LinkedBlockingQueue<T> {

private final TaskDef taskDef;
private final LinkedBlockingQueue<T> delegate;

public TaskDefAwareLinkedBlockingQueueProxy(TaskDef taskDef, LinkedBlockingQueue<T> delegate) {
super();
this.taskDef = taskDef;
this.delegate = delegate;
DatadogWeaverReporter.start();
}

@Override
public T poll() {
T event = delegate.poll();
DatadogWeaverReporter.processEvent(event, taskDef);
return event;
}

@Override
public T poll(long timeout, TimeUnit unit) throws InterruptedException {
T event = delegate.poll(timeout, unit);
if (event != null) {
DatadogWeaverReporter.processEvent(event, taskDef);
}
return event;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.bytebuddy.asm.Advice;
import sbt.testing.TaskDef;
import weaver.framework.SuiteEvent;
Expand All @@ -28,7 +30,8 @@ public String[] helperClassNames() {
return new String[] {
packageName + ".WeaverUtils",
packageName + ".DatadogWeaverReporter",
packageName + ".TaskDefAwareQueueProxy",
packageName + ".TaskDefAwareLinkedBlockingQueueProxy",
packageName + ".TaskDefAwareConcurrentLinkedQueueProxy",
};
}

Expand All @@ -41,10 +44,26 @@ public void methodAdvice(MethodTransformer transformer) {
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);
@Advice.This Object sbtTask, @Advice.FieldValue("taskDef") TaskDef taskDef) {
try {
Field queueField = sbtTask.getClass().getDeclaredField("queue");
queueField.setAccessible(true);
Object queue = queueField.get(sbtTask);
if (queue instanceof ConcurrentLinkedQueue) {
// disney's implementation (0.8.4+) uses a ConcurrentLinkedQueue for the field
queueField.set(
sbtTask,
new TaskDefAwareConcurrentLinkedQueueProxy<SuiteEvent>(
taskDef, (ConcurrentLinkedQueue<SuiteEvent>) queue));
} else if (queue instanceof LinkedBlockingQueue) {
// typelevel's implementation (0.9+) uses a LinkedBlockingQueue for the field
queueField.set(
sbtTask,
new TaskDefAwareLinkedBlockingQueueProxy<SuiteEvent>(
taskDef, (LinkedBlockingQueue<SuiteEvent>) queue));
}
} catch (Exception ignored) {
}
}
}
}