otel.java-conventions.gradle.kts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.65.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:2.2.0")
  123. codenarc(platform("org.codehaus.groovy:groovy-bom:3.0.18"))
  124. }
  125. testing {
  126. suites.withType(JvmTestSuite::class).configureEach {
  127. dependencies {
  128. implementation("org.junit.jupiter:junit-jupiter-api")
  129. implementation("org.junit.jupiter:junit-jupiter-params")
  130. runtimeOnly("org.junit.jupiter:junit-jupiter-engine")
  131. runtimeOnly("org.junit.vintage:junit-vintage-engine")
  132. implementation("org.junit-pioneer:junit-pioneer")
  133. implementation("org.assertj:assertj-core")
  134. implementation("org.awaitility:awaitility")
  135. implementation("org.mockito:mockito-core")
  136. implementation("org.mockito:mockito-inline")
  137. implementation("org.mockito:mockito-junit-jupiter")
  138. implementation("org.objenesis:objenesis")
  139. implementation("org.spockframework:spock-core") {
  140. with(this as ExternalDependency) {
  141. // exclude optional dependencies
  142. exclude(group = "cglib", module = "cglib-nodep")
  143. exclude(group = "net.bytebuddy", module = "byte-buddy")
  144. exclude(group = "org.junit.platform", module = "junit-platform-testkit")
  145. exclude(group = "org.jetbrains", module = "annotations")
  146. exclude(group = "org.objenesis", module = "objenesis")
  147. exclude(group = "org.ow2.asm", module = "asm")
  148. }
  149. }
  150. implementation("org.spockframework:spock-junit4") {
  151. with(this as ExternalDependency) {
  152. // spock-core is already added as dependency
  153. // exclude it here to avoid pulling in optional dependencies
  154. exclude(group = "org.spockframework", module = "spock-core")
  155. }
  156. }
  157. implementation("ch.qos.logback:logback-classic")
  158. implementation("org.slf4j:log4j-over-slf4j")
  159. implementation("org.slf4j:jcl-over-slf4j")
  160. implementation("org.slf4j:jul-to-slf4j")
  161. implementation("com.github.stefanbirkner:system-rules")
  162. }
  163. }
  164. }
  165. tasks {
  166. named<Jar>("jar") {
  167. // By default Gradle Jar task can put multiple files with the same name
  168. // into a Jar. This may lead to confusion. For example if auto-service
  169. // annotation processing creates files with same name in `scala` and
  170. // `java` directory this would result in Jar having two files with the
  171. // same name in it. Which in turn would result in only one of those
  172. // files being actually considered when that Jar is used leading to very
  173. // confusing failures. Instead we should 'fail early' and avoid building such Jars.
  174. duplicatesStrategy = DuplicatesStrategy.FAIL
  175. manifest {
  176. attributes(
  177. "Implementation-Title" to project.name,
  178. "Implementation-Version" to project.version,
  179. "Implementation-Vendor" to "OpenTelemetry",
  180. "Implementation-URL" to "https://github.com/open-telemetry/opentelemetry-java-instrumentation"
  181. )
  182. }
  183. }
  184. named<Javadoc>("javadoc") {
  185. with(options as StandardJavadocDocletOptions) {
  186. source = "8"
  187. encoding = "UTF-8"
  188. docEncoding = "UTF-8"
  189. charSet = "UTF-8"
  190. breakIterator(true)
  191. // TODO (trask) revisit to see if url is fixed
  192. // currently broken because https://docs.oracle.com/javase/8/docs/api/element-list is missing
  193. // and redirects
  194. // links("https://docs.oracle.com/javase/8/docs/api/")
  195. addStringOption("Xdoclint:none", "-quiet")
  196. // non-standard option to fail on warnings, see https://bugs.openjdk.java.net/browse/JDK-8200363
  197. addStringOption("Xwerror", "-quiet")
  198. }
  199. }
  200. withType<AbstractArchiveTask>().configureEach {
  201. isPreserveFileTimestamps = false
  202. isReproducibleFileOrder = true
  203. dirMode = Integer.parseInt("0755", 8)
  204. fileMode = Integer.parseInt("0644", 8)
  205. }
  206. // Convenient when updating errorprone
  207. register("compileAllJava") {
  208. dependsOn(withType<JavaCompile>())
  209. }
  210. }
  211. normalization {
  212. runtimeClasspath {
  213. metaInf {
  214. ignoreAttribute("Implementation-Version")
  215. }
  216. }
  217. }
  218. fun isJavaVersionAllowed(version: JavaVersion): Boolean {
  219. if (otelJava.minJavaVersionSupported.get().compareTo(version) > 0) {
  220. return false
  221. }
  222. if (otelJava.maxJavaVersionForTests.isPresent && otelJava.maxJavaVersionForTests.get().compareTo(version) < 0) {
  223. return false
  224. }
  225. return true
  226. }
  227. abstract class TestcontainersBuildService : BuildService<BuildServiceParameters.None>
  228. // To limit number of concurrently running resource intensive tests add
  229. // tasks {
  230. // test {
  231. // usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
  232. // }
  233. // }
  234. gradle.sharedServices.registerIfAbsent("testcontainersBuildService", TestcontainersBuildService::class.java) {
  235. maxParallelUsages.convention(2)
  236. }
  237. val resourceNames = listOf("Host", "Os", "Process", "ProcessRuntime")
  238. val resourceClassesCsv = resourceNames.joinToString(",") { "io.opentelemetry.sdk.extension.resources.${it}ResourceProvider" }
  239. tasks.withType<Test>().configureEach {
  240. useJUnitPlatform()
  241. // There's no real harm in setting this for all tests even if any happen to not be using context
  242. // propagation.
  243. jvmArgs("-Dio.opentelemetry.context.enableStrictContext=${rootProject.findProperty("enableStrictContext") ?: true}")
  244. // TODO(anuraaga): Have agent map unshaded to shaded.
  245. jvmArgs("-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=${rootProject.findProperty("enableStrictContext") ?: true}")
  246. // Disable default resource providers since they cause lots of output we don't need.
  247. jvmArgs("-Dotel.java.disabled.resource.providers=$resourceClassesCsv")
  248. val trustStore = project(":testing-common").file("src/misc/testing-keystore.p12")
  249. // Work around payara not working when this is set for some reason.
  250. // Don't set for camel as we have tests that interact with AWS and need normal trustStore
  251. if (project.name != "jaxrs-2.0-payara-testing" && project.description != "camel-2-20") {
  252. jvmArgumentProviders.add(KeystoreArgumentsProvider(trustStore))
  253. }
  254. // All tests must complete within 15 minutes.
  255. // This value is quite big because with lower values (3 mins) we were experiencing large number of false positives
  256. timeout.set(Duration.ofMinutes(15))
  257. retry {
  258. // You can see tests that were retried by this mechanism in the collected test reports and build scans.
  259. if (System.getenv().containsKey("CI") || rootProject.hasProperty("retryTests")) {
  260. maxRetries.set(5)
  261. }
  262. }
  263. reports {
  264. junitXml.isOutputPerTestCase = true
  265. }
  266. testLogging {
  267. exceptionFormat = TestExceptionFormat.FULL
  268. showStandardStreams = true
  269. }
  270. }
  271. class KeystoreArgumentsProvider(
  272. @InputFile
  273. @PathSensitive(PathSensitivity.RELATIVE)
  274. val trustStore: File
  275. ) : CommandLineArgumentProvider {
  276. override fun asArguments(): Iterable<String> = listOf(
  277. "-Djavax.net.ssl.trustStore=${trustStore.absolutePath}",
  278. "-Djavax.net.ssl.trustStorePassword=testing"
  279. )
  280. }
  281. afterEvaluate {
  282. val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
  283. val useJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" }
  284. ?: false
  285. tasks.withType<Test>().configureEach {
  286. if (testJavaVersion != null) {
  287. javaLauncher.set(
  288. javaToolchains.launcherFor {
  289. languageVersion.set(JavaLanguageVersion.of(testJavaVersion.majorVersion))
  290. implementation.set(if (useJ9) JvmImplementation.J9 else JvmImplementation.VENDOR_SPECIFIC)
  291. }
  292. )
  293. isEnabled = isEnabled && isJavaVersionAllowed(testJavaVersion)
  294. } else {
  295. // We default to testing with Java 11 for most tests, but some tests don't support it, where we change
  296. // the default test task's version so commands like `./gradlew check` can test all projects regardless
  297. // of Java version.
  298. if (!isJavaVersionAllowed(DEFAULT_JAVA_VERSION) && otelJava.maxJavaVersionForTests.isPresent) {
  299. javaLauncher.set(
  300. javaToolchains.launcherFor {
  301. languageVersion.set(JavaLanguageVersion.of(otelJava.maxJavaVersionForTests.get().majorVersion))
  302. }
  303. )
  304. }
  305. }
  306. }
  307. }
  308. codenarc {
  309. configFile = rootProject.file("buildscripts/codenarc.groovy")
  310. }
  311. checkstyle {
  312. configFile = rootProject.file("buildscripts/checkstyle.xml")
  313. // this version should match the version of google_checks.xml used as basis for above configuration
  314. toolVersion = "8.37"
  315. maxWarnings = 0
  316. }
  317. dependencyCheck {
  318. skipConfigurations = listOf("errorprone", "checkstyle", "annotationProcessor")
  319. suppressionFile = "buildscripts/dependency-check-suppressions.xml"
  320. failBuildOnCVSS = 7.0f // fail on high or critical CVE
  321. }
  322. idea {
  323. module {
  324. isDownloadJavadoc = false
  325. isDownloadSources = false
  326. }
  327. }
  328. when (projectDir.name) {
  329. "bootstrap", "javaagent", "library", "library-autoconfigure", "testing" -> {
  330. // We don't use this group anywhere in our config, but we need to make sure it is unique per
  331. // instrumentation so Gradle doesn't merge projects with same name due to a bug in Gradle.
  332. // https://github.com/gradle/gradle/issues/847
  333. // In otel.publish-conventions, we set the maven group, which is what matters, to the correct
  334. // value.
  335. group = "io.opentelemetry.dummy.${projectDir.parentFile.name}"
  336. }
  337. }
  338. configurations.configureEach {
  339. resolutionStrategy {
  340. // While you might think preferProjectModules would do this, it doesn't. If this gets hard to
  341. // manage, we could consider having the io.opentelemetry.instrumentation add information about
  342. // what modules they add to reference generically.
  343. dependencySubstitution {
  344. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")).using(project(":instrumentation-api"))
  345. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv")).using(project(":instrumentation-api-semconv"))
  346. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations")).using(project(":instrumentation-annotations"))
  347. substitute(module("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support")).using(
  348. project(":instrumentation-annotations-support")
  349. )
  350. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap")).using(project(":javaagent-bootstrap"))
  351. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")).using(project(":javaagent-extension-api"))
  352. substitute(module("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")).using(project(":javaagent-tooling"))
  353. substitute(module("io.opentelemetry.javaagent:opentelemetry-agent-for-testing")).using(project(":testing:agent-for-testing"))
  354. substitute(module("io.opentelemetry.javaagent:opentelemetry-testing-common")).using(project(":testing-common"))
  355. substitute(module("io.opentelemetry.javaagent:opentelemetry-muzzle")).using(project(":muzzle"))
  356. }
  357. // The above substitutions ensure dependencies managed by this BOM for external projects refer to this repo's projects here.
  358. // Excluding the bom as well helps ensure if we miss a substitution, we get a resolution failure instead of using the
  359. // wrong version.
  360. exclude("io.opentelemetry.instrumentation", "opentelemetry-instrumentation-bom-alpha")
  361. }
  362. }