Skip to content

Commit

Permalink
Extract agent shadow configuration to conventions script. (open-telem…
Browse files Browse the repository at this point in the history
…etry#3256)

* Extract agent shadow configuration to conventions script.

* Remove redundant plugin version declaration

* Resource loader doesn't load from buildSrc

* Comments about byte buddy version

* Fix ReferenceCollectorTest

Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
2 people authored and robododge committed Jun 17, 2021
1 parent 5b985ea commit beccd8a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 188 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ plugins {
id 'org.unbroken-dome.test-sets' apply false
id 'com.github.ben-manes.versions'

id "com.github.johnrengelman.shadow" apply false

id "com.diffplug.spotless"
id "net.ltgt.errorprone" apply false
id "net.ltgt.nullaway" apply false
Expand Down
49 changes: 0 additions & 49 deletions buildSrc/build.gradle

This file was deleted.

53 changes: 53 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
plugins {
`java-gradle-plugin`
`kotlin-dsl`
id("com.diffplug.spotless") version "5.12.4"
}

spotless {
java {
googleJavaFormat("1.10.0")
licenseHeaderFile(rootProject.file("../gradle/enforcement/spotless.license.java"), "(package|import|public)")
target("src/**/*.java")
}
}

gradlePlugin {
plugins {
create("muzzle-plugin") {
id = "muzzle"
implementationClass = "io.opentelemetry.instrumentation.gradle.muzzle.MuzzlePlugin"
}
}
}

repositories {
mavenCentral()
gradlePluginPortal()
mavenLocal()
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}

dependencies {
implementation(gradleApi())
implementation(localGroovy())

implementation("org.eclipse.aether:aether-connector-basic:1.1.0")
implementation("org.eclipse.aether:aether-transport-http:1.1.0")
implementation("org.apache.maven:maven-aether-provider:3.3.9")

implementation("com.google.guava:guava:30.1-jre")
implementation("gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.0.0")
implementation("org.ow2.asm:asm:7.0-beta")
implementation("org.ow2.asm:asm-tree:7.0-beta")
implementation("org.apache.httpcomponents:httpclient:4.5.10")
// When updating, also update dependencyManagement/dependencyManagement.gradle.kts
implementation("net.bytebuddy:byte-buddy-gradle-plugin:1.11.2")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
testImplementation("org.assertj:assertj-core:3.19.0")
}
33 changes: 33 additions & 0 deletions buildSrc/src/main/kotlin/otel.shadow-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
id("com.github.johnrengelman.shadow")
}

tasks.withType<ShadowJar>().configureEach {
mergeServiceFiles()

exclude("**/module-info.class")

// Prevents conflict with other SLF4J instances. Important for premain.
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
// rewrite dependencies calling Logger.getLogger
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")

// prevents conflict with library instrumentation
relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation")

// relocate(OpenTelemetry API)
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")

// relocate(the OpenTelemetry extensions that are used by instrumentation modules)
// these extensions live in the AgentClassLoader, and are injected into the user"s class loader
// by the instrumentation modules that use them
relocate("io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws")
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")

// this is for instrumentation on opentelemetry-api itself
relocate("application.io.opentelemetry", "io.opentelemetry")
}
1 change: 1 addition & 0 deletions dependencyManagement/dependencyManagement.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ val DEPENDENCY_SETS = listOf(
),
DependencySet(
"net.bytebuddy",
// When updating, also update buildSrc/build.gradle.kts
"1.11.2",
listOf("byte-buddy", "byte-buddy-agent")
),
Expand Down
28 changes: 1 addition & 27 deletions gradle/instrumentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io.opentelemetry.instrumentation.gradle.bytebuddy.ByteBuddyPluginConfigurator

apply plugin: 'net.bytebuddy.byte-buddy'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'otel.shadow-conventions'

ext {
mavenGroupId = 'io.opentelemetry.javaagent.instrumentation'
Expand Down Expand Up @@ -85,34 +85,8 @@ configurations {

tasks.named('shadowJar').configure {
configurations = [project.configurations.runtimeClasspath, project.configurations.testInstrumentation]
mergeServiceFiles()

archiveFileName = 'agent-testing.jar'

// rewrite library instrumentation dependencies
relocate "io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation"

// Prevents conflict with other SLF4J instances. Important for premain.
relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'
// rewrite dependencies calling Logger.getLogger
relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

// prevents conflict with library instrumentation
relocate 'io.opentelemetry.instrumentation.api', 'io.opentelemetry.javaagent.shaded.instrumentation.api'

// relocate OpenTelemetry API usage
relocate "io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api"
relocate "io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv"
relocate "io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context"

// relocate the OpenTelemetry extensions that are used by instrumentation modules
// these extensions live in the AgentClassLoader, and are injected into the user's class loader
// by the instrumentation modules that use them
relocate "io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws"
relocate "io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin"

// this is for instrumentation on opentelemetry-api itself
relocate "application.io.opentelemetry", "io.opentelemetry"
}

evaluationDependsOn(":testing:agent-for-testing")
Expand Down
29 changes: 1 addition & 28 deletions instrumentation/instrumentation.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// this project will run in isolation under the agent's classloader
plugins {
id "com.github.johnrengelman.shadow"
id "otel.shadow-conventions"
}
apply from: "$rootDir/gradle/java.gradle"

Expand Down Expand Up @@ -36,13 +36,6 @@ configurations {
}

shadowJar {
mergeServiceFiles()

exclude '**/module-info.class'

// Prevents conflict with other SLF4J instances. Important for premain.
relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'

duplicatesStrategy = DuplicatesStrategy.FAIL

dependencies {
Expand All @@ -51,26 +44,6 @@ shadowJar {
exclude(project(':instrumentation-api'))
exclude(project(':javaagent-api'))
}

// rewrite library instrumentation dependencies
relocate "io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation"

// rewrite dependencies calling Logger.getLogger
relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

// relocate OpenTelemetry API usage
relocate "io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api"
relocate "io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv"
relocate "io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context"

// relocate the OpenTelemetry extensions that are used by instrumentation modules
// these extensions live in the AgentClassLoader, and are injected into the user's class loader
// by the instrumentation modules that use them
relocate "io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws"
relocate "io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin"

// this is for instrumentation on opentelemetry-api itself
relocate "application.io.opentelemetry", "io.opentelemetry"
}

tasks.register("listInstrumentations") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.javaagent.extension.muzzle.MethodRef;
import io.opentelemetry.javaagent.extension.muzzle.Source;
import io.opentelemetry.javaagent.tooling.Utils;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -158,7 +159,14 @@ private ReferenceCollector collectReferences() {
typeInstrumentation.transform(adviceClassNameCollector);
}

ReferenceCollector collector = new ReferenceCollector(instrumentationModule::isHelperClass);
// the classloader has a parent including the Gradle classpath, such as buildSrc dependencies.
// These may have resources take precedence over ones we define, so we need to make sure to
// not include them when loading resources.
ClassLoader resourceLoader =
new URLClassLoader(
((URLClassLoader) MuzzleCodeGenerator.class.getClassLoader()).getURLs(), null);
ReferenceCollector collector =
new ReferenceCollector(instrumentationModule::isHelperClass, resourceLoader);
for (String adviceClass : adviceClassNameCollector.getAdviceClassNames()) {
collector.collectReferencesFromAdvice(adviceClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,24 @@
* plugin.
*/
public class ReferenceCollector {

private final Map<String, ClassRef> references = new LinkedHashMap<>();
private final MutableGraph<String> helperSuperClassGraph = GraphBuilder.directed().build();
private final Map<String, String> contextStoreClasses = new LinkedHashMap<>();
private final Set<String> visitedClasses = new HashSet<>();
private final InstrumentationClassPredicate instrumentationClassPredicate;
private final ClassLoader resourceLoader;

// only used by tests
public ReferenceCollector(Predicate<String> libraryInstrumentationPredicate) {
this(libraryInstrumentationPredicate, ReferenceCollector.class.getClassLoader());
}

public ReferenceCollector(
Predicate<String> libraryInstrumentationPredicate, ClassLoader resourceLoader) {
this.instrumentationClassPredicate =
new InstrumentationClassPredicate(libraryInstrumentationPredicate);
this.resourceLoader = resourceLoader;
}

/**
Expand Down Expand Up @@ -158,16 +167,13 @@ private void visitClassesAndCollectReferences(
}
}

private static InputStream getClassFileStream(String className) throws IOException {
private InputStream getClassFileStream(String className) throws IOException {
return getResourceStream(Utils.getResourceName(className));
}

private static InputStream getResourceStream(String resource) throws IOException {
private InputStream getResourceStream(String resource) throws IOException {
URLConnection connection =
checkNotNull(
ReferenceCollector.class.getClassLoader().getResource(resource),
"Couldn't find resource %s",
resource)
checkNotNull(resourceLoader.getResource(resource), "Couldn't find resource %s", resource)
.openConnection();

// Since the JarFile cache is not per class loader, but global with path as key, using cache may
Expand Down
26 changes: 1 addition & 25 deletions javaagent/javaagent.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import com.github.jk1.license.filter.LicenseBundleNormalizer
import com.github.jk1.license.render.InventoryMarkdownReportRenderer

plugins {
id "com.github.johnrengelman.shadow"
id "otel.shadow-conventions"
id "com.github.jk1.dependency-license-report" version "1.16"
}

Expand Down Expand Up @@ -60,8 +60,6 @@ shadowJar {

//Includes instrumentations, but not exporters
task lightShadow(type: ShadowJar) {
from sourceSets.main.output

dependsOn ':instrumentation:shadowJar'
def projectsWithShadowJar = [project(':instrumentation')]
with isolateSpec(projectsWithShadowJar)
Expand All @@ -77,32 +75,10 @@ publishing {

tasks.withType(ShadowJar).configureEach {
configurations = [project.configurations.shadowInclude]
mergeServiceFiles()

manifest {
inheritFrom project.tasks.jar.manifest
}

exclude '**/module-info.class'

// Prevents conflict with other SLF4J instances. Important for premain.
relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'
// rewrite dependencies calling Logger.getLogger
relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

// prevents conflict with library instrumentation
relocate 'io.opentelemetry.instrumentation.api', 'io.opentelemetry.javaagent.shaded.instrumentation.api'

// relocate OpenTelemetry API
relocate "io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api"
relocate "io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv"
relocate "io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context"

// relocate the OpenTelemetry extensions that are used by instrumentation modules
// these extensions live in the AgentClassLoader, and are injected into the user's class loader
// by the instrumentation modules that use them
relocate "io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws"
relocate "io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin"
}

configurations {
Expand Down
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pluginManagement {
plugins {
id "com.diffplug.spotless" version "5.12.4"
id 'com.github.ben-manes.versions' version '0.27.0'
id "com.github.johnrengelman.shadow" version "6.1.0"
id "io.github.gradle-nexus.publish-plugin" version "1.0.0"
id "me.champeau.jmh" version "0.6.4"
id "net.ltgt.errorprone" version "1.3.0"
Expand Down
Loading

0 comments on commit beccd8a

Please sign in to comment.