Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/distro/gradle/instrumentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ shadowJar {

mergeServiceFiles()
// mergeServiceFiles requires that duplicate strategy is set to include
filesMatching("inst/META-INF/services/**") {
filesMatching("META-INF/services/**") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ private void testAndVerify() throws IOException, InterruptedException {
Assertions.assertNotEquals(
0, countResourcesByValue(traces, "telemetry.distro.version", currentAgentVersion));
Assertions.assertNotEquals(0, countResourcesByValue(traces, "custom.resource", "demo"));
Assertions.assertEquals(1, countSpansByAttributeValue(traces, "demo.custom", "demo-extension"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,54 @@ val shadowMuzzleBootstrap by tasks.registering(ShadowJar::class) {

// this is a copied from io.opentelemetry.instrumentation.javaagent-shadowing for now at least to
// avoid publishing io.opentelemetry.instrumentation.javaagent-shadowing publicly
tasks.withType<ShadowJar>().configureEach {
mergeServiceFiles()
// mergeServiceFiles requires that duplicate strategy is set to include
filesMatching("META-INF/services/**") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
// Merge any AWS SDK service files that may be present (too bad they didn't just use normal
// service loader...)
mergeServiceFiles("software/amazon/awssdk/global/handlers")
// mergeServiceFiles requires that duplicate strategy is set to include
filesMatching("software/amazon/awssdk/global/handlers/**") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
listOf(shadowModule, shadowMuzzleTooling, shadowMuzzleBootstrap).forEach { task ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out that in our example extension InstrumenterCustomizerProvider SPI worked because the extension uses muzzle-check and also shades a dependency inside the extensions. Since muzzle-check added relocation rules to all ShadowJar tasks the SPI file got relocated under the shaded name.

task.configure {
mergeServiceFiles()
// mergeServiceFiles requires that duplicate strategy is set to include
filesMatching("META-INF/services/**") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
// Merge any AWS SDK service files that may be present (too bad they didn't just use normal
// service loader...)
mergeServiceFiles("software/amazon/awssdk/global/handlers")
// mergeServiceFiles requires that duplicate strategy is set to include
filesMatching("software/amazon/awssdk/global/handlers/**") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

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

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

if (project.findProperty("disableShadowRelocate") != "true") {
// prevents conflict with library instrumentation, since these classes live in the bootstrap class loader
relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") {
// Exclude resource providers since they live in the agent class loader
exclude("io.opentelemetry.instrumentation.resources.*")
exclude("io.opentelemetry.instrumentation.spring.resources.*")
}
if (project.findProperty("disableShadowRelocate") != "true") {
// prevents conflict with library instrumentation, since these classes live in the bootstrap class loader
relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") {
// Exclude resource providers since they live in the agent class loader
exclude("io.opentelemetry.instrumentation.resources.*")
exclude("io.opentelemetry.instrumentation.spring.resources.*")
}

// relocate(OpenTelemetry API) since these classes live in the bootstrap class loader
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("io.opentelemetry.common", "io.opentelemetry.javaagent.shaded.io.opentelemetry.common")
}
// relocate(OpenTelemetry API) since these classes live in the bootstrap class loader
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("io.opentelemetry.common", "io.opentelemetry.javaagent.shaded.io.opentelemetry.common")
}

// 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.contrib.awsxray", "io.opentelemetry.javaagent.shaded.io.opentelemetry.contrib.awsxray")
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
// 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.contrib.awsxray", "io.opentelemetry.javaagent.shaded.io.opentelemetry.contrib.awsxray")
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")

// this is for instrumentation of opentelemetry-api and opentelemetry-instrumentation-api
relocate("application.io.opentelemetry", "io.opentelemetry")
relocate("application.io.opentelemetry.instrumentation.api", "io.opentelemetry.instrumentation.api")
// this is for instrumentation of opentelemetry-api and opentelemetry-instrumentation-api
relocate("application.io.opentelemetry", "io.opentelemetry")
relocate("application.io.opentelemetry.instrumentation.api", "io.opentelemetry.instrumentation.api")

// this is for instrumentation on java.util.logging (since java.util.logging itself is shaded above)
relocate("application.java.util.logging", "java.util.logging")
// this is for instrumentation on java.util.logging (since java.util.logging itself is shaded above)
relocate("application.java.util.logging", "java.util.logging")
}
}

val compileMuzzle by tasks.registering {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -201,4 +202,50 @@ private ExtensionClassLoader(
super(new URL[] {url}, parent);
this.isSecurityManagerSupportEnabled = isSecurityManagerSupportEnabled;
}

@Override
public Enumeration<URL> findResources(String name) throws IOException {
Enumeration<URL> result = super.findResources(name);
// Agent shades instrumentation-api-incubator, in extensions references to these classes are
// remapped at load time. Here we handle looking up the service files for the classes that
// were renamed using the original name.
if (name.startsWith(
"META-INF/services/io.opentelemetry.javaagent.shaded.instrumentation.api.incubator")) {
String originalName =
name.replace(
"opentelemetry.javaagent.shaded.instrumentation", "opentelemetry.instrumentation");
return new CompoundEnumeration<>(result, super.findResources(originalName));
}
return result;
}

private static class CompoundEnumeration<E> implements Enumeration<E> {
private final Enumeration<E>[] enumerations;
private int index = 0;

@SafeVarargs
@SuppressWarnings("varargs")
CompoundEnumeration(Enumeration<E>... enumerations) {
this.enumerations = enumerations;
}

@Override
public boolean hasMoreElements() {
while (index < enumerations.length) {
if (enumerations[index].hasMoreElements()) {
return true;
}
index++;
}
return false;
}

@Override
public E nextElement() {
if (!hasMoreElements()) {
throw new NoSuchElementException();
}
return enumerations[index].nextElement();
}
}
}
Loading