Skip to content

Emit gen_ai.usage.* token metrics with LLM Observability disabled - #12489

Closed
mz1119 wants to merge 2 commits into
max.zhang/llmobs-gen-ai-apm-tagsfrom
max.zhang/llmobs-gen-ai-usage-no-llmobs
Closed

mz1119 wants to merge 2 commits into
max.zhang/llmobs-gen-ai-apm-tagsfrom
max.zhang/llmobs-gen-ai-usage-no-llmobs

Conversation

@mz1119

@mz1119 mz1119 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #12450.

What Does This Do

The openai-java decorators already read the token counts off each response, but tagged them behind the llmObsEnabled guard, so an APM span carried gen_ai.operation.name, gen_ai.request.model, gen_ai.provider.name and gen_ai.application.name but no usage when LLM Observability was off.

Each count now goes through TokenUsage.set, which keeps writing the _ml_obs_metric.* tag when LLM Observability is enabled (byte-identical to today) and writes the matching gen_ai.usage.* APM metric when it is not. Covers all four endpoints: chat completions (streaming included, usage only, as dd-trace-js does), responses, embeddings and legacy completions.

Motivation

Parity with dd-trace-py, which already does this.

Additional Notes

_ml_obs_metric.* tags stay behind the guard, so APM spans of users who never enabled LLM Observability do not pick up LLM Observability internal tags.

The shared mock OpenAI server in the forked tests now returns real chat-completion and embedding JSON with a usage block, so the token counts have something to come from.

Testing

trace queryable by usage tag

The openai-java decorators read the response token counts but tagged them
behind the LLM Observability guard, so APM spans carried no usage when LLM
Observability was off. Route each count through TokenUsage, which keeps the
_ml_obs_metric.* tags when LLM Observability is enabled and writes the matching
gen_ai.usage.* metric when it is not. Matches dd-trace-js.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@mz1119 mz1119 added tag: ai generated Largely based on code generated by an AI or LLM comp: mlobs ML Observability (LLMObs) type: feature Enhancements and improvements labels Sep 14, 2026
@datadog-prod-us1-3

This comment has been minimized.

@dd-octo-sts

dd-octo-sts Bot commented Sep 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 13.96 s 13.88 s [-0.2%; +1.3%] (no difference)
startup:insecure-bank:tracing:Agent 12.98 s 12.94 s [-0.4%; +1.0%] (no difference)
startup:petclinic:appsec:Agent 16.96 s 16.83 s [-0.2%; +1.8%] (no difference)
startup:petclinic:iast:Agent 16.93 s 16.45 s [-1.4%; +7.3%] (no difference)
startup:petclinic:profiling:Agent 16.13 s 16.86 s [-8.6%; -0.0%] (maybe better)
startup:petclinic:sca:Agent 16.82 s 16.75 s [-0.5%; +1.4%] (no difference)
startup:petclinic:tracing:Agent 15.73 s 16.15 s [-6.7%; +1.6%] (no difference)

Commit: f5e31c85 · 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.

@mz1119

mz1119 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-16T14:22:13.776955Z f5e31c8 Draft marked ready
🔒 Security Review ✅ Completed 2026-09-16T14:23:00.968888Z f5e31c8 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3e59d9a249

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 3e59d9a249

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@mz1119
mz1119 marked this pull request as ready for review September 16, 2026 14:19
@mz1119
mz1119 requested review from a team as code owners September 16, 2026 14:19
@mz1119
mz1119 requested review from ValentinZakharov and removed request for a team September 16, 2026 14:19

@datadog-prod-us1-3 datadog-prod-us1-3 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.

Datadog Autotest: PASS

More details

Token routing stays mode-specific. Existing LLM Observability metrics remain enabled, and disabled mode writes the matching APM usage metrics without a reportable defect.

Was this helpful? React 👍 or 👎

Open Bits AI session

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

@mz1119

mz1119 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

closing because we have decided not to emit when llmobs is disabled.

@mz1119 mz1119 closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: mlobs ML Observability (LLMObs) tag: ai generated Largely based on code generated by an AI or LLM type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant