etsy / statsd-jvm-profiler

Simple JVM Profiler Using StatsD and Other Metrics Backends
MIT License
330 stars 93 forks source link

Loading agent dynamically after VM has started #32

Closed AlejandroRivera closed 8 years ago

AlejandroRivera commented 8 years ago

This PR adds the ability for the JVM Profiler Agent to be loaded dynamically after the VM has already started.

The code was inspired from a blog post and from reading: Oracle's Starting Agents After VM Startup

ajsquared commented 8 years ago

Very interesting idea! I'll look this over.

AlejandroRivera commented 8 years ago

Btw, i just ran into some issues because the JAR currently being generated is ab uber (shaded) jar. When the JAR is added as a maven/ivy/gradle/sbt dependency, this causes conflicts if the project is already relying on the same libraries (guava, jackson for example) but perhaps different versions. I'm trying to think of a way that preserves backwards compatibility with your current design, but allows for a non-shaded JAR. I'm reading the maven-shade-plugin to see if it's possible to generate two artifacts (artifact.jar and artifact-core.jar), or maybe consider using separate maven profiles, but maybe you have a better idea.

ajsquared commented 8 years ago

That's a fair point, and an issue I hadn't run into because all the library versions match up with what we use internally :)

I've opened https://github.com/etsy/statsd-jvm-profiler/issues/33 to look into a solution; I think can use the maven-assembly-plugin to build both a standard and uberjar

ajsquared commented 8 years ago

Thanks again for this! I'll publish a release with this included once I work out the uberjar thing.

AlejandroRivera commented 8 years ago

Sweet! good luck!

ajsquared commented 8 years ago

I've just published version 1.0.0 that contains this feature as well as fixes the issue with the uberjar. The Maven coordinates for the standard and uberjar are here: https://github.com/etsy/statsd-jvm-profiler#installation

AlejandroRivera commented 8 years ago

Well that was quick!

You might want to update the usage section to reflect using the uber-jar for the -javaagent command line approach.

ajsquared commented 8 years ago

Yeah, it turns out maven-shade-plugin already had that feature, it just had to be turned on!

Good call on updating the usage instructions.

ajsquared commented 8 years ago

Actually, I realized that the maven-shade-plugin approach isn't doing the right thing. The Premain-Class and Agent-Class settings are only applied to the uberjar. I'll have to look into it more.

AlejandroRivera commented 8 years ago

Try with:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Agent-Class>com.etsy.statsd.profiler.Agent</Agent-Class>
                            <Premain-Class>com.etsy.statsd.profiler.Agent</Premain-Class>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

(Updated comment to fix the Premain name)

ajsquared commented 8 years ago

Thanks, that did it. I've publish version 1.0.1 with the manifest entries set correctly.