Fix Jetty 12 HTTP server metrics collection without virtual threads #15568
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.
After upgrading to Dropwizard 5 (which uses Jetty 12), HTTP server metrics were not being collected or exposed correctly when virtual threads were disabled (enableVirtualThreads=false). The metrics appeared correctly only when virtual threads were enabled.
Root Cause:
The Jetty12ServerInstrumentation was ending spans in two places:
When virtual threads were disabled, handle() often completed synchronously BEFORE the HttpStream callbacks fired. This caused the span to end prematurely in onMethodExit, and metrics were not properly captured. The HttpStream callbacks would then try to end an already-ended span.
When virtual threads were enabled, the asynchronous nature ensured callbacks fired before method exit, so metrics were captured correctly.
Solution:
Modified Jetty12ServerInstrumentation.HandlerAdvice.end() to NOT end the span in onMethodExit (except when there's an exception). The span is now ended exclusively by the HttpStream completion callbacks.
Added AtomicBoolean in Jetty12Helper.start() to ensure the span ends exactly once, preventing double-ending issues regardless of callback order.
This ensures HTTP metrics (request counts, latency, body size, per-endpoint and per-status-code metrics) are collected correctly whether virtual threads are enabled or disabled.
Fixes the issue where Dropwizard 5 applications with enableVirtualThreads=false would not report HTTP server metrics.