Skip to content

Keep Netty 4.1 server spans open until streaming responses complete - #11941

Open
ygree wants to merge 42 commits into
masterfrom
ygree/fix-netty41-chunked-tracing
Open

Keep Netty 4.1 server spans open until streaming responses complete#11941
ygree wants to merge 42 commits into
masterfrom
ygree/fix-netty41-chunked-tracing

Conversation

@ygree

@ygree ygree commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Updates Netty 4.1 server instrumentation to finish server spans when the final LastHttpContent write completes. It preserves spans across interim 1xx responses, reports asynchronous terminal-write failures as errors, and finishes remaining response spans normally when the channel closes without inferring framing errors from Content-Length.

Motivation

Netty response writes can complete asynchronously. Finishing a server span when the final response content is submitted, rather than when its write completes, can underreport response duration and miss asynchronous write failures.

Additional Notes

This change builds on the FIFO request-context tracking introduced by #11937.

Based on: #11937

Supersedes: #10734

Contributor Checklist

Jira ticket: APMS-20050

@ygree ygree self-assigned this Jul 14, 2026
@ygree ygree added type: bug fix Bug fix inst: netty Netty instrumentation tag: ai generated Largely based on code generated by an AI or LLM labels Jul 14, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 69.73%

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: a5af354 | Docs | Datadog PR Page | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.74 s 14.62 s [+0.1%; +1.5%] (maybe worse)
startup:insecure-bank:tracing:Agent 13.58 s 13.72 s [-1.8%; -0.1%] (maybe better)
startup:petclinic:appsec:Agent 17.56 s 16.98 s [+2.5%; +4.3%] (significantly worse)
startup:petclinic:iast:Agent 17.41 s 17.53 s [-1.5%; +0.2%] (no difference)
startup:petclinic:profiling:Agent 17.42 s 17.42 s [-1.3%; +1.2%] (no difference)
startup:petclinic:sca:Agent 17.47 s 17.52 s [-1.2%; +0.7%] (no difference)
startup:petclinic:tracing:Agent 16.49 s 16.62 s [-1.7%; +0.2%] (no difference)

Commit: 670d5c49 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch 2 times, most recently from 8a32be2 to f65d4a4 Compare July 14, 2026 06:10
@ygree
ygree marked this pull request as ready for review July 14, 2026 18:01
@ygree
ygree requested a review from a team as a code owner July 14, 2026 18:01
@ygree
ygree requested review from jordan-wong and removed request for a team July 14, 2026 18:01

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The PR correctly refactors Netty 4.1 server span lifecycle to keep spans open until response completion instead of finishing on first headers. A FIFO queue ensures pipelined requests match responses correctly, and guards protect against double-finishing. All critical scenarios (chunked responses, bodyless responses, pipelined requests, connection drops) are tested and pass validation. No behavioral regressions found in diff analysis or test coverage review.

Was this helpful? React 👍 or 👎

📊 Validated against 7 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit de23316 · What is Autotest? · Any feedback? Reach out in #autotest

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de233160c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ygree
ygree marked this pull request as draft July 14, 2026 21:55
@ygree
ygree marked this pull request as ready for review July 15, 2026 02:19

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The PR's isTerminalResponse logic was traced through 15 concrete response type/method combinations (FullHttpResponse, chunked, 1xx, 204/205/304, HEAD, CONNECT, WebSocket, close-delimited, Content-Length:0) and the channelInactive handler was verified against 6 channel-close scenarios. No path produces a span leak, a double-reported span under normal conditions, or a misclassified terminal/non-terminal response. The PR also silently fixes two pre-existing bugs: 103 Early Hints was incorrectly treated as terminal in the old code (consuming the one-shot AppSec analysis slot), and Upgrade: WebSocket with a capital W was not recognised as a WebSocket upgrade due to a case-sensitive string comparison.

Was this helpful? React 👍 or 👎

📊 Validated against 26 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit efe7144 · What is Autotest? · Any feedback? Reach out in #autotest

@gtukmachev

Copy link
Copy Markdown

BTW, @ygree ,
I've tested the jar you provided and may confirm that trace latencies and errors are captured correctly! Good job, thanks!!!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Netty 4.1 server instrumentation span lifecycle so request spans stay open until the HTTP response is actually complete (including chunked/streaming bodies and connection-close-delimited responses), with added tests covering pipelining and multiple completion edge cases.

Changes:

  • Extend ServerRequestContext to track request method and response/finish state needed to decide when a span can be completed.
  • Update Netty 4.1 request/response handlers to match responses to queued request contexts and finish spans on LastHttpContent, header-only terminals, or channel close (with error only for incomplete declared bodies).
  • Add/refactor JUnit tests to validate chunked streaming, interim 1xx handling, header-only responses, close-delimited completion, and pipelined request ordering.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
dd-java-agent/instrumentation/netty/netty-common/src/main/java/datadog/trace/instrumentation/netty41/ServerRequestContext.java Adds request method + response lifecycle flags and supports removing all queued contexts on channel close.
dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/HttpServerResponseTracingHandler.java Defers span completion to response terminal conditions (incl. LastHttpContent), and supports close-delimited responses.
dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/HttpServerRequestTracingHandler.java Enqueues full HttpRequest context (method-aware) and finishes/publishes pending spans on channel close with incomplete-body error handling.
dd-java-agent/instrumentation/netty/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/server/MaybeBlockResponseHandler.java Avoids consuming one-shot response analysis on interim 1xx responses (except 101).
dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyHttpServerTestSupport.java New shared Netty server + raw socket HTTP parsing utilities for tests.
dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyHttp11PipeliningTest.java Refactors pipelining test to reuse shared server/socket helpers.
dd-java-agent/instrumentation/netty/netty-4.1/src/test/java/datadog/trace/instrumentation/netty41/server/NettyChunkedResponseSpanTest.java New tests asserting span lifecycle correctness for chunked/streaming, interim 1xx, header-only, websocket upgrade, and close-delimited responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mcculls

mcculls commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: efe7144263

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

ygree added 3 commits July 16, 2026 12:32
Track server request contexts per pipelined response, snapshot only the
Accept header, and keep AppSec response-block state channel-level so
late streaming chunks are dropped until close.
Keep server spans open until terminal response writes, finish incomplete
responses on channel close, and use queued request contexts to avoid
keep-alive/pipelining misattribution. Cover chunked, bodyless, HEAD,
CONNECT, interim, and WebSocket upgrade response cases.
ygree added 5 commits July 23, 2026 12:23
Snapshot request metadata before scheduling blocking responses instead
of
retaining and replaying reference-counted HTTP requests.

Reject scheduled blocking responses when their server request context
has
already completed, preventing stale commits from blocking keep-alive
connections.
Finish server spans and dequeue pipelined request contexts only when
Netty writes LastHttpContent. Use the terminal write promise so
asynchronous failures retain error reporting and request context.

Remove Content-Length, bodyless-response, request-method, and
close-delimited completion inference, along with synthetic terminators
and the HTTP/2 marker used by that model. Preserve channel-close cleanup
for responses that never reach terminal content.

Update pipelining, AppSec, HTTP/2, framing, and asynchronous write tests
for the terminal-content contract.
@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch from 5a106cf to ffb560d Compare July 23, 2026 23:53
@ygree

ygree commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: ffb560daac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ygree

ygree commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

BTW, @ygree , I've tested the jar you provided and may confirm that trace latencies and errors are captured correctly! Good job, thanks!!!

@gtukmachev Could you test the latest snapshot? There have been a few changes since the last one.

@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch from 3332358 to ffb560d Compare July 24, 2026 02:06
@ygree ygree changed the title Fix Netty 4.1 server span completion for streaming responses Keep Netty 4.1 server spans open until streaming responses complete Jul 24, 2026
Base automatically changed from ygree/fix-netty41-http11-pipelining-context-queue to master July 24, 2026 18:26
The agent jar exceeds the previous budget by about 1 KiB. Raise the
budget to the next 16 KiB boundary to pass the verification check.
@ygree
ygree requested a review from a team as a code owner July 24, 2026 18:32
@ygree
ygree requested review from bric3 and removed request for a team July 24, 2026 18:32

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The request-context FIFO and terminal-write lifecycle changes were reviewed across pipelined, interim-response, close-delimited, header-only, websocket, deferred-blocking, and delayed-write-failure paths. No clear behavioral regression was identified; local execution could not start because the sandbox has no Java 25 toolchain.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit 952e59b · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch from 964c376 to df8095a Compare July 24, 2026 19:47
Comment thread metadata/agent-jar-checks.properties Outdated
# Max agent jar size in bytes. Raise only when the size growth is intentional (~33.02 MiB).
jar.size.budget = 34619392
# Max agent jar size in bytes. Raise only when the size growth is intentional (~33.03 MiB).
jar.size.budget = 34635776

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The limit was exceeded due to a normal size increase: Jar size 34621751 B exceeds budget 34619392 B.
Increased the limit by 16Kb.

@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch 2 times, most recently from 4565ac1 to 3191577 Compare July 24, 2026 20:39
@ygree
ygree force-pushed the ygree/fix-netty41-chunked-tracing branch from 3191577 to 670d5c4 Compare July 24, 2026 21:06
@vandonr

vandonr commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 670d5c49bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@vandonr vandonr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok, idk if we want to wait for customer's feedback after your latest changes before merging ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: netty Netty instrumentation tag: ai generated Largely based on code generated by an AI or LLM type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants