Skip to content

Commit f5bed1c

Browse files
authored
fix(llmobs): make sure correct llmobs trace id is returned from export_span (#13620)
We recently changed how we record our LLMObs trace IDs - from APM trace IDs to new randomly generated ones. However, we missed updating `export_span`. This PR ensures we export the correct trace ID from `export_span`. ## Checklist - [x] 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](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] 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](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) 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](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 3186f73 commit f5bed1c

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

ddtrace/llmobs/_llmobs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,10 @@ def export_span(cls, span: Optional[Span] = None) -> Optional[ExportedLLMObsSpan
736736
error = "invalid_span"
737737
log.warning("Span must be an LLMObs-generated span.")
738738
return None
739-
return ExportedLLMObsSpan(span_id=str(span.span_id), trace_id=format_trace_id(span.trace_id))
739+
return ExportedLLMObsSpan(
740+
span_id=str(span.span_id),
741+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID) or span.trace_id),
742+
)
740743
except (TypeError, AttributeError):
741744
error = "invalid_span"
742745
log.warning("Failed to export span. Span must be a valid Span object.")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
LLM Observability: Fixes an issue where the trace ID exported from ``export_span`` was incorrect.

tests/llmobs/test_llmobs_service.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ddtrace.llmobs._constants import INPUT_PROMPT
1717
from ddtrace.llmobs._constants import INPUT_VALUE
1818
from ddtrace.llmobs._constants import IS_EVALUATION_SPAN
19+
from ddtrace.llmobs._constants import LLMOBS_TRACE_ID
1920
from ddtrace.llmobs._constants import METADATA
2021
from ddtrace.llmobs._constants import METRICS
2122
from ddtrace.llmobs._constants import ML_APP
@@ -914,7 +915,7 @@ def test_export_span_specified_span_returns_span_context(llmobs):
914915
span_context = llmobs.export_span(span=span)
915916
assert span_context is not None
916917
assert span_context["span_id"] == str(span.span_id)
917-
assert span_context["trace_id"] == format_trace_id(span.trace_id)
918+
assert span_context["trace_id"] == format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID))
918919

919920

920921
def test_export_span_no_specified_span_no_active_span_raises_warning(llmobs, mock_llmobs_logs):
@@ -933,7 +934,7 @@ def test_export_span_no_specified_span_returns_exported_active_span(llmobs):
933934
span_context = llmobs.export_span()
934935
assert span_context is not None
935936
assert span_context["span_id"] == str(span.span_id)
936-
assert span_context["trace_id"] == format_trace_id(span.trace_id)
937+
assert span_context["trace_id"] == format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID))
937938

938939

939940
def test_submit_evaluation_ml_app_raises_warning(llmobs, mock_llmobs_logs):
@@ -1197,7 +1198,7 @@ def test_submit_evaluation_enqueues_writer_with_categorical_metric(llmobs, mock_
11971198
_expected_llmobs_eval_metric_event(
11981199
ml_app="dummy",
11991200
span_id=str(span.span_id),
1200-
trace_id=format_trace_id(span.trace_id),
1201+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID)),
12011202
label="toxicity",
12021203
metric_type="categorical",
12031204
categorical_value="high",
@@ -1226,7 +1227,7 @@ def test_submit_evaluation_enqueues_writer_with_score_metric(llmobs, mock_llmobs
12261227
mock_llmobs_eval_metric_writer.enqueue.assert_called_with(
12271228
_expected_llmobs_eval_metric_event(
12281229
span_id=str(span.span_id),
1229-
trace_id=format_trace_id(span.trace_id),
1230+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID)),
12301231
label="sentiment",
12311232
metric_type="score",
12321233
score_value=0.9,
@@ -1263,7 +1264,7 @@ def test_submit_evaluation_with_numerical_metric_enqueues_writer_with_score_metr
12631264
_expected_llmobs_eval_metric_event(
12641265
ml_app="dummy",
12651266
span_id=str(span.span_id),
1266-
trace_id=format_trace_id(span.trace_id),
1267+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID)),
12671268
label="token_count",
12681269
metric_type="score",
12691270
score_value=35,
@@ -2148,7 +2149,7 @@ def test_submit_evaluation_for_enqueues_writer_with_categorical_metric(llmobs, m
21482149
_expected_llmobs_eval_metric_event(
21492150
ml_app="dummy",
21502151
span_id=str(span.span_id),
2151-
trace_id=format_trace_id(span.trace_id),
2152+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID)),
21522153
label="toxicity",
21532154
metric_type="categorical",
21542155
categorical_value="high",
@@ -2177,7 +2178,7 @@ def test_submit_evaluation_for_enqueues_writer_with_score_metric(llmobs, mock_ll
21772178
mock_llmobs_eval_metric_writer.enqueue.assert_called_with(
21782179
_expected_llmobs_eval_metric_event(
21792180
span_id=str(span.span_id),
2180-
trace_id=format_trace_id(span.trace_id),
2181+
trace_id=format_trace_id(span._get_ctx_item(LLMOBS_TRACE_ID)),
21812182
label="sentiment",
21822183
metric_type="score",
21832184
score_value=0.9,

0 commit comments

Comments
 (0)