Skip to content

Commit

Permalink
refactor: inspectit-gepard-agent/build.gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 24, 2024
1 parent 4e3db6a commit 6de21c9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 46 deletions.
9 changes: 8 additions & 1 deletion inspectit-gepard-agent/README.md
Original file line number Diff line number Diff line change
@@ -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).
89 changes: 50 additions & 39 deletions inspectit-gepard-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -218,6 +231,4 @@ tasks {
dockerfile file('docker/Dockerfile')
files 'docker/entrypoint.sh', agentJar.get().asFile
}

jacocoTestReport.dependsOn(test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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-";
Expand All @@ -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");
}

/**
Expand All @@ -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");
Expand Down
10 changes: 9 additions & 1 deletion inspectit-gepard-bootstrap/README.md
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit 6de21c9

Please sign in to comment.