io.opentelemetry.instrumentation.javaagent-testing.gradle.kts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. plugins {
  2. `java-library`
  3. id("io.opentelemetry.instrumentation.base")
  4. id("io.opentelemetry.instrumentation.muzzle-generation")
  5. id("io.opentelemetry.instrumentation.javaagent-shadowing")
  6. }
  7. dependencies {
  8. /*
  9. Dependencies added to this configuration will be found by the muzzle gradle plugin during code
  10. generation phase. These classes become part of the code that plugin inspects and traverses during
  11. references collection phase.
  12. */
  13. add("codegen", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")
  14. }
  15. dependencies {
  16. // Integration tests may need to define custom instrumentation modules so we include the standard
  17. // instrumentation infrastructure for testing too.
  18. compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")
  19. compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv")
  20. compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap")
  21. // Apply common dependencies for instrumentation.
  22. compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api") {
  23. // OpenTelemetry SDK is not needed for compilation
  24. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk")
  25. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-metrics")
  26. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-logs")
  27. }
  28. compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") {
  29. // OpenTelemetry SDK is not needed for compilation
  30. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk")
  31. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-metrics")
  32. exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-logs")
  33. }
  34. // Used by byte-buddy but not brought in as a transitive dependency
  35. compileOnly("com.google.code.findbugs:annotations")
  36. }
  37. testing {
  38. suites.withType(JvmTestSuite::class).configureEach {
  39. dependencies {
  40. implementation("io.opentelemetry.javaagent:opentelemetry-testing-common")
  41. }
  42. }
  43. }
  44. val testInstrumentation by configurations.creating {
  45. isCanBeConsumed = false
  46. isCanBeResolved = true
  47. }
  48. tasks.shadowJar {
  49. configurations = listOf(project.configurations.runtimeClasspath.get(), testInstrumentation)
  50. archiveFileName.set("agent-testing.jar")
  51. }
  52. val agentForTesting by configurations.creating {
  53. isCanBeConsumed = false
  54. isCanBeResolved = true
  55. }
  56. dependencies {
  57. agentForTesting("io.opentelemetry.javaagent:opentelemetry-agent-for-testing")
  58. }
  59. class JavaagentTestArgumentsProvider(
  60. @InputFile
  61. @PathSensitive(PathSensitivity.RELATIVE)
  62. val agentShadowJar: File,
  63. @InputFile
  64. @PathSensitive(PathSensitivity.RELATIVE)
  65. val shadowJar: File,
  66. ) : CommandLineArgumentProvider {
  67. override fun asArguments(): Iterable<String> = listOf(
  68. "-Dotel.javaagent.debug=true",
  69. "-javaagent:${agentShadowJar.absolutePath}",
  70. "-Dotel.javaagent.experimental.initializer.jar=${shadowJar.absolutePath}",
  71. "-Dotel.javaagent.testing.additional-library-ignores.enabled=false",
  72. "-Dotel.javaagent.testing.fail-on-context-leak=${findProperty("failOnContextLeak") != false}",
  73. // prevent sporadic gradle deadlocks, see SafeLogger for more details
  74. "-Dotel.javaagent.testing.transform-safe-logging.enabled=true",
  75. // Reduce noise in assertion messages since we don't need to verify this in most tests. We check
  76. // in smoke tests instead.
  77. "-Dotel.javaagent.add-thread-details=false",
  78. "-Dotel.metrics.exporter=otlp",
  79. // suppress repeated logging of "No metric data to export - skipping export."
  80. // since PeriodicMetricReader is configured with a short interval
  81. "-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO",
  82. // suppress a couple of verbose ClassNotFoundException stack traces logged at debug level
  83. "-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO",
  84. "-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO",
  85. "-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO",
  86. "-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO"
  87. )
  88. }
  89. // need to run this after evaluate because testSets plugin adds new test tasks
  90. afterEvaluate {
  91. tasks.withType<Test>().configureEach {
  92. val shadowJar = tasks.shadowJar.get()
  93. val agentShadowJar = agentForTesting.resolve().first()
  94. dependsOn(shadowJar)
  95. // TODO(anuraaga): Figure out why dependsOn override is still needed in otel.javaagent-testing
  96. // despite this dependency.
  97. dependsOn(agentForTesting.buildDependencies)
  98. jvmArgumentProviders.add(JavaagentTestArgumentsProvider(agentShadowJar, shadowJar.archiveFile.get().asFile))
  99. // We do fine-grained filtering of the classpath of this codebase's sources since Gradle's
  100. // configurations will include transitive dependencies as well, which tests do often need.
  101. classpath = classpath.filter {
  102. if (file("$buildDir/resources/main").equals(it) || file("$buildDir/classes/java/main").equals(it)) {
  103. // The sources are packaged into the testing jar, so we need to exclude them from the test
  104. // classpath, which automatically inherits them, to ensure our shaded versions are used.
  105. return@filter false
  106. }
  107. // TODO(anuraaga): Better not to have this naming constraint, we can likely use
  108. // plugin identification instead.
  109. val lib = it.absoluteFile
  110. if (lib.name.startsWith("opentelemetry-javaagent-")) {
  111. // These dependencies are packaged into the testing jar, so we need to exclude them from the test
  112. // classpath, which automatically inherits them, to ensure our shaded versions are used.
  113. return@filter false
  114. }
  115. if (lib.name.startsWith("opentelemetry-") && lib.name.contains("-autoconfigure-")) {
  116. // These dependencies should not be on the test classpath, because they will auto-instrument
  117. // the library and the tests could pass even if the javaagent instrumentation fails to apply
  118. return@filter false
  119. }
  120. return@filter true
  121. }
  122. }
  123. }
  124. // shadowJar is only used for creating a jar for testing, but the shadow plugin automatically adds
  125. // it to a project's published Java component. Skip it if publishing is configured for this
  126. // project.
  127. plugins.withId("maven-publish") {
  128. configure<PublishingExtension> {
  129. (components["java"] as AdhocComponentWithVariants).run {
  130. withVariantsFromConfiguration(configurations["shadowRuntimeElements"]) {
  131. skip()
  132. }
  133. }
  134. }
  135. }
  136. configurations.configureEach {
  137. if (name.endsWith("testruntimeclasspath", ignoreCase = true)) {
  138. // Added by agent, don't let Gradle bring it in when running tests.
  139. exclude("io.opentelemetry.javaagent", "opentelemetry-javaagent-bootstrap")
  140. }
  141. }