fix(llmobs/openai-java): inherit session_id on auto-instrumented openai.request span#11372
Draft
jessicagamio wants to merge 1 commit into
Conversation
…ai.request span OpenAiDecorator.afterStart() already consulted LLMObsContext.current() to set the LLMObs parent_id tag but never read session_id from the context. Auto-instrumented openai.request spans therefore carried no _ml_obs_tag.session_id even when a manual LLMObs workflow parent had one. dd-trace-py and dd-trace-js both auto-propagate session_id to auto-instrumented LLM spans via parent context. Add a SESSION_ID constant to CommonTags. In OpenAiDecorator.afterStart(), after the existing parent_id block, read LLMObsContext.currentSessionId() and stamp it on the span as CommonTags.SESSION_ID when present. With this change, auto-instrumented openai.request spans now appear under their session in the LLM Trace Explorer's Sessions view, matching Python and Node behavior. Depends on the LLMObsContext.currentSessionId() API added in the preceding fix(llmobs): propagate session_id from parent context commit. Originating issue: MLOS-646.
ygree
reviewed
May 15, 2026
| * OpenAiDecorator's session_id inheritance behavior, which only depends on | ||
| * LLMObsContext.currentSessionId() being set. | ||
| */ | ||
| class SessionIdPropagationTest extends OpenAiTest { |
Contributor
There was a problem hiding this comment.
It's now a requirement to use Java/JUnit instead of Groovy/Spock for all new tests the repo wide.
ygree
requested changes
May 15, 2026
Contributor
ygree
left a comment
There was a problem hiding this comment.
Looks good! One suggestion is to use JUnit for testing. Also, perhaps we should keep this change in the parent PR since they're closely related.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
dd-trace-java's openai-java auto-instrumentation (
OpenAiDecorator) doesn't inheritsession_idfrom the active LLMObs parent context. Auto-instrumentedopenai.requestspans carry no_ml_obs_tag.session_ideven when a manual workflow parent has one set.This PR brings the openai-java integration to parity with dd-trace-py (
ddtrace/llmobs/_llmobs.py:_on_span_start) and dd-trace-js (packages/dd-trace/src/llmobs/tagger.js:105-106), both of which auto-propagatesession_idfrom parent context to auto-instrumented LLM spans.MLOB tracking: MLOB-7480
Stacked on #11371
This PR is stacked on #11371 (MLOB-7479 — emit session_id as top-level field + propagate via context). It uses
LLMObsContext.currentSessionId(), an API added in that PR.Once #11371 merges, the base branch of this PR will be re-pointed to
master.Changes
dd-java-agent/instrumentation/openai-java/.../CommonTags.java— addSESSION_ID = TAG_PREFIX + LLMObsTags.SESSION_IDconstant, mirroring the existingPARENT_IDpattern.dd-java-agent/instrumentation/openai-java/.../OpenAiDecorator.java— inafterStart(), after the existingparent_idblock, readLLMObsContext.currentSessionId()and stamp it on the span asCommonTags.SESSION_IDwhen present.dd-java-agent/instrumentation/openai-java/.../SessionIdPropagationTest.groovy— new file, 2 tests: (1) auto-instrumentedopenai.requestspan inherits session_id from active LLMObs context; (2) no session_id tag when no LLMObs context is active.Test plan
./gradlew :dd-java-agent:instrumentation:openai-java:openai-java-3.0:test— 97 tests pass (2 new + 95 existing, 0 regressions)./gradlew :dd-java-agent:instrumentation:openai-java:openai-java-3.0:spotlessCheckcleanopenai.requestnow appears under the same Session as its workflow parent in the LLM Trace ExplorerFuture considerations
The openai-java module is currently the only LLMObs auto-instrumentation in dd-trace-java. When additional ones are added (Anthropic, Vertex, Bedrock, etc.), each will need the same
LLMObsContext.currentSessionId()lookup. A shared helper could consolidate this in a future refactor — out of scope for this PR.