Collect client IP tags for AI Guard requests by smola · Pull Request #11233 · DataDog/dd-trace-java · GitHub
Skip to content

Collect client IP tags for AI Guard requests#11233

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
masterfrom
smola/ai-guard-client-ip-tags
May 7, 2026
Merged

Collect client IP tags for AI Guard requests#11233
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
masterfrom
smola/ai-guard-client-ip-tags

Conversation

@smola

@smola smola commented Apr 29, 2026

Copy link
Copy Markdown
Member

What Does This Do

When DD_AI_GUARD_ENABLED=true, resolve the client IP eagerly during HTTP
server request decoration and stash it on the request context. Apply the
tags (http.client_ip and network.client.ip) on the local root span only
when an ai_guard span is actually created, so non-AI requests of an
AI-Guard-enabled service do not get IP tags.

APPSEC-62199

Motivation

Additional Notes

Contributor Checklist

Jira ticket: [PROJ-IDENT]

Note: Once your PR is ready to merge, add it to the merge queue by commenting /merge. /merge -c cancels the queue request. /merge -f --reason "reason" skips all merge queue checks; please use this judiciously, as some checks do not run at the PR-level. For more information, see this doc.

@smola smola force-pushed the smola/ai-guard-client-ip-tags branch from 93c0a25 to 3b41551 Compare April 29, 2026 15:09
@pr-commenter

pr-commenter Bot commented Apr 29, 2026

Copy link
Copy Markdown

@smola smola force-pushed the smola/ai-guard-client-ip-tags branch from 3b41551 to 041ae77 Compare May 6, 2026 08:00
@smola smola changed the title Smola/ai guard client ip tags Collect client IP tags for AI Guard requests May 6, 2026
@smola smola added type: enhancement Enhancements and improvements tag: ai generated Largely based on code generated by an AI or LLM comp: ai-guard AI Guard labels May 6, 2026
@smola smola marked this pull request as ready for review May 6, 2026 10:11
@smola smola requested review from a team as code owners May 6, 2026 10:11
@smola smola requested review from a team, daniel-romano-DD, manuel-alvarez-alvarez and mtoffl01 and removed request for a team May 6, 2026 10:11

@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: 041ae77c3d

ℹ️ 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".

@manuel-alvarez-alvarez manuel-alvarez-alvarez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, to review the Codex comment

@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, needs a review from core too

Comment on lines +235 to +241
if (peerIp != null && localRootSpan.getTag(Tags.NETWORK_CLIENT_IP) == null) {
localRootSpan.setTag(Tags.NETWORK_CLIENT_IP, peerIp);
}
final String inferredClientIp = clientIpAddressData.getInferredClientIp();
if (inferredClientIp != null && localRootSpan.getTag(Tags.HTTP_CLIENT_IP) == null) {
localRootSpan.setTag(Tags.HTTP_CLIENT_IP, inferredClientIp);
}

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.

if it's frequent enough that both tags are already set on the root span, it could make sense to check that before unfolding the request context ? maybe ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think the tags would generally not be present in the root span. In the normal path, if the http decorator is tagging these for all requests, then the IPs would not be stashed for later, and this function would see requestContext.getAndResetClientIpAddressData() == null).

Comment on lines +253 to +255
// This should be enabled if:
// - DD_TRACE_CLIENT_IP_ENABLED=true, or
// - DD_APPSEC_ENABLED=true (or AppSec enabled at runtime)

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.

this is kinda repeating the logic that's written below, not sure it's very useful + it risks being outdated if the condition changes

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The logic below is scattered across ~3 separate code blocks intermingled with other stuff. At least for me it was quite hard to follow every subtle detail about what gets collected when, and why. The whole logic for IP tagging should probably be splitted to a separate function so that it's grouped and fits once screen (material for another PR I guess). But until then, I would rather keep some consolidated clarification about what's going on.

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.

ok, makes sense, I saw it as mostly rewording the bool computation below, but it's indeed carrying more information.

@smola smola force-pushed the smola/ai-guard-client-ip-tags branch from 041ae77 to 25e6058 Compare May 6, 2026 15:16
@smola

smola commented May 7, 2026

Copy link
Copy Markdown
Member Author

@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: 25e6058ade

ℹ️ 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".

new AIGuard.Options().block(false) | true | false
}

void 'test evaluate applies captured client ip tags to local root span'() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Move new Spock coverage to JUnit 5

The repo-level AGENTS.md explicitly says, "Do not write new Groovy / Spock tests" and to migrate existing Groovy tests to JUnit 5 when touching them. This commit adds new Spock test methods in this Groovy spec, increasing the legacy test surface instead of following the maintained JUnit 5 convention.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh well, once we migrate the test class as a whole.

smola and others added 2 commits May 7, 2026 10:38
When DD_AI_GUARD_ENABLED=true, resolve the client IP eagerly during HTTP
server request decoration and stash it on the request context. Apply the
tags (http.client_ip and network.client.ip) on the local root span only
when an ai_guard span is actually created, so non-AI requests of an
AI-Guard-enabled service do not get IP tags.

APPSEC-62199
…spans

Replace getAndResetClientIpAddressData with a non-destructive
getClientIpAddressData, and guard the stash write in HttpServerDecorator
so that a child server span cannot overwrite IP data already resolved from
the root span's proxy headers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@smola smola force-pushed the smola/ai-guard-client-ip-tags branch from 25e6058 to 1cccdee Compare May 7, 2026 08:39
@smola

smola commented May 7, 2026

Copy link
Copy Markdown
Member Author

@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: 1cccdeed34

ℹ️ 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".

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Move added Spock coverage to JUnit 5

The repo-level AGENTS.md test guideline says to always use JUnit 5 and not write new Groovy/Spock tests, migrating existing Groovy tests instead. This change adds new Spock test coverage here (and similarly in the HTTP decorator/smoke specs), so the new AI Guard client-IP coverage should be implemented as JUnit 5 tests rather than extending Groovy specs.

Useful? React with 👍 / 👎.

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit 886db92 into master May 7, 2026
570 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the smola/ai-guard-client-ip-tags branch May 7, 2026 16:42
@github-actions github-actions Bot added this to the 1.63.0 milestone May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: ai-guard AI Guard tag: ai generated Largely based on code generated by an AI or LLM type: enhancement Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants