Skip to content

fix(llmobs): set session ID on span if annotated as a tag #13220

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

Merged
merged 1 commit into from
Apr 16, 2025

Conversation

Yun-Kim
Copy link
Contributor

@Yun-Kim Yun-Kim commented Apr 16, 2025

This PR makes a fix to how session ID is set on a LLM Obs span.

Session ID (for session tracking) can be explicitly set when starting a span via LLMObs._start_span(session_id=...). We then set this both as the span's session_id field and as a tag on the span, "session_id:...".

However, currently session ID can also be set by using LLMObs.annotate/annotation_context() as a tag. This is meant for use cases where the session ID may not be immediately available. The issue here is that when setting as a tag, we don't actually set that value on the span's top level session_id field and only set it as a tag. This means that our backend and other event queries made by other products (like RUM/APM) can't 100% rely on that top level event field even if a user did indeed set it as a tag. (queries based on tags are also slower/more expensive than top-level queries)

This fixes the issue by adding a step when setting tags via LLMObs.annotate/annotation_context() that we also add a session_id tag as the span's top-level session_id field.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@Yun-Kim Yun-Kim requested review from a team as code owners April 16, 2025 20:00
@Yun-Kim Yun-Kim requested review from emmettbutler and lievan April 16, 2025 20:00
Copy link
Contributor

CODEOWNERS have been resolved as:

releasenotes/notes/fix-llmobs-session-id-via-tags-6f6b0f7fca47e516.yaml  @DataDog/apm-python
ddtrace/llmobs/_llmobs.py                                               @DataDog/ml-observability
tests/llmobs/test_llmobs_service.py                                     @DataDog/ml-observability

Copy link
Contributor

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 231 ± 4 ms.

The average import time from base is: 234 ± 3 ms.

The import time difference between this PR and base is: -2.9 ± 0.2 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 2.012 ms (0.87%)
ddtrace.bootstrap.sitecustomize 1.334 ms (0.58%)
ddtrace.bootstrap.preload 1.334 ms (0.58%)
ddtrace.internal.products 1.334 ms (0.58%)
ddtrace.internal.remoteconfig.client 0.616 ms (0.27%)
ddtrace 0.678 ms (0.29%)

@Yun-Kim Yun-Kim enabled auto-merge (squash) April 16, 2025 21:06
@pr-commenter
Copy link

pr-commenter bot commented Apr 16, 2025

Benchmarks

Benchmark execution time: 2025-04-16 21:13:49

Comparing candidate commit 383a246 in PR branch yunkim/llmobs-fix-session-id with baseline commit dc4cd33 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 506 metrics, 2 unstable metrics.

@Yun-Kim Yun-Kim merged commit bdca42d into main Apr 16, 2025
765 checks passed
@Yun-Kim Yun-Kim deleted the yunkim/llmobs-fix-session-id branch April 16, 2025 22:39
Copy link
Contributor

The backport to 2.21 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.21 2.21
# Navigate to the new working tree
cd .worktrees/backport-2.21
# Create a new branch
git switch --create backport-13220-to-2.21
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 bdca42d6005262a2bf803f8e928f8ae5f6daec38
# Push it to GitHub
git push --set-upstream origin backport-13220-to-2.21
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.21

Then, create a pull request where the base branch is 2.21 and the compare/head branch is backport-13220-to-2.21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants