-
-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
201 lines (172 loc) · 7.55 KB
/
build.gradle.kts
File metadata and controls
201 lines (172 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
`java-library`
id("io.sentry.javadoc")
alias(libs.plugins.shadow)
}
fun relocatePackages(shadowJar: ShadowJar) {
// rewrite dependencies calling Logger.getLogger
shadowJar.relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
// prevents conflict with library instrumentation, since these classes live in the bootstrap class
// loader
shadowJar.relocate(
"io.opentelemetry.instrumentation",
"io.opentelemetry.javaagent.shaded.instrumentation",
) {
// Exclude resource providers since they live in the agent class loader
exclude("io.opentelemetry.instrumentation.resources.*")
exclude("io.opentelemetry.instrumentation.spring.resources.*")
}
// relocate OpenTelemetry API usage
shadowJar.relocate(
"io.opentelemetry.api",
"io.opentelemetry.javaagent.shaded.io.opentelemetry.api",
)
shadowJar.relocate(
"io.opentelemetry.semconv",
"io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv",
)
shadowJar.relocate(
"io.opentelemetry.context",
"io.opentelemetry.javaagent.shaded.io.opentelemetry.context",
)
// relocate the OpenTelemetry extensions that are used by instrumentation modules
// these extensions live in the AgentClassLoader, and are injected into the user's class loader
// by the instrumentation modules that use them
shadowJar.relocate(
"io.opentelemetry.extension.aws",
"io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws",
)
shadowJar.relocate(
"io.opentelemetry.extension.kotlin",
"io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin",
)
}
// this configuration collects libs that will be placed in the bootstrap classloader
val bootstrapLibs =
configurations.create("bootstrapLibs") {
isCanBeResolved = true
isCanBeConsumed = false
}
// this configuration collects libs that will be placed in the agent classloader, isolated from the
// instrumented application code
val javaagentLibs =
configurations.create("javaagentLibs") {
isCanBeResolved = true
isCanBeConsumed = false
}
// this configuration stores the upstream agent dep that's extended by this project
val upstreamAgent =
configurations.create("upstreamAgent") {
isCanBeResolved = true
isCanBeConsumed = false
}
dependencies {
bootstrapLibs(projects.sentry)
bootstrapLibs(projects.sentryOpentelemetry.sentryOpentelemetryBootstrap)
javaagentLibs(projects.sentryOpentelemetry.sentryOpentelemetryAgentcustomization)
upstreamAgent(libs.otel.javaagent)
}
fun isolateClasses(jars: Iterable<File>): CopySpec = copySpec {
jars.forEach {
from(zipTree(it)) {
into("inst")
rename("^(.*)\\.class\$", "\$1.classdata")
// Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
rename("^LICENSE\$", "LICENSE.renamed")
exclude("META-INF/INDEX.LIST")
exclude("META-INF/*.DSA")
exclude("META-INF/*.SF")
}
}
}
// Don't publish non-shadowed jar (shadowJar is in shadowRuntimeElements)
with(components["java"] as AdhocComponentWithVariants) {
configurations.forEach {
withVariantsFromConfiguration(configurations["apiElements"]) { skip() }
withVariantsFromConfiguration(configurations["runtimeElements"]) { skip() }
}
}
tasks {
jar { archiveClassifier.set("dontuse") }
// building the final javaagent jar is done in 3 steps:
// 1. all distro specific javaagent libs are relocated
register("relocateJavaagentLibs", ShadowJar::class.java) {
configurations = listOf(javaagentLibs)
duplicatesStrategy = DuplicatesStrategy.FAIL
archiveFileName.set("javaagentLibs-relocated.jar")
mergeServiceFiles()
exclude("**/module-info.class")
relocatePackages(this)
// exclude known bootstrap dependencies - they can't appear in the inst/ directory
dependencies {
exclude("org.slf4j:slf4j-api")
exclude("io.opentelemetry:opentelemetry-api")
exclude("io.opentelemetry:opentelemetry-api-logs")
exclude("io.opentelemetry:opentelemetry-context")
exclude("io.opentelemetry:opentelemetry-semconv")
}
}
// 2. the distro javaagent libs are then isolated - moved to the inst/ directory
// having a separate task for isolating javaagent libs is required to avoid duplicates with the
// upstream javaagent
// duplicatesStrategy in shadowJar won't be applied when adding files with with(CopySpec) because
// each CopySpec has
// its own duplicatesStrategy
register("isolateJavaagentLibs", Copy::class.java) {
findByName("relocateJavaagentLibs")?.let { task -> dependsOn(task) }
with(isolateClasses(findByName("relocateJavaagentLibs")!!.outputs.files))
into(project.layout.buildDirectory.file("isolated/javaagentLibs").get().asFile)
}
// 3. the relocated and isolated javaagent libs are merged together with the bootstrap libs (which
// undergo relocation
// in this task) and the upstream javaagent jar; duplicates are removed
named("shadowJar", ShadowJar::class) {
configurations = listOf(bootstrapLibs) + listOf(upstreamAgent)
findByName("isolateJavaagentLibs")?.let { task -> dependsOn(task) }
from(findByName("isolateJavaagentLibs")!!.outputs)
archiveClassifier.set("")
duplicatesStrategy = DuplicatesStrategy.FAIL
filesMatching("META-INF/services/**") { duplicatesStrategy = DuplicatesStrategy.INCLUDE }
filesMatching("inst/META-INF/services/**") { duplicatesStrategy = DuplicatesStrategy.INCLUDE }
// Shadow 9.x only applies relocations to service files handled by a ServiceFileTransformer.
// We need two mergeServiceFiles calls:
// 1. Default path (META-INF/services) — ensures bootstrap service files get relocated
// (e.g., ContextStorageProvider → shaded path). Without this, Shadow 9.x skips
// relocation for service file names/contents not claimed by a transformer.
// 2. inst/ path — merges isolated agent service files from both the upstream agent
// and the distro libs. Uses `path` instead of `include` filter because Shadow 9.x's
// include() strips the `inst/` prefix on output.
mergeServiceFiles()
mergeServiceFiles { path = "inst/META-INF/services" }
exclude("**/module-info.class")
relocatePackages(this)
manifest {
attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
attributes.put("Agent-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
attributes.put("Premain-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
attributes.put("Can-Redefine-Classes", "true")
attributes.put("Can-Retransform-Classes", "true")
attributes.put("Implementation-Vendor", "Sentry")
attributes.put("Implementation-Title", project.name)
attributes.put("Implementation-Version", project.version)
attributes.put(
"Sentry-Version-Name",
"sentry-${project.version}-otel-${libs.versions.otelInstrumentation.get()}",
)
attributes.put("Sentry-SDK-Name", Config.Sentry.SENTRY_OPENTELEMETRY_AGENT_SDK_NAME)
attributes.put("Sentry-SDK-Package-Name", "maven:io.sentry:sentry-opentelemetry-agent")
attributes.put(
"Sentry-Opentelemetry-SDK-Name",
Config.Sentry.SENTRY_OPENTELEMETRY_AGENT_SDK_NAME,
)
attributes.put("Sentry-Opentelemetry-Version-Name", libs.versions.otel.get())
attributes.put(
"Sentry-Opentelemetry-Javaagent-Version-Name",
libs.versions.otelInstrumentation.get(),
)
}
}
assemble { dependsOn(shadowJar) }
}
tasks.named("distZip").configure { this.dependsOn("jar") }