Skip to content

[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 3 commits into from
Apr 11, 2025
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
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);
});

Check warning on line 41 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L38-L41

Added lines #L38 - L41 were not covered by tests

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);

Check warning on line 50 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L50

Added line #L50 was not covered by tests

final String opensearchVersion = getOpensearchVersion(project);

Check warning on line 52 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L52

Added line #L52 was not covered by tests

testTask.doFirst(task -> {
File agentJar = new File(project.getBuildDir(), "agent/opensearch-agent-" + opensearchVersion + ".jar");

Check warning on line 55 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L54-L55

Added lines #L54 - L55 were not covered by tests

testTask.jvmArgs("-javaagent:" + agentJar.getAbsolutePath());
});
});

Check warning on line 59 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L57-L59

Added lines #L57 - L59 were not covered by tests
}

/**
* 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();

Check warning on line 69 in buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java

View check run for this annotation

Codecov / codecov/patch

buildSrc/src/main/java/org/opensearch/gradle/agent/JavaAgent.java#L69

Added line #L69 was not covered by tests
}
}
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
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()
);
}
}
Loading