Skip to content

Enhance dd-java-agent ClassLoading#218

Closed
realark wants to merge 3 commits intomasterfrom
ark/stable_classloading
Closed

Enhance dd-java-agent ClassLoading#218
realark wants to merge 3 commits intomasterfrom
ark/stable_classloading

Conversation

@realark
Copy link
Copy Markdown
Contributor

@realark realark commented Feb 7, 2018

@tylerbenson If you have time, would you mind taking a look at the gradle project layout and config? Not urgent, and this isn't ready for a reveiw anyways.

Gradle Projects

  • dd-java-agent:agent-bootstrap: Runs on bootstrap loader. Contains api classes like opentracing, dd-trace-api
    dd-java-agent:agent-tooling: renaming of tooling. Contains dd-trace-ot and agent dependencies.
  • dd-java-agent:instrumentation: A single project which depends on agent-tooling and all the instrumentation. Used to create a single shadowJar which contains both the core agent and the instrumentation.
  • dd-java-agent: Bare-bones jar with no dependencies. Extracts the bootstrap and instrumentation jar, sets up the classpath and invokes agent and opentracing startup.

@realark realark added the tag: do not merge Do not merge changes label Feb 7, 2018
@realark realark force-pushed the ark/stable_classloading branch from 43ccb2b to acf28e4 Compare February 7, 2018 03:25
@realark realark changed the title Reorganize dd-java-agent subprojects Enhance dd-java-agent ClassLoading Feb 7, 2018

// TODO: Write test
// for every class in bootstrap jar:
// assert class not present in agent jar
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be easier to do a whitelist, maybe using something like https://google.github.io/guava/releases/17.0/api/docs/index.html?com/google/common/reflect/ClassPath.html to scan the classpath.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Neat. I'll look into that.

compile deps.opentracing
compile deps.slf4j
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
// ^ Generally a bad idea for libraries, but we're shadowing.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You will need to apply the shadow rename rules here too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Never mind... I see it is handled by dd-java-agent instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I wanted to keep the shadow rules in the same block so they're consistently applied.


// 1. Skip classloaders which don't delegate to bootstrap

// 2. Skip incompatible versions of OpenTracing
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These would be nice, but I'm curious how you plan to achieve them.

Copy link
Copy Markdown
Contributor Author

@realark realark Feb 7, 2018

Choose a reason for hiding this comment

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

When we transform classes we're given an instance of the classloader the class is about to load on. We can test that classloader to ensure its loading strategy and classpath are compatible with our instrumentation, and abort the instrumentation otherwise.

@Slf4j
public class TracerInstaller {
/** Register a global tracer if no global tracer is already registered. */
public static synchronized void installGlobalTracer() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One idea that @adriancole suggested to me is instead of trying to register with GlobalTracer, just rewrite the get() method to just return our instance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What's the benefit of a bytecode rewrite instead of registering the global tracer?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Primarily avoiding the volatile field lookup, but also preventing a possible exception if they are registering their own tracer.


final ClassLoader agentClassLoader = TracingAgent.class.getClassLoader();

inst.appendToSystemClassLoaderSearch(bootStrapJar);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be appendToBootstrapClassLoaderSearch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It will be. At this stage I'm trying to get the gradle project and agent bootstrapping working. Next step is to start using the bootstrap and custom classloader.

// in static initializers
DDJavaAgentInfo.VERSION.toString();
DDTraceOTInfo.VERSION.toString();
DDTraceApiInfo.VERSION.toString();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems these were lost in the shuffle. Can you think of a better way to handle this? Do you think I'm being overly paranoid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They'll be back. I commented them out because the new dd-java-agent project has no dependencies and can't log. I'm going to move this over to the tooling project.

"agent-bootstrap.jar.zip",
"agent-bootstrap.jar"));

final ClassLoader agentClassLoader = TracingAgent.class.getClassLoader();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I assume the actual classloader will be used later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@tylerbenson
Copy link
Copy Markdown
Contributor

I realize you asked me to focus on the gradle stuff... I think that is all good. Sorry if I added unnecessary comments to the other stuff.

@realark
Copy link
Copy Markdown
Contributor Author

realark commented Feb 7, 2018

No problem, reviewing the other stuff is appreciated!

@realark realark force-pushed the ark/stable_classloading branch 3 times, most recently from a942380 to fd022a1 Compare February 8, 2018 19:46
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What was wrong with using TraceResolver?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Opentracing is now on the bootstrap loader. When the TracerResolver calls the service loader it checks the classpath of the bootstrap loader. Our DDTracer is in the agent's classloader, so it doesn't see it.

@realark realark force-pushed the ark/stable_classloading branch 3 times, most recently from c8ded5f to faa2129 Compare February 9, 2018 02:04
@realark realark force-pushed the ark/stable_classloading branch from faa2129 to f6dddc9 Compare February 9, 2018 22:05
@realark realark force-pushed the ark/stable_classloading branch from f6dddc9 to 55e5f57 Compare February 9, 2018 22:17
@realark
Copy link
Copy Markdown
Contributor Author

realark commented Feb 9, 2018

Closing this out. Going to break this PR into several smaller ones.

@realark realark closed this Feb 9, 2018
@realark realark deleted the ark/stable_classloading branch February 9, 2018 22:32
@tylerbenson tylerbenson added this to the Closed milestone Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: do not merge Do not merge changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants