@@ -17,41 +17,153 @@ dependencies {
17
17
18
18
base.archivesBaseName = " hypertrace-agent"
19
19
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)
27
97
}
28
- exclude(" **/META-INF/LICENSE" )
29
- dependsOn(customizationShadowTask)
30
98
}
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 {
31
141
32
142
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" )
34
144
35
145
dependencies {
36
146
exclude(dependency(" org.codehaus.mojo:animal-sniffer-annotations" ))
37
147
exclude(dependency(" javax.annotation:javax.annotation-api" ))
38
148
}
39
149
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" )
44
154
45
155
// 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" )
47
157
48
158
// 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" )
55
167
56
168
mergeServiceFiles {
57
169
include(" inst/META-INF/services/*" )
@@ -61,15 +173,15 @@ tasks {
61
173
// Fix CVE-2024-7254, opentelemetry-javaagent brings in io.prometheus.metrics which uses deps of high vulnerability protobuf-java version
62
174
// This was fixed in 2.x.x versions of opentelemetry-javaagent(which needs us to upgrade from 1.33.0)
63
175
// 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/**" )
65
177
exclude(" **/module-info.class" )
66
178
manifest {
67
179
attributes.put(" Implementation-Title" , " javaagent" )
68
180
attributes.put(" Implementation-Version" , project.version)
69
181
attributes.put(" OpenTelemetry-Instrumentation-Version" , " ${versions[" opentelemetry_java_agent" ]} " )
70
182
attributes.put(" Implementation-Vendor" , " Hypertrace.org" )
71
183
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" )
73
185
attributes.put(" Agent-Class" , " org.hypertrace.agent.instrument.HypertraceAgent" )
74
186
attributes.put(" Premain-Class" , " org.hypertrace.agent.instrument.HypertraceAgent" )
75
187
attributes.put(" Can-Redefine-Classes" , true )
0 commit comments