build.gradle.kts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  2. plugins {
  3. id("io.opentelemetry.instrumentation.javaagent-shadowing")
  4. id("otel.java-conventions")
  5. id("otel.publish-conventions")
  6. }
  7. description = "OpenTelemetry Javaagent for testing"
  8. group = "io.opentelemetry.javaagent"
  9. // this configuration collects libs that will be placed in the bootstrap classloader
  10. val bootstrapLibs by configurations.creating {
  11. isCanBeResolved = true
  12. isCanBeConsumed = false
  13. }
  14. // this configuration collects libs that will be placed in the agent classloader, isolated from the instrumented application code
  15. val javaagentLibs by configurations.creating {
  16. isCanBeResolved = true
  17. isCanBeConsumed = false
  18. // exclude dependencies that are to be placed in bootstrap - they won't be added to inst/
  19. exclude("org.slf4j")
  20. exclude("io.opentelemetry", "opentelemetry-api")
  21. exclude("io.opentelemetry", "opentelemetry-api-metrics")
  22. exclude("io.opentelemetry", "opentelemetry-semconv")
  23. }
  24. dependencies {
  25. bootstrapLibs(project(":instrumentation-api"))
  26. bootstrapLibs(project(":instrumentation-api-annotation-support"))
  27. bootstrapLibs(project(":javaagent-bootstrap"))
  28. bootstrapLibs(project(":javaagent-instrumentation-api"))
  29. bootstrapLibs("org.slf4j:slf4j-simple")
  30. javaagentLibs(project(":testing:agent-exporter"))
  31. javaagentLibs(project(":javaagent-extension-api"))
  32. javaagentLibs(project(":javaagent-tooling"))
  33. javaagentLibs(project(":muzzle"))
  34. // Include instrumentations instrumenting core JDK classes tp ensure interoperability with other instrumentation
  35. javaagentLibs(project(":instrumentation:executors:javaagent"))
  36. // FIXME: we should enable this, but currently this fails tests for google http client
  37. //testImplementation project(":instrumentation:http-url-connection:javaagent")
  38. javaagentLibs(project(":instrumentation:internal:internal-class-loader:javaagent"))
  39. javaagentLibs(project(":instrumentation:internal:internal-eclipse-osgi-3.6:javaagent"))
  40. javaagentLibs(project(":instrumentation:internal:internal-proxy:javaagent"))
  41. javaagentLibs(project(":instrumentation:internal:internal-url-class-loader:javaagent"))
  42. // Many tests use OpenTelemetry API calls, e.g. via InstrumentationTestRunner.runWithSpan
  43. javaagentLibs(project(":instrumentation:opentelemetry-annotations-1.0:javaagent"))
  44. javaagentLibs(project(":instrumentation:opentelemetry-api-1.0:javaagent"))
  45. testImplementation(project(":testing-common"))
  46. testImplementation("io.opentelemetry:opentelemetry-api")
  47. }
  48. val javaagentDependencies = dependencies
  49. // collect all bootstrap instrumentation dependencies
  50. project(":instrumentation").subprojects {
  51. val subProj = this
  52. plugins.withId("otel.javaagent-bootstrap") {
  53. javaagentDependencies.run {
  54. add(bootstrapLibs.name, project(subProj.path))
  55. }
  56. }
  57. }
  58. tasks {
  59. jar {
  60. enabled = false
  61. }
  62. val relocateJavaagentLibs by registering(ShadowJar::class) {
  63. configurations = listOf(javaagentLibs)
  64. archiveFileName.set("javaagentLibs-relocated.jar")
  65. // exclude bootstrap projects from javaagent libs - they won't be added to inst/
  66. dependencies {
  67. exclude(project(":instrumentation-api"))
  68. exclude(project(":instrumentation-api-annotation-support"))
  69. exclude(project(":javaagent-bootstrap"))
  70. exclude(project(":javaagent-instrumentation-api"))
  71. }
  72. }
  73. val shadowJar by existing(ShadowJar::class) {
  74. configurations = listOf(bootstrapLibs)
  75. dependsOn(relocateJavaagentLibs)
  76. isolateClasses(relocateJavaagentLibs.get().outputs.files)
  77. archiveClassifier.set("")
  78. manifest {
  79. attributes(
  80. "Main-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  81. "Agent-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  82. "Premain-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  83. "Can-Redefine-Classes" to true,
  84. "Can-Retransform-Classes" to true
  85. )
  86. }
  87. }
  88. afterEvaluate {
  89. withType<Test>().configureEach {
  90. dependsOn(shadowJar)
  91. inputs.file(shadowJar.get().archiveFile)
  92. jvmArgs("-Dotel.javaagent.debug=true")
  93. jvmArgs("-javaagent:${shadowJar.get().archiveFile.get().asFile.absolutePath}")
  94. }
  95. }
  96. // Because shadow does not use default configurations
  97. publishing {
  98. publications {
  99. named<MavenPublication>("maven") {
  100. project.shadow.component(this)
  101. }
  102. }
  103. }
  104. }
  105. fun CopySpec.isolateClasses(jars: Iterable<File>) {
  106. jars.forEach {
  107. from(zipTree(it)) {
  108. // important to keep prefix "inst" short, as it is prefixed to lots of strings in runtime mem
  109. into("inst")
  110. rename("(^.*)\\.class\$", "\$1.classdata")
  111. // Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
  112. rename("""^LICENSE$""", "LICENSE.renamed")
  113. }
  114. }
  115. }