-
Notifications
You must be signed in to change notification settings - Fork 335
fix(llmobs): emit session_id as top-level field + propagate via context #11371
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
2
commits into
master
Choose a base branch
from
jessica.gamio/llmobs-session-id-mlos-646
base: master
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -297,6 +297,126 @@ class LLMObsSpanMapperTest extends DDCoreSpecification { | |
| spanNames.contains("chat-completion-3") | ||
| } | ||
|
|
||
| def "test LLMObsSpanMapper writes top-level session_id when set"() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we don't need separate test cases that are very repetitive and can instead be just part of "test LLMObsSpanMapper serialization". |
||
| setup: | ||
| def mapper = new LLMObsSpanMapper() | ||
| def tracer = tracerBuilder().writer(new ListWriter()).build() | ||
|
|
||
| def sessionId = "abc-123-session" | ||
|
|
||
| def llmSpan = tracer.buildSpan("datadog", "openai.request") | ||
| .withResourceName("createCompletion") | ||
| .withTag("_ml_obs_tag.span.kind", Tags.LLMOBS_LLM_SPAN_KIND) | ||
| .withTag("_ml_obs_tag.model_name", "gpt-4") | ||
| .withTag("_ml_obs_tag.model_provider", "openai") | ||
| .withTag("_ml_obs_tag.session_id", sessionId) | ||
| .start() | ||
| llmSpan.setSpanType(InternalSpanTypes.LLMOBS) | ||
| llmSpan.finish() | ||
|
|
||
| def trace = [llmSpan] | ||
| CapturingByteBufferConsumer sink = new CapturingByteBufferConsumer() | ||
| MsgPackWriter packer = new MsgPackWriter(new FlushingBuffer(16 * 1024, sink)) | ||
|
|
||
| when: | ||
| packer.format(trace, mapper) | ||
| packer.flush() | ||
|
|
||
| then: | ||
| sink.captured != null | ||
| def payload = mapper.newPayload() | ||
| payload.withBody(1, sink.captured) | ||
|
|
||
| def channel = new ByteArrayOutputStream() | ||
| payload.writeTo(new WritableByteChannel() { | ||
| @Override | ||
| int write(ByteBuffer src) throws IOException { | ||
| def bytes = new byte[src.remaining()] | ||
| src.get(bytes) | ||
| channel.write(bytes) | ||
| return bytes.length | ||
| } | ||
|
|
||
| @Override | ||
| boolean isOpen() { | ||
| return true | ||
| } | ||
|
|
||
| @Override | ||
| void close() throws IOException { } | ||
| }) | ||
|
|
||
| def result = objectMapper.readValue(channel.toByteArray(), Map) | ||
| def spanData = result["spans"][0] | ||
|
|
||
| then: | ||
| // Top-level session_id field is present with the right value — this is what | ||
| // the LLM Trace Explorer's Sessions filter queries. | ||
| spanData.containsKey("session_id") | ||
| spanData["session_id"] == sessionId | ||
|
|
||
| // The session_id:<value> entry is ALSO present in the tags[] array, matching | ||
| // dd-trace-py and dd-trace-js wire-format behavior. | ||
| spanData["tags"].contains("session_id:${sessionId}".toString()) | ||
| } | ||
|
|
||
| def "test LLMObsSpanMapper omits top-level session_id when not set"() { | ||
| setup: | ||
| def mapper = new LLMObsSpanMapper() | ||
| def tracer = tracerBuilder().writer(new ListWriter()).build() | ||
|
|
||
| def llmSpan = tracer.buildSpan("datadog", "openai.request") | ||
| .withResourceName("createCompletion") | ||
| .withTag("_ml_obs_tag.span.kind", Tags.LLMOBS_LLM_SPAN_KIND) | ||
| .withTag("_ml_obs_tag.model_name", "gpt-4") | ||
| .withTag("_ml_obs_tag.model_provider", "openai") | ||
| .start() | ||
| llmSpan.setSpanType(InternalSpanTypes.LLMOBS) | ||
| llmSpan.finish() | ||
|
|
||
| def trace = [llmSpan] | ||
| CapturingByteBufferConsumer sink = new CapturingByteBufferConsumer() | ||
| MsgPackWriter packer = new MsgPackWriter(new FlushingBuffer(16 * 1024, sink)) | ||
|
|
||
| when: | ||
| packer.format(trace, mapper) | ||
| packer.flush() | ||
|
|
||
| then: | ||
| sink.captured != null | ||
| def payload = mapper.newPayload() | ||
| payload.withBody(1, sink.captured) | ||
|
|
||
| def channel = new ByteArrayOutputStream() | ||
| payload.writeTo(new WritableByteChannel() { | ||
| @Override | ||
| int write(ByteBuffer src) throws IOException { | ||
| def bytes = new byte[src.remaining()] | ||
| src.get(bytes) | ||
| channel.write(bytes) | ||
| return bytes.length | ||
| } | ||
|
|
||
| @Override | ||
| boolean isOpen() { | ||
| return true | ||
| } | ||
|
|
||
| @Override | ||
| void close() throws IOException { } | ||
| }) | ||
|
|
||
| def result = objectMapper.readValue(channel.toByteArray(), Map) | ||
| def spanData = result["spans"][0] | ||
|
|
||
| then: | ||
| // No top-level session_id field when the tag was never set. | ||
| !spanData.containsKey("session_id") | ||
|
|
||
| // And no session_id entry leaks into tags[] either. | ||
| spanData["tags"].every { !it.startsWith("session_id:") } | ||
| } | ||
|
|
||
| static class CapturingByteBufferConsumer implements ByteBufferConsumer { | ||
|
|
||
| ByteBuffer captured | ||
|
|
||
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
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.
Maybe keep position numbers for following keys in the comment?