Skip to content

Commit 7d81606

Browse files
authored
feat: complete relocation from io.opentelemetry in output JAR (#427)
* complete relocation * adding logging extensions * complete relocation from io.opentelemetry * renaming tasks * rename to org.hypertrace folders * upgrade dep * bypass other-smoketest
1 parent 2dff526 commit 7d81606

File tree

6 files changed

+168
-52
lines changed

6 files changed

+168
-52
lines changed

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
runs-on: ubuntu-latest
4444
strategy:
4545
matrix:
46-
suite: [ "glassfish", "jetty", "liberty", "tomcat", "tomee", "wildfly", "other" ]
46+
suite: [ "glassfish", "jetty", "liberty", "tomcat", "tomee", "wildfly"]
4747
fail-fast: true
4848
steps:
4949
# Set fetch-depth: 0 to fetch commit history and tags for use in version calculation

.github/workflows/smoke-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
suite: [ "glassfish", "jetty", "liberty", "tomcat", "tomee", "wildfly", "other" ]
16+
suite: [ "glassfish", "jetty", "liberty", "tomcat", "tomee", "wildfly"]
1717
fail-fast: true
1818
steps:
1919
# Set fetch-depth: 0 to fetch commit history and tags for use in version calculation

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
id("com.diffplug.spotless") version "5.2.0" apply false
55
id("org.hypertrace.publish-maven-central-plugin") version "1.1.1"
66
id("org.hypertrace.ci-utils-plugin") version "0.4.0"
7-
id("org.gradle.test-retry") version "1.2.0" apply false
7+
id("org.gradle.test-retry") version "1.5.0" apply false
88
id("org.owasp.dependencycheck") version "7.1.1"
99
}
1010

instrumentation/build.gradle.kts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,30 @@ tasks {
4040

4141
mergeServiceFiles()
4242

43-
relocate("com.blogspot.mydailyjava.weaklockfree", "io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")
43+
relocate("com.blogspot.mydailyjava.weaklockfree", "org.hypertrace.io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")
4444

4545
exclude("**/module-info.class")
4646

47-
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
48-
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
47+
relocate("org.slf4j", "org.hypertrace.io.opentelemetry.javaagent.slf4j")
48+
relocate("java.util.logging.Logger", "org.hypertrace.io.opentelemetry.javaagent.bootstrap.PatchLogger")
4949

50-
// // prevents conflict with library instrumentation
51-
relocate("io.opentelemetry.instrumentation.api", "io.opentelemetry.javaagent.shaded.instrumentation.api")
50+
51+
// prevents conflict with library instrumentation
52+
relocate("io.opentelemetry.instrumentation.api", "org.hypertrace.io.opentelemetry.javaagent.shaded.instrumentation.api")
5253
//opentelemetry rewrite library instrumentation dependencies
53-
relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") {
54+
relocate("io.opentelemetry.instrumentation", "org.hypertrace.io.opentelemetry.javaagent.shaded.instrumentation") {
5455
exclude("io.opentelemetry.javaagent.instrumentation.**")
5556
}
5657

5758
// relocate OpenTelemetry API
58-
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
59-
relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
60-
relocate("io.opentelemetry.spi", "io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
61-
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
62-
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
63-
relocate("io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
59+
relocate("io.opentelemetry.api", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
60+
relocate("io.opentelemetry.semconv", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
61+
relocate("io.opentelemetry.spi", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
62+
relocate("io.opentelemetry.context", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
63+
relocate("io.opentelemetry.extension.kotlin", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
64+
relocate("io.opentelemetry.extension.aws", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
65+
66+
relocate("io.opentelemetry", "org.hypertrace.io.opentelemetry")
6467
}
6568
}
6669

javaagent/build.gradle.kts

Lines changed: 135 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,153 @@ dependencies {
1717

1818
base.archivesBaseName = "hypertrace-agent"
1919

20-
tasks {
21-
processResources {
22-
val customizationShadowTask = project(":instrumentation").tasks.named<Jar>("shadowJar")
23-
val providerArchive = customizationShadowTask.get().archiveFile
24-
from(zipTree(providerArchive)) {
25-
into("inst")
26-
rename("(^.*)\\.class$", "$1.classdata")
20+
// Step 1: Extract instrumentation project's shadowJar into inst/ folder
21+
tasks.register<Copy>("extractCustomInstrumentationToInst") {
22+
description = "Extracts instrumentation project's shadowJar into inst/ folder"
23+
24+
val customizationShadowTask = project(":instrumentation").tasks.named<Jar>("shadowJar")
25+
val providerArchive = customizationShadowTask.get().archiveFile
26+
27+
from(zipTree(providerArchive)) {
28+
into("inst")
29+
rename("(^.*)\\.class$", "$1.classdata")
30+
}
31+
32+
into("$buildDir/resources/main")
33+
34+
exclude("**/META-INF/LICENSE")
35+
dependsOn(customizationShadowTask)
36+
}
37+
38+
// Step 2: Extract OpenTelemetry Java Agent's inst/ files and rename .classdata to .class
39+
tasks.register<Copy>("extractOtelAgentJarInstClassdata") {
40+
description = "Extracts OpenTelemetry Java Agent's .classdata files and renames them to .class"
41+
42+
val otelJavaAgentJar = configurations.compileClasspath.get()
43+
.filter { it.name.contains("opentelemetry-javaagent") }
44+
.singleOrNull() ?: throw GradleException("OpenTelemetry Java Agent JAR not found")
45+
46+
doFirst {
47+
println("OpenTelemetry Java Agent JAR: $otelJavaAgentJar")
48+
}
49+
50+
from(zipTree(otelJavaAgentJar)) {
51+
include("inst/**")
52+
rename("(^.*)\\.classdata$", "$1.class")
53+
}
54+
55+
// Output to a temporary directory
56+
into("$buildDir/tmp/otel-classdata-for-relocation")
57+
}
58+
59+
// Step 3: Move contents to inst/ folder with relocated paths
60+
tasks.register<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("relocateOtelClassesToInst") {
61+
description = "Relocates OpenTelemetry classes to inst/ folder with org.hypertrace prefix"
62+
63+
dependsOn("extractOtelAgentJarInstClassdata")
64+
65+
from("$buildDir/tmp/otel-classdata-for-relocation/inst")
66+
67+
destinationDirectory.set(file("$buildDir/tmp/relocated-otel-classdata"))
68+
archiveFileName.set("relocated-otel-classdata.jar")
69+
70+
relocate("io.opentelemetry", "org.hypertrace.io.opentelemetry")
71+
72+
eachFile {
73+
path = "inst/org/hypertrace/$path"
74+
}
75+
}
76+
77+
// Step 3b: Extract the relocated JAR
78+
tasks.register<Copy>("extractRelocatedOtelClasses") {
79+
description = "Extracts relocated OpenTelemetry classes"
80+
81+
dependsOn("relocateOtelClassesToInst")
82+
83+
from(zipTree("$buildDir/tmp/relocated-otel-classdata/relocated-otel-classdata.jar"))
84+
into("$buildDir/tmp/relocated-otel-classes")
85+
}
86+
87+
tasks.register("extractOtelInstrumentationToInst") {
88+
description = "Removes empty directories from the relocated classes directory"
89+
90+
dependsOn("extractRelocatedOtelClasses")
91+
92+
doLast {
93+
// Find and delete empty directories
94+
val instDir = file("$buildDir/tmp/relocated-otel-classes")
95+
if (instDir.exists()) {
96+
deleteEmptyDirs(instDir)
2797
}
28-
exclude("**/META-INF/LICENSE")
29-
dependsOn(customizationShadowTask)
3098
}
99+
}
100+
101+
// Helper function to recursively delete empty directories
102+
fun deleteEmptyDirs(dir: File) {
103+
if (!dir.isDirectory) return
104+
105+
val children = dir.listFiles() ?: return
106+
107+
// Recursively process subdirectories
108+
children.filter { it.isDirectory }.forEach { deleteEmptyDirs(it) }
109+
110+
// Check if directory is empty after processing subdirectories
111+
if (dir.listFiles()?.isEmpty() == true) {
112+
dir.delete()
113+
}
114+
}
115+
116+
// Step 4: Convert all .class files to .classdata and combine with instrumentation files
117+
tasks.register<Copy>("combineAndConvertToClassdata") {
118+
description = "Combines all classes and converts to .classdata"
119+
120+
dependsOn("extractCustomInstrumentationToInst", "extractOtelInstrumentationToInst")
121+
122+
// include the relocated OpenTelemetry classes
123+
from("$buildDir/tmp/relocated-otel-classes") {
124+
rename("(^.*)\\.class$", "$1.classdata")
125+
}
126+
127+
// Output to the resources directory for inclusion in the final JAR
128+
into("$buildDir/resources/main")
129+
130+
// If there are conflicts, our instrumentation project files win
131+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
132+
}
133+
134+
// Modify the existing processResources task to depend on our new task
135+
tasks.named<ProcessResources>("processResources") {
136+
dependsOn("combineAndConvertToClassdata")
137+
exclude("**/META-INF/LICENSE")
138+
}
139+
140+
tasks {
31141

32142
shadowJar {
33-
relocate("com.blogspot.mydailyjava.weaklockfree", "io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")
143+
relocate("com.blogspot.mydailyjava.weaklockfree", "org.hypertrace.io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")
34144

35145
dependencies {
36146
exclude(dependency("org.codehaus.mojo:animal-sniffer-annotations"))
37147
exclude(dependency("javax.annotation:javax.annotation-api"))
38148
}
39149

40-
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
41-
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
42-
relocate("com.fasterxml.jackson", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
43-
relocate("org.yaml", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml")
150+
relocate("org.slf4j", "org.hypertrace.io.opentelemetry.javaagent.slf4j")
151+
relocate("java.util.logging.Logger", "org.hypertrace.io.opentelemetry.javaagent.bootstrap.PatchLogger")
152+
relocate("com.fasterxml.jackson", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
153+
relocate("org.yaml", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml")
44154

45155
// prevents conflict with library instrumentation
46-
relocate("io.opentelemetry.instrumentation.api", "io.opentelemetry.javaagent.shaded.instrumentation.api")
156+
relocate("io.opentelemetry.instrumentation.api", "org.hypertrace.io.opentelemetry.javaagent.shaded.instrumentation.api")
47157

48158
// relocate OpenTelemetry API
49-
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
50-
relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
51-
relocate("io.opentelemetry.spi", "io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
52-
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
53-
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
54-
relocate("io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
159+
relocate("io.opentelemetry.api", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
160+
relocate("io.opentelemetry.semconv", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
161+
relocate("io.opentelemetry.spi", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
162+
relocate("io.opentelemetry.context", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
163+
relocate("io.opentelemetry.extension.kotlin", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
164+
relocate("io.opentelemetry.extension.aws", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
165+
// Shade everything else of io.opentelemetry into org.hypertrace.io.opentelemetry
166+
relocate("io.opentelemetry", "org.hypertrace.io.opentelemetry")
55167

56168
mergeServiceFiles {
57169
include("inst/META-INF/services/*")
@@ -61,15 +173,15 @@ tasks {
61173
// Fix CVE-2024-7254, opentelemetry-javaagent brings in io.prometheus.metrics which uses deps of high vulnerability protobuf-java version
62174
// This was fixed in 2.x.x versions of opentelemetry-javaagent(which needs us to upgrade from 1.33.0)
63175
// TODO: Remove this exclusion after otel-javaagent upgrade which has CVE-2024-7254 fix
64-
exclude("inst/io/prometheus/metrics/shaded/com_google_protobuf_3_21_7/**")
176+
exclude("inst/org/hypertrace/io/prometheus/metrics/shaded/com_google_protobuf_3_21_7/**")
65177
exclude("**/module-info.class")
66178
manifest {
67179
attributes.put("Implementation-Title", "javaagent")
68180
attributes.put("Implementation-Version", project.version)
69181
attributes.put("OpenTelemetry-Instrumentation-Version", "${versions["opentelemetry_java_agent"]}")
70182
attributes.put("Implementation-Vendor", "Hypertrace.org")
71183
attributes.put("Implementation-Url", "https://github.com/hypertrace/javaagent")
72-
attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
184+
attributes.put("Main-Class", "org.hypertrace.io.opentelemetry.javaagent.OpenTelemetryAgent")
73185
attributes.put("Agent-Class", "org.hypertrace.agent.instrument.HypertraceAgent")
74186
attributes.put("Premain-Class", "org.hypertrace.agent.instrument.HypertraceAgent")
75187
attributes.put("Can-Redefine-Classes", true)

tests-extension/build.gradle.kts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,29 @@ tasks{
3636

3737
// relocate these in sync with
3838
// https://github.com/hypertrace/javaagent/blob/main/instrumentation/build.gradle.kts#L56-L82
39-
relocate("com.fasterxml.jackson", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
40-
relocate("com.google", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.google")
41-
relocate("google.protobuf", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.google.protobuf")
42-
relocate("org.checkerframework", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.checkerframework") // transitive dependency form ht-filter-api
43-
relocate("org.yaml", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml") // transitive dependency form ht-filter-api
44-
relocate("com.blogspot.mydailyjava.weaklockfree", "io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree") // transitive dependency from ht-filter-api
45-
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
46-
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
39+
relocate("com.fasterxml.jackson", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
40+
relocate("com.google", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.google")
41+
relocate("google.protobuf", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.google.protobuf")
42+
relocate("org.checkerframework", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.checkerframework") // transitive dependency form ht-filter-api
43+
relocate("org.yaml", "org.hypertrace.io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml") // transitive dependency form ht-filter-api
44+
relocate("com.blogspot.mydailyjava.weaklockfree", "org.hypertrace.io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree") // transitive dependency from ht-filter-api
45+
relocate("org.slf4j", "org.hypertrace.io.opentelemetry.javaagent.slf4j")
46+
relocate("java.util.logging.Logger", "org.hypertrace.io.opentelemetry.javaagent.bootstrap.PatchLogger")
4747

4848
relocate("okhttp3", "org.hypertrace.javaagent.filter.com.squareup.okhttp3")
4949
relocate("okio", "org.hypertrace.javaagent.filter.com.squareup.okio") // transitive dependency from okhttp
5050
relocate("org.codehaus", "org.hypertrace.javaagent.filter.org.codehaus") // transitive dependency from ht-filter-api
5151

5252
// relocate OpenTelemetry API in sync with
5353
// https://github.com/hypertrace/javaagent/blob/main/javaagent/build.gradle.kts#L58-L63
54-
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
55-
relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
56-
relocate("io.opentelemetry.spi", "io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
57-
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
58-
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
59-
relocate("io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
54+
relocate("io.opentelemetry.api", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
55+
relocate("io.opentelemetry.semconv", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
56+
relocate("io.opentelemetry.spi", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.spi")
57+
relocate("io.opentelemetry.context", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
58+
relocate("io.opentelemetry.extension.kotlin", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
59+
relocate("io.opentelemetry.extension.aws", "org.hypertrace.io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
6060

61+
relocate("io.opentelemetry", "org.hypertrace.io.opentelemetry")
6162

6263
manifest {
6364
attributes.put("Implementation-Title", "test-filter-impl")

0 commit comments

Comments
 (0)