Skip to content
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

chore(llmobs): decorators can infer span links #12043

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

lievan
Copy link
Contributor

@lievan lievan commented Jan 23, 2025

Decorators set span links by tracking common objects passed as inputs & outputs of functions.

We maintain a dictionary in the LLMObs service that remembers which objects are used as the input/output for a spans generated by LLM Obs decorators. This is how we record the from direction of span links.

When objects are encountered again as the input/output for another span, we now know to set the to direction for a span link.

In my opinion, this does not need to be gated behind a feature flag since it's a read-only on app data. A follow up PR will mutate data actually used in the user app for enhanced span link inferencing, and the features introduced then should be gated by a flag.

Implementation notes:

  • Objects are remembered by generating a string object id through the type + memory location of the object.
  • We ignore "input" -> "output" edges. This is not a valid edge.
  • Spans can only be linked to other spans belonging to the same trace

Limitations:

  • it is very easy for link information to be lost if an object is mutated or used to create another object. A follow up PR will implement a best-effort attempt for objects to inherit link info from other objects
  • doesn't work for distributed tracing scenarios

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

)

def _tag_span_links(self, span, span_links):
span_links = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

variable name is the same as a function parameter (...read more)

A function parameter should only be read and not be modified. If your intent is to modify the value of the parameter, return the value in the function and handle the new value in the caller of the function.

View in Datadog  Leave us feedback  Documentation

Copy link
Contributor

github-actions bot commented Jan 23, 2025

CODEOWNERS have been resolved as:

ddtrace/llmobs/_llmobs.py                                               @DataDog/ml-observability
ddtrace/llmobs/_utils.py                                                @DataDog/ml-observability
ddtrace/llmobs/decorators.py                                            @DataDog/ml-observability
tests/llmobs/_utils.py                                                  @DataDog/ml-observability
tests/llmobs/test_llmobs_decorators.py                                  @DataDog/ml-observability

@lievan lievan changed the title chore(llmobs): decorators infer span links chore(llmobs): decorators can infer span links Jan 23, 2025
@datadog-dd-trace-py-rkomorn
Copy link

Datadog Report

Branch report: evan.li/span-links-dec
Commit report: f2ed301
Test service: dd-trace-py

✅ 0 Failed, 130 Passed, 1468 Skipped, 5m 8.1s Total duration (35m 45.03s time saved)

@lievan lievan marked this pull request as ready for review January 23, 2025 16:16
@lievan lievan requested a review from a team as a code owner January 23, 2025 16:16
@pr-commenter
Copy link

pr-commenter bot commented Jan 23, 2025

Benchmarks

Benchmark execution time: 2025-01-23 18:47:06

Comparing candidate commit 7b76ed1 in PR branch evan.li/span-links-dec with baseline commit 2f52abb in branch main.

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

@@ -231,6 +237,10 @@ def wrapper(*args, **kwargs):
_, span_name = _get_llmobs_span_options(name, None, func)
traced_operation = getattr(LLMObs, operation_kind, LLMObs.workflow)
with traced_operation(name=span_name, session_id=session_id, ml_app=ml_app) as span:
for arg in args:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should guard these so we don't run this for users not opted into the feature

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