diff --git a/build.gradle.kts b/build.gradle.kts index 907940e..a70c5d6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,6 +12,8 @@ plugins { group = "org.digma.instrumentation" version = common.semanticversion.getSemanticVersion(project) +val otelJarsFolder = "otelJars" + repositories { mavenCentral() } @@ -118,21 +120,41 @@ tasks { finalizedBy("shadowJar") } - val packageOtelJar by registering(Copy::class) { - val src = otelApiJar.files - val dest = File(project.sourceSets.main.get().output.resourcesDir, "otelJars") - from(src) - into(dest) - //bug in shadowJar that it ignores .jar files in the resource folder, the suggestion in the issue was to rename it - // to something other than jar. the agent code will rename to .jar when it uses it. - //see https://github.com/johnrengelman/shadow/issues/276 - rename { - it.replace(".jar",".myjar") + val packageOtelJar by registering { + + val output = layout.buildDirectory.dir(otelJarsFolder) + val outputDirectory = output.get().dir(otelJarsFolder) + + inputs.files(otelApiJar) + outputs.dir(output) + + doLast { + outputDirectory.asFile.mkdirs() + copy { + from(inputs.files) + into(outputDirectory) + //bug in shadowJar that it ignores .jar files in the resource folder, the suggestion in the issue was to rename it + // to something other than jar. the agent code will rename to .jar when it uses it. + //see https://github.com/johnrengelman/shadow/issues/276 + rename { + it.replace(".jar", ".myjar") + } + } + } + + //generate a file with the list of files to use at runtime. + //the list of files is used by org.digma.OtelApiInjector.getOtelJarsFiles. + //we use this way because it is tricky in java to list files in a jar resource folder and may not always work. + doLast{ + val outputFile = outputDirectory.file("otel-jars-list.txt") + outputFile.asFile.bufferedWriter().use { writer -> + for (file in inputs.files) writer.appendLine(file.name.replace(".jar",".myjar")) + } } } - processResources{ - dependsOn(packageOtelJar) + sourceSets.main{ + resources.srcDir(packageOtelJar) } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9233180..8c2f066 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,6 @@ opentelemetryJavaagentAlpha = "2.10.0-alpha" # plugins -## when upgrading otel version change also the file names in class OtelApiInjector [libraries] otelExtensionApi = { group = "io.opentelemetry.javaagent", name = "opentelemetry-javaagent-extension-api", version.ref = "opentelemetryJavaagentAlpha" } otelInstrumentationAnnotations = { group = "io.opentelemetry.instrumentation", name = "opentelemetry-instrumentation-annotations", version.ref = "opentelemetryJavaagent" } diff --git a/src/main/java/org/digma/OtelApiInjector.java b/src/main/java/org/digma/OtelApiInjector.java index 70e53c1..6dbf824 100644 --- a/src/main/java/org/digma/OtelApiInjector.java +++ b/src/main/java/org/digma/OtelApiInjector.java @@ -1,14 +1,13 @@ package org.digma; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.jar.JarFile; +import java.util.stream.Collectors; import java.util.zip.ZipFile; public class OtelApiInjector { @@ -24,7 +23,7 @@ public static void injectOtelApiJarToSystemClassLoader() throws IOException { } private static List createOtelJarFiles() throws IOException { - List files = createFilesFromPaths(); + List files = createOtelJarFilesFromPaths(); List jarFiles = new ArrayList<>(); for (File file : files) { JarFile jarFile = new JarFile(file, false, ZipFile.OPEN_READ); @@ -35,11 +34,11 @@ private static List createOtelJarFiles() throws IOException { } - private static List createFilesFromPaths() throws IOException { - List resourcePaths = getResourceFolderFiles(); + private static List createOtelJarFilesFromPaths() throws IOException { + List resourcePaths = getOtelJarsFiles(); List files = new ArrayList<>(); for (String path : resourcePaths) { - Log.debug("creating file from resource path:" + path); + Log.debug("creating file from resource path " + path); InputStream resourceStream = OtelApiInjector.class.getResourceAsStream(path); if (resourceStream == null) { throw new FileNotFoundException("could not find resource " + path); @@ -48,19 +47,34 @@ private static List createFilesFromPaths() throws IOException { out.deleteOnExit(); Files.copy(resourceStream, out.toPath(), StandardCopyOption.REPLACE_EXISTING); files.add(out); + Log.debug("file from resource path " + path + " created " + out); } return files; } - private static List getResourceFolderFiles() { - //todo: improve , find the way to list files in resource folder inside a jar. - // if we change the versions in gradle this must be changed too + private static List getOtelJarsFiles() throws IOException { + + //the file otel-jars-list.txt is generated at build time and contains the list of file that are + // packaged as the otel jars. + // it is done this way because it s tricky to just list files in a resource folder in a jar and may not always work. + + String otelJarsList = OTEL_JARS_RESOURCE_FOLDER + "/otel-jars-list.txt"; List files = new ArrayList<>(); - files.add(OTEL_JARS_RESOURCE_FOLDER + "/opentelemetry-api-1.44.1.myjar"); - files.add(OTEL_JARS_RESOURCE_FOLDER + "/opentelemetry-context-1.44.1.myjar"); - files.add(OTEL_JARS_RESOURCE_FOLDER + "/opentelemetry-instrumentation-annotations-2.10.0.myjar"); + + try (InputStream resourceStream = OtelApiInjector.class.getResourceAsStream(otelJarsList)) { + if (resourceStream == null) { + throw new FileNotFoundException("could not find otel-jars-list.txt"); + } + Reader reader = new InputStreamReader(resourceStream, StandardCharsets.UTF_8); + BufferedReader bufferedReader = new BufferedReader(reader); + List lines = bufferedReader.lines().collect(Collectors.toList()); + lines.forEach(line -> { + files.add(OTEL_JARS_RESOURCE_FOLDER + "/" + line); + }); + } + return files; }