-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathbuild.gradle
More file actions
350 lines (301 loc) · 12.3 KB
/
build.gradle
File metadata and controls
350 lines (301 loc) · 12.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.util.concurrent.atomic.AtomicBoolean
plugins {
id 'com.gradleup.shadow'
}
description = 'dd-java-agent'
apply from: "$rootDir/gradle/java.gradle"
apply from: "$rootDir/gradle/publish.gradle"
configurations {
shadowInclude
sharedShadowInclude
traceShadowInclude
}
// The special pre-check should be compiled with Java 6 to detect unsupported Java versions
// and prevent issues for users that still using them.
sourceSets {
"main_java6" {
java.srcDirs "${project.projectDir}/src/main/java6"
}
}
compileMain_java6Java.configure {
setJavaVersion(it, 8)
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
}
tasks.compileJava.dependsOn compileMain_java6Java
dependencies {
main_java6CompileOnly 'de.thetaphi:forbiddenapis:3.8'
testImplementation sourceSets.main_java6.output
}
/*
* Several shadow jars are created
* - The main "dd-java-agent" jar that also has the bootstrap project
* - Major feature jars (trace, instrumentation, jmxfetch, profiling, appsec, iast, debugger, ci-visibility)
* - A shared dependencies jar
* This general config is shared by all of them
*/
ext.generalShadowJarConfig = {
mergeServiceFiles()
duplicatesStrategy = DuplicatesStrategy.FAIL
// Include AgentPreCheck compiled with Java 6.
from sourceSets.main_java6.output
// Remove some cruft from the final jar.
// These patterns should NOT include **/META-INF/maven/**/pom.properties, which is
// used to report our own dependencies, but we should remove the top-level metadata
// of vendored packages because those could trigger unwanted framework checks.
exclude '/META-INF/maven/org.slf4j/**'
exclude '/META-INF/maven/org.snakeyaml/**'
exclude '**/META-INF/maven/**/pom.xml'
exclude '**/META-INF/proguard/'
exclude '**/META-INF/*.kotlin_module'
exclude '**/module-info.class'
exclude '**/liblz4-java.so'
exclude '**/liblz4-java.dylib'
exclude '**/inst/META-INF/versions/**'
exclude '**/META-INF/versions/*/org/yaml/**'
// Replaced by 'instrumenter.index', no need to include original service file
exclude '**/META-INF/services/datadog.trace.agent.tooling.InstrumenterModule'
// Prevents conflict with other SLF4J instances. Important for premain.
relocate 'org.slf4j', 'datadog.slf4j'
// Prevent conflicts with flat class-path when using GraalVM native-images
relocate 'org.jctools', 'datadog.jctools'
relocate 'net.jpountz', 'datadog.jpountz'
// rewrite dependencies calling Logger.getLogger
relocate 'java.util.logging.Logger', 'datadog.trace.bootstrap.PatchLogger'
// patch JFFI loading mechanism to maintain isolation
exclude '**/com/kenai/jffi/Init.class'
relocate('com.kenai.jffi.Init', 'com.kenai.jffi.PatchInit')
// Minimize and relocate the airlift compressor dependency for ZSTD
exclude '**/io/airlift/compress/bzip2/**'
exclude '**/io/airlift/compress/deflate/**'
exclude '**/io/airlift/compress/gzip/**'
exclude '**/io/airlift/compress/hadoop/**'
exclude '**/io/airlift/compress/lz4/**'
exclude '**/io/airlift/compress/lzo/**'
exclude '**/io/airlift/compress/snappy/**'
relocate 'io.airlift', 'datadog.io.airlift'
final String projectName = "${project.name}"
// Prevents conflict with other instances, but doesn't relocate instrumentation
if (!projectName.equals('instrumentation')) {
relocate 'org.snakeyaml.engine', 'datadog.snakeyaml.engine'
relocate 'okhttp3', 'datadog.okhttp3'
relocate 'okio', 'datadog.okio'
}
if (!project.hasProperty("disableShadowRelocate") || !disableShadowRelocate) {
// shadow OT impl to prevent casts to implementation
relocate 'datadog.trace.common', 'datadog.trace.agent.common'
relocate 'datadog.trace.core', 'datadog.trace.agent.core'
relocate 'datadog.opentracing', 'datadog.trace.agent.ot'
// shadow things in internal API that has slf4j in the API and is accessed from core
relocate 'datadog.trace.relocate', 'datadog.trace.agent.relocate'
}
}
def includeShadowJar(TaskProvider<ShadowJar> shadowJarTask, String jarname) {
def opentracingFound = new AtomicBoolean()
project.processResources {
doFirst {
eachFile {
// We seem unlikely to use this name somewhere else.
if (it.path.contains("opentracing") && it.name.contains("Format\$Builtin")) {
opentracingFound.set(true)
}
}
}
doLast {
if (opentracingFound.get()) {
throw new GradleException("OpenTracing direct dependency found!")
}
}
from(zipTree(shadowJarTask.get().archiveFile)) {
into jarname
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'
if (jarname == 'inst') {
// byte-buddy now ships classes optimized for Java8+ under META-INF/versions/9
// since we target Java8+ we can promote these classes over the pre-Java8 ones
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
eachFile {
if (it.path.contains('META-INF/versions/9/net/bytebuddy')) {
it.path = it.path.replace('META-INF/versions/9/', '')
}
}
}
}
}
project.tasks.named("processResources").configure {
dependsOn shadowJarTask
}
shadowJarTask.configure generalShadowJarConfig
}
def includeSubprojShadowJar(String projName, String jarname) {
evaluationDependsOn projName
def proj = project(projName)
includeShadowJar proj.tasks.named("shadowJar"), jarname
}
includeSubprojShadowJar ':dd-java-agent:instrumentation', 'inst'
includeSubprojShadowJar ':dd-java-agent:agent-jmxfetch', 'metrics'
includeSubprojShadowJar ':dd-java-agent:agent-profiling', 'profiling'
includeSubprojShadowJar ':dd-java-agent:appsec', 'appsec'
includeSubprojShadowJar ':dd-java-agent:agent-iast', 'iast'
includeSubprojShadowJar ':dd-java-agent:agent-debugger', 'debugger'
includeSubprojShadowJar ':dd-java-agent:agent-ci-visibility', 'ci-visibility'
includeSubprojShadowJar ':dd-java-agent:agent-logs-intake', 'logs-intake'
includeSubprojShadowJar ':dd-java-agent:cws-tls', 'cws-tls'
def sharedShadowJar = tasks.register('sharedShadowJar', ShadowJar) {
configurations = [project.configurations.sharedShadowInclude]
// Put the jar in a different directory so we don't overwrite the normal shadow jar and
// break caching, and also to not interfere with CI scripts that copy everything in the
// libs directory
it.destinationDirectory.set(file("${project.buildDir}/shared-lib"))
// Add a classifier so we don't confuse the jar file with the normal shadow jar
archiveClassifier = 'shared'
it.dependencies {
exclude(project(':dd-java-agent:agent-bootstrap'))
exclude(project(':dd-java-agent:agent-logging'))
exclude(project(':dd-trace-api'))
exclude(project(':internal-api'))
exclude(project(':components:context'))
exclude(project(':utils:time-utils'))
exclude(dependency('org.slf4j::'))
}
}
includeShadowJar(sharedShadowJar, 'shared')
// place the tracer in its own shadow jar separate to instrumentation
def traceShadowJar = tasks.register('traceShadowJar', ShadowJar) {
configurations = [project.configurations.traceShadowInclude]
it.destinationDirectory.set(file("${project.buildDir}/trace-lib"))
archiveClassifier = 'trace'
it.dependencies deps.excludeShared
}
includeShadowJar(traceShadowJar, 'trace')
shadowJar generalShadowJarConfig >> {
configurations = [project.configurations.shadowInclude]
archiveClassifier = ''
manifest {
attributes(
"Main-Class": "datadog.trace.bootstrap.AgentBootstrap",
"Agent-Class": "datadog.trace.bootstrap.AgentBootstrap",
"Premain-Class": "datadog.trace.bootstrap.AgentPreCheck",
"Can-Redefine-Classes": true,
"Can-Retransform-Classes": true,
)
}
}
tasks.register('generateAgentJarIndex', JavaExec) {
def indexName = 'dd-java-agent.index'
def contentDir = "${sourceSets.main.output.resourcesDir}"
def indexFile = "${contentDir}/${indexName}"
it.group = 'Build'
it.description = "Generate dd-java-agent.index"
it.inputs.files(fileTree(contentDir).exclude(indexName))
it.outputs.files(indexFile)
it.mainClass = 'datadog.trace.bootstrap.AgentJarIndex$IndexGenerator'
it.classpath = project.configurations.shadowInclude
it.args = [contentDir]
dependsOn 'processResources'
dependsOn 'writeVersionNumberFile'
}
compileJava.dependsOn 'generateAgentJarIndex'
subprojects { Project subProj ->
// Don't need javadoc task run for internal projects.
subProj.tasks.withType(Javadoc).configureEach { enabled = false }
}
// We don't want bundled dependencies to show up in the pom.
tasks.withType(GenerateMavenPom).configureEach { task ->
doFirst {
task.pom.withXml { XmlProvider provider ->
Node dependencies = provider.asNode().dependencies[0]
dependencies.children().clear()
}
}
}
dependencies {
implementation project(path: ':components:json')
// Depend on the bootstrap-specific shadow configuration to avoid having the same component reused by both bootstrap and agent
implementation project(path: ':components:environment', configuration: 'shadow')
modules {
module("com.squareup.okio:okio") {
replacedBy("com.datadoghq.okio:okio") // embed our patched fork
}
}
testImplementation(project(':dd-java-agent:agent-bootstrap')) {
exclude group: 'com.datadoghq', module: 'agent-logging'
}
testImplementation project(':dd-trace-api')
testImplementation project(':dd-trace-core')
testImplementation project(':utils:test-utils')
testImplementation libs.bundles.test.logging
testImplementation libs.guava
testImplementation libs.okhttp
testImplementation group: 'io.opentracing', name: 'opentracing-util', version: '0.31.0'
// Includes for the top level shadow jar
shadowInclude project(path: ':components:environment', configuration: 'shadow')
shadowInclude project(path: ':dd-java-agent:agent-bootstrap')
shadowInclude project(path: ':dd-java-agent:agent-debugger:debugger-bootstrap')
shadowInclude project(path: ':dd-java-agent:agent-otel:otel-bootstrap', configuration: 'shadow')
// Includes for the shared internal shadow jar
sharedShadowInclude deps.shared
// force a controlled version of ASM that is used by Debugger while pulled transitively by jnr
sharedShadowInclude libs.bundles.asm
sharedShadowInclude project(':communication'), {
transitive = false
// do not bring along slf4j and dependent subprojects
// (which are loaded on the bootstrap cl)
}
sharedShadowInclude project(':telemetry'), {
transitive = false
// do not bring along slf4j and dependent subprojects
// (which are loaded on the bootstrap cl)
}
sharedShadowInclude libs.bundles.cafe.crypto
sharedShadowInclude project(':remote-config:remote-config-api'), {
transitive = false
}
sharedShadowInclude project(':remote-config:remote-config-core'), {
transitive = false
}
sharedShadowInclude project(':utils:container-utils'), {
transitive = false
}
sharedShadowInclude project(':utils:socket-utils'), {
transitive = false
}
sharedShadowInclude project(':utils:version-utils'), {
transitive = false
}
sharedShadowInclude project(':dd-java-agent:agent-crashtracking'), {
transitive = false
}
traceShadowInclude project(':dd-trace-core')
}
tasks.withType(Test).configureEach {
jvmArgs "-Ddd.service.name=java-agent-tests"
jvmArgs "-Ddd.writer.type=LoggingWriter"
// Multi-threaded logging seems to be causing deadlocks with Gradle's log capture.
// jvmArgs "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=debug"
// jvmArgs "-Dorg.slf4j.simpleLogger.defaultLogLevel=debug"
doFirst {
// Defining here to allow jacoco to be first on the command line.
jvmArgs "-javaagent:${shadowJar.archiveFile.get()}"
}
testLogging {
events "started"
}
if (project.hasProperty("disableShadowRelocate") && disableShadowRelocate) {
exclude 'datadog/trace/agent/integration/classloading/ShadowPackageRenamingTest.class'
}
dependsOn "shadowJar"
}
tasks.register('checkAgentJarSize').configure {
doLast {
// Arbitrary limit to prevent unintentional increases to the agent jar size
// Raise or lower as required
assert shadowJar.archiveFile.get().getAsFile().length() <= 32 * 1024 * 1024
}
dependsOn "shadowJar"
}
tasks.named('check').configure {
dependsOn 'checkAgentJarSize'
}