-
Notifications
You must be signed in to change notification settings - Fork 969
Add arch test to detect usage of shared internal code #6978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
34084c0
5dbf3b6
dec4710
bcc692a
7bbf059
96831a9
96f172a
573e45a
23e6e1e
b2eb978
3441b9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,34 +1,19 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| plugins { | ||||||||||||||||||||||||||||||||||||||||||||||||
| id("otel.java-conventions") | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| description = "OpenTelemetry All" | ||||||||||||||||||||||||||||||||||||||||||||||||
| otelJava.moduleName.set("io.opentelemetry.all") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| tasks { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // We don't compile much here, just some API boundary tests. This project is mostly for | ||||||||||||||||||||||||||||||||||||||||||||||||
| // aggregating jacoco reports and it doesn't work if this isn't at least as high as the | ||||||||||||||||||||||||||||||||||||||||||||||||
| // highest supported Java version in any of our projects. All of our | ||||||||||||||||||||||||||||||||||||||||||||||||
| // projects target Java 8 except :exporters:http-sender:jdk, which targets | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Java 11 | ||||||||||||||||||||||||||||||||||||||||||||||||
| withType(JavaCompile::class) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| options.release.set(11) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| val testJavaVersion: String? by project | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (testJavaVersion == "8") { | ||||||||||||||||||||||||||||||||||||||||||||||||
| test { | ||||||||||||||||||||||||||||||||||||||||||||||||
| enabled = false | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Skip OWASP dependencyCheck task on test module | ||||||||||||||||||||||||||||||||||||||||||||||||
| dependencyCheck { | ||||||||||||||||||||||||||||||||||||||||||||||||
| skip = true | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| val testTasks = mutableListOf<Task>() | ||||||||||||||||||||||||||||||||||||||||||||||||
| val jarTasks = mutableListOf<Jar>() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| dependencies { | ||||||||||||||||||||||||||||||||||||||||||||||||
| rootProject.subprojects.forEach { subproject -> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,13 +26,50 @@ dependencies { | |||||||||||||||||||||||||||||||||||||||||||||||
| subproject.tasks.withType<Test>().configureEach { | ||||||||||||||||||||||||||||||||||||||||||||||||
| testTasks.add(this) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| subproject.tasks.withType<Jar>().forEach { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (it.archiveClassifier.get().isEmpty() && !it.archiveFile.get().toString().contains("jmh")) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| jarTasks.add(it) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| testImplementation("com.tngtech.archunit:archunit-junit5") | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| val artifactsAndJarsFile = layout.buildDirectory.file("artifacts_and_jars.txt").get().asFile | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| var writeArtifactsAndJars = tasks.register("writeArtifactsAndJars") { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| dependsOn(jarTasks) | ||||||||||||||||||||||||||||||||||||||||||||||||
| artifactsAndJarsFile.parentFile.mkdirs() | ||||||||||||||||||||||||||||||||||||||||||||||||
| artifactsAndJarsFile.createNewFile() | ||||||||||||||||||||||||||||||||||||||||||||||||
| val content = jarTasks.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .map { | ||||||||||||||||||||||||||||||||||||||||||||||||
| it.archiveBaseName.get() + ":" + it.archiveFile.get().toString() | ||||||||||||||||||||||||||||||||||||||||||||||||
| }.collect(Collectors.joining("\n")) | ||||||||||||||||||||||||||||||||||||||||||||||||
| artifactsAndJarsFile.writeText(content) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what AI is saying and suggesting for me (and I've found it's a lot better at Gradle than me 😄)
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah very helpful for demystifying gradle
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This specific code ran into issues with the configuration cache. I iterated on it a bit and made it work. |
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| tasks { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // We don't compile much here, just some API boundary tests. This project is mostly for | ||||||||||||||||||||||||||||||||||||||||||||||||
| // aggregating jacoco reports and it doesn't work if this isn't at least as high as the | ||||||||||||||||||||||||||||||||||||||||||||||||
| // highest supported Java version in any of our projects. All of our | ||||||||||||||||||||||||||||||||||||||||||||||||
| // projects target Java 8 except :exporters:http-sender:jdk, which targets | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Java 11 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
jack-berg marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||
| withType(JavaCompile::class) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| options.release.set(11) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| val testJavaVersion: String? by project | ||||||||||||||||||||||||||||||||||||||||||||||||
| test { | ||||||||||||||||||||||||||||||||||||||||||||||||
| enabled = testJavaVersion != "8" | ||||||||||||||||||||||||||||||||||||||||||||||||
| dependsOn(writeArtifactsAndJars) | ||||||||||||||||||||||||||||||||||||||||||||||||
| environment("ARTIFACTS_AND_JARS", artifactsAndJarsFile.absolutePath) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // https://docs.gradle.org/current/samples/sample_jvm_multi_project_with_code_coverage.html | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| val sourcesPath by configurations.creating { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.all; | ||
|
|
||
| import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; | ||
|
|
||
| import com.tngtech.archunit.base.DescribedPredicate; | ||
| import com.tngtech.archunit.core.domain.JavaClass; | ||
| import com.tngtech.archunit.core.domain.JavaClasses; | ||
| import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
| import com.tngtech.archunit.lang.syntax.elements.ClassesShouldConjunction; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.jar.JarFile; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| class NoSharedInternalCodeTest { | ||
|
|
||
| private static final Set<String> exemptions = | ||
| new HashSet<>( | ||
| Arrays.asList( | ||
|
jack-berg marked this conversation as resolved.
Outdated
|
||
| "opentelemetry-api-incubator", | ||
| "opentelemetry-exporter-common", | ||
| "opentelemetry-exporter-logging", | ||
| "opentelemetry-exporter-logging-otlp", | ||
| "opentelemetry-exporter-prometheus", | ||
| "opentelemetry-exporter-zipkin", | ||
| "opentelemetry-extension-trace-propagators", | ||
| "opentelemetry-opencensus-shim", | ||
| "opentelemetry-sdk-common", | ||
| "opentelemetry-sdk-logs", | ||
| "opentelemetry-sdk-metrics", | ||
| "opentelemetry-sdk-testing", | ||
| "opentelemetry-sdk-trace", | ||
| "opentelemetry-sdk-extension-autoconfigure", | ||
| "opentelemetry-sdk-extension-autoconfigure-spi", | ||
| "opentelemetry-sdk-extension-incubator", | ||
| "opentelemetry-sdk-extension-jaeger-remote-sampler", | ||
| "opentelemetry-exporter-otlp", | ||
| "opentelemetry-exporter-otlp-common", | ||
| "opentelemetry-exporter-sender-grpc-managed-channel", | ||
| "opentelemetry-exporter-sender-jdk", | ||
| "opentelemetry-exporter-sender-okhttp")); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the TODO list. All these modules use shared internal code. In contrast, the list of modules which DO NOT use shared internal code is small:
|
||
|
|
||
| private static final String OTEL_BASE_PACKAGE = "io.opentelemetry"; | ||
| private static final Logger logger = Logger.getLogger(NoSharedInternalCodeTest.class.getName()); | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("artifactsAndJars") | ||
| void noSharedInternalCode(String artifactId, String absolutePath) throws IOException { | ||
| JavaClasses artifactClasses = | ||
| new ClassFileImporter().importJar(new JarFile(new File(absolutePath))); | ||
|
jack-berg marked this conversation as resolved.
Outdated
|
||
|
|
||
| Set<String> artifactOtelPackages = | ||
| artifactClasses.stream() | ||
| .map(JavaClass::getPackageName) | ||
| .filter(packageName -> packageName.startsWith(OTEL_BASE_PACKAGE)) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| ClassesShouldConjunction noSharedInternalCodeRule = | ||
| noClasses() | ||
| .that() | ||
| .resideInAnyPackage(artifactOtelPackages.toArray(new String[0])) | ||
| .should() | ||
| .dependOnClassesThat( | ||
| new DescribedPredicate<>( | ||
| "are in internal modules of other opentelemetry artifacts") { | ||
| @Override | ||
| public boolean test(JavaClass javaClass) { | ||
| String packageName = javaClass.getPackageName(); | ||
| return packageName.startsWith(OTEL_BASE_PACKAGE) | ||
| && packageName.contains(".internal") | ||
| && !artifactOtelPackages.contains(packageName); | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| noSharedInternalCodeRule | ||
| .as(artifactId + " should not use internal code from other artifacts") | ||
| .check(artifactClasses); | ||
| // To view artifacts which do not contain shared internal code, change test log level or | ||
| // increase log level of this statement to WARNING | ||
| logger.log(Level.INFO, artifactId + " does not contain shared internal code"); | ||
| } catch (AssertionError e) { | ||
| if (exemptions.contains(artifactId)) { | ||
| // To view details, remove from exemptions list | ||
| logger.log( | ||
| Level.WARNING, artifactId + " contains shared internal code but is temporarily exempt"); | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Stream<Arguments> artifactsAndJars() throws IOException { | ||
| List<String> lines = Files.readAllLines(Path.of(System.getenv("ARTIFACTS_AND_JARS"))); | ||
| return lines.stream() | ||
| .map( | ||
| line -> { | ||
| String[] parts = line.split(":", 2); | ||
| return Arguments.of(parts[0], parts[1]); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.