|
31 | 31 | * limitations under the License. |
32 | 32 | */ |
33 | 33 |
|
34 | | -import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
35 | | - |
36 | 34 | plugins { |
37 | | - id("splunk.java-conventions") |
38 | | - id("splunk.shadow-conventions") |
39 | | -} |
40 | | - |
41 | | -val testInstrumentation = configurations.create("testInstrumentation") |
42 | | - |
43 | | -dependencies { |
44 | | - add("testInstrumentation", platform(project(":dependencyManagement"))) |
45 | | - compileOnly("io.opentelemetry:opentelemetry-sdk") |
46 | | - compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") |
47 | | - compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") |
48 | | - compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api") |
49 | | - compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator") |
50 | | - compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api") |
51 | | - compileOnly("io.opentelemetry.semconv:opentelemetry-semconv") |
52 | | - annotationProcessor("com.google.auto.service:auto-service") |
53 | | - compileOnly("com.google.auto.service:auto-service") |
54 | | - compileOnly(project(":bootstrap")) |
55 | | - |
56 | | - // test |
57 | | - testImplementation(project(":testing:common")) |
58 | | - // the bootstrap module is provided by the javaagent in the instrumentation test runtime, no need to include it |
59 | | - // (especially when it's not being shaded) |
60 | | - testCompileOnly(project(":bootstrap")) |
61 | | -} |
62 | | - |
63 | | -tasks.named<ShadowJar>("shadowJar").configure { |
64 | | - configurations = listOf(project.configurations.runtimeClasspath.get(), testInstrumentation) |
65 | | - |
66 | | - archiveFileName.set("agent-testing.jar") |
67 | | -} |
68 | | - |
69 | | -evaluationDependsOn(":testing:agent-for-testing") |
70 | | - |
71 | | -tasks.withType<Test>().configureEach { |
72 | | - val shadowJar = tasks.shadowJar.get() |
73 | | - val agentShadowJar = project(":testing:agent-for-testing").tasks.shadowJar |
74 | | - |
75 | | - inputs.file(shadowJar.archiveFile) |
76 | | - |
77 | | - dependsOn(shadowJar) |
78 | | - dependsOn(agentShadowJar.get()) |
79 | | - |
80 | | - jvmArgumentProviders.add(JavaagentProvider(agentShadowJar.flatMap { it.archiveFile })) |
81 | | - |
82 | | - jvmArgs("-Dotel.javaagent.debug=true") |
83 | | - jvmArgs("-Dotel.javaagent.experimental.initializer.jar=${shadowJar.archiveFile.get().asFile.absolutePath}") |
84 | | - jvmArgs("-Dotel.javaagent.testing.additional-library-ignores.enabled=false") |
85 | | - jvmArgs("-Dotel.javaagent.testing.fail-on-context-leak=true") |
86 | | - // prevent sporadic gradle deadlocks, see SafeLogger for more details |
87 | | - jvmArgs("-Dotel.javaagent.testing.transform-safe-logging.enabled=true") |
88 | | - // Reduce noise in assertion messages since we don't need to verify this in most tests. We check |
89 | | - // in smoke tests instead. |
90 | | - jvmArgs("-Dotel.javaagent.add-thread-details=false") |
91 | | - // suppress repeated logging of "No metric data to export - skipping export." |
92 | | - // since PeriodicMetricReader is configured with a short interval |
93 | | - jvmArgs("-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO") |
94 | | - |
95 | | - val trustStore = project(":testing:common").file("src/misc/testing-keystore.p12") |
96 | | - inputs.file(trustStore) |
97 | | - jvmArgs("-Djavax.net.ssl.trustStore=${trustStore.absolutePath}") |
98 | | - jvmArgs("-Djavax.net.ssl.trustStorePassword=testing") |
99 | | - |
100 | | - // The sources are packaged into the testing jar so we need to make sure to exclude from the test |
101 | | - // classpath, which automatically inherits them, to ensure our shaded versions are used. |
102 | | - classpath = classpath.filter { |
103 | | - if (layout.buildDirectory.file("resources/main") == it || layout.buildDirectory.file("classes/java/main") == it) { |
104 | | - return@filter false |
105 | | - } |
106 | | - return@filter true |
107 | | - } |
108 | | -} |
109 | | - |
110 | | -/** |
111 | | - * We define three dependency configurations to use when adding dependencies to libraries being |
112 | | - * instrumented. |
113 | | - * |
114 | | - * - library: A dependency on the instrumented library. Results in the dependency being added to |
115 | | - * compileOnly and testImplementation. If the build is run with -PtestLatestDeps=true, the |
116 | | - * version when added to testImplementation will be overridden by `+`, the latest version |
117 | | - * possible. For simple libraries without different behavior between versions, it is possible |
118 | | - * to have a single dependency on library only. |
119 | | - * |
120 | | - * - testLibrary: A dependency on a library for testing. This will usually be used to either |
121 | | - * a) use a different version of the library for compilation and testing and b) to add a helper |
122 | | - * that is only required for tests (e.g., library-testing artifact). The dependency will be |
123 | | - * added to testImplementation and will have a version of `+` when testing latest deps as |
124 | | - * described above. |
125 | | - * |
126 | | - * - latestDepTestLibrary: A dependency on a library for testing when testing of latest dependency |
127 | | - * version is enabled. This dependency will be added as-is to testImplementation, but only if |
128 | | - * -PtestLatestDeps=true. The version will not be modified but it will be given highest |
129 | | - * precedence. Use this to restrict the latest version dependency from the default `+`, for |
130 | | - * example to restrict to just a major version by specifying `2.+`. |
131 | | - */ |
132 | | - |
133 | | -val testLatestDeps = gradle.startParameter.projectProperties["testLatestDeps"] == "true" |
134 | | -extra["testLatestDeps"] = testLatestDeps |
135 | | - |
136 | | -@CacheableRule |
137 | | -abstract class TestLatestDepsRule : ComponentMetadataRule { |
138 | | - override fun execute(context: ComponentMetadataContext) { |
139 | | - val version = context.details.id.version |
140 | | - if (version.contains("-alpha", true) |
141 | | - || version.contains("-beta", true) |
142 | | - || version.contains("-rc", true) |
143 | | - || version.contains(".rc", true) |
144 | | - || version.contains("-m", true) // e.g. spring milestones are published to grails repo |
145 | | - || version.contains(".m", true) // e.g. lettuce |
146 | | - || version.contains(".alpha", true) // e.g. netty |
147 | | - || version.contains(".beta", true) // e.g. hibernate |
148 | | - || version.contains(".cr", true) // e.g. hibernate |
149 | | - || version.endsWith("-nf-execution") // graphql |
150 | | - || GIT_SHA_PATTERN.matches(version) // graphql |
151 | | - || DATETIME_PATTERN.matches(version) // graphql |
152 | | - ) { |
153 | | - context.details.status = "milestone" |
154 | | - } |
155 | | - } |
156 | | - |
157 | | - companion object { |
158 | | - private val GIT_SHA_PATTERN = Regex("^.*-[0-9a-f]{7,}$") |
159 | | - private val DATETIME_PATTERN = Regex("^\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}.*$") |
160 | | - } |
161 | | -} |
162 | | - |
163 | | -configurations { |
164 | | - val library = configurations.create("library") { |
165 | | - isCanBeResolved = false |
166 | | - isCanBeConsumed = false |
167 | | - } |
168 | | - val testLibrary = configurations.create("testLibrary") { |
169 | | - isCanBeResolved = false |
170 | | - isCanBeConsumed = false |
171 | | - } |
172 | | - val latestDepTestLibrary = configurations.create("latestDepTestLibrary") { |
173 | | - isCanBeResolved = false |
174 | | - isCanBeConsumed = false |
175 | | - } |
176 | | - |
177 | | - val testImplementation = configurations.getByName("testImplementation") |
178 | | - |
179 | | - listOf(library, testLibrary).forEach { configuration -> |
180 | | - // We use whenObjectAdded and copy into the real configurations instead of extension to allow |
181 | | - // mutating the version for latest dep tests. |
182 | | - configuration.dependencies.whenObjectAdded { |
183 | | - val dep = copy() |
184 | | - if (testLatestDeps) { |
185 | | - (dep as ExternalDependency).version { |
186 | | - require("latest.release") |
187 | | - } |
188 | | - } |
189 | | - testImplementation.dependencies.add(dep) |
190 | | - } |
191 | | - } |
192 | | - if (testLatestDeps) { |
193 | | - dependencies { |
194 | | - components { |
195 | | - all<TestLatestDepsRule>() |
196 | | - } |
197 | | - } |
198 | | - |
199 | | - latestDepTestLibrary.dependencies.whenObjectAdded { |
200 | | - val dep = copy() |
201 | | - val declaredVersion = dep.version |
202 | | - if (declaredVersion != null) { |
203 | | - (dep as ExternalDependency).version { |
204 | | - strictly(declaredVersion) |
205 | | - } |
206 | | - } |
207 | | - testImplementation.dependencies.add(dep) |
208 | | - } |
209 | | - } |
210 | | - named("compileOnly") { |
211 | | - extendsFrom(library) |
212 | | - } |
213 | | -} |
214 | | - |
215 | | -if (testLatestDeps) { |
216 | | - afterEvaluate { |
217 | | - tasks { |
218 | | - withType<JavaCompile>().configureEach { |
219 | | - with(options) { |
220 | | - // We may use methods that are deprecated in future versions, we check lint on the normal |
221 | | - // build and don't need this for testLatestDeps. |
222 | | - compilerArgs.add("-Xlint:-deprecation") |
223 | | - } |
224 | | - } |
225 | | - } |
226 | | - |
227 | | - if (tasks.names.contains("latestDepTest")) { |
228 | | - val latestDepTest = tasks.getByName("latestDepTest") |
229 | | - tasks.named("test").configure { |
230 | | - dependsOn(latestDepTest) |
231 | | - } |
232 | | - } |
233 | | - } |
234 | | -} else { |
235 | | - afterEvaluate { |
236 | | - // Disable compiling latest dep tests for non latest dep builds in CI. This is needed to avoid |
237 | | - // breaking build because of a new library version which could force backporting latest dep |
238 | | - // fixes to release branches. |
239 | | - // This is only needed for modules where base version and latest dep tests use a different |
240 | | - // source directory. |
241 | | - var latestDepCompileTaskNames = arrayOf("compileLatestDepTestJava", "compileLatestDepTestGroovy", "compileLatestDepTestScala") |
242 | | - for (compileTaskName in latestDepCompileTaskNames) { |
243 | | - if (tasks.names.contains(compileTaskName)) { |
244 | | - tasks.named(compileTaskName).configure { |
245 | | - enabled = false |
246 | | - } |
247 | | - } |
248 | | - } |
249 | | - } |
250 | | -} |
251 | | - |
252 | | -tasks { |
253 | | - register("generateInstrumentationVersionFile") { |
254 | | - val name = "com.splunk.${project.name}" |
255 | | - val version = project.version.toString() |
256 | | - inputs.property("instrumentation.name", name) |
257 | | - inputs.property("instrumentation.version", version) |
258 | | - |
259 | | - val propertiesDir = layout.buildDirectory.dir("generated/instrumentationVersion/META-INF/io/opentelemetry/instrumentation") |
260 | | - outputs.dir(propertiesDir) |
261 | | - |
262 | | - doLast { |
263 | | - File(propertiesDir.get().asFile, "$name.properties").writeText("version=$version") |
264 | | - } |
265 | | - } |
266 | | -} |
267 | | - |
268 | | -sourceSets { |
269 | | - main { |
270 | | - output.dir("build/generated/instrumentationVersion", "builtBy" to "generateInstrumentationVersionFile") |
271 | | - } |
272 | | -} |
273 | | - |
274 | | -class JavaagentProvider( |
275 | | - @InputFile |
276 | | - @PathSensitive(PathSensitivity.RELATIVE) |
277 | | - val agentJar: Provider<RegularFile>, |
278 | | -) : CommandLineArgumentProvider { |
279 | | - override fun asArguments(): Iterable<String> = listOf( |
280 | | - "-javaagent:${file(agentJar).absolutePath}", |
281 | | - ) |
| 35 | + id("splunk.instrumentation-testing-conventions") |
282 | 36 | } |
0 commit comments