-
Notifications
You must be signed in to change notification settings - Fork 318
Call Site Instrumentation plugin #3729
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
Merged
manuel-alvarez-alvarez
merged 1 commit into
master
from
malvarez/call-site-instrumentation-plugin
Oct 10, 2022
Merged
Changes from all commits
Commits
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 hidden or 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
87 changes: 87 additions & 0 deletions
87
buildSrc/call-site-instrumentation-plugin/build.gradle.kts
This file contains hidden or 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,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() | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
...l-site-instrumentation-plugin/src/main/java/datadog/trace/plugin/csi/AdviceGenerator.java
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...e-instrumentation-plugin/src/main/java/datadog/trace/plugin/csi/AdvicePointcutParser.java
This file contains hidden or 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,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); | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...-site-instrumentation-plugin/src/main/java/datadog/trace/plugin/csi/CallSiteReporter.java
This file contains hidden or 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,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); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.