Skip to content

Commit 16b7f5b

Browse files
author
Anuraag Agrawal
authored
Move AgentClassLoader out of public API. (#1182)
* Move AgentClassLoader out of public API. * Move tests too * Keep auto-api on bootstrap classpath.
1 parent 4959aa7 commit 16b7f5b

File tree

9 files changed

+29
-19
lines changed

9 files changed

+29
-19
lines changed

javaagent-bootstrap/javaagent-bootstrap.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ dependencies {
1212
implementation group: 'org.slf4j', name: 'slf4j-simple', version: versions.slf4j
1313
// ^ Generally a bad idea for libraries, but we're shadowing.
1414

15-
implementation project(':instrumentation-api')
1615
implementation project(':auto-api')
16+
implementation project(':instrumentation-api')
1717

1818
testImplementation project(':testing-common')
1919
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.19.0'

auto-api/src/main/java/io/opentelemetry/instrumentation/auto/api/AgentClassLoader.java renamed to javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opentelemetry.instrumentation.auto.api;
17+
package io.opentelemetry.javaagent.bootstrap;
1818

1919
import java.io.File;
2020
import java.net.MalformedURLException;
@@ -29,7 +29,6 @@
2929
* <p>It is built around the concept of a jar inside another jar. This classloader loads the files
3030
* of the internal jar to load classes and resources.
3131
*/
32-
// TODO remove this from api
3332
public class AgentClassLoader extends URLClassLoader {
3433
static {
3534
ClassLoader.registerAsParallelCapable();

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static ClassLoader createAgentClassLoader(String innerJarFilename, URL b
214214

215215
Class<?> loaderClass =
216216
ClassLoader.getSystemClassLoader()
217-
.loadClass("io.opentelemetry.instrumentation.auto.api.AgentClassLoader");
217+
.loadClass("io.opentelemetry.javaagent.bootstrap.AgentClassLoader");
218218
Constructor constructor =
219219
loaderClass.getDeclaredConstructor(URL.class, String.class, ClassLoader.class);
220220
return (ClassLoader) constructor.newInstance(bootstrapURL, innerJarFilename, agentParent);

auto-api/src/main/java/io/opentelemetry/instrumentation/auto/api/InternalJarURLHandler.java renamed to javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/InternalJarURLHandler.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opentelemetry.instrumentation.auto.api;
17+
package io.opentelemetry.javaagent.bootstrap;
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
@@ -31,18 +31,17 @@
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333

34-
// TODO remove this from api
3534
public class InternalJarURLHandler extends URLStreamHandler {
3635

3736
private static final Logger log = LoggerFactory.getLogger(InternalJarURLHandler.class);
3837

39-
private static final WeakReference<Pair<String, JarEntry>> NULL = new WeakReference<>(null);
38+
private static final WeakReference<ResolvedJarEntry> NULL = new WeakReference<>(null);
4039

4140
private final String name;
4241
private final FileNotInInternalJar notFound;
4342
private final JarFile bootstrapJarFile;
4443

45-
private WeakReference<Pair<String, JarEntry>> cache = NULL;
44+
private WeakReference<ResolvedJarEntry> cache = NULL;
4645

4746
public InternalJarURLHandler(String internalJarFileName, URL bootstrapJarLocation) {
4847
name = internalJarFileName;
@@ -71,11 +70,11 @@ protected URLConnection openConnection(URL url) throws IOException {
7170
}
7271
// believe it or not, we're going to get called twice for this,
7372
// and the key will be a new object each time.
74-
Pair<String, JarEntry> pair = cache.get();
75-
if (null == pair || !filename.equals(pair.getLeft())) {
73+
ResolvedJarEntry pair = cache.get();
74+
if (null == pair || !filename.equals(pair.filename)) {
7675
JarEntry entry = bootstrapJarFile.getJarEntry(getResourcePath(filename));
7776
if (null != entry) {
78-
pair = Pair.of(filename, entry);
77+
pair = new ResolvedJarEntry(filename, entry);
7978
// the cache field is not volatile as a performance optimization
8079
// in the rare event this write is not visible to another thread,
8180
// it just means the same work is recomputed but does not affect consistency
@@ -88,7 +87,7 @@ protected URLConnection openConnection(URL url) throws IOException {
8887
// so dismiss cache after a hit
8988
cache = NULL;
9089
}
91-
return new InternalJarURLConnection(url, bootstrapJarFile.getInputStream(pair.getRight()));
90+
return new InternalJarURLConnection(url, bootstrapJarFile.getInputStream(pair.entry));
9291
}
9392

9493
private String getResourcePath(String filename) {
@@ -141,4 +140,14 @@ public Throwable fillInStackTrace() {
141140
return this;
142141
}
143142
}
143+
144+
private static class ResolvedJarEntry {
145+
private final String filename;
146+
private final JarEntry entry;
147+
148+
private ResolvedJarEntry(String filename, JarEntry entry) {
149+
this.filename = filename;
150+
this.entry = entry;
151+
}
152+
}
144153
}

auto-api/src/test/groovy/io/opentelemetry/instrumentation/auto/api/AgentClassLoaderTest.groovy renamed to javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoaderTest.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opentelemetry.instrumentation.auto.api
17+
package io.opentelemetry.javaagent.bootstrap
1818

19+
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader
1920
import java.util.concurrent.Phaser
2021
import java.util.concurrent.TimeUnit
2122
import spock.lang.Specification

auto-api/src/test/groovy/io/opentelemetry/instrumentation/auto/api/InternalJarURLHandlerTest.groovy renamed to javaagent-bootstrap/src/test/java/io/opentelemetry/javaagent/bootstrap/InternalJarURLHandlerTest.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opentelemetry.instrumentation.auto.api
17+
package io.opentelemetry.javaagent.bootstrap
1818

1919
import io.opentelemetry.auto.util.test.AgentSpecification
20+
import io.opentelemetry.javaagent.bootstrap.InternalJarURLHandler
2021
import spock.lang.Shared
2122

2223
class InternalJarURLHandlerTest extends AgentSpecification {

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ClassLoaderMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static class SkipClassLoaderMatcher
5454
public static final SkipClassLoaderMatcher INSTANCE = new SkipClassLoaderMatcher();
5555
/* Cache of classloader-instance -> (true|false). True = skip instrumentation. False = safe to instrument. */
5656
private static final String AGENT_CLASSLOADER_NAME =
57-
"io.opentelemetry.instrumentation.auto.api.AgentClassLoader";
57+
"io.opentelemetry.javaagent.bootstrap.AgentClassLoader";
5858
private static final String EXPORTER_CLASSLOADER_NAME =
5959
"io.opentelemetry.javaagent.tooling.ExporterClassLoader";
6060
private static final WeakCache<ClassLoader, Boolean> skipCache = AgentTooling.newWeakCache();

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import static net.bytebuddy.matcher.ElementMatchers.named;
2020

21-
import io.opentelemetry.instrumentation.auto.api.AgentClassLoader;
22-
import io.opentelemetry.instrumentation.auto.api.AgentClassLoader.BootstrapClassLoaderProxy;
21+
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
22+
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader.BootstrapClassLoaderProxy;
2323
import java.lang.reflect.Method;
2424
import java.net.URL;
2525
import net.bytebuddy.description.method.MethodDescription;

javaagent-tooling/src/test/groovy/io/opentelemetry/javaagent/test/ClassLoaderMatcherTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package io.opentelemetry.auto.test
1818

1919
import io.opentelemetry.auto.util.test.AgentSpecification
20-
import io.opentelemetry.instrumentation.auto.api.AgentClassLoader
20+
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader
2121
import io.opentelemetry.javaagent.tooling.ClassLoaderMatcher
2222
import io.opentelemetry.javaagent.tooling.ExporterClassLoader
2323
import io.opentelemetry.javaagent.tooling.log.LogContextScopeListener
@@ -54,7 +54,7 @@ class ClassLoaderMatcherTest extends AgentSpecification {
5454

5555
def "AgentClassLoader class name is hardcoded in ClassLoaderMatcher"() {
5656
expect:
57-
AgentClassLoader.name == "io.opentelemetry.instrumentation.auto.api.AgentClassLoader"
57+
AgentClassLoader.name == "io.opentelemetry.javaagent.bootstrap.AgentClassLoader"
5858
}
5959

6060
def "ExporterClassLoader class name is hardcoded in ClassLoaderMatcher"() {

0 commit comments

Comments
 (0)