ryandens / javaagent-gradle-plugin

A set of Gradle plugins to ease building Java applications that leverage instrumentation agents in development and/or in production
Apache License 2.0
47 stars 8 forks source link

How to use with springboot project? #37

Closed meiMingle closed 1 year ago

ryandens commented 1 year ago

Hi @meiMingle - currently this plugin supports being built into a distribution with the Gradle application plugin and the jib Gradle plugin for building container images. If your spring boot build leverages either of those plugins, the distributions should work just fine and have the javaagent configured. If there is an alternative packaging/distribution method you would like, feel free to file an issue with specific details. I won't necessarily have time for it but can prioritize it with other initiatives. I'm sure there are many ways to package spring boot applications into a distribution for deployment, some of which are a good fit for this plugin to handle.

punya commented 1 year ago

The Spring Boot Gradle plugin provides a number of Boot-specific tasks for running and packaging the application:

% ./gradlew tasks | grep boot
bootRun - Runs this project as a Spring Boot application.
bootTestRun - Runs this project as a Spring Boot application using the test runtime classpath.
bootBuildImage - Builds an OCI image of the application using the output of the bootJar task
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
assembleBootDist - Assembles the boot distributions
bootDistTar - Bundles the project as a distribution.
bootDistZip - Bundles the project as a distribution.

As far as I can tell, these tasks differ from the built-in ones because:

  1. They detect the main class name by scanning for the @SpringBootApplication annotation, rather than making the user specify it.
  2. The distribution contains a single jar that contains a few .class files and a lot of nested .jar files (as opposed to .jar files at the top level, or a shaded jar).
  3. The launch script is different.

Hopefully that helps to understand the effort required to add support for these tasks.

punya commented 1 year ago

The OTel "Getting Started" instructions for Java use Spring Boot and Gradle. Fixing this issue would help to improve the onboarding experience for all new users of OTel-Java.

ryandens commented 1 year ago

I currently have no plans to develop spring-specific support for this plugin personally. If someone would like to lead this initiative, I'm happy to re-open this issue and provide guidance on the kind of contributions I'd be open to accepting.

The shape that I expect this to take is a spring-specific plugin that either directly applies the existing plugins or shares build logic via a library to extend these tasks to enable instrumentation. Note, if you're interested in instrumenting a standalone jar like the one created by the bootJar task, the best approach would not be with a javaagent but rather to leverage an approach similar to the OTel static instrumenter library or using the bytebuddy gradle plugin.

Extending this plugin as it exists today to work with these spring-specific tasks is likely possible. I recommend trying that first for anyone interested in contributing these features.

punya commented 1 year ago

Thanks @ryandens, it's great to know that you're open to reviewing a contribution for Spring support. When I have time, I'll draft a PR and seek your help for any blockers.

ryandens commented 1 year ago

No problem! Note, given the complexity of this change it will require unit and functional tests