-
Notifications
You must be signed in to change notification settings - Fork 934
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
Open
jack-berg
wants to merge
10
commits into
open-telemetry:main
Choose a base branch
from
jack-berg:detect-shared-internal-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
34084c0
Add arch test to detect usage of shared internal code
jack-berg 5dbf3b6
Remove debug print statement
jack-berg dec4710
Debug
jack-berg bcc692a
stacktrace
jack-berg 7bbf059
mkdirs
jack-berg 96831a9
add println to debug on windows
jack-berg 96f172a
Fix on windows
jack-berg 573e45a
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 23e6e1e
Cleanup for review
jack-berg b2eb978
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
all/src/test/java/io/opentelemetry/all/NoSharedInternalCodeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| "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")); | ||
|
|
||
| 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))); | ||
|
|
||
| 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]); | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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:
opentelemetry-commonopentelemetry-contextopentelemetry-tracing-shimopentelemetry-apiopentelemetry-extension-kotlinopentelemetry-sdk