Skip to content

Commit

Permalink
add an spi to get access to bootstrap proxy from muzzle module
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Apr 6, 2022
1 parent 37e0f14 commit 2311d71
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public static ResettableClassFileTransformer installBytebuddyAgent(
runBeforeAgentListeners(agentListeners, config, autoConfiguredSdk);
}

AgentTooling.init(Utils.getBootstrapProxy());
AgentBuilder agentBuilder =
new AgentBuilder.Default()
.disableClassFormatChanges()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.bootstrap;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.Utils;
import io.opentelemetry.javaagent.tooling.muzzle.BootstrapProxyProvider;

@AutoService(BootstrapProxyProvider.class)
public class BootstrapProxyProviderImpl implements BootstrapProxyProvider {

@Override
public ClassLoader getBootstrapProxy() {
return Utils.getBootstrapProxy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.javaagent.tooling.muzzle;

import java.util.Iterator;
import java.util.ServiceLoader;
import net.bytebuddy.agent.builder.AgentBuilder;

/**
Expand All @@ -14,7 +16,8 @@
*/
public final class AgentTooling {

private static volatile ClassLoader bootstrapProxy;
private static final AgentBuilder.PoolStrategy POOL_STRATEGY =
new AgentCachingPoolStrategy(locationStrategy(getBootstrapProxy()));

public static AgentLocationStrategy locationStrategy() {
return locationStrategy(null);
Expand All @@ -25,17 +28,19 @@ public static AgentLocationStrategy locationStrategy(ClassLoader bootstrapProxy)
}

public static AgentBuilder.PoolStrategy poolStrategy() {
return PoolStrategyHolder.POOL_STRATEGY;
return POOL_STRATEGY;
}

public static void init(ClassLoader classLoader) {
bootstrapProxy = classLoader;
private static ClassLoader getBootstrapProxy() {
Iterator<BootstrapProxyProvider> iterator =
ServiceLoader.load(BootstrapProxyProvider.class).iterator();
if (iterator.hasNext()) {
BootstrapProxyProvider bootstrapProxyProvider = iterator.next();
return bootstrapProxyProvider.getBootstrapProxy();
}

return null;
}

private AgentTooling() {}

private static class PoolStrategyHolder {
private static final AgentBuilder.PoolStrategy POOL_STRATEGY =
new AgentCachingPoolStrategy(locationStrategy(bootstrapProxy));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.muzzle;

/** This SPI can be used to find a class loader which can be used to look up bootstrap resources. */
public interface BootstrapProxyProvider {

/** Provide a class loader which can be used to look up bootstrap resources. */
ClassLoader getBootstrapProxy();
}

0 comments on commit 2311d71

Please sign in to comment.