otel.java-conventions.gradle.kts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import com.gradle.enterprise.gradleplugin.testretry.retry
  2. import io.opentelemetry.instrumentation.gradle.OtelJavaExtension
  3. import org.gradle.api.tasks.testing.logging.TestExceptionFormat
  4. import java.time.Duration
  5. plugins {
  6. `java-library`
  7. groovy
  8. checkstyle
  9. codenarc
  10. idea
  11. id("otel.errorprone-conventions")
  12. id("otel.spotless-conventions")
  13. id("org.owasp.dependencycheck")
  14. }
  15. val otelJava = extensions.create<OtelJavaExtension>("otelJava")
  16. afterEvaluate {
  17. val previousBaseArchiveName = base.archivesName.get()
  18. if (findProperty("mavenGroupId") == "io.opentelemetry.javaagent.instrumentation") {
  19. base.archivesName.set("opentelemetry-javaagent-$previousBaseArchiveName")
  20. } else if (!previousBaseArchiveName.startsWith("opentelemetry-")) {
  21. base.archivesName.set("opentelemetry-$previousBaseArchiveName")
  22. }
  23. }
  24. // Version to use to compile code and run tests.
  25. val DEFAULT_JAVA_VERSION = JavaVersion.VERSION_17
  26. java {
  27. toolchain {
  28. languageVersion.set(
  29. otelJava.minJavaVersionSupported.map { JavaLanguageVersion.of(Math.max(it.majorVersion.toInt(), DEFAULT_JAVA_VERSION.majorVersion.toInt())) }
  30. )
  31. }
  32. // See https://docs.gradle.org/current/userguide/upgrading_version_5.html, Automatic target JVM version
  33. disableAutoTargetJvm()
  34. withJavadocJar()
  35. withSourcesJar()
  36. }
  37. // this task is helpful for tracking down and aligning dependency versions across modules in order
  38. // to limit versions that are be loaded by Intellij
  39. // (the normal dependencies task selector only executes the task on a single project)
  40. //
  41. // ./gradlew intellijDeps
  42. // | grep -Po "\--- \K.*"
  43. // | sed "s/:[0-9].* -> \(.*\)/:\1/"
  44. // | sed "s/:[0-9].* -> \(.*\)/:\1/"
  45. // | sort -u
  46. tasks.register<DependencyReportTask>("intellijDeps")
  47. tasks.withType<JavaCompile>().configureEach {
  48. with(options) {
  49. release.set(otelJava.minJavaVersionSupported.map { it.majorVersion.toInt() })
  50. if (name != "jmhCompileGeneratedClasses") {
  51. compilerArgs.addAll(
  52. listOf(
  53. "-Xlint:all",
  54. // We suppress the "try" warning because it disallows managing an auto-closeable with
  55. // try-with-resources without referencing the auto-closeable within the try block.
  56. "-Xlint:-try",
  57. // We suppress the "processing" warning as suggested in
  58. // https://groups.google.com/forum/#!topic/bazel-discuss/_R3A9TJSoPM
  59. "-Xlint:-processing",
  60. // We suppress the "options" warning because it prevents compilation on modern JDKs
  61. "-Xlint:-options",
  62. // Fail build on any warning
  63. "-Werror"
  64. )
  65. )
  66. }
  67. encoding = "UTF-8"
  68. if (name.contains("Test")) {
  69. // serialVersionUID is basically guaranteed to be useless in tests
  70. compilerArgs.add("-Xlint:-serial")
  71. }
  72. }
  73. }
  74. // Groovy and Scala compilers don't actually understand --release option
  75. afterEvaluate {
  76. tasks.withType<GroovyCompile>().configureEach {
  77. var javaVersion = otelJava.minJavaVersionSupported.get().majorVersion
  78. if (javaVersion == "8") {
  79. javaVersion = "1.8"
  80. }
  81. sourceCompatibility = javaVersion
  82. targetCompatibility = javaVersion
  83. }
  84. tasks.withType<ScalaCompile>().configureEach {
  85. sourceCompatibility = otelJava.minJavaVersionSupported.get().majorVersion
  86. targetCompatibility = otelJava.minJavaVersionSupported.get().majorVersion
  87. }
  88. }
  89. evaluationDependsOn(":dependencyManagement")
  90. val dependencyManagementConf = configurations.create("dependencyManagement") {
  91. isCanBeConsumed = false
  92. isCanBeResolved = false
  93. isVisible = false
  94. }
  95. afterEvaluate {
  96. configurations.configureEach {
  97. if (isCanBeResolved && !isCanBeConsumed) {
  98. extendsFrom(dependencyManagementConf)
  99. }
  100. }
  101. }
  102. // Force 4.0, or 4.1 to the highest version of that branch. Since 4.0 and 4.1 often have
  103. // compatibility issues we can't just force to the highest version using normal BOM dependencies.
  104. abstract class NettyAlignmentRule : ComponentMetadataRule {
  105. override fun execute(ctx: ComponentMetadataContext) {
  106. with(ctx.details) {
  107. if (id.group == "io.netty" && id.name != "netty") {
  108. if (id.version.startsWith("4.1.")) {
  109. belongsTo("io.netty:netty-bom:4.1.107.Final", false)
  110. } else if (id.version.startsWith("4.0.")) {
  111. belongsTo("io.netty:netty-bom:4.0.56.Final", false)
  112. }
  113. }
  114. }
  115. }
  116. }
  117. dependencies {
  118. add(dependencyManagementConf.name, platform(project(":dependencyManagement")))
  119. components.all<NettyAlignmentRule>()
  120. compileOnly("com.google.code.findbugs:jsr305")
  121. compileOnly("com.google.errorprone:error_prone_annotations")
  122. codenarc("org.codenarc:CodeNarc:3.4.0")
  123. codenarc(platform("org.codehaus.groovy:groovy-bom:3.0.20"))
  124. modules {
  125. // checkstyle uses the very old google-collections which causes Java 9 module conflict with
  126. // guava which is also on the classpath
  127. module("com.google.collections:google-collections") {
  128. replacedBy("com.google.guava:guava", "google-collections is now part of Guava")
  129. }
  130. }
  131. }
  132. testing {
  133. suites.withType(JvmTestSuite::class).configureEach {
  134. dependencies {
  135. implementation("org.junit.jupiter:junit-jupiter-api")
  136. implementation("org.junit.jupiter:junit-jupiter-params")
  137. runtimeOnly("org.junit.jupiter:junit-jupiter-engine")
  138. runtimeOnly("org.junit.vintage:junit-vintage-engine")
  139. implementation("org.junit-pioneer:junit-pioneer")
  140. implementation("org.assertj:assertj-core")
  141. implementation("org.awaitility:awaitility")
  142. implementation("org.mockito:mockito-core")
  143. implementation("org.mockito:mockito-inline")
  144. implementation("org.mockito:mockito-junit-jupiter")
  145. implementation("org.objenesis:objenesis")
  146. implementation("org.spockframework:spock-core") {
  147. with(this as ExternalDependency) {
  148. // exclude optional dependencies
  149. exclude(group = "cglib", module = "cglib-nodep")
  150. exclude(group = "net.bytebuddy", module = "byte-buddy")
  151. exclude(group = "org.junit.platform", module = "junit-platform-testkit")
  152. exclude(group = "org.jetbrains", module = "annotations")
  153. exclude(group = "org.objenesis", module = "objenesis")
  154. exclude(group = "org.ow2.asm", module = "asm")
  155. }
  156. }
  157. implementation("org.spockframework:spock-junit4") {
  158. with(this as ExternalDependency) {
  159. // spock-core is already added as dependency
  160. // exclude it here to avoid pulling in optional dependencies
  161. exclude(group = "org.spockframework", module = "spock-core")
  162. }
  163. }
  164. implementation("ch.qos.logback:logback-classic")
  165. implementation("org.slf4j:log4j-over-slf4j")
  166. implementation("org.slf4j:jcl-over-slf4j")
  167. implementation("org.slf4j:jul-to-slf4j")
  168. implementation("com.github.stefanbirkner:system-rules")
  169. }
  170. }
  171. }
  172. var path = project.path
  173. if (path.startsWith(":instrumentation:")) {
  174. // remove segments that are a prefix of the next segment
  175. // for example :instrumentation:log4j:log4j-context-data:log4j-context-data-2.17 is transformed to log4j-context-data-2.17
  176. var tmpPath = path
  177. val suffix = tmpPath.substringAfterLast(':')
  178. var prefix = ":instrumentation:"
  179. if (suffix == "library") {
  180. // strip ":library" suffix
  181. tmpPath = tmpPath.substringBeforeLast(':')
  182. } else if (suffix == "library-autoconfigure") {
  183. // replace ":library-autoconfigure" with "-autoconfigure"
  184. tmpPath = tmpPath.substringBeforeLast(':') + "-autoconfigure"
  185. } else if (suffix == "javaagent") {
  186. // strip ":javaagent" suffix and add it to prefix
  187. prefix += "javaagent:"
  188. tmpPath = tmpPath.substringBeforeLast(':')
  189. }
  190. val segments = tmpPath.substring(":instrumentation:".length).split(':')
  191. var newPath = ""
  192. var done = false
  193. for (s in segments) {
  194. if (!done && (newPath.isEmpty() || s.startsWith(newPath))) {
  195. newPath = s
  196. } else {
  197. newPath += ":$s"
  198. done = true
  199. }
  200. }
  201. if (newPath.isNotEmpty()) {
  202. path = prefix + newPath
  203. }
  204. }
  205. var javaModuleName = "io.opentelemetry" + path.replace(".", "_").replace("-", "_").replace(":", ".")
  206. tasks {
  207. named<Jar>("jar") {
  208. // By default Gradle Jar task can put multiple files with the same name
  209. // into a Jar. This may lead to confusion. For example if auto-service
  210. // annotation processing creates files with same name in `scala` and
  211. // `java` directory this would result in Jar having two files with the
  212. // same name in it. Which in turn would result in only one of those
  213. // files being actually considered when that Jar is used leading to very
  214. // confusing failures. Instead we should 'fail early' and avoid building such Jars.
  215. duplicatesStrategy = DuplicatesStrategy.FAIL
  216. manifest {
  217. attributes(
  218. "Implementation-Title" to project.name,
  219. "Implementation-Version" to project.version,
  220. "Implementation-Vendor" to "OpenTelemetry",
  221. "Implementation-URL" to "https://github.com/open-telemetry/opentelemetry-java-instrumentation",
  222. "Automatic-Module-Name" to javaModuleName
  223. )
  224. }
  225. }
  226. named<Javadoc>("javadoc") {
  227. with(options as StandardJavadocDocletOptions) {
  228. source = "8"
  229. encoding = "UTF-8"
  230. docEncoding = "UTF-8"
  231. charSet = "UTF-8"
  232. breakIterator(true)
  233. // TODO (trask) revisit to see if url is fixed
  234. // currently broken because https://docs.oracle.com/javase/8/docs/api/element-list is missing
  235. // and redirects
  236. // links("https://docs.oracle.com/javase/8/docs/api/")
  237. addStringOption("Xdoclint:none", "-quiet")
  238. // non-standard option to fail on warnings, see https://bugs.openjdk.java.net/browse/JDK-8200363
  239. addStringOption("Xwerror", "-quiet")
  240. }
  241. }
  242. withType<AbstractArchiveTask>().configureEach {
  243. isPreserveFileTimestamps = false
  244. isReproducibleFileOrder = true
  245. dirMode = Integer.parseInt("0755", 8)
  246. fileMode = Integer.parseInt("0644", 8)
  247. }
  248. // Convenient when updating errorprone
  249. register("compileAllJava") {
  250. dependsOn(withType<JavaCompile>())
  251. }
  252. }
  253. normalization {
  254. runtimeClasspath {
  255. metaInf {
  256. ignoreAttribute("Implementation-Version")
  257. }
  258. }
  259. }
  260. fun isJavaVersionAllowed(version: JavaVersion): Boolean {
  261. if (otelJava.minJavaVersionSupported.get().compareTo(version) > 0) {
  262. return false
  263. }
  264. if (otelJava.maxJavaVersionForTests.isPresent && otelJava.maxJavaVersionForTests.get().compareTo(version) < 0) {
  265. return false
  266. }
  267. return true
  268. }
  269. abstract class TestcontainersBuildService : BuildService<BuildServiceParameters.None>
  270. // To limit number of concurrently running resource intensive tests add
  271. // tasks {
  272. // test {
  273. // usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
  274. // }
  275. // }
  276. gradle.sharedServices.registerIfAbsent("testcontainersBuildService", TestcontainersBuildService::class.java) {
  277. maxParallelUsages.convention(2)
  278. }
  279. val resourceNames = listOf("Host", "Os", "Process", "ProcessRuntime")
  280. val resourceClassesCsv = resourceNames.joinToString(",") { "io.opentelemetry.sdk.extension.resources.${it}ResourceProvider" }
  281. tasks.withType<Test>().configureEach {
  282. useJUnitPlatform()
  283. // There's no real harm in setting this for all tests even if any happen to not be using context
  284. // propagation.
  285. jvmArgs("-Dio.opentelemetry.context.enableStrictContext=${rootProject.findProperty("enableStrictContext") ?: true}")
  286. // TODO(anuraaga): Have agent map unshaded to shaded.
  287. if (project.findProperty("disableShadowRelocate") != "true") {
  288. jvmArgs("-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=${rootProject.findProperty("enableStrictContext") ?: true}")
  289. } else {
  290. jvmArgs("-Dotel.instrumentation.opentelemetry-api.enabled=false")
  291. jvmArgs("-Dotel.instrumentation.opentelemetry-instrumentation-api.enabled=false")
  292. }
  293. // Disable default resource providers since they cause lots of output we don't need.
  294. jvmArgs("-Dotel.java.disabled.resource.providers=$resourceClassesCsv")
  295. val trustStore = project(":testing-common").file("src/misc/testing-keystore.p12")
  296. // Work around payara not working when this is set for some reason.
  297. // Don't set for camel as we have tests that interact with AWS and need normal trustStore
  298. if (project.name != "jaxrs-2.0-payara-testing" && project.description != "camel-2-20") {
  299. jvmArgumentProviders.add(KeystoreArgumentsProvider(trustStore))
  300. }
  301. // All tests must complete within 15 minutes.
  302. // This value is quite big because with lower values (3 mins) we were experiencing large number of false positives
  303. timeout.set(Duration.ofMinutes(15))
  304. retry {
  305. // You can see tests that were retried by this mechanism in the collected test reports and build scans.
  306. if (System.getenv().containsKey("CI") || rootProject.hasProperty("retryTests")) {
  307. maxRetries.set(5)
  308. }
  309. }
  310. reports {
  311. junitXml.isOutputPerTestCase = true
  312. }
  313. testLogging {
  314. exceptionFormat = TestExceptionFormat.FULL
  315. showStandardStreams = true
  316. }
  317. }
  318. class KeystoreArgumentsProvider(
  319. @InputFile
  320. @PathSensitive(PathSensitivity.RELATIVE)
  321. val trustStore: File
  322. ) : CommandLineArgumentProvider {
  323. override fun asArguments(): Iterable<String> = listOf(
  324. "-Djavax.net.ssl.trustStore=${trustStore.absolutePath}",
  325. "-Djavax.net.ssl.trustStorePassword=testing"
  326. )
  327. }
  328. afterEvaluate {
  329. val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
  330. val useJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" }
  331. ?: false
  332. tasks.withType<Test>().configureEach {
  333. if (testJavaVersion != null) {
  334. javaLauncher.set(
  335. javaToolchains.launcherFor {
  336. languageVersion.set(JavaLanguageVersion.of(testJavaVersion.majorVersion))
  337. implementation.set(if (useJ9) JvmImplementation.J9 else JvmImplementation.VENDOR_SPECIFIC)
  338. }
  339. )
  340. isEnabled = isEnabled && isJavaVersionAllowed(testJavaVersion)
  341. } else {
  342. // We default to testing with Java 11 for most tests, but some tests don't support it, where we change
  343. // the default test task's version so commands like `./gradlew check` can test all projects regardless
  344. // of Java version.
  345. if (!isJavaVersionAllowed(DEFAULT_JAVA_VERSION) && otelJava.maxJavaVersionForTests.isPresent) {
  346. javaLauncher.set(
  347. javaToolchains.launcherFor {
  348. languageVersion.set(JavaLanguageVersion.of(otelJava.maxJavaVersionForTests.get().majorVersion))
  349. }
  350. )
  351. }
  352. }
  353. }
  354. }
  355. codenarc {
  356. configFile = rootProject.file("buildscripts/codenarc.groovy")
  357. }
  358. checkstyle {
  359. configFile = rootProject.file("buildscripts/checkstyle.xml")
  360. // this version should match the version of google_checks.xml used as basis for above configuration
  361. toolVersion = "10.14.0"
  362. maxWarnings = 0
  363. }
  364. dependencyCheck {
  365. skipConfigurations = listOf("errorprone", "checkstyle", "annotationProcessor")
  366. suppressionFile = "buildscripts/dependency-check-suppressions.xml"
  367. failBuildOnCVSS = 7.0f // fail on high or critical CVE
  368. nvd.apiKey = System.getenv("NVD_API_KEY")
  369. nvd.delay = 3500 // until next dependency check release (https://github.com/jeremylong/DependencyCheck/pull/6333)
  370. }
  371. idea {
  372. module {
  373. isDownloadJavadoc = false
  374. isDownloadSources = false
  375. }
  376. }
  377. when (projectDir.name) {
  378. "bootstrap", "javaagent", "library", "library-autoconfigure", "testing" -> {
  379. // We don't use this group anywhere in our config, but we need to make sure it is unique per
  380. // instrumentation so Gradle doesn't merge projects with same name due to a bug in Gradle.
  381. // https://github.com/gradle/gradle/issues/847
  382. // In otel.publish-conventions, we set the maven group, which is what matters, to the correct
  383. // value.
  384. group = "io.opentelemetry.dummy.${projectDir.parentFile.name}"
  385. }
  386. }
  387. configurations.configureEach {
  388. resolutionStrategy {
  389. // While you might think preferProjectModules would do this, it doesn't. If this gets hard to
  390. // manage, we could consider having the io.opentelemetry.instrumentation add information about
  391. // what modules they add to reference generically.
  392. dependencySubstitution {
  393. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")).using(project(":instrumentation-api"))
  394. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator")).using(project(":instrumentation-api-incubator"))
  395. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations")).using(project(":instrumentation-annotations"))
  396. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support")).using(
  397. project(":instrumentation-annotations-support")
  398. )
  399. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap")).using(project(":javaagent-bootstrap"))
  400. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")).using(project(":javaagent-extension-api"))
  401. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")).using(project(":javaagent-tooling"))
  402. substitute(module("io.opentelemetry.javaagent:opentelemetry-agent-for-testing")).using(project(":testing:agent-for-testing"))
  403. substitute(module("io.opentelemetry.javaagent:opentelemetry-testing-common")).using(project(":testing-common"))
  404. substitute(module("io.opentelemetry.javaagent:opentelemetry-muzzle")).using(project(":muzzle"))
  405. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent")).using(project(":javaagent"))
  406. }
  407. // The above substitutions ensure dependencies managed by this BOM for external projects refer to this repo's projects here.
  408. // Excluding the bom as well helps ensure if we miss a substitution, we get a resolution failure instead of using the
  409. // wrong version.
  410. exclude("io.opentelemetry.instrumentation", "opentelemetry-instrumentation-bom-alpha")
  411. }
  412. }