-
Notifications
You must be signed in to change notification settings - Fork 335
fix(llmobs/openai-java): inherit session_id on auto-instrumented openai.request span #11372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jessicagamio
wants to merge
1
commit into
jessica.gamio/llmobs-session-id-mlos-646
Choose a base branch
from
jessica.gamio/llmobs-openai-java-session-id-mlos-646
base: jessica.gamio/llmobs-session-id-mlos-646
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
...strumentation/openai-java/openai-java-3.0/src/test/groovy/SessionIdPropagationTest.groovy
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace | ||
|
|
||
| import datadog.context.ContextScope | ||
| import datadog.trace.api.llmobs.LLMObsContext | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer | ||
|
|
||
| /** | ||
| * Verifies that auto-instrumented openai.request spans inherit session_id from an active | ||
| * LLMObs parent context, matching the behavior of dd-trace-py and dd-trace-js. | ||
| * | ||
| * Background: customer apps typically set session_id on a manual LLMObs workflow root | ||
| * via LLMObs.startWorkflowSpan(..., sessionId) and then call OpenAI without manually | ||
| * wrapping in LLMObs.startLLMSpan. The auto-instrumented LLM span needs to inherit the | ||
| * session_id from the workflow parent so the trace appears under its session in the | ||
| * LLM Trace Explorer's Sessions view. | ||
| * | ||
| * Note: this test attaches the LLMObsContext directly rather than going through | ||
| * LLMObs.startWorkflowSpan() — the latter resolves to a no-op factory in this | ||
| * test module since agent-llmobs isn't loaded here. We're specifically validating | ||
| * OpenAiDecorator's session_id inheritance behavior, which only depends on | ||
| * LLMObsContext.currentSessionId() being set. | ||
| */ | ||
| class SessionIdPropagationTest extends OpenAiTest { | ||
|
|
||
| def "openai.request span inherits session_id from active LLMObs context"() { | ||
| setup: | ||
| def expectedSessionId = "session-propagation-test-abc" | ||
|
|
||
| when: | ||
| runUnderTrace("parent") { | ||
| def parentCtx = AgentTracer.activeSpan().context() | ||
| ContextScope scope = LLMObsContext.attach(parentCtx, expectedSessionId) | ||
| try { | ||
| openAiClient.chat().completions().create(chatCompletionCreateParams(false)) | ||
| } finally { | ||
| scope.close() | ||
| } | ||
| } | ||
| TEST_WRITER.waitForTraces(1) | ||
| def openAiSpan = TEST_WRITER.flatten().find { | ||
| it.operationName.toString() == "openai.request" | ||
| } | ||
|
|
||
| then: | ||
| openAiSpan != null | ||
| expectedSessionId == openAiSpan.getTag("_ml_obs_tag.session_id") | ||
| } | ||
|
|
||
| def "openai.request span has no session_id when no LLMObs parent context is active"() { | ||
| when: | ||
| runUnderTrace("non-llmobs-parent") { | ||
| openAiClient.chat().completions().create(chatCompletionCreateParams(false)) | ||
| } | ||
| TEST_WRITER.waitForTraces(1) | ||
| def openAiSpan = TEST_WRITER.flatten().find { | ||
| it.operationName.toString() == "openai.request" | ||
| } | ||
|
|
||
| then: | ||
| openAiSpan != null | ||
| null == openAiSpan.getTag("_ml_obs_tag.session_id") | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's now a requirement to use Java/JUnit instead of Groovy/Spock for all new tests the repo wide.