From 6de21c92d8ba7ab5c77f87df58a1b56c01f60cf7 Mon Sep 17 00:00:00 2001 From: EddeCCC Date: Tue, 24 Sep 2024 12:51:51 +0200 Subject: [PATCH] refactor: inspectit-gepard-agent/build.gradle --- inspectit-gepard-agent/README.md | 9 +- inspectit-gepard-agent/build.gradle | 89 +++++++++++-------- .../bootstrap/InspectitBootstrapManager.java | 7 +- inspectit-gepard-bootstrap/README.md | 10 ++- 4 files changed, 69 insertions(+), 46 deletions(-) diff --git a/inspectit-gepard-agent/README.md b/inspectit-gepard-agent/README.md index 4640904..13a5eae 100644 --- a/inspectit-gepard-agent/README.md +++ b/inspectit-gepard-agent/README.md @@ -1 +1,8 @@ -# TODO +# inspectIT Gepard - Agent + +This project contains the core and actual implementation of the inspectIT Gepard agent extension. +All logic for runtime instrumentation, configuration processing etc. are located in ths project. + + +To run this extension, u will need to provide it to an +[OpenTelemetry Java agent](https://github.com/open-telemetry/opentelemetry-java-instrumentation). diff --git a/inspectit-gepard-agent/build.gradle b/inspectit-gepard-agent/build.gradle index d18c53c..2464f07 100644 --- a/inspectit-gepard-agent/build.gradle +++ b/inspectit-gepard-agent/build.gradle @@ -130,18 +130,51 @@ dependencies { add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}") } -// Produces a copy of upstream javaagent with this extension jar included inside it +/* + Since we cannot include additional submodules into the shadowJar that easily, we use the following approach + to create one extension-jar, containing all of our submodules: + + 1. Copy the output of all submodules to a temporary directory + 2. Unzip the output of the shadowJar task (which includes our core logic) + 3. Move the jars of the other submodules into the unzipped directory + 4. Re-zip the directory + 5. Insert the re-zipped jar into the opentelemetry-javaagent.jar + 6. Delete the temporary directory + */ +def mergeModules = tasks.register("mergeModules", Zip) { + dependsOn(copyModules) + from tasks.copyModules.destinationDir + destinationDirectory = layout.buildDirectory.dir('libs').get().asFile + archiveFileName.set("inspectit-gepard-agent-extension-${version}.jar") +} + +// Copies the output of your submodules to a temporary directory +def copyModules = tasks.register("copyModules", Copy) { + dependsOn(shadowJar) + dependsOn(project(":inspectit-gepard-bootstrap").tasks.jar) + + destinationDir = layout.buildDirectory.dir('copy').get().asFile + + from(zipTree(tasks.shadowJar.archiveFile.get())) + from(project(":inspectit-gepard-bootstrap").jar.outputs) + + finalizedBy(mergeModules) +} + +shadowJar.finalizedBy(copyModules) + +assemble.dependsOn(shadowJar) + +// Produces a copy of upstream javaagent with this extension jar included inside it. // The location of extension directory inside agent jar is hard-coded in the agent source code tasks.register('extendedAgent', Jar) { dependsOn(configurations.otel) + dependsOn(mergeModules) + archiveFileName = "inspectit-gepard-agent.jar" from zipTree(configurations.otel.singleFile) -// from(tasks.shadowJar.archiveFile) { -// into "extensions" -// } - dependsOn(repack) - from(tasks.repack.archiveFile) { + from(tasks.mergeModules.archiveFile) { into "extensions" } @@ -153,41 +186,21 @@ tasks.register('extendedAgent', Jar) { }.singleFile ) } -} - -def repack = tasks.register("repack", Zip) { - dependsOn(horst) - from layout.buildDirectory.dir('bootstrap/tmp').get().asFile - destinationDirectory.set(file("${buildDir}/libs")) - archiveFileName.set("inspectit-gepard-agent-extension.jar") -} -def horst = tasks.register("horst", Copy) { - dependsOn(shadowJar) - - destinationDir = layout.buildDirectory.dir('bootstrap').get().asFile - - from(zipTree(tasks.shadowJar.archiveFile.get())) { - into "tmp" + // Remove temporary directory + doLast { + delete tasks.copyModules.destinationDir } - - from(project(":inspectit-gepard-bootstrap").jar.outputs) { - into "tmp" - } - - finalizedBy(repack) -} - -shadowJar { - finalizedBy(horst) } tasks { test { - dependsOn(extendedAgent) + // we need to merge our submodules before running tests, + // so all classes are accessible for integration tests + dependsOn(mergeModules) useJUnitPlatform() - inputs.files(layout.files(tasks.repack)) + inputs.files(layout.files(tasks.mergeModules)) inputs.files(layout.files(tasks.extendedAgent)) // Necessary to use system-lambda in tests @@ -197,17 +210,17 @@ tasks { systemProperty 'io.opentelemetry.smoketest.agentPath', configurations.otel.singleFile.absolutePath systemProperty 'io.opentelemetry.smoketest.extendedAgentPath', tasks.extendedAgent.archiveFile.get().asFile.absolutePath - systemProperty 'io.opentelemetry.smoketest.extensionPath', tasks.repack.archiveFile.get().asFile.absolutePath + systemProperty 'io.opentelemetry.smoketest.extensionPath', tasks.mergeModules.archiveFile.get().asFile.absolutePath - finalizedBy jacocoTestReport + finalizedBy(jacocoTestReport) } + jacocoTestReport.dependsOn(test) + compileJava { options.release.set(17) } - assemble.dependsOn(shadowJar) - dockerPrepare.dependsOn(extendedAgent) docker { @@ -218,6 +231,4 @@ tasks { dockerfile file('docker/Dockerfile') files 'docker/entrypoint.sh', agentJar.get().asFile } - - jacocoTestReport.dependsOn(test) } diff --git a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/bootstrap/InspectitBootstrapManager.java b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/bootstrap/InspectitBootstrapManager.java index 98999b9..96fec3a 100644 --- a/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/bootstrap/InspectitBootstrapManager.java +++ b/inspectit-gepard-agent/src/main/java/rocks/inspectit/gepard/agent/bootstrap/InspectitBootstrapManager.java @@ -15,8 +15,6 @@ public class InspectitBootstrapManager { private static final Logger log = LoggerFactory.getLogger(InspectitBootstrapManager.class); - // private static final String INSPECTIT_BOOTSTRAP_JAR_PATH = - // "/extensions/inspectit-gepard-bootstrap.jar"; private static final String INSPECTIT_BOOTSTRAP_JAR_PATH = "/inspectit-gepard-bootstrap.jar"; private static final String INSPECTIT_BOOTSTRAP_JAR_TEMP_PREFIX = "gepard-bootstrap-"; @@ -32,10 +30,10 @@ public static synchronized void appendToBootstrapClassLoader() { JarFile bootstrapJar = new JarFile(bootstrapJarPath.toFile()); instrumentation.appendToBootstrapClassLoaderSearch(bootstrapJar); } catch (Exception e) { - log.error("Could not add inspectIT Gepard interfaces to bootstrap classloader", e); + log.error("Could not append inspectIT Gepard interfaces to bootstrap classloader", e); return; } - log.info("Successfully added inspectIT Gepard interfaces to bootstrap classloader"); + log.info("Successfully appended inspectIT Gepard interfaces to bootstrap classloader"); } /** @@ -47,7 +45,6 @@ public static synchronized void appendToBootstrapClassLoader() { */ private static Path copyResourceToTempJarFile(String resourcePath, String prefix) throws IOException { - // try (InputStream inputStream = OpenTelemetryAgent.class.getResourceAsStream(resourcePath)) { try (InputStream inputStream = InspectitBootstrapManager.class.getResourceAsStream(resourcePath)) { Path targetFile = Files.createTempFile(prefix, ".jar"); diff --git a/inspectit-gepard-bootstrap/README.md b/inspectit-gepard-bootstrap/README.md index 4640904..82098cd 100644 --- a/inspectit-gepard-bootstrap/README.md +++ b/inspectit-gepard-bootstrap/README.md @@ -1 +1,9 @@ -# TODO +# inspectIT Gepard - Bootstrap + +This project contains interfaces which have to be implemented by inspectIT Gepard Java agents and +are injected into the user application. + +The resulting Jar file of this package will be pushed to the JVM's bootstrap class loader. +This is necessary in order to create a bridge between the user (application) classes +and the classes which will be loaded by the OpenTelemetry extension class loader +(classes of the inspectIT Gepard Java agent).