-
Notifications
You must be signed in to change notification settings - Fork 881
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
Fix ClassCastException in JDBC instrumentation #6088
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,21 @@ | ||
plugins { | ||
id("otel.javaagent-bootstrap") | ||
} | ||
|
||
/* | ||
JDDC instrumentation uses VirtualField<Connection, DbInfo>. Add DbInfo, that is used as the value of | ||
VirtualField, to boot loader. We do this because when JDBC instrumentation is started in multiple | ||
class loaders in the same hierarchy, each would define their own version of DbInfo. It is possible | ||
that the value read from virtual field would be from the wrong class loader and could produce a | ||
ClassCastException. Having a single copy of DbInfo that is in boot loader avoids this issue. | ||
*/ | ||
|
||
sourceSets { | ||
main { | ||
val shadedDep = project(":instrumentation:jdbc:library") | ||
output.dir( | ||
shadedDep.file("build/extracted/shadow-bootstrap"), | ||
"builtBy" to ":instrumentation:jdbc:library:extractShadowJarBootstrap" | ||
) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
36 changes: 36 additions & 0 deletions
36
...test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/ProxyStatementFactory.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Proxy; | ||
import java.sql.Statement; | ||
|
||
public class ProxyStatementFactory { | ||
|
||
public static Statement proxyStatement(Statement statement) throws Exception { | ||
TestClassLoader classLoader = new TestClassLoader(ProxyStatementFactory.class.getClassLoader()); | ||
Class<?> testInterface = classLoader.loadClass(TestInterface.class.getName()); | ||
if (testInterface.getClassLoader() != classLoader) { | ||
throw new IllegalStateException("wrong class loader"); | ||
} | ||
InvocationHandler invocationHandler = (proxy, method, args) -> method.invoke(statement, args); | ||
Statement proxyStatement = | ||
(Statement) | ||
Proxy.newProxyInstance( | ||
classLoader, new Class<?>[] {Statement.class, testInterface}, invocationHandler); | ||
// adding package private interface TestInterface to jdk proxy forces defining the proxy class | ||
// in the same package as the package private interface | ||
if (!proxyStatement | ||
.getClass() | ||
.getName() | ||
.startsWith("io.opentelemetry.javaagent.instrumentation.jdbc.test")) { | ||
throw new IllegalStateException("proxy statement is in wrong package"); | ||
} | ||
|
||
return proxyStatement; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...t/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/TestClassLoader.java
This file contains 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,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
public class TestClassLoader extends URLClassLoader { | ||
|
||
public TestClassLoader(ClassLoader parent) { | ||
super( | ||
new URL[] {TestClassLoader.class.getProtectionDomain().getCodeSource().getLocation()}, | ||
parent); | ||
} | ||
|
||
@Override | ||
protected synchronized Class<?> loadClass(String name, boolean resolve) | ||
throws ClassNotFoundException { | ||
Class<?> clazz = findLoadedClass(name); | ||
if (clazz != null) { | ||
return clazz; | ||
} | ||
if (name.startsWith("io.opentelemetry.javaagent.instrumentation.jdbc.test")) { | ||
try { | ||
return findClass(name); | ||
} catch (ClassNotFoundException exception) { | ||
// ignore | ||
} | ||
} | ||
return super.loadClass(name, resolve); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/TestInterface.java
This file contains 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,12 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
// Adding a package private interface to jdk proxy forces defining the proxy class in the package | ||
// of the package private class. Usually proxy classes are defined in a package that we exclude from | ||
// instrumentation. We use this class to force proxy into a different package so it would get | ||
// instrumented. | ||
interface TestInterface {} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
I think this file would be a good place to document why DbInfo needs to be in the bootstrap class loader, maybe walking through an example of how the wrong DbInfo class can get picked up without this