Fix profiling init for Spring and Spring Boot w Agent auto-init#4815
Fix profiling init for Spring and Spring Boot w Agent auto-init#4815
Conversation
…options, add configuration class to load the profiler and converter after spring boot starts in agent mode
| import org.springframework.context.annotation.Import; | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| @ConditionalOnClass(name = {"io.sentry.opentelemetry.agent.AgentMarker"}) |
There was a problem hiding this comment.
Make it conditional on the async profiler class too?
There was a problem hiding this comment.
Sounds good, I see no reason to run this if profiler isn't there.
There was a problem hiding this comment.
What might become an issue is, if we ever have a second IContinuousProfiler implementation, we also have to update the condition
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 806307f | 357.85 ms | 424.64 ms | 66.79 ms |
| ee747ae | 554.98 ms | 611.50 ms | 56.52 ms |
| 17a0955 | 372.53 ms | 446.70 ms | 74.17 ms |
| 2124a46 | 319.19 ms | 415.04 ms | 95.85 ms |
| 9fbb112 | 404.51 ms | 475.65 ms | 71.14 ms |
| b6702b0 | 395.86 ms | 409.98 ms | 14.12 ms |
| ee747ae | 357.79 ms | 421.84 ms | 64.05 ms |
| 27d7cf8 | 314.17 ms | 347.00 ms | 32.83 ms |
| 9fbb112 | 361.43 ms | 427.57 ms | 66.14 ms |
| 96449e8 | 361.30 ms | 423.39 ms | 62.09 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 806307f | 1.58 MiB | 2.10 MiB | 533.42 KiB |
| ee747ae | 1.58 MiB | 2.10 MiB | 530.95 KiB |
| 17a0955 | 1.58 MiB | 2.10 MiB | 533.20 KiB |
| 2124a46 | 1.58 MiB | 2.12 MiB | 551.51 KiB |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
| b6702b0 | 1.58 MiB | 2.12 MiB | 551.79 KiB |
| ee747ae | 1.58 MiB | 2.10 MiB | 530.95 KiB |
| 27d7cf8 | 1.58 MiB | 2.12 MiB | 549.42 KiB |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
| 96449e8 | 1.58 MiB | 2.11 MiB | 539.35 KiB |
Previous results on branch: feat/profiling-w-spring-otel-agent
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| b59fc8e | 399.24 ms | 444.62 ms | 45.38 ms |
| c4729d6 | 315.52 ms | 362.84 ms | 47.32 ms |
| c773a3a | 406.73 ms | 491.63 ms | 84.90 ms |
| 3d7551b | 388.73 ms | 462.98 ms | 74.24 ms |
| db75a56 | 331.37 ms | 402.82 ms | 71.45 ms |
| d76029f | 339.65 ms | 357.82 ms | 18.17 ms |
| 913e482 | 313.91 ms | 378.00 ms | 64.09 ms |
| 30b9948 | 314.20 ms | 354.71 ms | 40.51 ms |
| 3832729 | 297.73 ms | 353.21 ms | 55.48 ms |
| 2b6b804 | 312.35 ms | 370.80 ms | 58.45 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| b59fc8e | 1.58 MiB | 2.11 MiB | 539.90 KiB |
| c4729d6 | 1.58 MiB | 2.12 MiB | 548.31 KiB |
| c773a3a | 1.58 MiB | 2.12 MiB | 549.51 KiB |
| 3d7551b | 1.58 MiB | 2.12 MiB | 552.68 KiB |
| db75a56 | 1.58 MiB | 2.12 MiB | 551.94 KiB |
| d76029f | 1.58 MiB | 2.12 MiB | 549.52 KiB |
| 913e482 | 1.58 MiB | 2.12 MiB | 552.67 KiB |
| 30b9948 | 1.58 MiB | 2.12 MiB | 551.90 KiB |
| 3832729 | 1.58 MiB | 2.12 MiB | 551.94 KiB |
| 2b6b804 | 1.58 MiB | 2.12 MiB | 551.86 KiB |
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean(name = "sentryOpenTelemetryProfilerConfiguration") | ||
| public IContinuousProfiler sentryOpenTelemetryProfilerConfiguration() { |
There was a problem hiding this comment.
Extract init into util to streamline initialization code
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean(name = "sentryOpenTelemetryProfilerConverterConfiguration") | ||
| public IProfileConverter sentryOpenTelemetryProfilerConverterConfiguration() { |
There was a problem hiding this comment.
Extract init into util to streamline initialization code
|
@sentry review |
|
@cursor review |
sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/init/AsyncProfilerInitUtilTest.kt
Show resolved
Hide resolved
sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/init/AsyncProfilerInitUtilTest.kt
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| @Test | ||
| fun `initialize converter returns no-op converter if converter already initialized`() { |
There was a problem hiding this comment.
m should it simply return the existing one instead of noop that potentially causes profiling to break?
sentry-spring-7/src/main/java/io/sentry/spring7/SentryProfilerConfiguration.java
Outdated
Show resolved
Hide resolved
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean(name = "sentryOpenTelemetryProfilerConfiguration") | ||
| public IContinuousProfiler sentryOpenTelemetryProfilerConfiguration() { |
There was a problem hiding this comment.
Is there an expected order to these bean evaluations?
There was a problem hiding this comment.
Thought about that again, and came to the conclusion that no, the order does not matter.
- It doesn't matter if the Profiler or the Converter are created first as they have no direct dependency to each other.
- Initially I was worried, that this needs to run after the original
SentryAutoConfiguration, but based on the following scenarios it should not matter:
a) No Agent -> Configuration does not run at all, as we depend on the AgentMarker
b) Agent with AutoInit -> Sentry init happened through Agent and we set the profiler on the options.SentryAutoConfigurationshouldn't run at all as the dsn config should be done by OTEL. (caveat misconfiguration by also setting the dsn in application.properties)
c1) Agent without AutoInit andAutoConfigurationruns beforeSentryProfilerConfiguration-> Sentry is enabled and profiler is already set if profiling is enabled, thus nothing happens.
c2) Agent without AutoInit andSentryAutoConfigurationruns afterSentryProfilerConfiguration-> Sentry is not yet enabled, thusSentryProfilerConfigurationdoes nothing andSentryAutoConfigurationconfigures profiler afterwards.
WDYT?
| contextRunner | ||
| .withPropertyValues( | ||
| "sentry.dsn=http://key@localhost/proj", | ||
| "sentry.traces-sample-rate=1.0", |
There was a problem hiding this comment.
Should this also have "sentry.profile-session-sample-rate=1.0",?
| "debug=true", | ||
| ) | ||
| .run { | ||
| assertThat(it).hasSingleBean(IContinuousProfiler::class.java) |
There was a problem hiding this comment.
Is this currently simply asserting it has noop instances?
| import org.springframework.context.annotation.Import; | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| @ConditionalOnClass(name = {"io.sentry.opentelemetry.agent.AgentMarker"}) |
There was a problem hiding this comment.
Sounds good, I see no reason to run this if profiler isn't there.
| return previousOptions.getInitPriority().ordinal() <= newOptions.getInitPriority().ordinal(); | ||
| } | ||
|
|
||
| public static IContinuousProfiler initializeProfiler(@NotNull SentryOptions options) { |
There was a problem hiding this comment.
m should we instead check if options already have a profiler / converter set (that's not noop) and return that?
IMO that makes it easier to reason about SDK init.
Looking at the tests it seems we're potentially replacing a working profiler / converter with noop (if misusing this util).
…Configuration.java Co-authored-by: Alexander Dinauer <adinauer@users.noreply.github.com>
…ntry/sentry-java into feat/profiling-w-spring-otel-agent
sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt
Show resolved
Hide resolved
…oke ProfilerAutoConfig
# Conflicts: # CHANGELOG.md
| return InitUtil.initializeProfiler(options); | ||
| } else { | ||
| return profiler; | ||
| } |
There was a problem hiding this comment.
Bug: NoOp Profiler Overwrites Custom Configuration
When Sentry.isEnabled() returns false, the method creates and returns a new NoOpContinuousProfiler instance instead of returning the profiler already stored in options. This can inadvertently overwrite a custom profiler that was configured through OptionsConfiguration, as the bean will always return a fresh NoOp instance rather than preserving existing configuration.
| return InitUtil.initializeProfileConverter(options); | ||
| } else { | ||
| return converter; | ||
| } |
There was a problem hiding this comment.
Bug: Custom Configuration Unexpectedly Overwritten
When Sentry.isEnabled() returns false, the method creates and returns a new NoOpProfileConverter instance instead of returning the converter already stored in options. This can inadvertently overwrite a custom converter that was configured through OptionsConfiguration, as the bean will always return a fresh NoOp instance rather than preserving existing configuration.
| return InitUtil.initializeProfiler(options); | ||
| } else { | ||
| return profiler; | ||
| } |
There was a problem hiding this comment.
Bug: Sentry profiler: Custom configuration discarded.
When Sentry is disabled, the method returns a new NoOpContinuousProfiler instance instead of the existing profiler from options. This is inconsistent with sentry-spring/SentryProfilerConfiguration.java which returns options.getContinuousProfiler(). If options already has a configured profiler (e.g., set via custom configuration), returning a NoOp instance would discard it and potentially break profiling functionality. The unused local variable profiler suggests this was meant to be options.getContinuousProfiler().
| return InitUtil.initializeProfileConverter(options); | ||
| } else { | ||
| return converter; | ||
| } |
There was a problem hiding this comment.
Bug: Sentry Disabled: Configured Converter Ignored
When Sentry is disabled, the method returns a new NoOpProfileConverter instance instead of the existing converter from options. This is inconsistent with sentry-spring/SentryProfilerConfiguration.java which returns options.getProfilerConverter(). If options already has a configured converter, returning a NoOp instance would discard it and potentially break profile conversion. The unused local variable converter suggests this was meant to be options.getProfilerConverter().
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
📜 Description
Add
@Configurationclasses to initialize the profiler when running in OTEL Agent auto-init mode.💡 Motivation and Context
Fixes #4855
💚 How did you test it?
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps