Add support for lazy loading Pyroscope SDK from Application ClassLoader#47
Open
dordor12 wants to merge 1 commit intografana:mainfrom
Open
Add support for lazy loading Pyroscope SDK from Application ClassLoader#47dordor12 wants to merge 1 commit intografana:mainfrom
dordor12 wants to merge 1 commit intografana:mainfrom
Conversation
This adds support for capturing ProfilerSdk instances from the Application ClassLoader, enabling span-to-profile correlation when using Pyroscope SDK as an application dependency (e.g., via async-profiler-actuator-endpoint) instead of the Pyroscope Java Agent. Changes: - Add ProfilerSdkInstrumentation to capture ProfilerSdk on construction - Add PyroscopeSdkInstrumentationModule for OTel agent extension registration - Add otel.pyroscope.app.sdk.enabled config option (default: false) - Update OtelProfilerSdkBridge with static fields and setSdkInstance() method - Cache reflection methods for better performance When enabled, the instrumentation intercepts ProfilerSdk constructor calls and captures the instance via OtelProfilerSdkBridge.setSdkInstance(), allowing the span processor to call setTracingContext() for correlation.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #46
Summary
This PR adds support for capturing Pyroscope SDK instances loaded by the Application ClassLoader, enabling integration with libraries like async-profiler-actuator-endpoint that provide profiling as a Spring Boot dependency.
Changes
New Configuration Option
Added
otel.pyroscope.app.sdk.enabledconfiguration property (default:false):-Dotel.pyroscope.app.sdk.enabled=true # or OTEL_PYROSCOPE_APP_SDK_ENABLED=trueImplementation
Uses OTel's bytecode instrumentation to lazily capture
ProfilerSdkinstances when they're constructed in the Application ClassLoader:OtelProfilerSdkBridge- Made fields static, addedappSdkEnabledflag andsetSdkInstance()method. Cached reflection methods for better performance. The instrumentation only captures the SDK whenappSdkEnabled=true.ProfilerSdkInstrumentation- ByteBuddy instrumentation that hooks intoProfilerSdkconstructor and callsOtelProfilerSdkBridge.setSdkInstance().PyroscopeSdkInstrumentationModule- Registers the instrumentation and injectsOtelProfilerSdkBridgeas a helper class into the Application ClassLoader.PyroscopeOtelConfiguration- AddedappSdkEnabledconfiguration field.PyroscopeOtelAutoConfigurationCustomizerProvider- SetsOtelProfilerSdkBridge.appSdkEnabledflag. SystemClassLoader loading is preserved as fallback.How It Works
Files Changed
src/main/java/io/otel/pyroscope/OtelProfilerSdkBridge.java- Static fields,appSdkEnabledflag, cached methods,setSdkInstance()src/main/java/io/otel/pyroscope/PyroscopeOtelConfiguration.java- AddedappSdkEnabledsrc/main/java/io/otel/pyroscope/PyroscopeOtelAutoConfigurationCustomizerProvider.java- SetsappSdkEnabledflagsrc/main/java/io/otel/pyroscope/instrumentation/ProfilerSdkInstrumentation.java- Newsrc/main/java/io/otel/pyroscope/instrumentation/PyroscopeSdkInstrumentationModule.java- Newsrc/main/resources/META-INF/services/io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule- NewUsage
For applications using Pyroscope SDK as a dependency (e.g., with
async-profiler-actuator-endpoint):java -javaagent:opentelemetry-javaagent.jar \ -Dotel.javaagent.extensions=pyroscope-otel.jar \ -Dotel.pyroscope.app.sdk.enabled=true \ -jar myapp.jarBackwards Compatibility
appSdkEnabled=false)appSdkEnabled=false, instrumentation does not capture SDKTesting
Span to Profile Link in Grafana