Skip to content
Merged
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
5 changes: 5 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ gradlePlugin {
id = "muzzle"
implementationClass = "MuzzlePlugin"
}
create("call-site-instrumentation-plugin") {
id = "call-site-instrumentation"
implementationClass = "CallSiteInstrumentationPlugin"
}
}
}

Expand Down Expand Up @@ -43,4 +47,5 @@ dependencies {

tasks.test {
useJUnitPlatform()
dependsOn(":call-site-instrumentation-plugin:build")
}
87 changes: 87 additions & 0 deletions buildSrc/call-site-instrumentation-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
java
groovy
id("com.diffplug.spotless") version "5.11.0"
id("com.github.johnrengelman.shadow") version "7.1.2"
}

spotless {
java {
toggleOffOn()
// set explicit target to workaround https://github.com/diffplug/spotless/issues/1163
target("src/**/*.java")
// ignore embedded test projects
targetExclude("src/test/resources/**")
googleJavaFormat()
}
}

repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}

dependencies {
compileOnly("com.google.code.findbugs", "jsr305", "3.0.2")

implementation("org.freemarker", "freemarker", "2.3.30")
implementation("org.ow2.asm", "asm", "9.0")
implementation("org.ow2.asm", "asm-tree", "9.0")

testImplementation("org.spockframework", "spock-core", "2.0-groovy-3.0")
testImplementation("org.codehaus.groovy", "groovy-all", "3.0.10")
testImplementation("com.github.javaparser", "javaparser-symbol-solver-core", "3.24.4")
testImplementation("javax.servlet", "javax.servlet-api", "3.0.1")
}

sourceSets {
test {
java {
srcDirs("src/test/java", "$buildDir/generated/sources/csi")
}
}
}

val copyCallSiteSources = tasks.register<Copy>("copyCallSiteSources") {
val csiPackage = "datadog/trace/agent/tooling/csi"
val source = layout.projectDirectory.file("../../dd-java-agent/agent-tooling/src/main/java/$csiPackage")
val target = layout.buildDirectory.dir("generated/sources/csi/$csiPackage")
doFirst {
val folder = target.get().asFile
if (folder.exists() && !folder.deleteRecursively()) {
throw GradleException("Cannot delete files in $folder")
}
}
from(source)
into(target)
group = "build"
}

tasks {
withType<AbstractCompile>() {
dependsOn(copyCallSiteSources)
}
}

tasks {
named<ShadowJar>("shadowJar") {
archiveBaseName.set("call-site-instrumentation-plugin")
archiveClassifier.set("")
archiveVersion.set("")
mergeServiceFiles()
manifest {
attributes(mapOf("Main-Class" to "datadog.trace.plugin.csi.PluginApplication"))
}
}
}

tasks.build {
dependsOn(tasks.shadowJar)
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package datadog.trace.plugin.csi;

import datadog.trace.plugin.csi.ValidationContext.BaseValidationContext;
import datadog.trace.plugin.csi.impl.CallSiteSpecification;
import datadog.trace.plugin.csi.impl.CallSiteSpecification.AdviceSpecification;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

/**
* Implementors of this interface will build the final Java source code files implementing the
* {@link datadog.trace.agent.tooling.csi.CallSiteAdvice} interface
*/
public interface AdviceGenerator {

@Nonnull
CallSiteResult generate(@Nonnull CallSiteSpecification callSite);

final class CallSiteResult extends BaseValidationContext {

private final CallSiteSpecification specification;
private final List<AdviceResult> advices = new ArrayList<>();

public CallSiteResult(@Nonnull final CallSiteSpecification specification) {
this.specification = specification;
}

@Override
public boolean isSuccess() {
return super.isSuccess() && getAdvices().allMatch(AdviceResult::isSuccess);
}

public Stream<AdviceResult> getAdvices() {
return advices.stream();
}

public void addAdvice(final AdviceResult advice) {
this.advices.add(advice);
}

public CallSiteSpecification getSpecification() {
return specification;
}
}

final class AdviceResult extends BaseValidationContext {

private final AdviceSpecification specification;
private final File file;

public AdviceResult(
@Nonnull final AdviceSpecification specification, @Nonnull final File file) {
this.specification = specification;
this.file = file;
}

public AdviceSpecification getSpecification() {
return specification;
}

public File getFile() {
return file;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package datadog.trace.plugin.csi;

import datadog.trace.plugin.csi.HasErrors.HasErrorsException;
import datadog.trace.plugin.csi.util.MethodType;
import java.util.Collection;
import javax.annotation.Nonnull;

/**
* Implementors of this interface will parse pointcut expressions (e.g. {@code
* java.lang.StringBuilder java.lang.StringBuilder.append(java.lang.String)}) and return the related
* {@link MethodType} instance.
*/
public interface AdvicePointcutParser {

@Nonnull
MethodType parse(@Nonnull String signature);

class SignatureParsingError extends HasErrorsException {

public SignatureParsingError(@Nonnull final HasErrors errors) {
super(errors);
}

public SignatureParsingError(@Nonnull final Collection<Failure> errors) {
super(errors);
}

public SignatureParsingError(@Nonnull final Failure... errors) {
super(errors);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package datadog.trace.plugin.csi;

import datadog.trace.plugin.csi.AdviceGenerator.CallSiteResult;
import freemarker.ext.beans.StringModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface CallSiteReporter {

void report(List<CallSiteResult> results, boolean error);

static CallSiteReporter getReporter(final String type) {
if ("CONSOLE".equals(type)) {
return new ConsoleReporter();
}
throw new IllegalArgumentException("Reporter of type '" + type + "' not supported");
}

abstract class FreemarkerReporter implements CallSiteReporter {
private final String template;

protected FreemarkerReporter(final String template) {
this.template = template;
}

protected void write(final List<CallSiteResult> results, final Writer writer) {
try {
final Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
cfg.setClassLoaderForTemplateLoading(Thread.currentThread().getContextClassLoader(), "csi");
cfg.setDefaultEncoding("UTF-8");
final Map<String, Object> input = new HashMap<>();
input.put("results", results);
input.put("toList", new ToListDirective());
final Template template = cfg.getTemplate("console.ftl");
template.process(input, writer);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

private static class ToListDirective implements TemplateMethodModelEx {

@Override
public Object exec(final List arguments) throws TemplateModelException {
final StringModel model = (StringModel) arguments.get(0);
final Stream<?> stream = (Stream<?>) model.getWrappedObject();
return stream.collect(Collectors.toList());
}
}
}

class ConsoleReporter extends FreemarkerReporter {

protected ConsoleReporter() {
super("console.ftl");
}

@Override
public void report(final List<CallSiteResult> results, final boolean error) {
final PrintStream stream = error ? System.err : System.out;
final StringWriter writer = new StringWriter();
write(results, writer);
stream.println(writer);
}
}
}
Loading