Skip to content

Commit 85b3bae

Browse files
committed
Revert llm kind check removal, fix llmobs_service tests
1 parent 0555fbd commit 85b3bae

File tree

5 files changed

+568
-738
lines changed

5 files changed

+568
-738
lines changed

ddtrace/llmobs/_llmobs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def _on_span_finish(self, span):
120120
def _submit_llmobs_span(self, span: Span) -> None:
121121
"""Generate and submit an LLMObs span event to be sent to LLMObs."""
122122
span_event = None
123+
is_llm_span = span._get_ctx_item(SPAN_KIND) == "llm"
123124
is_ragas_integration_span = False
124125
try:
125126
span_event, is_ragas_integration_span = self._llmobs_span_event(span)
@@ -129,7 +130,7 @@ def _submit_llmobs_span(self, span: Span) -> None:
129130
"Error generating LLMObs span event for span %s, likely due to malformed span", span, exc_info=True
130131
)
131132
finally:
132-
if not span_event or is_ragas_integration_span:
133+
if not span_event or not is_llm_span or is_ragas_integration_span:
133134
return
134135
if self._evaluator_runner:
135136
self._evaluator_runner.enqueue(span_event, span)

riotfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,8 @@ def select_pys(min_version: str = MIN_PYTHON_VERSION, max_version: str = MAX_PYT
28832883
name="llmobs",
28842884
command="pytest {cmdargs} tests/llmobs",
28852885
pkgs={"vcrpy": latest, "pytest-asyncio": "==0.21.1"},
2886-
pys=select_pys(min_version="3.7"),
28872886
venvs=[
2887+
Venv(pys="3.7"),
28882888
Venv(pys=select_pys(min_version="3.8"), pkgs={"ragas": "==0.1.21", "langchain": latest}),
28892889
],
28902890
),

tests/llmobs/conftest.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ def mock_http_writer_logs():
129129
yield m
130130

131131

132+
@pytest.fixture
133+
def mock_llmobs_logs():
134+
with mock.patch("ddtrace.llmobs._llmobs.log") as m:
135+
yield m
136+
m.reset_mock()
137+
138+
132139
@pytest.fixture
133140
def ddtrace_global_config():
134141
config = {}
@@ -243,15 +250,25 @@ def llmobs_span_writer():
243250

244251

245252
@pytest.fixture
246-
def llmobs(monkeypatch, tracer, llmobs_env, llmobs_span_writer):
253+
def llmobs(
254+
ddtrace_global_config,
255+
monkeypatch,
256+
tracer,
257+
llmobs_env,
258+
llmobs_span_writer,
259+
mock_llmobs_eval_metric_writer,
260+
mock_llmobs_evaluator_runner,
261+
):
247262
for env, val in llmobs_env.items():
248263
monkeypatch.setenv(env, val)
249-
264+
global_config = default_global_config()
265+
global_config.update(dict(_llmobs_ml_app=llmobs_env.get("DD_LLMOBS_ML_APP")))
266+
global_config.update(ddtrace_global_config)
250267
# TODO: remove once rest of tests are moved off of global config tampering
251-
with override_global_config(dict(_llmobs_ml_app=llmobs_env.get("DD_LLMOBS_ML_APP"))):
268+
with override_global_config(global_config):
252269
llmobs_service.enable(_tracer=tracer)
253270
llmobs_service._instance._llmobs_span_writer = llmobs_span_writer
254-
yield llmobs
271+
yield llmobs_service
255272
llmobs_service.disable()
256273

257274

tests/llmobs/test_llmobs.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import mock
21
import pytest
32

43
from ddtrace.ext import SpanTypes
@@ -8,12 +7,6 @@
87
from tests.llmobs._utils import _expected_llmobs_llm_span_event
98

109

11-
@pytest.fixture
12-
def mock_logs():
13-
with mock.patch("ddtrace.llmobs._trace_processor.log") as mock_logs:
14-
yield mock_logs
15-
16-
1710
class TestMLApp:
1811
@pytest.mark.parametrize("llmobs_env", [{"DD_LLMOBS_ML_APP": "<not-a-real-app-name>"}])
1912
def test_tag_defaults_to_env_var(self, tracer, llmobs_env, llmobs_events):
@@ -228,19 +221,19 @@ def test_model_and_provider_are_set(tracer, llmobs_events):
228221
assert span_event["meta"]["model_provider"] == "model_provider"
229222

230223

231-
def test_malformed_span_logs_error_instead_of_raising(mock_logs, tracer, llmobs_events):
224+
def test_malformed_span_logs_error_instead_of_raising(tracer, llmobs_events, mock_llmobs_logs):
232225
"""Test that a trying to create a span event from a malformed span will log an error instead of crashing."""
233226
with tracer.trace("root_llm_span", span_type=SpanTypes.LLM) as llm_span:
234227
# span does not have SPAN_KIND tag
235228
pass
236-
mock_logs.error.assert_called_once_with(
237-
"Error generating LLMObs span event for span %s, likely due to malformed span", llm_span
229+
mock_llmobs_logs.error.assert_called_with(
230+
"Error generating LLMObs span event for span %s, likely due to malformed span", llm_span, exc_info=True
238231
)
239232
assert len(llmobs_events) == 0
240233

241234

242-
def test_processor_only_creates_llmobs_span_event(tracer, llmobs_events):
243-
"""Test that the LLMObsTraceProcessor only creates LLMObs span events for LLM span types."""
235+
def test_only_generate_span_events_from_llmobs_spans(tracer, llmobs_events):
236+
"""Test that we only generate LLMObs span events for LLM span types."""
244237
with tracer.trace("root_llm_span", service="tests.llmobs", span_type=SpanTypes.LLM) as root_span:
245238
root_span._set_ctx_item(const.SPAN_KIND, "llm")
246239
with tracer.trace("child_span"):
@@ -250,5 +243,5 @@ def test_processor_only_creates_llmobs_span_event(tracer, llmobs_events):
250243
expected_grandchild_llmobs_span["parent_id"] = str(root_span.span_id)
251244

252245
assert len(llmobs_events) == 2
253-
assert llmobs_events[0] == _expected_llmobs_llm_span_event(root_span, "llm")
254-
assert llmobs_events[1] == expected_grandchild_llmobs_span
246+
assert llmobs_events[1] == _expected_llmobs_llm_span_event(root_span, "llm")
247+
assert llmobs_events[0] == expected_grandchild_llmobs_span

0 commit comments

Comments
 (0)