-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[Build Tools] Custom Gradle plugin to leverage java agent #17900
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions
71
buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.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,71 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.gradle.agent; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.tasks.Copy; | ||
import org.gradle.api.tasks.TaskProvider; | ||
import org.gradle.api.tasks.testing.Test; | ||
|
||
import java.io.File; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Gradle plugin to automatically configure the OpenSearch Java agent | ||
* for test tasks in OpenSearch plugin projects. | ||
*/ | ||
public class JavaAgent implements Plugin<Project> { | ||
|
||
/** | ||
* Plugin implementation that sets up java agent configuration and applies it to test tasks. | ||
*/ | ||
@Override | ||
public void apply(Project project) { | ||
Configuration agentConfiguration = project.getConfigurations().findByName("agent"); | ||
if (agentConfiguration == null) { | ||
agentConfiguration = project.getConfigurations().create("agent"); | ||
} | ||
|
||
project.afterEvaluate(p -> { | ||
String opensearchVersion = getOpensearchVersion(p); | ||
p.getDependencies().add("agent", "org.opensearch:opensearch-agent-bootstrap:" + opensearchVersion); | ||
p.getDependencies().add("agent", "org.opensearch:opensearch-agent:" + opensearchVersion); | ||
}); | ||
|
||
Configuration finalAgentConfiguration = agentConfiguration; | ||
TaskProvider<Copy> prepareJavaAgent = project.getTasks().register("prepareJavaAgent", Copy.class, task -> { | ||
task.from(finalAgentConfiguration); | ||
task.into(new File(project.getBuildDir(), "agent")); | ||
}); | ||
|
||
project.getTasks().withType(Test.class).configureEach(testTask -> { | ||
testTask.dependsOn(prepareJavaAgent); | ||
|
||
final String opensearchVersion = getOpensearchVersion(project); | ||
|
||
testTask.doFirst(task -> { | ||
File agentJar = new File(project.getBuildDir(), "agent/opensearch-agent-" + opensearchVersion + ".jar"); | ||
|
||
testTask.jvmArgs("-javaagent:" + agentJar.getAbsolutePath()); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the OpenSearch version from project properties, with a fallback default. | ||
* | ||
* @param project The Gradle project | ||
* @return The OpenSearch version to use | ||
*/ | ||
private String getOpensearchVersion(Project project) { | ||
return Objects.requireNonNull(project.property("opensearch_version")).toString(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
buildSrc/src/main/resources/META-INF/gradle-plugins/opensearch.java-agent.properties
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,9 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
# | ||
|
||
implementation-class=org.opensearch.gradle.agent.JavaAgent |
66 changes: 66 additions & 0 deletions
66
buildSrc/src/test/java/org/opensearch/gradle/agent/JavaAgentTests.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,66 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.gradle.agent; | ||
|
||
import org.opensearch.gradle.test.GradleUnitTestCase; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.tasks.Copy; | ||
import org.gradle.testfixtures.ProjectBuilder; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class JavaAgentTests extends GradleUnitTestCase { | ||
private TemporaryFolder projectDir; | ||
private final String PREPARE_JAVA_AGENT_TASK = "prepareJavaAgent"; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
projectDir = new TemporaryFolder(); | ||
projectDir.create(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
projectDir.delete(); | ||
} | ||
|
||
/** | ||
* This test is used to verify that adding the 'opensearch.java-agent' to the project | ||
* creates the necessary agent configuration and tasks. This is basically | ||
* a behavioral test of the {@link JavaAgent#apply(Project)} method. | ||
*/ | ||
@Test | ||
public void applyJavaAgentPlugin() { | ||
// Create an empty project and apply the JavaAgent plugin | ||
Project project = ProjectBuilder.builder().build(); | ||
project.getPluginManager().apply(JavaAgent.class); | ||
|
||
// Verify the agent configuration was created | ||
Configuration agentConfig = project.getConfigurations().findByName("agent"); | ||
assertNotNull("Agent configuration should be created", agentConfig); | ||
|
||
// Verify the prepareJavaAgent task was created and is of the right type | ||
assertNotNull("prepareJavaAgent task should be created", project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK)); | ||
assertTrue("prepareJavaAgent task should be of type Copy", project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK) instanceof Copy); | ||
|
||
// Verify the destination directory of the Copy task | ||
Copy prepareTask = (Copy) project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK); | ||
assertEquals( | ||
"Destination directory should be build/agent", | ||
new File(project.getBuildDir(), "agent"), | ||
prepareTask.getDestinationDir() | ||
); | ||
} | ||
} |
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.