build.gradle.kts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  2. import com.github.jk1.license.filter.LicenseBundleNormalizer
  3. import com.github.jk1.license.render.InventoryMarkdownReportRenderer
  4. plugins {
  5. id("com.github.jk1.dependency-license-report")
  6. id("otel.java-conventions")
  7. id("otel.publish-conventions")
  8. id("io.opentelemetry.instrumentation.javaagent-shadowing")
  9. }
  10. description = "OpenTelemetry Javaagent"
  11. group = "io.opentelemetry.javaagent"
  12. // this configuration collects libs that will be placed in the bootstrap classloader
  13. val bootstrapLibs by configurations.creating {
  14. isCanBeResolved = true
  15. isCanBeConsumed = false
  16. }
  17. // this configuration collects libs that will be placed in the agent classloader, isolated from the instrumented application code
  18. val javaagentLibs by configurations.creating {
  19. isCanBeResolved = true
  20. isCanBeConsumed = false
  21. }
  22. // this configuration collects just exporter libs (also placed in the agent classloader & isolated from the instrumented application)
  23. val exporterLibs by configurations.creating {
  24. isCanBeResolved = true
  25. isCanBeConsumed = false
  26. }
  27. // exclude dependencies that are to be placed in bootstrap from agent libs - they won't be added to inst/
  28. listOf(javaagentLibs, exporterLibs).forEach {
  29. it.run {
  30. exclude("org.slf4j")
  31. exclude("io.opentelemetry", "opentelemetry-api")
  32. exclude("io.opentelemetry", "opentelemetry-api-metrics")
  33. exclude("io.opentelemetry", "opentelemetry-semconv")
  34. }
  35. }
  36. val licenseReportDependencies by configurations.creating {
  37. extendsFrom(bootstrapLibs)
  38. }
  39. dependencies {
  40. bootstrapLibs(project(":instrumentation-api"))
  41. bootstrapLibs(project(":instrumentation-api-annotation-support"))
  42. bootstrapLibs(project(":javaagent-bootstrap"))
  43. bootstrapLibs(project(":javaagent-instrumentation-api"))
  44. bootstrapLibs("org.slf4j:slf4j-simple")
  45. javaagentLibs(project(":javaagent-extension-api"))
  46. javaagentLibs(project(":javaagent-tooling"))
  47. javaagentLibs(project(":muzzle"))
  48. exporterLibs(project(":javaagent-exporters"))
  49. // We only have compileOnly dependencies on these to make sure they don't leak into POMs.
  50. licenseReportDependencies("com.github.ben-manes.caffeine:caffeine") {
  51. isTransitive = false
  52. }
  53. licenseReportDependencies("com.blogspot.mydailyjava:weak-lock-free")
  54. // TODO ideally this would be :instrumentation instead of :javaagent-tooling
  55. // in case there are dependencies (accidentally) pulled in by instrumentation modules
  56. // but I couldn"t get that to work
  57. licenseReportDependencies(project(":javaagent-tooling"))
  58. licenseReportDependencies(project(":javaagent-extension-api"))
  59. testCompileOnly(project(":javaagent-bootstrap"))
  60. testCompileOnly(project(":javaagent-instrumentation-api"))
  61. testImplementation("com.google.guava:guava")
  62. testImplementation("io.opentracing.contrib.dropwizard:dropwizard-opentracing:0.2.2")
  63. }
  64. val javaagentDependencies = dependencies
  65. // collect all bootstrap and javaagent instrumentation dependencies
  66. project(":instrumentation").subprojects {
  67. val subProj = this
  68. plugins.withId("otel.javaagent-bootstrap") {
  69. javaagentDependencies.run {
  70. add(bootstrapLibs.name, project(subProj.path))
  71. }
  72. }
  73. plugins.withId("otel.javaagent-instrumentation") {
  74. javaagentDependencies.run {
  75. add(javaagentLibs.name, project(subProj.path))
  76. }
  77. }
  78. }
  79. tasks {
  80. processResources {
  81. from(rootProject.file("licenses")) {
  82. into("META-INF/licenses")
  83. }
  84. }
  85. val relocateJavaagentLibs by registering(ShadowJar::class) {
  86. configurations = listOf(javaagentLibs)
  87. duplicatesStrategy = DuplicatesStrategy.FAIL
  88. archiveFileName.set("javaagentLibs-relocated.jar")
  89. // exclude bootstrap projects from javaagent libs - they won't be added to inst/
  90. dependencies {
  91. exclude(project(":instrumentation-api"))
  92. exclude(project(":instrumentation-api-annotation-support"))
  93. exclude(project(":javaagent-bootstrap"))
  94. exclude(project(":javaagent-instrumentation-api"))
  95. }
  96. }
  97. val relocateExporterLibs by registering(ShadowJar::class) {
  98. configurations = listOf(exporterLibs)
  99. archiveFileName.set("exporterLibs-relocated.jar")
  100. }
  101. // Includes instrumentations, but not exporters
  102. val shadowJar by existing(ShadowJar::class) {
  103. configurations = listOf(bootstrapLibs)
  104. dependsOn(relocateJavaagentLibs)
  105. isolateClasses(relocateJavaagentLibs.get().outputs.files)
  106. archiveClassifier.set("")
  107. manifest {
  108. attributes(jar.get().manifest.attributes)
  109. attributes(
  110. "Main-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  111. "Agent-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  112. "Premain-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  113. "Can-Redefine-Classes" to true,
  114. "Can-Retransform-Classes" to true
  115. )
  116. }
  117. }
  118. // Includes everything needed for OOTB experience
  119. val fullJavaagentJar by registering(ShadowJar::class) {
  120. configurations = listOf(bootstrapLibs)
  121. dependsOn(relocateJavaagentLibs, relocateExporterLibs)
  122. isolateClasses(relocateJavaagentLibs.get().outputs.files)
  123. isolateClasses(relocateExporterLibs.get().outputs.files)
  124. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  125. archiveClassifier.set("all")
  126. manifest {
  127. attributes(shadowJar.get().manifest.attributes)
  128. }
  129. }
  130. assemble {
  131. dependsOn(shadowJar, fullJavaagentJar)
  132. }
  133. withType<Test>().configureEach {
  134. dependsOn(fullJavaagentJar)
  135. inputs.file(fullJavaagentJar.get().archiveFile)
  136. jvmArgs("-Dotel.javaagent.debug=true")
  137. doFirst {
  138. // Defining here to allow jacoco to be first on the command line.
  139. jvmArgs("-javaagent:${fullJavaagentJar.get().archiveFile.get().asFile}")
  140. }
  141. testLogging {
  142. events("started")
  143. }
  144. }
  145. val cleanLicenses by registering(Delete::class) {
  146. delete(rootProject.file("licenses"))
  147. }
  148. named("generateLicenseReport").configure {
  149. dependsOn(cleanLicenses)
  150. }
  151. publishing {
  152. publications {
  153. named<MavenPublication>("maven") {
  154. artifact(fullJavaagentJar)
  155. }
  156. }
  157. }
  158. }
  159. licenseReport {
  160. outputDir = rootProject.file("licenses").absolutePath
  161. renderers = arrayOf(InventoryMarkdownReportRenderer())
  162. configurations = arrayOf(licenseReportDependencies.name)
  163. excludeGroups = arrayOf(
  164. "io.opentelemetry.instrumentation",
  165. "io.opentelemetry.javaagent"
  166. )
  167. filters = arrayOf(LicenseBundleNormalizer("$projectDir/license-normalizer-bundle.json", true))
  168. }
  169. fun CopySpec.isolateClasses(jars: Iterable<File>) {
  170. jars.forEach {
  171. from(zipTree(it)) {
  172. // important to keep prefix "inst" short, as it is prefixed to lots of strings in runtime mem
  173. into("inst")
  174. rename("(^.*)\\.class\$", "\$1.classdata")
  175. // Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
  176. rename("""^LICENSE$""", "LICENSE.renamed")
  177. }
  178. }
  179. }