Skip to content
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
46 changes: 34 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ plugins {
group = "org.digma.instrumentation"
version = common.semanticversion.getSemanticVersion(project)

val otelJarsFolder = "otelJars"

repositories {
mavenCentral()
}
Expand Down Expand Up @@ -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)
}

}
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/org/digma/OtelApiInjector.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -24,7 +23,7 @@ public static void injectOtelApiJarToSystemClassLoader() throws IOException {
}

private static List<JarFile> createOtelJarFiles() throws IOException {
List<File> files = createFilesFromPaths();
List<File> files = createOtelJarFilesFromPaths();
List<JarFile> jarFiles = new ArrayList<>();
for (File file : files) {
JarFile jarFile = new JarFile(file, false, ZipFile.OPEN_READ);
Expand All @@ -35,11 +34,11 @@ private static List<JarFile> createOtelJarFiles() throws IOException {
}


private static List<File> createFilesFromPaths() throws IOException {
List<String> resourcePaths = getResourceFolderFiles();
private static List<File> createOtelJarFilesFromPaths() throws IOException {
List<String> resourcePaths = getOtelJarsFiles();
List<File> 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);
Expand All @@ -48,19 +47,34 @@ private static List<File> 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<String> 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<String> 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<String> 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<String> lines = bufferedReader.lines().collect(Collectors.toList());
lines.forEach(line -> {
files.add(OTEL_JARS_RESOURCE_FOLDER + "/" + line);
});
}

return files;
}

Expand Down